ML之LoR:基于LoR(逻辑回归)算法对乳腺癌肿瘤(9+1)进行二分类预测(良/恶性)
ML之LoR:基于LoR(逻辑回归)算法对乳腺癌肿瘤(9+1)进行二分类预测(良/恶性)
输出结果
Testing accuracy (10 training samples): 0.8685714285714285
Testing accuracy (all training samples): 0.9371428571428572
设计思路
数据集
可知该原始数据共有699条样本,每条样本有11列不同的数值:1列用于检索的id,9列与肿瘤相关的医学特征,以及一列表征肿瘤类型的数值。所有9列用于表示肿瘤医学特质的数值均被量化为1~10之间的数字,而肿瘤的类型也借由数字2和数字4分别指代良性与恶性。不过,这份数据也声明其中包含16个缺失值,并且用“?”标出。本代码中,对于存在缺失值的数据,都暂时子以忽略。
Number of Instances:699 (as of 15 July 1992)
Number of Attributes:10 plus the class attribute
Attribute Information:(class attribute has been moved to last column
#Attribute Domain
1 .Sample code number id number
2 .Clump Thickness 1一10
3 .Uniformity of Cell 1一10
4 .Uniformity of Cell 1一10
5 .Marginal Adhesion 1一10
6 .Single Epithelial Cell Size 1一10
7 .Bare Nuclei 1一10
8 .Bland Chromatin 1一10
9. Normal Nucleoli 1一10
10 .Mitoses 1一10
11 .Class: (2 for benign, 4 for malignant)
核心代码
import numpy as np
intercept = np.random.random([1])
coef = np.random.random([2])
lx = np.arange(0, 12)
ly = (-intercept - lx * coef[0]) / coef[1]
from sklearn.linear_model import LogisticRegression
lr = LogisticRegression()
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, :]
ly = (-intercept - lx * coef[0]) / coef[1]
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']))
intercept = lr.intercept_
coef = lr.coef_[0, :]
ly = (-intercept - lx * coef[0]) / coef[1]