先得到鸢尾花数据包:

image

image

image

image

image

出错了,不要紧找错误:

image
image
image
import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
iris = load_iris() # 载入数据集
X=iris.data[:,:2] #获取前两列数据,花瓣的长度和宽度 才完整,特征
Y=iris.target #150

#逻辑回归模型

lr=LogisticRegression(C=1e5)
lr.fit(X,Y)
h=0.02
x_min,x_max=X[:,0].min()-0.5,X[:,0].max()+0.5
print(x_min,x_max)
y_min,y_max=X[:,1].min()-0.5,X[:,1].max()+0.5
xx,yy=np.meshgrid(np.arange(x_min,x_max,h),np.arange(y_min,y_max,h))
Z=lr.predict(np.c_[xx.ravel(),yy.ravel()])
Z=Z.reshape(xx.shape)
plt.figure(1,figsize=(8,6))
plt.pcolormesh(xx,yy,Z,cmap=plt.cm.Paired)
plt.scatter(X[:50,0],X[:50,1],c='y',marker='o',label='setosa')
plt.scatter(X[50:100,0],X[50:100,1],c='b',marker='+',label='versicolor')
plt.scatter(X[100:,0],X[100:,1],c='r',marker='x',label='Virginica')
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.xlim(xx.min(),xx.max())
plt.ylim(yy.min(),yy.max())
plt.xticks()
plt.yticks()
plt.legend(loc=1)
plt.show()
image.png