正在读取一条错误消息:ModuleNotFoundError:没有名为“ matplotlib.artist”的模块

时间:2019-01-12 17:33:34

标签: python python-3.x

我正在尝试运行下面的代码,这些代码是在AI for Python书中找到的。

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()

当我尝试运行上面的代码时,出现此错误:

ModuleNotFoundError: No module named 'matplotlib.artist'

有点奇怪,因为我在名为“ matplotlib.artist”的代码中没有任何内容。因此,这必须依赖于我正在运行的代码中的某些内容。该代码肯定在很大程度上依赖scikit学习,因此在重新运行上述代码之前,请确保更新所有软件包。

conda update scikit-learn 

我仍然遇到相同的错误:

No module named 'matplotlib.artist'

1 个答案:

答案 0 :(得分:0)

有人在Github上发布了类似的问题,他建议重新安装matplotlib。

https://github.com/matplotlib/matplotlib/issues/6771/