使用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("邏輯斯蒂回歸的其他指標:\n", classification_report(y_test, lr_y_predict, target_names=["Benign", "Malignant"])) 86
87 # 隨機參數估計的性能分析
88 print("隨機參數估計准確率:", sgdc.score(x_test, y_test)) 89 # 隨機參數估計的其他指標
90 print("隨機參數估計的其他指標:\n", 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 '''