我正在尝试使用管道执行简单的回归任务,以分配用于回归的多项式的次数(次数 = 3)。所以我定义:
pipe = make_pipeline(PolynomialFeatures(3), BayesianRidge())
然后是配件:
pipe.fit(X_train, y_train)
最后是预测位:
y_pred = pipe.predict(X_test)
sklearn 的 BayesianRidge() 的 predict 方法有一个 return_std
参数,当设置为 True 时,它会返回查询点预测分布的标准差。
无论如何,我可以使用管道获得这个标准差数组吗?
最佳答案
您需要从 their github repository 安装最新版本的 scikit-learn| .接下来你只需要使用 partial from functools .我使用了类似于 Bayesian Ridge Regression docs 中提到的例子.
from sklearn import linear_model
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
from functools import partial
clf = linear_model.BayesianRidge()
#Make the pipeline
pipe = make_pipeline(PolynomialFeatures(3), clf)
#Patch the predict function of the classifier using partial
clf.predict = partial(clf.predict,return_std=True )
#Fit the pipeline
pipe.fit([[0,0], [1, 1], [2, 2]], [0, 1, 2])
#Retrieve the prediction and standard deviation
y_pred, y_std = pipe.predict([[1,2]])
#Output : (array([ 1.547614]), array([ 0.25034696]))
注意: 显然这是 sklearn 管道模块中的错误 described here .现在已在最新版本中修复。
引用:
关于python - 使用管道查询点预测分布的标准差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46908676/