机器学习——逻辑回归、肿瘤预测案例(恶性乳腺)

机器学习——逻辑回归、肿瘤预测案例(恶性乳腺),第1张

机器学习——逻辑回归、肿瘤预测案例(恶性乳腺)

# coding:utf-8
# 1.获取数据
#2.数据基本处理
#2.1.数据划分
#3.特征工程——标准化
#4.机器学习(逻辑回归)
#5.模型评估

import pandas as pd
import numpy as np
# 1.获取数据集,引用网上数据https://archive.ics.uci.edu/ml/machine-learning-databases/
#breast-cancer-wisconsin/breast-cancer-wisconsin.data",names=names)
from sklearn.model_selection import train_test_split #2.数据基本处理,所使用的引用
from sklearn.preprocessing import StandardScaler #3.特征工程——标准化,所使用的引用
from sklearn.linear_model import LogisticRegression#4.机器学习,所使用的引用
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
 #5.模型评估,所使用的引用



    
# 1.获取数据集
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)
#2.数据基本处理
#2.1缺失值处理
# 2.1 缺失值处理
data = data.replace(to_replace="?", value=np.NaN)#值替换
data = data.dropna()#nan的值所在行都删除

# 2.2 确定特征值,目标值
x = data.iloc[:, 1:10]#特征值,取的是所有行,第二列到倒数第十一列
y = data["Class"]#目标值

# 2.3 分割数据
#2.3.数据划分,20%测试集,80%训练集
x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=22,test_size=0.2)

# 3.特征工程(标准化)
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)
# 4.机器学习(逻辑回归)
estimator = LogisticRegression()
estimator.fit(x_train, y_train)


# 5.模型评估
#5.1预测值和准确值
y_pre = estimator.predict(x_test)
print("预测值是:n",y_pre)
    
score = estimator.score(x_test,y_test)
print("准确率是:n",score)

运行结果:


 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5711998.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存