python基础综合实践

作者: idatadesign | 来源:发表于2017-11-12 20:36 被阅读21次
    import pandas as pd
    df_train=pd.read_csv('/Users/daqi/Downloads/Python机器学习及实践/Datasets/Breast-Cancer/breast-cancer-train.csv')
    df_test=pd.read_csv('/Users/daqi/Downloads/Python机器学习及实践/Datasets/Breast-Cancer/breast-cancer-test.csv')
    

    选取‘clump’与‘cell size’作为特征,构建测试集中的正负分类样本。

    #良性肿瘤样本点
    df_test_negative=df_test.loc[df_test['Type']==0][['Clump Thickness','Cell Size']]
    #恶性肿瘤样本点
    df_test_positive=df_test.loc[df_test['Type']==1][['Clump Thickness','Cell Size']]
    
    import matplotlib.pyplot as plt
    #良性肿瘤样本点,标记为红色的o
    plt.scatter(df_test_negative['Clump Thickness'],df_test_negative['Cell Size'],marker='o',s=200,c='red')
    #恶性肿瘤样本点,标记为黑色的x
    plt.scatter(df_test_positive['Clump Thickness'],df_test_positive['Cell Size'],marker='x',s=150,c='black')
    

    绘制图1-2

    #绘制x,y轴
    plt.xlabel('Clump Thickness')
    plt.ylabel('Cell Size')
    plt.show()
    
    import numpy as np
    #利用numpy中的random函数随机采样直线的截距和系数
    #截距
    intercept=np.random.random([1])
    #系数
    coef=np.random.random([2])
    #创建等差数组
    lx=np.arange(0,12)
    #实际上表达式是:coef[0]*lx + coef[1]*ly + intercept = 0
    ly=(-intercept-lx*coef[0])/coef[1]
    #绘制一条随机直线
    plt.plot(lx,ly,c='yellow')
    

    绘制图1-3

    plt.scatter(df_test_negative['Clump Thickness'],df_test_negative['Cell Size'],marker='o',s=200,c='red')
    plt.scatter(df_test_positive['Clump Thickness'],df_test_positive['Cell Size'],marker='x',s=150,c='black')
    plt.xlabel('Clump Thickness')
    plt.ylabel('Cell Size')
    plt.show()
    

    导入sklearn中的逻辑斯蒂回归分类器

    from sklearn.linear_model import LogisticRegression
    lr=LogisticRegression()
    

    使用前10条训练样本学习直线的系数和截距

    lr.fit(df_train[['Clump Thickness','Cell Size']][:10],df_train['Type'][:10])
    print('Testing accuracy (10 training sample):',lr.score(df_test[['Clump Thickness','Cell Size']],df_test['Type']))
    

    Testing accuracy (10 training sample): 0.868571428571

    intercept=lr.intercept_
    # 原本这个分类面应该是lx*coef[0] + ly*coef[1] + intercept=0 映射到2维平面上之后,应该是:  
    ly = (-intercept - lx * coef[0]) / coef[1]  
    

    绘制图1-4

    plt.plot(lx,ly,c='green')
    plt.scatter(df_test_negative['Clump Thickness'],df_test_negative['Cell Size'],marker='o',s=200,c='red')
    plt.scatter(df_test_positive['Clump Thickness'],df_test_positive['Cell Size'],marker='x',s=150,c='black')
    plt.xlabel('Clump Thickness')
    plt.ylabel('Cell Size')
    plt.show()
    
    lr=LogisticRegression()
    # 使用所有训练样本学习直线的系数和截距  
    lr.fit(df_train[['Clump Thickness','Cell Size']],df_train['Type'])
    print('Testing accuracy (10 training sample):',lr.score(df_test[['Clump Thickness','Cell Size']],df_test['Type']))
    

    Testing accuracy (10 training sample): 0.937142857143

    intercept=lr.intercept_
    coef=lr.coef_[0,:]
    ly = (-intercept - lx * coef[0]) / coef[1]  
    

    绘制图1-5

    plt.plot(lx,ly,c='blue')
    plt.scatter(df_test_negative['Clump Thickness'],df_test_negative['Cell Size'],marker='o',s=200,c='red')
    plt.scatter(df_test_positive['Clump Thickness'],df_test_positive['Cell Size'],marker='x',s=150,c='black')
    plt.xlabel('Clump Thickness')
    plt.ylabel('Cell Size')
    plt.show()
    

    相关文章

      网友评论

        本文标题:python基础综合实践

        本文链接:https://www.haomeiwen.com/subject/xouomxtx.html