【问题标题】:Calculate P-value in Sklearn using python?使用python计算Sklearn中的P值?
【发布时间】:2017-11-13 16:58:30
【问题描述】:

我是机器学习的新手,我使用 sklearn 创建了一个逻辑模型,但我没有得到任何关于如何为我的特征变量和模型找到 P 值的文档。我检查了堆栈link,但没有得到所需的输出。请帮忙。提前致谢

【问题讨论】:

标签: python-2.7 machine-learning scikit-learn


【解决方案1】:

为此可以使用regressors 包。以下代码来自:https://regressors.readthedocs.io/en/latest/usage.html

import numpy as np
from sklearn import datasets
boston = datasets.load_boston()
which_betas = np.ones(13, dtype=bool)
which_betas[3] = False  # Eliminate dummy variable
X = boston.data[:, which_betas]
y = boston.target

from sklearn import linear_model
from regressors import stats
ols = linear_model.LinearRegression()
ols.fit(X, y)

# To calculate the p-values of beta coefficients: 
print("coef_pval:\n", stats.coef_pval(ols, X, y))

# to print summary table:
print("\n=========== SUMMARY ===========")
xlabels = boston.feature_names[which_betas]
stats.summary(ols, X, y, xlabels)

输出:

coef_pval:
 [2.66897615e-13 4.15972994e-04 1.36473287e-05 4.67064962e-01
 1.70032518e-06 0.00000000e+00 7.67610259e-01 1.55431223e-15
 1.51691918e-07 0.00000000e+00 0.00000000e+00 0.00000000e+00
 0.00000000e+00]

=========== SUMMARY ===========
Residuals:
Min      1Q  Median      3Q      Max
-26.3743 -1.9207  0.6648  2.8112  13.3794

Coefficients:
             Estimate  Std. Error  t value   p value
_intercept  36.925033    4.915647   7.5117  0.000000
CRIM        -0.112227    0.031583  -3.5534  0.000416
ZN           0.047025    0.010705   4.3927  0.000014
INDUS        0.040644    0.055844   0.7278  0.467065
NOX        -17.396989    3.591927  -4.8434  0.000002
RM           3.845179    0.272990  14.0854  0.000000
AGE          0.002847    0.009629   0.2957  0.767610
DIS         -1.485557    0.180530  -8.2289  0.000000
RAD          0.327895    0.061569   5.3257  0.000000
TAX         -0.013751    0.001055 -13.0395  0.000000
PTRATIO     -0.991733    0.088994 -11.1438  0.000000
B            0.009827    0.001126   8.7256  0.000000
LSTAT       -0.534914    0.042128 -12.6973  0.000000
---
R-squared:  0.73547,    Adjusted R-squared:  0.72904
F-statistic: 114.23 on 12 features

【讨论】:

  • 请注意regressors 模块给出的 p 值与statsmodels 不一致,因为regressors 使用sklearn 的内置方法来计算MSE,它只除以数量样本 n 而不是 n-p,其中 p 是特征数。
猜你喜欢
  • 2014-04-13
  • 2014-06-23
  • 1970-01-01
  • 1970-01-01
  • 2014-09-30
  • 2017-11-30
  • 2017-12-20
  • 2018-03-16
  • 2020-02-11
相关资源
最近更新 更多