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

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指标是评估二分类的模型性能的指标
相关推荐
qq_359716232 小时前
openpi使用过程中相关问题
人工智能·深度学习·机器学习
阿钱真强道4 小时前
08 从 MLP 到 LeNet:为什么一个神经元不够?
深度学习·神经网络·机器学习·mlp·决策边界
罗西的思考4 小时前
【OpenClaw】通过Nanobot源码学习架构---(2)外层控制逻辑
人工智能·机器学习
FluxMelodySun6 小时前
机器学习(二十八) 特征选择与常见的特征选择方法
人工智能·机器学习
水中加点糖7 小时前
多模态数据标注平台LabelStudio——部署与智能标注体验
人工智能·机器学习·自动标注·数据标注·labelstudio·ai标注·标注平台
热爱生活的猴子9 小时前
什么情况是训练好验证差,什么情况判定为收敛慢,什么情况下判定为震荡,什么情况下说明是泛化差
人工智能·深度学习·机器学习
Ujimatsu10 小时前
数据分析相关面试题-A/B 测试 & 统计学部分
算法·机器学习·数据分析
FPGA-ADDA10 小时前
第五篇(下):智能无线电与6G候选技术——从机器学习到通感一体化
人工智能·机器学习·信号处理·fpga·通信系统
程序媛徐师姐10 小时前
Python基于机器学习的就业岗位推荐系统【附源码、文档说明】
python·机器学习·python机器学习·就业岗位推荐系统·python就业岗位推荐系统·python机器学习就业推荐·就业岗位推荐
Omics Pro10 小时前
空间组学下一代机器学习与深度学习
大数据·人工智能·深度学习·算法·机器学习·语言模型·自然语言处理