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以上,模型效果较好!

好了,今天就到这吧!大家熟悉流程了吗?如有疑问,欢迎关注留!!!


分享到:


相關文章: