【问题标题】:Standard deviation of predictive distribution of query points using a pipeline使用管道的查询点预测分布的标准差
【发布时间】:2018-04-05 03:24:15
【问题描述】:

我正在尝试使用管道执行简单的回归任务,以分配用于回归的多项式的次数(次数 = 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 时,它​​返回查询点预测分布的标准差。

无论如何我可以使用管道获得这个标准差数组吗?

【问题讨论】:

    标签: python scikit-learn regression


    【解决方案1】:

    您需要从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。现在已在最新版本中修复。

    参考:

    【讨论】:

      猜你喜欢
      • 2022-07-20
      • 1970-01-01
      • 2019-01-25
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 2019-01-04
      • 1970-01-01
      • 2022-01-11
      相关资源
      最近更新 更多