Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在pandas中尝试使用plt.countourf绘制图形时,如何修复此错误?_Python_Pandas_Machine Learning_Logistic Regression_Matplotlib Basemap - Fatal编程技术网

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

Python 在pandas中尝试使用plt.countourf绘制图形时,如何修复此错误?,python,pandas,machine-learning,logistic-regression,matplotlib-basemap,Python,Pandas,Machine Learning,Logistic Regression,Matplotlib Basemap,在这个关于机器学习的练习中,我想使用matplotlib包中包含的countourf函数normaly绘制一个2D图形。但是我得到了错误信息:模块“matplotlib.pyplot”没有属性“countourf”。我在下面的代码中输入了它。我使用的是anaconda3平台和Jupyter 6.0.1,我确信matplotlib(版本3.1.1)中存在此函数,但是我不知道为什么会发生此错误。谁能帮我渡过这个阶段,策划我的爸爸。提前谢谢你 `导入库` 作为pd进口熊猫 将numpy作为np导入 将

在这个关于机器学习的练习中,我想使用matplotlib包中包含的countourf函数normaly绘制一个2D图形。但是我得到了错误信息:模块“matplotlib.pyplot”没有属性“countourf”。我在下面的代码中输入了它。我使用的是anaconda3平台和Jupyter 6.0.1,我确信matplotlib(版本3.1.1)中存在此函数,但是我不知道为什么会发生此错误。谁能帮我渡过这个阶段,策划我的爸爸。提前谢谢你

`导入库`
作为pd进口熊猫
将numpy作为np导入
将scipy作为sp导入
将matplotlib.pyplot作为plt导入
以某人的身份出生
%matplotlib内联
`导入数据`
SUV_data=pd.read_csv(“C:/Users/Wolf/Documents/ExoData/data Science Tutorial/archive/SocialNetworkAds.csv”)
SUV_数据
`仅提取依赖变量`
x=SUV_数据.iloc[:,[2,3]]。值
`仅提取独立变量`
y=SUV_data.iloc[:,4]。值
打印(“x_因变量=“+str(x))
打印(“y_独立变量=“+str(y))
x、 形状,y形
((400, 2), (400,))
`绘制地图,显示因变量和独立变量如何相互关联(兼性)`
YES_map=sb.热图(SUV_data.corr())
`将数据拆分为训练模型和集合模型`
从sklearn.model\u选择导入列车\u测试\u拆分
X_序列,X_测试,y_序列,y_测试=序列测试分割(X,y,测试大小=0.25,随机状态=0)
X_train.shape,y_train.shape
((300, 2), (300,))
`训练数据的logistic回归拟合`
从sklearn.linear_模型导入逻辑回归
分类器=逻辑回归(随机状态=0)
分类器。配合(X_系列,y_系列)
`预测测试集结果`
y_pred=分类器。预测(X_测试)
尤普雷德
`可视化训练集结果`
从matplotlib.colors导入ListedColormap
X_集,y_集=X_列,y_列
X1,X2=np.meshgrid(np.arange(start=X_集[:,0].min()-1,stop=X_集[:,0].max()+1,step=0.5),np.arange(start=X_集[:,1].min()-1,stop=X_集[:,1].max()+1,step=0.5))
plt.countourf(X1,X2,classifier.predict(np.array([X1.ravel(),X2.ravel()]).T).重塑(X1.shape),alpha=0.75,cmap=ListedColormap('red','green'))
plt.xlim(X1.mmin(),X1.max())
plt.ylim(X2.min(),X2.max())
对于枚举中的i,j(np.unique(y_集)):
plt.scatter(X_set[y_set==j,0],X_set[y_set==j,1],c=ListedColormap(('red','green'))(i),label=j)
产品名称(‘逻辑回归(训练集)’)
plt.xlabel(“年龄”)
plt.ylabel(“估计工资”)
plt.legend()
plt.show()
AttributeError回溯(最近一次呼叫上次)
在里面
3个X_组,y_组=X_列,y_列
4x1,X2=np.meshgrid(np.arange(start=X_集[:,0].min()-1,stop=X_集[:,0].max()+1,step=0.5),np.arange(start=X_集[:,1].min()-1,stop=X_集[:,1].max()+1,step=0.5))
---->5 ax=plt.countourf(X1,X2,分类器.predict(np.array([X1.ravel(),X2.ravel()]).T)。重塑(X1.shape),alpha=0.75,cmap=ListedColormap(‘红色’,‘绿色’))
6 plt.xlim(X1.mmin(),X1.max())
7 plt.ylim(X2.min(),X2.max())
AttributeError:模块“matplotlib.pyplot”没有属性“countourf”
`这里是发生错误的地方。如何使用matplotlib中的countorf函数或其他函数修复它`

我认为您需要的是“contourf”功能,而不是“countourf”功能

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

matplotlib库根本没有“countourf”函数


我希望这会有所帮助。

同时,我将卸载matplotlib,然后重新安装,看看是否可以获得更好的结果。是的!!!非常感谢你!我一点也没看见!现在有东西出现了!该死谢谢你的评论,现在没事了!Karl,您可以将答案标记为正确,以便其他用户立即发现它解决了问题
`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?`