我是 Python 的新手。我想得到像 R 中那样的逻辑回归的总结。我已经创建了变量 x_train 和 y_train,我正在尝试进行逻辑回归
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model
clf = linear_model.LogisticRegression(C=1e5)
clf.fit(x_train, y_train)
我得到的是:
LogisticRegression(C=100000.0, class_weight=None, dual=False,
fit_intercept=True, intercept_scaling=1, max_iter=100,
multi_class='ovr', n_jobs=1, penalty='l2', random_state=None,
solver='liblinear', tol=0.0001, verbose=0, warm_start=False)
我想要一个具有重要级别的摘要,R2 ecc。
请您参考如下方法:
我建议您看一下 statsmodels 库。 Sk-learn 很棒(其他答案提供了获得 R2 和其他指标的方法),但是 statsmodels 提供了一个回归总结,与您可能在 R 中使用的总结非常相似。
举个例子:
import statsmodels.api as sm
from sklearn.datasets import make_blobs
x, y = make_blobs(n_samples=50, n_features=2, cluster_std=5.0,
centers=[(0,0), (2,2)], shuffle=False, random_state=12)
logit_model = sm.Logit(y, sm.add_constant(x)).fit()
print logit_model.summary()
Optimization terminated successfully.
Current function value: 0.620237
Iterations 5
Logit Regression Results
==============================================================================
Dep. Variable: y No. Observations: 50
Model: Logit Df Residuals: 47
Method: MLE Df Model: 2
Date: Wed, 28 Dec 2016 Pseudo R-squ.: 0.1052
Time: 12:58:10 Log-Likelihood: -31.012
converged: True LL-Null: -34.657
LLR p-value: 0.02611
==============================================================================
coef std err z P>|z| [95.0% Conf. Int.]
------------------------------------------------------------------------------
const -0.0813 0.308 -0.264 0.792 -0.684 0.522
x1 0.1230 0.065 1.888 0.059 -0.005 0.251
x2 0.1104 0.060 1.827 0.068 -0.008 0.229
==============================================================================
如果您想添加正则化,而不是在 Logit 初始化后调用 .fit(),您可以调用 .fit_regularized() 并传入一个 alpha 参数(正则化力量)。如果这样做,请记住 sk-learn 中的 C 参数实际上是正则化强度的倒数。
