|
23、多项式回归与Pipeline用法
多项式回归思想:还是线性回归算法,只是增加了特征,是原有特征的多项式表达,实际上:升维了
PCA:降维了
PolynomialFeatures(degree=3) 多项式特征
- from sklearn.pipeline import Pipeline
- from sklearn.preprocessing import PolynomialFeatures
- from sklearn.preprocessing import StandardScaler
- def PolynomialRegression(degree):
- return Pipeline([
- ("poly", PolynomialFeatures(degree=degree)),
- ("std_scaler", StandardScaler()),
- ("lin_reg", LinearRegression())
- ])
- poly2_reg = PolynomialRegression(degree=2)
- poly2_reg.fit(X, y)
- y2_predict = poly2_reg.predict(X)
- mean_squared_error(y, y2_predict)
复制代码
Pipeline
一般做了多项式特征(维度扩展),如果degree比较大,则特征个数指数级增长,同时特征的数量级差距也会拉大,此时也会需要进行数据归一化
Pipeline提供了简化流程 from sklearn.pipeline import Pipeline
# List of (name, transform) tuples (implementing fit/transform)
poly_reg_pipe = Pipeline(steps=[
('poly', PolynomialFeatures(degree=2)),
('std_scaler', StandardScaler()),
('lin_reg', LinearRegression())
])
ipynb文件在附件,可提供下载!
视频教程请参考:http://www.ai111.vip/thread-349-1-1.html
东方老师微信:dfy_88888
|
|