繁体   English   中英

如何在 Python 中使用 StratifiedKFold 在 LogisticRegression 中进行参数调整?

[英]How to do parameter tuning in LogisticRegression using StratifiedKFold in Python?

我需要提供例如 6 个 C 值,并查看每个 C 值的每 10 倍的平均 roc_auc_score

到目前为止我的尝试:


lr = LogisticRegression(C = 1,
                          penalty='l1', 
                          solver='liblinear',  
                          tol=0.0001, 
                          max_iter=3000, 
                          intercept_scaling=1.0, 
                          multi_class='auto', 
                          random_state=42)


C = [0.01,0.05,0.1,1,10,12]

final_scores = []
mean_scores = {}
# Stratified KFold
skf = StratifiedKFold(n_splits=10, random_state=42, shuffle=False)

for c in C:
    for fold, (train_index, test_index) in enumerate(skf.split(X, y)):
        print("Fold:" , fold +1)
        X_train, X_test = X.iloc[train_index], X.iloc[test_index]
        y_train, y_test = y.iloc[train_index], y.iloc[test_index]
        lr.fit(X_train,y_train)
        predictions = lr.predict_proba(X_train)[:,1]
        final_score.append(roc_auc_score(y_train, predictions))
        print("AUC SCORE:" + str(roc_auc_score(y_train, predictions)))
        mean_scores[c] = np.mean(final_scores)
        print("---")

print(mean_scores)

我需要一个结果字典,因为键具有 c 值,而每个 c 的值的平均值为 10 倍。

编辑:


roc_dict = dict()

C = [0.01,0.05,0.1,1,10,12]
for c in C:
    final_scores = []
    mean_scores = {}
    for fold, (train_index, test_index) in enumerate(skf.split(X, y)):
        print("Fold:" , fold +1)
        X_train, X_test = X.iloc[train_index], X.iloc[test_index]
        y_train, y_test = y.iloc[train_index], y.iloc[test_index]
        lr.fit(X_train,y_train)
        predictions = lr.predict_proba(X_train)[:,1]
        final_scores.append(roc_auc_score(y_train, predictions))
        print("AUC SCORE:" + str(roc_auc_score(y_train, predictions)))
    roc_dict[c] = np.mean(final_scores)

您快到了。 您可以在循环之前定义一个空dict

roc_dict = dict()

运行您的循环,但将您的listdict放在里面,以便它重置每次迭代(或创建新的迭代):

for c in C:
    final_scores = []
    mean_scores = {}
    # no change here, paste your original code
    roc_dict[c] = final_scores # add this

这将导致:

Out[90]: 
{0.01: [0.7194940476190477,
  0.7681686046511628,
  0.653343023255814,
  0.6596194503171249],
 0.05: [0.7194940476190477,
  0.7681686046511628,
  0.653343023255814,
  0.6596194503171249],
 0.1: [0.7194940476190477,
  0.7681686046511628,
  0.653343023255814,
  0.6596194503171249], # ... etc. But with 10 folds instead.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM