Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
机器学习python中的For循环问题_Python_Machine Learning_Scikit Learn_K Fold - Fatal编程技术网

机器学习python中的For循环问题

机器学习python中的For循环问题,python,machine-learning,scikit-learn,k-fold,Python,Machine Learning,Scikit Learn,K Fold,嗨,伙计们,我试图在这个保险数据集上执行K-Fold交叉验证,但我试图使用for循环来迭代整数数组。输出显示以下错误: ValueError: The number of folds must be of Integral type. [3, 4, 5, 6, 7, 8, 9, 10, 11, 12] of type <class 'list'> was passed. 在KFold的n_splits参数中传递整个列表ind,而不是迭代其元素i,这是您的意图。还有,为什么要分配一个

嗨,伙计们,我试图在这个保险数据集上执行K-Fold交叉验证,但我试图使用for循环来迭代整数数组。输出显示以下错误:

ValueError: The number of folds must be of Integral type. [3, 4, 5, 6, 7, 8, 9, 10, 11, 12] of type <class 'list'> was passed.

在KFold的
n_splits
参数中传递整个列表
ind
,而不是迭代其元素
i
,这是您的意图。还有,为什么要分配一个列表?你不想这样做吗

for i in range(3, 13):
    kfold = KFold(n_splits=i,shuffle=True,random_state=0)
    model = LinearRegression()
    scores = cross_val_score(model,X,y,cv=kfold,scoring='neg_mean_squared_error')
    print(scores)

在while情况下不能使用for循环。但是你的输出给出了一个1D数组,我试图找出K=3,K=4,…K=12的错误。每个K都应该有Koutputs@Murad241D结果是因为
print(scores)
不在循环中(因此只打印最后一个值),与错误无关(您的问题是关于这个错误的)。这里显示的循环与您的问题中显示的循环完全相同,因此答案解决了问题,我建议您接受它-如您所见,与问题的确切上下文不同(并且没有任何实际增益,因为您的
while
循环实际上与OP使用的
for
循环+列表相同)很容易导致沟通错误,在并非绝对必要时应避免。@desertnaut我无意偏离原始问题,您所说的完全正确,因此我更新了我的答案。感谢您的观察。答案是否有用?
for i in range(3, 13):
    kfold = KFold(n_splits=i,shuffle=True,random_state=0)
    model = LinearRegression()
    scores = cross_val_score(model,X,y,cv=kfold,scoring='neg_mean_squared_error')
    print(scores)