Python 3.x - AttributeError: 'function' object has no attribute 'Kfold'

Python 3.x - AttributeError: 'function' object has no attribute 'Kfold'

我正在学习一个较旧的教程,其中包含使用遗传算法优化的 SVM。我最初认为问题只是 python and/or scikit 的版本,但我现在不确定问题可能是什么,因为它继续显示相同的错误。我目前在 Antergos 上使用 python-scikit-learn 0.20.3-1,发现 link 不幸的是似乎没有帮助。

到目前为止,我发现了一些 links 和示例,它们让我改变了代码的不同方面,总体上只是把所有东西都弄乱了。 This GitHub page was useful in at least understanding the version difference, as was the first link. This blog post was also neat, but again didn't really help me narrow down the exact issue as to why it's reading out the error. I even tried looking at the sklearn documentation但是还是没看懂

这些是我导入的:

import numpy as np
import pandas as pd
import random as rd
from sklearn.model_selection import cross_validate
from sklearn import preprocessing
from sklearn import svm

我在程序的前面定义了 "kfold":

kfold = 3

同样,这正是它似乎有问题的行:

kf = cross_validate.KFold(Cnt1,n_splits=kfold)

它应该做的只是应用交叉验证。但是,错误显示为:

AttributeError: 'function' object has no attribue 'KFold'

我无法判断问题是我不理解我应该通过我给出的 link 更改的内容,还是由于无知而产生的不同错误。为了使它正常工作,我是否缺少某些东西?

KFold 函数在 sklearn.model_selection 模块中,不在 sklearn.model_selection.cross_validate

所以你应该导入

from sklearn import model_selection

然后使用 like

model_selection.KFold(...)

或者您可以导入函数

from sklearn.model_selection import KFold

就像在 KFold Doc 示例中一样。