pythonsklearn logistic回归Khold crossvalidation:如何为coef创建drameframe_

2024-04-26 07:46:26 发布

您现在位置:Python中文网/ 问答频道 /正文

Python3.5

我有一个数据集存储在一个变量中,file,我尝试用logistic回归应用10个hold交叉验证。我想要的是列出clf.coef_平均值的方法。在

print(file.head())

   Result  Interest  Limit  Service  Convenience  Trust  Speed 
0       0         1      1        1            1      1      1   
1       0         1      1        1            1      1      1   
2       0         1      1        1            1      1      1   
3       0         4      4        3            4      2      3   
4       1         4      4        4            4      4      4 

下面是我编写的一个简单的logistic回归代码,用于显示coef_的列表。在

[英寸]

^{pr2}$

[出去]

0.823061630219  

             0          1
0     Interest   0.163577
1        Limit  -0.161104
2      Service   0.323073
3  Convenience   0.121573
4        Trust   0.370012
5        Speed   0.089934
6        Major   0.183002
7          Ads  0.0137151

然后,我尝试对同一个数据集应用10倍交叉验证。我有下面的代码,但是我不能像上面的分析那样生成coef_x,coeff_df列表的数据帧。有人能提供解决方案吗?在

[英寸]

from sklearn.cross_validation import cross_val_score
scores = cross_val_score(clf, X, y, cv=10)
print (scores)
print (np.average(scores))

[出去]

[ 0.82178218  0.7970297   0.84158416  0.80693069  0.84158416  0.80693069
  0.825       0.825       0.815       0.76      ]
0.814084158416

Tags: 数据service交叉filespeedlimitprintclf
1条回答
网友
1楼 · 发布于 2024-04-26 07:46:26

cross_val_score是一个helper函数,它包装scikit learn的各种对象以进行交叉验证(例如KFoldStratifiedKFold)。它根据使用的scoring参数返回一个分数列表(对于分类问题,我相信默认情况下是accuracy)。在

cross_val_score的return对象不允许您访问交叉验证中使用的底层折叠/模型,这意味着您无法获得每个模型的系数。在

要获得交叉验证的每一次的系数,您需要使用KFold(或者如果您的类是不平衡的,StratifiedKFold)。在

import pandas as pd
from sklearn.model_selection import StratifiedKFold
from sklearn.linear_model import LogisticRegression

df = pd.read_clipboard()
file = pd.concat([df, df, df]).reset_index()

X = file.drop(['Result'],1)
y = file['Result']

skf = StratifiedKFold(n_splits=2, random_state=0)

models, coefs = [], []  # in case you want to inspect the models later, too
for train, test in skf.split(X, y):
    print(train, test)
    clf = LogisticRegression(penalty='l1')
    clf.fit(X.loc[train], y.loc[train])
    models.append(clf)
    coefs.append(clf.coef_[0])

pd.DataFrame(coefs, columns=X.columns).mean()

让我们:

^{pr2}$

我必须从您的示例(只有一个正类的实例)中创建数据。我怀疑这些数字对你来说不会是0。在


编辑 由于StratifiedKFold(或KFold)为我们提供了数据集的交叉验证拆分,您仍然可以使用模型的score方法计算交叉验证分数。在

下面的版本与上面的版本稍有不同,目的是获取每个折叠的交叉验证分数。在

models, scores, coefs = [], [], []  # in case you want to inspect the models later, too
for train, test in skf.split(X, y):
    print(train, test)
    clf = LogisticRegression(penalty='l1')
    clf.fit(X.loc[train], y.loc[train])
    score = clf.score(X.loc[test], y.loc[test])
    models.append(clf)
    scores.append(score)
    coefs.append(clf.coef_[0])

相关问题 更多 >