美文网首页
《机器学习及实践——从零开始通往KAGGLE竞赛之路》读书笔记二

《机器学习及实践——从零开始通往KAGGLE竞赛之路》读书笔记二

作者: 风之旅人c | 来源:发表于2020-02-27 23:45 被阅读0次

Python综合实践

#导入pandas工具包,并且重命名位pd
import pandas as pd

#调用pandas工具包的read_csv函数,传入训练文件地址参数,获得返回的数据并且存至变量df_train。

df_train = pd.read_csv('C:\\Users\\cao\\Jupyter\\Datasets\\Breast-Cancer\\breast-cancer-train.csv')
df_test = pd.read_csv('C:\\Users\\cao\\Jupyter\\Datasets\\Breast-Cancer\\breast-cancer-test.csv')

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')
<matplotlib.collections.PathCollection at 0x1ed75d4c7f0>
output_2_1
#绘制恶性肿瘤样本点,标记位黑色的x。
plt.scatter(df_test_positive['Clump Thickness'], df_test_positive['Cell Size'], marker = 'x', s = 150, c = 'black')
<matplotlib.collections.PathCollection at 0x1ed75db43c8>
output_3_1
#绘制x,y轴的说明
plt.xlabel('Clump Thickness')
plt.ylabel('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')
plt.show()
output_4_0
import numpy as np

#利用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')
[<matplotlib.lines.Line2D at 0x1ed75e5f9b0>]
output_5_1
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()
output_6_0
from sklearn.linear_model import LogisticRegression
lr = LogisticRegression()

#使用前10条训练样本学习直线的系数和截距
lr.fit(df_train[['Clump Thickness', 'Cell Size']][:10], df_train['Type'][:10])
print('Testing accuract (10 training samples):',lr.score(df_test[['Clump Thickness','Cell Size']], df_test['Type']))
Testing accuract (10 training samples): 0.8342857142857143
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')
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()
output_8_0
lr = LogisticRegression()

#使用所有训练样本学习直线的系数和截距
lr.fit(df_train[['Clump Thickness', 'Cell Size']], df_train['Type'])
print('Testing accuracy (all training samples):', lr.score(df_test[['Clump Thickness', 'Cell Size']], df_test['Type']))
Testing accuracy (all training samples): 0.9371428571428572
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 = 'x', s = 150, c = 'black')
plt.xlabel('Clump Thickness')
plt.ylabel('Cell Size')
plt.show()
output_10_0

相关文章

网友评论

      本文标题:《机器学习及实践——从零开始通往KAGGLE竞赛之路》读书笔记二

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