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

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指标是评估二分类的模型性能的指标
相关推荐
لا معنى له1 小时前
目标检测的内涵、发展和经典模型--学习笔记
人工智能·笔记·深度学习·学习·目标检测·机器学习
齐齐大魔王5 小时前
COCO 数据集
人工智能·机器学习
式5167 小时前
线性代数(八)非齐次方程组的解的结构
线性代数·算法·机器学习
Coding茶水间7 小时前
基于深度学习的非机动车头盔检测系统演示与介绍(YOLOv12/v11/v8/v5模型+Pyqt5界面+训练代码+数据集)
图像处理·人工智能·深度学习·yolo·目标检测·机器学习·计算机视觉
brave and determined8 小时前
CANN训练营 学习(day9)昇腾AscendC算子开发实战:从零到性能冠军
人工智能·算法·机器学习·ai·开发环境·算子开发·昇腾ai
brave and determined9 小时前
CANN训练营 学习(day8)昇腾大模型推理调优实战指南
人工智能·算法·机器学习·ai实战·昇腾ai·ai推理·实战记录
源于花海9 小时前
迁移学习的第一类方法:数据分布自适应(1)——边缘分布自适应
人工智能·机器学习·迁移学习·数据分布自适应
科士威传动10 小时前
丝杆支撑座同轴度如何安装?
人工智能·科技·机器学习·自动化
_Li.11 小时前
机器学习-集成学习
人工智能·机器学习·集成学习
极度畅想11 小时前
脑电模型实战系列(三):基于 KNN 的 DEAP 脑电情绪识别 KNN 算法与 Canberra 距离深度剖析(三)
机器学习·knn·脑机接口·情绪识别·bci·canberra距离