目录
一、基础理论
1、混淆矩阵
预测结果与正确标记之间存在四种不同的组合:
2、精确率
精确率: 预测结果为正例样本中真实为正例的比例。
3、召回率
召回率:真实为正例样本中,预测结果为正例的比例。(真实为癌症能够被检测出来的概率)
(重要,检测查的全不全)
4、F1-score
反映模型的稳健性。
二、查看癌症预测结果的精确率、召回率
API
sklearn.metrics.classification_report
查看癌症预测结果的精确率、召回率
# 查看精确率、召回率、F1-score
report = classification_report(test_target, predict, target_names=['良性', '恶性'])
print(report)
总代码
# 逻辑回归:癌症分类
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
# 1、读取数据
column_name=['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']
pd.set_option('display.max_columns',1000) # 设置最大列数
pd.set_option('display.max_rows',1000) # 设置最大行数
data=pd.read_csv('breast-cancer-wisconsin.data', names=column_name)
# print(data)
# 2、缺失数据处理
data = data.replace(to_replace='?', value = np.nan)
data.dropna(inplace=True)
# 3、划分数据集
train = data.iloc[:, 1:-1]
target = data['Class']
train_data, test_data, train_target, test_target = train_test_split(train, target)
# print(train_data)
# 4、标准化(对训练集和测试集标准化)
transfer = StandardScaler()
train_data = transfer.fit_transform(train_data)
test_data = transfer.transform(test_data)
# print(train_data)
# 5、创建逻辑回归预估器,训练
estimator = LogisticRegression()
estimator.fit(train_data, train_target)
# print(train_data)
# 查看模型参数:回归系数 和 偏置
print('回归系数:', estimator.coef_)
print('偏置:', estimator.intercept_)
# 6、模型评估
# 方法一:比对
predict = estimator.predict(test_data)
print(predict==test_target)
# 方法二:计算正确率
score = estimator.score(test_data, test_target)
print('逻辑回归分类正确率为:', score)
# 查看精确率、召回率、F1-score
report = classification_report(test_target, predict, target_names=['良性', '恶性'])
print(report)