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

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指标是评估二分类的模型性能的指标
相关推荐
2301_7644413338 分钟前
主流手机pc品牌的端侧模型部署梳理
人工智能·windows·机器学习·智能手机·产品运营
硅谷秋水2 小时前
HumanEgo:基于人类第一人称视角数分钟视频的零样本机器人学习
人工智能·机器学习·计算机视觉·机器人
湘美书院--湘美谈教育4 小时前
湘美谈教育AI系列经验集锦:赋能整理聊斋志异大寓言
大数据·人工智能·深度学习·神经网络·机器学习
追梦人电立电子6 小时前
X、Y电容的分类与选择
人工智能·分类·数据挖掘·追梦人电力电子
大模型最新论文速读6 小时前
小红书提出 RedKnot:分头处理 kv 缓存,延时降低 60%效果还提升
论文阅读·人工智能·深度学习·机器学习·缓存·自然语言处理
一楼的猫7 小时前
茄子写作助手——品牌搜索突破9万后的技术型品牌认知与官网入口指南
人工智能·学习·机器学习·chatgpt·ai写作
苏州邦恩精密8 小时前
江苏三维扫描仪厂家如何选择合适的工业测量方案?
人工智能·科技·机器学习·3d·自动化·制造
叫我:松哥8 小时前
基于深度卷积神经网络的水果图片分类算法设计与实现,有ResNet50的迁移学习模型,准确率达95%
人工智能·python·神经网络·机器学习·分类·cnn·迁移学习
装不满的克莱因瓶9 小时前
PyTorch 与它的自动微分工具:Autograd
人工智能·pytorch·python·深度学习·神经网络·机器学习·ai