加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – ValueError:在图中找不到Axes实例参数

发布时间:2020-12-20 13:14:53 所属栏目:Python 来源:网络整理
导读:我正在学习scikit-learn’学习scikit-learn:用 Python学习机器学习RaúlGarreta’. 在jupyter Notebook中,从代码In [1]到In [7],它可以工作.但在[8]中代码不起作用.哪个错了? # In[1]:from sklearn import datasetsiris = datasets.load_iris()X_iris,y_ir
我正在学习scikit-learn’学习scikit-learn:用 Python学习机器学习RaúlGarreta’.

在jupyter Notebook中,从代码In [1]到In [7],它可以工作.但在[8]中代码不起作用.哪个错了?

# In[1]:
from sklearn import datasets
iris = datasets.load_iris()
X_iris,y_iris = iris.data,iris.target
print X_iris.shape,y_iris.shape

# In[2]:
from sklearn.cross_validation import train_test_split
from sklearn import preprocessing
X,y = X_iris[:,:2],y_iris
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.25,random_state=33)
print X_train.shape,y_train.shape

# In[3]:
scaler = preprocessing.StandardScaler().fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)

# In[4]:
get_ipython().magic(u'matplotlib inline')
import matplotlib
from matplotlib import pylab
import numpy as np
import matplotlib.pyplot as plt
colors = ['red','greenyellow','blue']
for i in xrange(len(colors)):
    xs = X_train[:,0][y_train == i]
    ys = X_train[:,1][y_train == i]
    plt.scatter(xs,ys,c=colors[i])
plt.legend(iris.target_names) 
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')

# In[5]:
from sklearn.linear_model import SGDClassifier
clf = SGDClassifier()
clf.fit(X_train,y_train)

# In[6]:
print clf.coef_

# In[7]:
print clf.intercept_

In [8]中的代码不起作用.

# In[8]:
x_min,x_max = X_train[:,0].min() - .5,X_train[:,0].max() +.5
y_min,y_max = X_train[:,1].min() - .5,1].max() +.5
xs = np.arange(x_min,x_max,0.5)
fig,axes = plt.subplots(1,3)
fig.set_size_inches(10,6)
for i in [0,1,2]:
    axes[i].set_aspect('equal')
    axes[i].set_title('Class '+ str(i) + ' versus the rest')
    axes[i].set_xlabel('Sepal length')
    axes[i].set_ylabel('Sepal width')
    axes[i].set_xlim(x_min,x_max)
    axes[i].set_ylim(y_min,y_max)
    pylab.sca(axes[i])
    plt.scatter(X_train[:,0],1],c=y_train,cmap=plt.cm.prism)
    ys = (-clf.intercept_[i] - xs * clf.coef_[i,0]) / clf.coef_[i,1]
    plt.plot(xs,hold=True)
    plt.show()

运行时出现以下错误消息.

enter image description here

解决方法

plt.sca(轴[I])

那没关系

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读