01.09 機器學習實戰|通過邏輯斯蒂迴歸算法預測及模型性能評估

前面在 一文中介紹了邏輯迴歸模型分類的算法,本文件通過具體實例來看看如何使用邏輯迴歸模型進行預測及對模型進行評估(評估參數已在 中進行了介紹,有興趣的可以點擊鏈接閱讀) 。

機器學習實戰|通過邏輯斯蒂迴歸算法預測及模型性能評估

數據描述分析

癌腫瘤預測數據來源於互聯網,數據下載地址為:

https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/

打開breast-cancer-wisconsin.names ,可以觀察數據的具體描述:

1. Number of Instances: 699 (as of 15 July 1992)

2. Number of Attributes: 10 plus the class attribute

3. 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 Size 1 - 10

4. Uniformity of Cell Shape 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)

4. Missing attribute values: 16

There are 16 instances in Groups 1 to 6 that contain a single missing

(i.e., unavailable) attribute value, now denoted by "?".

5. Class distribution:

Benign: 458 (65.5%)

Malignant: 241 (34.5%)

從上述描述可以看出:

  • ①數據共有699條記錄,共有11列數據,其中第1列為編號,最後1列為分類標籤;其餘9列為具體特徵。
  • ②這份數據共有16個缺失值,均用“?”代替。
  • ③標籤統計:良性癌腫瘤數量為458個,佔比65.50%,惡性數量為241,佔比34.5%。

數據讀取

使用pandas中的read_csv方法從下面鏈接https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data獲取數據,並將描述中的列名通過data.columns傳遞給數據。

機器學習實戰|通過邏輯斯蒂迴歸算法預測及模型性能評估

#數據獲取及預處理

import pandas as pd

import numpy as np

data = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data')

data.columns =['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']

數據預處理及分割

本次預處理主要是用replace函數將空數替換“?”,並通過dropna函數丟棄缺失值。在使用train_test_split函數將訓練和測試數據按1:3比例進行分割。

#數據缺失值處理,

data = data.replace(to_replace='?',value=np.nan) #用缺失值替換?

data = data.dropna() #丟棄缺失值

data.shape #輸出數據的記錄數和維度

  • Out[4]: (698, 11)

#數據分割

from sklearn.cross_validation import train_test_split

x_train,x_test,y_train,y_test = train_test_split(data[data.columns[1:10]],data[data.columns[10]],test_size=0.25,random_state=30)

數據標準化

為了防止預測結果不被某些維度過大的特徵而主導,因此數據需要進行標準化,可以使用sklearn.preprocessing 中的 StandardScaler函數保證每個維度的特徵數據方差為1,均值為0。

#數據標準化

from sklearn.preprocessing import StandardScaler

#標準化數據

ss= StandardScaler()

x_train=ss.fit_transform(x_train)

x_test=ss.transform(x_test)

邏輯迴歸建模

使用邏輯迴歸分類算法對上述額訓練數據進行學習,並根據測試樣本進行預測。

機器學習實戰|通過邏輯斯蒂迴歸算法預測及模型性能評估

#邏輯迴歸分類模型建模

from sklearn.linear_model import LogisticRegression

lr = LogisticRegression() #初始化模型

lr.fit(x_train,y_train) #調用fit函數來訓練參數

lr_y_predict = lr.predict(x_test) #使用predict 函數進行預測

模型性能評估

通過logisticRegression自帶的評分函數獲得模型得分、,使用classification_report函數獲取召回率、精準率、f1-score。

#模型評估

from sklearn.metrics import classification_report

print("模型得分:",lr.score(x_test,y_test))

print(classification_report(y_test,lr_y_predict,target_names=['良性','惡性']))

模型結果:

機器學習實戰|通過邏輯斯蒂迴歸算法預測及模型性能評估

從預測結果看,整體分類預測得分較高,召回率、精準率均在0.90以上,模型效果較好!

好了,今天就到這吧!大家熟悉流程了嗎?如有疑問,歡迎關注留!!!


分享到:


相關文章: