我刚开始使用Python进行机器学习,目前正在YouTube教程之后尝试进行KNN分类。这是代码。
import numpy as np
#from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_validate
import pandas as pd
df=pd.read_csv('breast-cancer-wisconsin.data.txt')
df.replace('?', -99999, inplace=True)
df.drop(['id'],1,inplace=True)
X=np.array(df.drop(['class'],1))
y=np.array(df['class'])
X_train, X_test, y_train, y_test=cross_validate.train_test_split(X,y,test_size=0.2)
我收到以下错误:-
X_train, X_test, y_train, y_test=cross_validate.train_test_split(X,y,test_size=0.2)
AttributeError: 'function' object has no attribute 'train_test_split'
我尝试将train_test_split导入为
from sklearn.model_selection import train_test_split
但是我得到了同样的错误。任何帮助表示赞赏。谢谢!
答案 0 :(得分:1)
train_test_split
是一个单独的模块(docs),不可与cross_validate
结合使用;正确的用法是(假设scikit-learn v0.20):
from sklearn.model_selection import train_test_split
# [...]
X_train, X_test, y_train, y_test=train_test_split(X,y,test_size=0.2)
答案 1 :(得分:0)
sklearn.cross_validation 。
使用sklearn.model_selection.train_test_split。
答案 2 :(得分:0)
截至 2021 年 4 月,我正在观看相同的教程和正确答案,如下所示:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2)
这里是本课的完整代码:
import numpy as np
from sklearn import preprocessing, neighbors
from sklearn.model_selection import train_test_split
import pandas as pd
df= pd.read_csv('breast-cancer-wisconsin.data')
# print(df.head())
df.replace('?', -99999, inplace=True)
df.drop(['id'], 1, inplace=True)
# print(df.columns)
X= np.array(df.drop(['class'],1))
y= np.array(df['class'])
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2)
clf = neighbors.KNeighborsClassifier()
clf.fit(X_train,y_train)
accuracy= clf.score(X_test,y_test)
print(accuracy)