zoukankan      html  css  js  c++  java
  • 《Python machine learning and practice》—— 良\恶性乳腺癌肿瘤预测

    数据集

    良\恶性乳腺癌肿瘤预测数据集

    代码分析

    第三方库文件

    from sklearn.linear_model import LogisticRegression
    #导入sklearn中的逻辑斯蒂回归分类器
    import matplotlib.pyplot as plt
    import pandas as pd
    import numpy as np
    

    都是在机器学习中常用的第三方库。

    导入文件

    df_train=pd.read_csv(r'F:\code\AI\Python machine learning and practice\Datasets\Breast-Cancer\breast-cancer-train.csv')
    df_test=pd.read_csv(r'F:\code\AI\Python machine learning and practice\Datasets\Breast-Cancer\breast-cancer-test.csv')
    

    通过pandas的read_csv导入数据集。

    确定正负分类样本

    df_test_nagative=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']]
    

    选取“Clump Thickness”与“Cell Size”作为特征,构件测试集中的正负分类样本。

    绘制图形

    #绘制良性肿瘤样本点,标记为红色的o
    plt.scatter(df_test_nagative['Clump Thickness'],df_test_nagative['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')
    
    #绘制x、y轴的说明
    plt.xlabel('Clump Thickness')
    plt.ylabel('Cell Size')
    
    #显示图
    plt.show()
    

    在这里插入图片描述

    numpy确定参数

    intercept=np.random.random([1])
    coef=np.random.random([2])
    lx=np.arange(0,12)
    ly=(-intercept-lx*coef[0])/coef[1]
    plt.plot(lx,ly,c='yellow')
    
    

    利用numpy中的random函数随机采样直线的截距和系数。
    在这里插入图片描述

    训练

    lr=LogisticRegression(solver='liblinear')
    
    lr.fit(df_train[['Clump Thickness','Cell Size']][:10],df_train['Type'][:10])
    print('Testing accuracy(10 training samples):',lr.score(df_test[['Clump Thickness','Cell Size']],df_test['Type']))
    
    intercept=lr.intercept_
    coef=lr.coef_[0,:]
    
    # 原本这个分类面应该是lx*coef[0]+ly*coef[1]+intercept=0,映射到2维平面上之后,应该是:
    ly=(-intercept-lx*coef[0])/coef[1]
    
    plt.plot(lx,ly,c='green')
    
    

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

    plt.plot(lx,ly,c='green')
    plt.scatter(df_test_nagative['Clump Thickness'],df_test_nagative['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(solver='liblinear')
    #使用所有的训练样本学习直线的系数和截距
    lr.fit(df_train[['Clump Thickness','Cell Size']],df_train['Type'])
    print('Testing accuracy(10 training samples):',lr.score(df_test[['Clump Thickness','Cell Size']],df_test['Type']))
    
    intercept=lr.intercept_
    coef=lr.coef_[0,:]
    ly=(-intercept-lx*coef[0])/coef[1]
    
    plt.plot(lx,ly,c='blue')
    plt.scatter(df_test_nagative['Clump Thickness'],df_test_nagative['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()
    

    在这里插入图片描述

  • 相关阅读:
    C语言得到当前系统时间
    【solr这四个主题】在Tomcat 部署Solr4.x
    MySQL 一般查询日志(General Query Log)
    iOS7 UIKit动力学-碰撞特性UICollisionBehavior 上
    Java Persistence with MyBatis 3(中国版) 第五章 与Spring集成
    Kaggle入门——使用scikit-learn解决DigitRecognition问题
    Effective C++:规定34:区分接口继承和实现继承
    Critical thinking and Thoughtful writing
    我的时间,GTD做主
    jquery自己主动旋转的登录界面的背景代码登录页背景图
  • 原文地址:https://www.cnblogs.com/AlexKing007/p/12338497.html
Copyright © 2011-2022 走看看