from sklearn.preprocessing import PolynomialFeatures import numpy as np X = np.array([1, 2, 3, 4]).reshape(-1, 1) poly = PolynomialFeatures(degree=2) X_poly = poly.fit_transform(X) print(X_poly)
[[ 1. 1. 1.] [ 1. 2. 4.] [ 1. 3. 9.] [ 1. 4. 16.]]
from sklearn.preprocessing import PolynomialFeatures import numpy as np X = np.array([[1, 2], [3, 4]]) poly = PolynomialFeatures(interaction_only=True) X_poly = poly.fit_transform(X) print(X_poly)
[[ 1. 1. 2. 2.] [ 1. 3. 4. 12.]]In both examples, we first import the PolynomialFeatures tool from the scikit-learn package. Then we create a sample dataset 'X' with the required dimensions. After that, we create an instance of the PolynomialFeatures tool with the desired order or interaction_only parameter. Finally, we use the fit_transform() method to create the new set of features 'X_poly'.