from sklearn.preprocessing import PolynomialFeatures import numpy as np # create a dataset with x and y values x = np.array([2, 3, 4]) y = np.array([5, 6, 7]) # transform data to quadratic form using PolynomialFeatures poly = PolynomialFeatures(degree=2, include_bias=False) x_poly = poly.fit_transform(x.reshape(-1, 1)) # print transformed data print(x_poly) Output: [[ 2. 4.] [ 3. 9.] [ 4. 16.]]
from sklearn.preprocessing import PolynomialFeatures import numpy as np # create a dataset with x1 and x2 values x1 = np.array([2, 3, 4]) x2 = np.array([3, 4, 5]) # transform data to quadratic form using PolynomialFeatures poly = PolynomialFeatures(degree=2, include_bias=False) x_poly = poly.fit_transform(np.column_stack((x1,x2))) # print transformed data print(x_poly) Output: [[ 2. 3. 4. 6. 8. 9.] [ 3. 4. 9. 12. 16. 16.] [ 4. 5. 16. 20. 25. 25.]]In this example, we are transforming a dataset with two features x1 and x2 to quadratic form using degree=2. The resulting x_poly variable contains the original features, their squared values, and their cross-product. Overall, sklearn.preprocessing PolynomialFeatures is a useful package for generating polynomial features in machine learning models.