python-3.x - Scikit 学习 sklearn.linear_model.LinearRegression : View the results of the generated model

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

因此,我可以获得 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}
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)

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

最佳答案

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

由于您对线性模型感兴趣,因此您可以从 statsmodels 中获取所需的信息,包括置信区间、拟合优度统计信息等。图书馆。查看他们的 OLS 示例:http://statsmodels.sourceforge.net/devel/examples/notebooks/generated/ols.html了解详情。

关于python-3.x - Scikit 学习 sklearn.linear_model.LinearRegression : View the results of the generated model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43575652/

相关文章:

Python列表 |如何删除列表中的括号、引号和逗号?

python - 如果匹配字符串(不区分大小写),Pandas 删除行

python - scikit-learn - 将管道预测转换为原始值/比例

python - 使用 apply 方法提高 pandas 性能

python - 属性错误 : 'DataFrame' object has no attribute 'group'

python - 有人可以解释逻辑和 bool 之间的区别吗在这种情况下,

python - 如何旋转 pandas 数据框?

python - 通过装饰器断言 Pandas 数据框具有日期时间索引

python - 管道中的 Sklearn_pandas 返回 TypeError : 'builtin_function_or_method' object is not iterable

python - ModuleNotFoundError : No module named 'xxx' ; 'xxx' is not a package