How can I see the effect of sklearn.preprocessing.PolynomialFeatures?

If I have a moderate amount of basic functions and I get a moderate order of polynomial functions from them, there may be confusion to know which column of the attribute array preprocess_XXcorresponds to the transformation of the basic functions.

I used something like the following, with the old version of sklearn (maybe 0.14?):

import numpy as np
from sympy import Symbol
from sklearn.preprocessing import PolynomialFeatures
poly = PolynomialFeatures(4)
x1 = Symbol('x1')
x2 = Symbol('x2')
x3 = Symbol('x3')
XX = np.random.rand(1000, 3)  # replace with the actual data array
preprocess_symXX = poly.fit_transform([x1, x2, x3])
preprocess_XX = poly.fit_transform(XX)
print preprocess_symXX

It was awesome. It will generate output, for example [1, x1, x2, x3, x1**2, ... ], that will let me know what functions of the polynomial my columns have preprocess_XX.

But now, when I do this, he complains TypeError: can't convert expression to float. This exception occurs due to a function in sklearn.utils.validation, called check_array(), which attempts to input the input signal poly.fit_transform()into dtype=float.

, , fit_transform()?, , sympy fit_transform?

+4
1

poly.powers_, . , :

import numpy as np
from sklearn.preprocessing import PolynomialFeatures

X = np.random.rand(1000, 3)

poly = PolynomialFeatures(4)
Y = poly.fit_transform(X)

features = ['X1','X2','X3']

print(poly.powers_)

for entry in poly.powers_:
    newFeature = []
    for feat, coef in zip(features, entry):
        if coef > 0:
            newFeature.append(feat+'**'+str(coef))
    if not newFeature:
        print(1) # If all powers are 0
    else:
        print(' + '.join(newFeature))

( poly.powers _):

1
X1**1
X2**1
X3**1
X1**2
X1**1 + X2**1
X1**1 + X3**1
X2**2
X2**1 + X3**1
X3**2
X1**3
X1**2 + X2**1
X1**2 + X3**1
X1**1 + X2**2
X1**1 + X2**1 + X3**1
X1**1 + X3**2
X2**3
X2**2 + X3**1
X2**1 + X3**2
X3**3
X1**4
X1**3 + X2**1
X1**3 + X3**1
X1**2 + X2**2
X1**2 + X2**1 + X3**1
X1**2 + X3**2
X1**1 + X2**3
X1**1 + X2**2 + X3**1
X1**1 + X2**1 + X3**2
X1**1 + X3**3
X2**4
X2**3 + X3**1
X2**2 + X3**2
X2**1 + X3**3
X3**4
+3

Source: https://habr.com/ru/post/1624907/


All Articles