python - 使用管道查询点预测分布的标准差

标签 python scikit-learn regression

我正在尝试使用管道执行简单的回归任务,以分配用于回归的多项式的次数(次数 = 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/

相关文章:

python - 仅在一个方向上使用膨胀?

python - 部分定义 scikit-learn K-Means 聚类的初始质心

python - 使用sklearn计算仅给定单词列表的tf-idf权重

r - 将残差绑定(bind)到具有缺失值的输入数据集

python - 使用 numpy 进行大量回归的有效方法?

python - 回归精度

python - RPX、OpenID - 如何为 AppEngine 编写正确的登录处理程序

python - 在 Pandas 中使用 Groupby 对象并重新采样

python - 如何从数据框中绘制折线图中的误差条

python - scikit-learn - 我应该用 TF 还是 TF-IDF 拟合模型?