良/恶性乳腺癌肿瘤预测---线性分析
2018-02-01 16:13
190 查看
1 线性分类器
介绍:假设特征与分类结果存在线性关系的模型;通过累加计算每个维度的特征与各自权重的乘积帮助类别决策。
2 实验代码及截图
#coding:utf-8
import pandas as pd
import numpy as np
#创建特征列表
column_names=['samples code number','Clump Thickness','Uniformity of Cell Size','Uniformity of Cell Shape','Marginal Adhesion',
'SIngle Epithelial Cell Size','Bare Nuclei','Bland Chromatin','Normal Nucleoli','Mitoses','Class']
#使用pandas.read_csv函数读取指定数据
data=pd.read_csv('/home/guo/桌面/Work/shuju/Breast-Cancer/breast-cancer-wisconsin.data',names=column_names)
#将?替换为标准缺失值表示
data=data.replace(to_replace='?',value=np.nan)
#丢弃带有缺失值的数据(只有有一个维度有缺失)
data=data.dropna(how='any')
#输出data的数据量和维度
print("数据量 维度")
print(data.shape)
#使用sklearn.cross_valiation中train_test_split模块用于分割数据
from sklearn.cross_validation import train_test_split
#随机25%用于测试,其他用于训练
X_train,X_test,y_train,y_test=train_test_split(data[column_names[1:10]],data[column_names[10]],test_size=0.25,random_state=33)
#查验训练样本的数量和类别
print('训练样本的数量和类别')
print(y_train.value_counts())
#查看测试样本的数量和类别
print('测试样本的数量和类别')
print(y_test.value_counts())
#使用线性分类模型预测
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import SGDClassifier
#标准化数据,保证每个维度的特征数据方差为1,均值为0
ss=StandardScaler()
X_train=ss.fit_transform(X_train)
X_test=ss.transform(X_test)
#初始化
lr=LogisticRegression()
sgdc=SGDClassifier()
#调用fit训练模型参数
lr.fit(X_train, y_train)
#进行预测
lr_y_predict=lr.predict(X_test)
#调用sgdclassifier中的fit训练模型
sgdc.fit(X_train,y_train)
#对X—test进行预测
sgdc_y_predict=sgdc.predict(X_test)
#使用线性分类模型记性预测任务的性能分析
from sklearn.metrics import classification_report
#获得测试集上的准确性结果
print('线性分类模型')
print('Accuracy of LR Classifier:',lr.score(X_test,y_test))
#获得其他三个自指标的结果
print classification_report(y_test,lr_y_predict,target_names=['Benign','Malignant'])
#使用随机梯度下降模型获得在测试集上的准确性结果
print('随机梯度下降模型')
print('Accuracy of SGD Classifier:',sgdc.score(X_test,y_test))
print classification_report(y_test,sgdc_y_predict,target_names=['Benign','Malignant'])
3 本文在最后使用了两种模型进行了性能的比较,以实验结果来看,LR相较于SDG在测试集表现有更高的准确性;
介绍:假设特征与分类结果存在线性关系的模型;通过累加计算每个维度的特征与各自权重的乘积帮助类别决策。
2 实验代码及截图
#coding:utf-8
import pandas as pd
import numpy as np
#创建特征列表
column_names=['samples code number','Clump Thickness','Uniformity of Cell Size','Uniformity of Cell Shape','Marginal Adhesion',
'SIngle Epithelial Cell Size','Bare Nuclei','Bland Chromatin','Normal Nucleoli','Mitoses','Class']
#使用pandas.read_csv函数读取指定数据
data=pd.read_csv('/home/guo/桌面/Work/shuju/Breast-Cancer/breast-cancer-wisconsin.data',names=column_names)
#将?替换为标准缺失值表示
data=data.replace(to_replace='?',value=np.nan)
#丢弃带有缺失值的数据(只有有一个维度有缺失)
data=data.dropna(how='any')
#输出data的数据量和维度
print("数据量 维度")
print(data.shape)
#使用sklearn.cross_valiation中train_test_split模块用于分割数据
from sklearn.cross_validation import train_test_split
#随机25%用于测试,其他用于训练
X_train,X_test,y_train,y_test=train_test_split(data[column_names[1:10]],data[column_names[10]],test_size=0.25,random_state=33)
#查验训练样本的数量和类别
print('训练样本的数量和类别')
print(y_train.value_counts())
#查看测试样本的数量和类别
print('测试样本的数量和类别')
print(y_test.value_counts())
#使用线性分类模型预测
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import SGDClassifier
#标准化数据,保证每个维度的特征数据方差为1,均值为0
ss=StandardScaler()
X_train=ss.fit_transform(X_train)
X_test=ss.transform(X_test)
#初始化
lr=LogisticRegression()
sgdc=SGDClassifier()
#调用fit训练模型参数
lr.fit(X_train, y_train)
#进行预测
lr_y_predict=lr.predict(X_test)
#调用sgdclassifier中的fit训练模型
sgdc.fit(X_train,y_train)
#对X—test进行预测
sgdc_y_predict=sgdc.predict(X_test)
#使用线性分类模型记性预测任务的性能分析
from sklearn.metrics import classification_report
#获得测试集上的准确性结果
print('线性分类模型')
print('Accuracy of LR Classifier:',lr.score(X_test,y_test))
#获得其他三个自指标的结果
print classification_report(y_test,lr_y_predict,target_names=['Benign','Malignant'])
#使用随机梯度下降模型获得在测试集上的准确性结果
print('随机梯度下降模型')
print('Accuracy of SGD Classifier:',sgdc.score(X_test,y_test))
print classification_report(y_test,sgdc_y_predict,target_names=['Benign','Malignant'])
3 本文在最后使用了两种模型进行了性能的比较,以实验结果来看,LR相较于SDG在测试集表现有更高的准确性;
相关文章推荐
- 使用Tensorflow自定义一个线性分类器对‘良/恶性乳腺癌肿瘤’进行预测
- 良/恶性乳腺癌肿瘤预测
- 以“良/恶性乳腺癌肿瘤预测”为例学习二分类问题
- P142-144使用Tensorflow自定义一个线性分类器用于对“良/恶性乳腺癌肿瘤”进行预测
- 良恶性乳腺癌肿瘤预测
- 机器学习之良/恶性乳腺癌肿瘤预测
- 《Python机器学习及实践》----良/恶性乳腺癌肿瘤预测
- DT:DT实现根据乳腺肿瘤特征向量高精度预测肿瘤的是恶性还是良性—Jason niu
- 【自然语言处理入门】03:利用线性回归对数据集进行分析预测(上)
- 在PostgreSQL中用线性回归分析linear regression做预测
- 在PostgreSQL中用线性回归分析linear regression做预测 - 例子1
- 数据分析之乳腺癌预测
- 在PostgreSQL中用线性回归分析linear regression做预测 - 例子2, 预测未来数日某股收盘价
- 【自然语言处理入门】03:利用线性回归对数据集进行分析预测(下)
- 机器学习之线性分类器(Linear Classifiers)——肿瘤预测实例
- 线性回归分析及预测
- 使用线性分类模型从事良/恶性肿瘤预测任务
- 语音信号分析之重要工具: 线性预测分析
- Logistic 回归线性回归-概率分析
- 线性判别分析(Linear Discriminant Analyst)