尝试在pandas中使用plt.countourf绘制图形时,如何解决此错误?

在关于机器学习的练习中,我想使用matplotlib软件包中包含的countourf函数正常绘制2D图。但是我得到了错误提示:模块'matplotlib.pyplot'没有属性'countourf'。我在下面的代码中添加了它。我正在使用anaconda3平台和Jupyter 6.0.1,我确定此功能存在于matplotlib(版本3.1.1)中,但是,我不知道为什么会发生此错误。任何人都可以,请帮助我度过这个阶段并策划我的dada吗? 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,(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,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_train
      4 X1,step = 0.5))
----> 5 ax = plt.countourf(X1,'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?`

yangxiong9852 回答:尝试在pandas中使用plt.countourf绘制图形时,如何解决此错误?

我认为您正在寻找的是“轮廓”而不是“ countourf”功能。

以下是“ contourf”功能文档的链接:

  

https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.contourf.html

matplotlib库根本没有“ countourf”功能。

我希望能有所帮助。

本文链接:https://www.f2er.com/3110252.html

大家都在问