在《Python数据科学手册》这本书的第306页“有监督学习:鸢尾花数据分类”这一小节中,如果按照其原文
from sklearn.cross_validation import train_test_split
Xtrain, Xtest, ytrain, ytest = train_test_split(X_iris, y_iris,
random_state=1)
会报错
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-92851cd519f2> in <module>
----> 1 from sklearn.cross_validation import train_test_split
2 Xtrain, Xtest, ytrain, ytest = train_test_split(X_iris, y_iris,
3 random_state=1)
ModuleNotFoundError: No module named 'sklearn.cross_validation'
这是因为目前,这个train_test_split 是根据如下的语句导入的:
from sklearn.model_selection import train_test_split
#进行训练集和测试集的划分
Xtrain,Xtest,ytrain,ytest = train_test_split(X_iris,y_iris,random_state=1)
再之后按照书上正常敲代码即可 正常运行
#接下来用模型来预测标签
from sklearn.naive_bayes import GaussianNB #1.选择模型类
model =GaussianNB() #2.初始化模型
model.fit(Xtrain,ytrain) #3.用模型拟合数据
y_model = model.predict(Xtest) #4.对新数据进行预测
#最后验证模型预测结果的准确率
from sklearn.metrics import accuracy_score
accuracy_score(ytest,y_model)
如果觉着有用劳您动动小手给我点个赞哦