gpt4 book ai didi

python - 被 ImportError : cannot import name 'cross_validation' from 'sklearn' 阻止

转载 作者:行者123 更新时间:2023-12-01 01:17:43 28 4
gpt4 key购买 nike

我正在尝试运行下面的代码。

import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import classification_report
from sklearn import cross_validation
from sklearn.tree import DecisionTreeClassifier

from utilities import visualize_classifier

# Load input data
input_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 labels
class_0 = np.array(X[y==0])
class_1 = np.array(X[y==1])

# Visualize input data
plt.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 performance
class_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-learn

pip install -U scikit-learn

在这两种情况下,我都收到了确认消息“要求已是最新的”。我不知道为什么当我尝试运行脚本时会收到这些错误信息,因为所有 sklearn 的东西似乎都是最新的。还有什么我可以尝试的吗?这段代码肯定可以工作,但它总是会自己绊倒。谢谢。

最佳答案

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

from sklearn.model_selection import train_test_split

关于python - 被 ImportError : cannot import name 'cross_validation' from 'sklearn' 阻止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54165894/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com