• 线性回归:鸢尾花数据iris


    # encoding: utf-8
    from sklearn.linear_model import LogisticRegression
    import numpy as np
    from sklearn import model_selection
    import matplotlib.pyplot as plt
    import matplotlib as mpl
    from matplotlib import colors
    from sklearn.preprocessing import StandardScaler
    from sklearn.pipeline import Pipeline
    from sklearn.datasets import load_iris
    
    #加载数据
    data=load_iris()
    X ,y=data['data'],data['target']
    x=X[:,0:2]
    x_train,x_test,y_train,y_test=model_selection.train_test_split(x,y,random_state=1,test_size=0.3)
    
    #模型训练
    classifier=Pipeline([('sc',StandardScaler()),('clf',LogisticRegression())])
    classifier.fit(x_train,y_train.ravel())
    
    #绘图
    x1_min, x1_max = x[:, 0].min(), x[:, 0].max()
    x2_min, x2_max = x[:, 1].min(), x[:, 1].max()
    x1, x2 = np.mgrid[x1_min:x1_max:200j, x2_min:x2_max:200j]
    grid_test = np.stack((x1.flat, x2.flat), axis=1)
    grid_hat = classifier.predict(grid_test)
    grid_hat = grid_hat.reshape(x1.shape)
    
    mpl.rcParams['font.sans-serif'] = [u'SimHei']
    mpl.rcParams['axes.unicode_minus'] = False
    cm_light = mpl.colors.ListedColormap(['#A0FFA0', '#FFA0A0', '#A0A0FF'])
    cm_dark = mpl.colors.ListedColormap(['g', 'r', 'b'])
    alpha=0.5
    
    plt.pcolormesh(x1, x2, grid_hat, cmap=cm_light)
    plt.plot(x[:, 0], x[:, 1], 'o', alpha=alpha, color='blue', markeredgecolor='k')
    plt.scatter(x_test[:, 0], x_test[:, 1], s=120, facecolors='none', zorder=10)
    plt.xlabel(u'length', fontsize=13)
    plt.ylabel(u'width', fontsize=13)
    plt.xlim(x1_min, x1_max)
    plt.ylim(x2_min, x2_max)
    plt.title(u'iris', fontsize=15)
    plt.grid()
    plt.show()

  • 相关阅读:
    指针
    day07
    day06
    oracle instr
    动态解析dll及使用类
    客户端调用接口
    Java中调用WebService
    Vs2015智能提示英文
    oracle中varchar(32)转nvarchar(32)
    C#创建XML节点
  • 原文地址:https://www.cnblogs.com/timlong/p/11170667.html
Copyright © 2020-2023  润新知