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

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指标是评估二分类的模型性能的指标
相关推荐
joopthd14 小时前
工业在线 TDLAS 器件源头厂商|连续监测场景器件选型要点
机器学习·激光气体检测·tdlas分析模块·仪器集成开发·工业在线监测
方方洛14 小时前
ray教程-00-前言与导读
人工智能·分布式·机器学习
方方洛14 小时前
ray教程-01-认识Ray
人工智能·分布式·机器学习
OCR_1337162127515 小时前
智能交通底层逻辑:国内车牌分类规范与AI识别适配技术解析
大数据·人工智能·分类
段一凡-华北理工大学15 小时前
高炉炼铁机器视觉与智能识别十八讲~系列文章11:从单帧到视频流:时序与异常行为识别
人工智能·机器学习·python开发·智能识别·高炉智能化·时序识别
FL162386312918 小时前
外来入侵植物检测数据集VOC+YOLO格式1111张5类别
人工智能·yolo·机器学习
oooost18 小时前
RecFno
人工智能·机器学习
Rocky Ding*19 小时前
VideoChat3 深度解析:视频理解的效率,来自时空压缩与主动感知的共同设计
论文阅读·人工智能·深度学习·机器学习·aigc·ai-native·视频理解
Omics Pro19 小时前
全新可重分析!代谢组质谱专用
数据库·人工智能·算法·机器学习·自然语言处理