【问题标题】:Scikit Learn sklearn.linear_model.LinearRegression: View the results of the model generatedScikit Learn sklearn.linear_model.LinearRegression:查看模型生成的结果
【发布时间】:2017-09-20 09:37:13
【问题描述】:

所以,我可以让 sklearn.linear_model.LinearRegression 来处理我的数据 - 至少可以在不引发任何异常或警告的情况下运行脚本。唯一的问题是,我不是想用 matplotlib 绘制结果,而是想查看模型的估计量和诊断统计数据。

如何获得模型摘要,例如斜率和截距 (B0,B1)、R 平方调整等,以显示在控制台中或填充到变量中而不是绘制出来?

这是我运行的脚本的通用副本:

import numpy as p
import pandas as pn
from sklearn import datasets, linear_model

z = pn.DataFrame(
{'a' : [1,2,3,4,5,6,7,8,9],
'b' : [9,8,7,6,5,4,3,2,1]
})



a2 = z['a'].values.reshape(9,1)
b2 = z['b'].values.reshape(9,1)

reg = linear_model.LinearRegression(fit_intercept=True)
reg.fit(a2,b2)
# print(reg.get_params(deep=True)) I tried this and it didn't print out the #information I wanted

# print(reg) # I tried this too

这运行没有错误,但控制台中没有出现除此之外的输出:

{'n_jobs': 1, 'fit_intercept': True, 'copy_X': True, 'normalize': False} 线性回归(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)

感谢您提供有关如何获取此信息以打印模型摘要的任何信息。

【问题讨论】:

    标签: python-3.x pandas scikit-learn regression linear-regression


    【解决方案1】:

    sklearn 的 API 是围绕拟合训练数据而设计的,然后对测试数据生成预测,而不会暴露太多关于模型如何拟合的信息。虽然您有时可以通过访问拟合模型对象的coef_ 属性找到模型的估计参数,但您不会在参数描述功能方面找到太多。这是因为可能无法以统一的方式提供此信息。该 API 旨在让您将线性回归视为与随机森林相同。

    由于您对线性模型感兴趣,因此您可以从 statsmodels 库中获取所需的信息,包括置信区间、拟合优度统计信息等。有关详细信息,请参阅他们的 OLS 示例:http://statsmodels.sourceforge.net/devel/examples/notebooks/generated/ols.html

    【讨论】:

    • 感谢您提供此信息。
    猜你喜欢
    • 2015-02-25
    • 2018-10-10
    • 2012-09-03
    • 2014-07-29
    • 2022-11-25
    • 1970-01-01
    • 2020-03-19
    • 2017-12-22
    • 2019-11-16
    相关资源
    最近更新 更多