为了账号安全,请及时绑定邮箱和手机立即绑定

被阻止 - ImportError:无法从“sklearn”导入名称

被阻止 - ImportError:无法从“sklearn”导入名称

慕容3067478 2021-10-10 14:03:15
我正在尝试运行下面的代码。import numpy as npimport matplotlib.pyplot as pltfrom sklearn.metrics import classification_reportfrom sklearn import cross_validationfrom sklearn.tree import DecisionTreeClassifierfrom utilities import visualize_classifier# Load input datainput_file = 'data_decision_trees.txt'data = np.loadtxt(input_file, delimiter=',')X, y = data[:, :-1], data[:, -1]# Separate input data into two classes based on labelsclass_0 = np.array(X[y==0])class_1 = np.array(X[y==1])# Visualize input dataplt.figure()plt.scatter(class_0[:, 0], class_0[:, 1], s=75, facecolors='black',         edgecolors='black', linewidth=1, marker='x')plt.scatter(class_1[:, 0], class_1[:, 1], s=75, facecolors='white',         edgecolors='black', linewidth=1, marker='o')plt.title('Input data')# Split data into training and testing datasets X_train, X_test, y_train, y_test = cross_validation.train_test_split(        X, y, test_size=0.25, random_state=5)# Decision Trees classifier params = {'random_state': 0, 'max_depth': 4}classifier = DecisionTreeClassifier(**params)classifier.fit(X_train, y_train)visualize_classifier(classifier, X_train, y_train, 'Training dataset')y_test_pred = classifier.predict(X_test)visualize_classifier(classifier, X_test, y_test, 'Test dataset')# Evaluate classifier performanceclass_names = ['Class-0', 'Class-1']print("\n" + "#"*40)print("\nClassifier performance on training dataset\n")print(classification_report(y_train, classifier.predict(X_train), target_names=class_names))print("#"*40 + "\n")print("#"*40)print("\nClassifier performance on test dataset\n")print(classification_report(y_test, y_test_pred, target_names=class_names))print("#"*40 + "\n")plt.show()错误发生在这一行:from sklearn import cross_validation我在 Anaconda 命令窗口中尝试了以下操作:conda update scikit-learnpip install -U scikit-learn在这两种情况下,我都得到了“要求已经是最新的”的确认信息。我不确定为什么我在尝试运行脚本时会收到这些错误消息,因为所有 sklearn 的东西似乎都是最新的。还有什么我可以尝试的吗?这段代码肯定可以工作,但它会不断地绊倒自己。谢谢。
查看完整描述

1 回答

?
慕哥9229398

TA贡献1877条经验 获得超6个赞

如果您的 sklearn 是最新的,则 sklearn.cross_validate 模块已被 sklearn.model_selection 弃用。尝试:

from sklearn.model_selection import train_test_split


查看完整回答
反对 回复 2021-10-10
  • 1 回答
  • 0 关注
  • 417 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信