一、多项式特征
想要丰富特征,特别是对于线性模型而言,除了分箱外,另一种方法是添加原始数据的交互特征和多项式特征。对于给定的特征x,我们可以考虑x,x**2、x**3等,可用preprocessing模块的PolynomialFeatures实现。同样采用wave数据集进行分析。
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.preprocessing import PolynomialFeatures
#导入数据
X, y = mglearn.datasets.make_wave(n_samples=100)
line = np.linspace(-3, 3, 1000, endpoint=False).reshape(-1, 1)
#包含到x**10的多项式
#默认的include_bias=True添加恒等于1的常数特征
poly = PolynomialFeatures(degree=10, include_bias=False)
poly.fit(X)
X_poly = poly.transform(X)
将多项式特征与回归模型一起使用,可以得到经典的多项式回归。