本文是以《Python机器学习及实践 从零开始通往kaggle竞赛之路》为参考书籍进行的实践
1 环境
ubuntu16.04+Python 2.7
其中,python 下需要安装相应的扩展包:Numpy ,Pandas ,Matplotlib ,Scikit-learn 。
2 实验代码及实验结果截图
#coding:utf-8
#良/恶性乳腺癌肿瘤预测
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
#传入测试文件地址
df_train=pd.read_csv('/home/guo/桌面/Work/shuju/Breast-Cancer/breast-cancer-train.csv')
df_test=pd.read_csv('/home/guo/桌面/Work/shuju/Breast-Cancer/breast-cancer-test.csv')
#//特征选取5
df_test_negative=df_test.loc[df_test['Type']==0][['Clump Thickness','Cell Size']]
df_test_positive=df_test.loc[df_test['Type']==0][['Clump Thickness','Cell Size']]
#//绘图,良性
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')
#//绘制x,y轴的说明
plt.xlabel('Clump Thickness')
plt.ylabel('Cell Size')
#//显示
plt.show()
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='o',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 samples):',lr.score(df_test[['Clump Thickness','Cell Size']],df_test['Type']))
<background="black">
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='o',s=150,c='black')
plt.xlabel('Clump lineThickness')
plt.ylabel('Cell Size')
plt.show()
lr=LogisticRegression()
#使用所有训练样本学习直线的系数和截距
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_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='o',s=150,c='black')
plt.xlabel('Clump lineThickness')
plt.ylabel('Cell Size')
plt.show()
lr=LogisticRegression()
#使用所有训练样本学习直线的系数和截距
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']))
数据集:链接: https://pan.baidu.com/s/1mjJORby 密码: ehsh
实验代码亲自测试,绝对可行!