逻辑回归----案例:癌症分类预测

案例:癌症分类预测-良/恶性乳腺癌肿瘤预测

数据介绍:

在这里插入图片描述

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, roc_auc_score

# 1.获取数据
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']

data = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data",
                  names=names)
# print(data.head())
# 2.基本数据处理
# 2.1 缺失值处理
data = data.replace(to_replace="?", value=np.nan)
data = data.dropna()

x = data.iloc[:, 1:-1]
#去掉目标值

print(x.head())

y = data["Class"]
print(y.head())


# 2.3 分割数据
x_train, x_test, y_train, y_test = train_test_split\
    (x, y, random_state=22, test_size=0.2)

# 3.特征工程(标准化)
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)


# 4.机器学习(逻辑回归)
estimator = LogisticRegression()
estimator.fit(x_train, y_train)

# 5.模型评估
# 5.1 准确率
ret = estimator.score(x_test, y_test)
print("准确率为:\n", ret)

# 5.2 预测值
y_pre = estimator.predict(x_test)
print("预测值为:\n", y_pre)

# 5.3 精确率\召回率指标评价
ret = classification_report(y_test, y_pre, labels=(2, 4), target_names=("良性", "恶性"))
print(ret)

# 5.4 auc指标计算
y_test = np.where(y_test>3, 1, 0)

print(roc_auc_score(y_test, y_pre))

注意:

预测调用函数和回归的不同
回归用mse,均方误差

分类用score,比较

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值