K折验证报错:TypeError: __init__() got multiple values for argument 'shuffle'

贴出原代码

import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.cross_validation import KFold
from sklearn.cross_validation import cross_val_score

admissions = pd.read_csv("./data/admissions.csv")
admissions["actual_label"] = admissions["admit"]
admissions = admissions.drop("admit", axis=1)

kf = KFold(len(admissions), 5, shuffle=True, random_state=8)
lr = LogisticRegression()
#roc_auc 
accuracies = cross_val_score(lr,admissions[["gpa"]], admissions["actual_label"], scoring="roc_auc", cv=kf)
average_accuracy = sum(accuracies) / len(accuracies)

print(accuracies)
print(average_accuracy)

这段代码本身是没有问题的,但由于库版本的原因,有的人在运行这段代码后,出现以下错误:

ModuleNotFoundError: No module named 'sklearn.cross_validation'

from sklearn.cross_validation import KFold改为from sklearn.model_selection import KFold,再运行却发现有了新的问题

TypeError: __init__() got multiple values for argument 'shuffle'

其实这是导入 KFold的方式不同引起的。如果使用:from sklearn.cross_validation import KFold ,来导包那么:

KFold(n,5,shuffle=False)  # n为总数,需要传入三个参数

但如果你使用:from sklearn.model_selection import KFold,那么:

fold = KFold(5,shuffle=False)  # 无需传入n

改进的代码

import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score

admissions = pd.read_csv("./data/admissions.csv")
admissions["actual_label"] = admissions["admit"]
admissions = admissions.drop("admit", axis=1)

kf = KFold(5, shuffle=True, random_state=8)
lr = LogisticRegression()
#roc_auc
accuracies = cross_val_score(lr,admissions[["gpa"]], admissions["actual_label"], scoring="roc_auc", cv=kf)
average_accuracy = sum(accuracies) / len(accuracies)

print(accuracies)
print(average_accuracy)

在这里插入图片描述

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值