逻辑回归是即可用于分类有可用于回归,即自变量既可以是连续的又可以是离散的。
# import some data to play with
iris = datasets.load_iris()
X = iris.data[:, :2] # we only take the first two features.第一维度所有都要,第二维度只要前两个
Y = iris.target
h = .02 # step size in the mesh
logreg = linear_model.LogisticRegression(C=1e5)
# we create an instance of Neighbours Classifier and fit the data.
logreg.fit(X, Y)
# Plot the decision boundary(边界). For that, we will assign a color to each
# point in the mesh [x_min, x_max]x[y_min, y_max].找出X,Y的最值
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
#绘出每个点的xy坐标
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))#meshgrid 坐标格点
#print(xx,yy)
Z = logreg.predict(np.c_[xx.ravel(), yy.ravel()])
# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.figure(1, figsize=(4, 3))#改为40,比30会扩大十倍,其中4和3指的是长和宽为4厘米和3厘米
plt.pcolormesh(xx, yy, Z, cmap=plt.cm.Paired)#增加底色
# Plot also the training points
plt.scatter(X[:, 0], X[:, 1], c=Y, edgecolors='k', cmap=plt.cm.Paired)
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.show()