機器學習實戰(2)—— 邏輯迴歸代碼實現

本案例爲:使用邏輯迴歸對良惡性腫瘤二分類

溫馨提示:代碼左右滑動可查看完整

# 導入pandas與numpy工具包。
import pandas as pd
import numpy as np

# 創建特徵列表。
column_names = ['Sample 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('https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data'names = column_names )
data.head()

特徵解釋:

Sample 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 類別 2爲良性,4爲惡性
每個維度特徵都是1~10之間的數字
# 將?替換爲標準缺失值表示。
data = data.replace(to_replace='?'value=np.nan)

# 丟棄帶有缺失值的數據(只要有一個維度有缺失)。
data = data.dropna(how='any')

# 輸出data的數據量和維度。
data.shape

data[column_names[1:10]].head()

#683條數據  9個特徵  1個id  1個分類結果
# 使用sklearn.cross_valiation裏的train_test_split模塊用於分割數據。
from sklearn.cross_validation import train_test_split

# 隨機採樣25%的數據用於測試,剩下的75%用於構建訓練集合。
X_train, X_test, y_train, y_test = train_test_split(data[column_names[1:10]]#特徵:第0列id去掉,第10列分類結果去掉
                                                    data[column_names[10]],#第10列的分類結果作爲目標分類變量 
                                                    test_size=0.25, 
                                                    random_state=33)
# 查驗訓練樣本的數量和類別分佈。
# 即訓練集中良性和惡性數量
y_train.value_counts()

# 查驗測試樣本的數量和類別分佈。
# 即測試集中良性和惡性數量
y_test.value_counts()

# 從sklearn.preprocessing裏導入StandardScaler。
from sklearn.preprocessing import StandardScaler
# 從sklearn.linear_model裏導入LogisticRegression
from sklearn.linear_model import LogisticRegression
# 標準化數據,保證每個維度的特徵數據方差爲1,均值爲0。使得預測結果不會被某些維度過大的特徵值而主導。
ss = StandardScaler()
X_train = ss.fit_transform(X_train)
X_test = ss.transform(X_test)
# 初始化LogisticRegression
#邏輯迴歸
lr = LogisticRegression()
# 調用LogisticRegression中的fit函數/模塊用來訓練模型參數。
lr.fit(X_train, y_train)

# 使用訓練好的模型lr對X_test進行預測,結果儲存在變量lr_y_predict中。
lr_y_predict = lr.predict(X_test)
# 從sklearn.metrics裏導入classification_report模塊。  分類報告
from sklearn.metrics import classification_report
# 使用邏輯斯蒂迴歸模型自帶的評分函數score獲得模型在測試集上的準確性結果。準確性:預測和真實值比較,預測正確的百分比,Accuracy。
print('Accuracy of LR Classifier:', lr.score(X_test, y_test))

在實際問題中,我們往往更加關注模型對某一特定類別的預測能力。這時,準確性指標就變得籠統了。

比如,在良 / 惡腫瘤預測中,醫生和患者往往更加關注有多少惡性腫瘤被正確地診斷出來,因爲這種腫瘤更加致命。

也就是說,在二分類任務下,預測結果和正確標記之間存在 4 種不同的組合,構成混淆矩陣:

如果惡性腫瘤爲陽性,良性腫瘤爲陰性

預測正確的惡性腫瘤爲 真陽性

預測正確的良性腫瘤爲 真陰性

原本是良性腫瘤,預測誤判成惡性腫瘤爲 假陽性

實際是惡性腫瘤,預測模型沒有檢測出來,爲假陰性

事實上,醫生和患者最不願意看到的是有假陰性的結果

因爲這種誤診回耽誤治療時間,進而危及生命!

召回率(Recall)

精確率(Precision)

我們顯然更關注召回率,也就是應該被正確識別的惡性腫瘤的百分比

# 利用classification_report模塊獲得LogisticRegression其他三個指標的結果。
print(classification_report(y_test, lr_y_predict, target_names=['Benign''Malignant']))

#support爲樣本個數

你學會了麼?

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/wq10sBf4aqJCuoOlvKSChQ