zoukankan      html  css  js  c++  java
  • 机器学习之路:python线性回归分类器 LogisticRegression SGDClassifier 进行良恶性肿瘤分类预测

     

    使用python3 学习了线性回归的api

    分别使用逻辑斯蒂回归  和   随机参数估计回归 对良恶性肿瘤进行预测

    我把数据集下载到了本地,可以来我的git下载源代码和数据集:https://github.com/linyi0604/MachineLearning

      1 import numpy as np
      2 import pandas as pd
      3 from sklearn.cross_validation import train_test_split
      4 from sklearn.preprocessing import StandardScaler
      5 from sklearn.linear_model import  LogisticRegression, SGDClassifier
      6 from sklearn.metrics import classification_report
      7 
      8 '''
      9 线性分类器
     10 最基本和常用的机器学习模型
     11 受限于数据特征与分类目标的线性假设
     12 逻辑斯蒂回归 计算时间长,模型性能略高
     13 随机参数估计 计算时间短,模型性能略低
     14 '''
     15 
     16 '''
     17 1 数据预处理
     18 '''
     19 # 创建特征列表
     20 column_names = ['Sample code number', 'Clump Thickness', 'Uniformity of Cell Size',
     21                 'Uniformity of Cell Shape', 'Marginal Adhesion', 'Single Epithelial Cell size',
     22                 'Bare Nuclei', 'Bland Chromatin', 'Normal Nucleoli', 'Mitoses', 'Class']
     23 # 使用pandas.read_csv取数据集
     24 data = pd.read_csv('./data/breast/breast-cancer-wisconsin.data', names=column_names)
     25 # 将?替换为标准缺失值表示
     26 data = data.replace(to_replace='?', value=np.nan)
     27 # 丢失带有缺失值的数据 只要有一个维度有缺失就丢弃
     28 data = data.dropna(how='any')
     29 # 输出data数据的数量和维度
     30 # print(data.shape)
     31 
     32 
     33 '''
     34 2 准备 良恶性肿瘤训练、测试数据部分
     35 '''
     36 # 随机采样25%数据用于测试 75%数据用于训练
     37 x_train, x_test, y_train, y_test = train_test_split(data[column_names[1:10]],
     38                                                     data[column_names[10]],
     39                                                     test_size=0.25,
     40                                                     random_state=33)
     41 # 查验训练样本和测试样本的数量和类别分布
     42 # print(y_train.value_counts())
     43 # print(y_test.value_counts())
     44 '''
     45 训练样本共512条 其中344条良性肿瘤  168条恶性肿瘤
     46 2    344
     47 4    168
     48 Name: Class, dtype: int64
     49 测试数据共171条 其中100条良性肿瘤 71条恶性肿瘤
     50 2    100
     51 4     71
     52 Name: Class, dtype: int64
     53 '''
     54 
     55 
     56 '''
     57 3 机器学习模型进行预测部分
     58 '''
     59 # 数据标准化,保证每个维度特征的方差为1 均值为0 预测结果不会被某些维度过大的特征值主导
     60 ss = StandardScaler()
     61 x_train = ss.fit_transform(x_train)     # 对x_train进行标准化
     62 x_test = ss.transform(x_test)       # 用与x_train相同的规则对x_test进行标准化,不重新建立规则
     63 
     64 # 分别使用 逻辑斯蒂回归 和 随机参数估计 两种方法进行学习预测
     65 
     66 lr = LogisticRegression()   # 初始化逻辑斯蒂回归模型
     67 sgdc = SGDClassifier()  # 初始化随机参数估计模型
     68 
     69 # 使用 逻辑斯蒂回归 在训练集合上训练
     70 lr.fit(x_train, y_train)
     71 # 训练好后 对测试集合进行预测 预测结果保存在 lr_y_predict中
     72 lr_y_predict = lr.predict(x_test)
     73 
     74 # 使用 随机参数估计 在训练集合上训练
     75 sgdc.fit(x_train, y_train)
     76 # 训练好后 对测试集合进行预测 结果保存在 sgdc_y_predict中
     77 sgdc_y_predict = sgdc.predict(x_test)
     78 
     79 '''
     80 4 性能分析部分
     81 '''
     82 # 逻辑斯蒂回归模型自带评分函数score获得模型在测试集合上的准确率
     83 print("逻辑斯蒂回归准确率:", lr.score(x_test, y_test))
     84 # 逻辑斯蒂回归的其他指标
     85 print("逻辑斯蒂回归的其他指标:
    ", classification_report(y_test, lr_y_predict, target_names=["Benign", "Malignant"]))
     86 
     87 # 随机参数估计的性能分析
     88 print("随机参数估计准确率:", sgdc.score(x_test, y_test))
     89 # 随机参数估计的其他指标
     90 print("随机参数估计的其他指标:
    ", classification_report(y_test, sgdc_y_predict, target_names=["Benign", "Malignant"]))
     91 
     92 '''
     93 recall 召回率
     94 precision 精确率
     95 fl-score
     96 support
     97 
     98 逻辑斯蒂回归准确率: 0.9707602339181286
     99 逻辑斯蒂回归的其他指标:
    100               precision    recall  f1-score   support
    101 
    102      Benign       0.96      0.99      0.98       100
    103   Malignant       0.99      0.94      0.96        71
    104 
    105 avg / total       0.97      0.97      0.97       171
    106 
    107 随机参数估计准确率: 0.9649122807017544
    108 随机参数估计的其他指标:
    109               precision    recall  f1-score   support
    110 
    111      Benign       0.97      0.97      0.97       100
    112   Malignant       0.96      0.96      0.96        71
    113 
    114 avg / total       0.96      0.96      0.96       171
    115 '''
  • 相关阅读:
    散列表(Hash Table)
    MVC中TextBox事件
    AJAX控制DropDownList两级联动
    唯一标示
    检查对象属性是否有空值
    foreach枚举div控制单个显示
    JS获取DropDownList其中一项的文本值
    随便
    MVC常用
    不可用输入框
  • 原文地址:https://www.cnblogs.com/Lin-Yi/p/8970510.html
Copyright © 2011-2022 走看看