机器学习实现逻辑回归-癌症分类预测

1.数据来源

复制代码
https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data

2.代码示例(已添加注释)

python 复制代码
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,Ridge
from sklearn.metrics import classification_report,roc_auc_score
#给列名字
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)

##数据预处理:去除以?表示的缺失值
data.replace('?',np.nan,inplace=True)
data.dropna(inplace=True)


##目标值和特征值
tezheng=data.iloc[:,1:10]
mubiao=data['Class']

##划分数据
x_train,x_test,y_train,y_test=train_test_split(tezheng,mubiao,random_state=22)

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

##生成模型
em = LogisticRegression(solver='sag')
em.fit(x_train,y_train)
y_pre=em.predict(x_test)
print(em.coef_,em.intercept_)
print(em.score(x_test,y_test))

##模型评估
print(y_test==y_pre)
report=classification_report(y_test,y_pre,labels=[2,4],target_names=['良性','恶性'])##求解准确率与召回率
print(report)
y_test1=np.where(y_test>3,1,0)##roc_auc函数的第一个参数要求必须是1,0分类才行,且1表示正例
print(roc_auc_score(y_test1,y_pre))##计算AUC指标,确定模型是否受样本不均衡影响,越接近1,越好,越接近0.5越差
##注意精确率,召回率,ROC_AUC指标是评估二分类的模型性能的指标
相关推荐
WangN27 小时前
Unitree RL Lab 学习笔记【通识】
人工智能·机器学习
人工智能培训10 小时前
大模型与传统小模型、传统NLP模型的核心差异解析
人工智能·深度学习·神经网络·机器学习·生成对抗网络
砥锋12 小时前
纯NumPy手写两层GCN:从零开始理解图神经网络核心思想
机器学习
Larcher12 小时前
🔥 告别抓瞎:用 Claude Code (cc) 优雅接手与维护已有项目
javascript·机器学习·前端框架
大模型最新论文速读13 小时前
PreFT:只在 prefill 时使用 LoRA,推理速度翻倍效果不降
论文阅读·人工智能·深度学习·机器学习·自然语言处理
AI算法沐枫14 小时前
大模型 | 大模型之机器学习基本理论
人工智能·python·神经网络·学习·算法·机器学习·计算机视觉
动物园猫15 小时前
面向智慧牧场的牛行为识别数据集分享(适用于YOLO系列深度学习分类检测任务)
深度学习·yolo·分类
larance15 小时前
[菜鸟教程] 机器学习教程第六课-机器学习基础术语
人工智能·机器学习
cxr82815 小时前
数据驱动的AI逆向材料设计:体系、方法与突破路径
人工智能·机器学习·智能体·逆向合成·材料设计合成·蜂群理论