问题
使用sklearn.pipeline进行多项式拟合时,pipeline如同LinearRegression有fit(),predict()方法,但是不能直接获取coef_,intercept_
coef_,intercept_是线性模型中的属性,用于表示模型的拟合参数和偏移量。
lin_reg2 = LinearRegression()
poly_reg = Pipeline([
('ploy',PolynomialFeatures(degree=2)),
('std_scaler',StandardScaler()),
('lin_reg',LinearRegression())
])
...
解决方案
可以通过steps索引,或者named_steps访问所定义标签的估计器
last_estimator = poly_reg.steps[-1][1] #索引最后一个估计器
last_estimator = pipeline.named_steps['lin_reg']
这样就可以访问pipeline内的估计器的属性和方法了
附上测试用的代码
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
size = 100
x = np.random.uniform(-3, 3, size)
X = x.reshape(-1, 1)
y = 0.5 * x**2 + x + 2 + np.random.normal(0, 1, size)
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import PolynomialFeatures
poly_reg = Pipeline([
('ploy',PolynomialFeatures(degree=2)),
('std_scaler',StandardScaler()),
('lin_reg',LinearRegression())
])
poly_reg.fit(X, y)
y_predict = poly_reg.predict(X)
plt.scatter(x, y)
plt.plot(np.sort(x),y_predict[np.argsort(x)], c='r')
plt.show()
last_estimator = poly_reg.steps[-1][1]
#last_estimator = pipeline.named_steps['lin_reg']
last_estimator.coef_,last_estimator.intercept_