【发布时间】:2020-03-09 05:32:45
【问题描述】:
在这个机器学习练习中,我想使用包含在 matplotlib 包中的 countourf 函数绘制一个二维图形。但我收到错误消息:模块'matplotlib.pyplot'没有属性'countourf'。我在下面的代码中指出了它。我使用的是anaconda3平台和Jupyter 6.0.1,我确信这个函数存在于matplotlib(版本3.1.1)中,但是,我不知道为什么会出现这个错误。谁能帮我度过这个阶段并策划我的达达? Here is the link to find data:。提前谢谢你!
`Importing the Librairies`
import pandas as pd
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
import seaborn as sb
%matplotlib inline
`Importing the data`
SUV_data = pd.read_csv("C:/Users/Wolf/Documents/ExoData/Data Science Tutorial/archive/SocialNetworkAds.csv")
SUV_data
`Extratcting only the dependant variables`
x = SUV_data.iloc[:, [2,3]].values
`Extratcting only the independant variables`
y = SUV_data.iloc[:, 4].values
print("x_dependent variables = " +str(x))
print("y_independent variables = " +str(y))
x.shape, y.shape
((400, 2), (400,))
`Plotting of a map showing how the dependent and independant variables are correlated with each other(facultative)`
YES_map = sb.heatmap(SUV_data.corr())
`Splitting the data into training and set models`
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size = 0.25, random_state = 0)
X_train.shape, y_train.shape
((300, 2), (300,))
`Fitting logistic regression to training data`
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(random_state =0)
classifier.fit(X_train, y_train)
`Predicting the test set esults`
y_pred = classifier.predict(X_test)
y_pred
`Visualize the Training set Results`
from matplotlib.colors import ListedColormap
X_set, y_set = X_train, y_train
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.5), np.arange(start = X_set[:, 1].min() -1, stop = X_set[:, 1].max() + 1, step = 0.5))
plt.countourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.mmin(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], c = ListedColormap(('red', 'green'))(i), label = j)
plt.title('Logistic Regression (Training set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()
AttributeError Traceback (most recent call last)
<ipython-input-27-7bff781bd170> in <module>
3 X_set, y_set = X_train, y_train
4 X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.5), np.arange(start = X_set[:, 1].min() -1, stop = X_set[:, 1].max() + 1, step = 0.5))
----> 5 ax = plt.countourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), alpha = 0.75, cmap = ListedColormap(('red', 'green')))
6 plt.xlim(X1.mmin(), X1.max())
7 plt.ylim(X2.min(), X2.max())
AttributeError: module 'matplotlib.pyplot' has no attribute 'countourf'
`Here is where the error occurs. How to fix it using countorf function or other in matplotlib?`
【问题讨论】:
-
在此期间,我将卸载然后重新安装matplotlib,看看是否可以获得更好的结果。
标签: python pandas machine-learning logistic-regression matplotlib-basemap