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

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指标是评估二分类的模型性能的指标
相关推荐
Keep_Trying_Go20 小时前
基于GAN的文生图算法详解ControlGAN(Controllable Text-to-Image Generation)
人工智能·python·深度学习·神经网络·机器学习·生成对抗网络·文生图
懒羊羊吃辣条20 小时前
电力负荷预测怎么做才不翻车
人工智能·深度学习·机器学习·时间序列
人工智能培训1 天前
基于Transformer的人工智能模型搭建与fine-tuning
人工智能·深度学习·机器学习·transformer·知识图谱·数字孪生·大模型幻觉
板面华仔1 天前
机器学习入门(二)——逻辑回归 (Logistic Regression)
python·机器学习
一人の梅雨1 天前
VVIC图片搜索接口进阶实战:服装批发场景下的精准识图与批量调度方案
开发语言·机器学习·php
矢志航天的阿洪1 天前
IGRF-13 数学细节与公式说明
线性代数·机器学习·矩阵
小鸡吃米…1 天前
机器学习 —— 数据缩放
人工智能·python·机器学习
JHC0000001 天前
智能体造论子--简单封装大模型输出审核器
开发语言·python·机器学习
龙腾AI白云1 天前
AI算法实战:逻辑回归在风控场景中的应用
深度学习·机器学习·知识图谱
九河云1 天前
数字韧性时代,华为云CBR为业务连续性注入“免疫基因”
大数据·人工智能·安全·机器学习·华为云