from sklearn.datasets import load_iris from sklearn.linear_model import LogisticRegression import numpy as np import matplotlib.pyplot as plt def main(): # 加载数据集,只加载两个特征 iris = load_iris() x = iris.data[:, :2] y = iris.target h = .02 # 逻辑回归训练并预测 lr = LogisticRegression(C=1e5) lr.fit(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 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=(4, 3)) plt.pcolormesh(xx, yy, Z, cmap=plt.cm.Paired) 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() if __name__ == "__main__": main()