Python练习之——肿瘤预测

良/恶性肿瘤预测问题属于典型的二分类问题,本文采用LR分类器来预测未知肿瘤患者的分类。本次学习任务,训练数据集有524条数据,测试数据集有174条数据。
数据集信息如下所示:
这里写图片描述

1.读取数据集,采用LR(Logistic Regression)分类器学习,
计算出不同情况下的准确率,并可视化的展示出来。具体实现代码如下所示:

# -*- coding: utf-8 -*-
"""
Created on Fri Mar 17 10:38:46 2017

@author: zch
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression

#读取训练数据集
df_train = pd.read_csv('E:\\Python\\kaggle_exercise\\Datasets\\Breast-Cancer\\breast-cancer-train.csv')
#读取测试数据集
df_test = pd.read_csv('E:\\Python\\kaggle_exercise\\Datasets\\Breast-Cancer\\breast-cancer-test.csv')

#print(df_train.info())
'''该数据总共有4列,除了编号之外,
包含Clump Thickness,Cell Size,Type三个特征,
其中前两个特征是数值型的,最后一项是布尔型,分别代表肿瘤患者的良性与恶性
'''
#选取'Clump Thickness'与'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']]

#绘制良性肿瘤样本点,标记为红色的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')

#绘制x,y轴说明
plt.xlabel('Clump Thickness')
plt.ylabel('Cell Size')
print(plt.show())

#利用numpy中的random函数随机采样直线的截距和系数。
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')

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')
print(plt.show())

#引入LR分类器
lr = LogisticRegression()
#使用前10条训练样本学习直线的系数和截距
lr.fit(df_train[['Clump Thickness','Cell Size']][:10],df_train['Type'][:10])
print("测试准确率:",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='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')
print(plt.show())

#使用所有训练样本学习直线的系数和截距
lr.fit(df_train[['Clump Thickness','Cell Size']],df_train['Type'])
print("测试准确率:",lr.score(df_test[['Clump Thickness','Cell Size']],df_test['Type']))

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')
print(plt.show())

这里写图片描述

图2
图3
图4

2.对比图3、图4,可以很明显的看出,同样采用LR分类器来学习,随着训练样本的增加,分类准确率得到了明显的提升。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值