机器学习-逻辑回归法实现肿瘤预测案例

2021-03-10  本文已影响0人  涓涓自然卷

1、分析步骤:
(1)获取数据:https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data
(2)基本数据处理:缺失值处理、确定特征值,目标值、分割数据
(3)特征工程
(4)机器学习 - 模型训练(逻辑回归)
(5)模型评估:准确率、预测值

2、所需API:
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

3、代码示例🌰:

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
import ssl

ssl._create_default_https_context = ssl._create_unverified_context

"""
肿瘤分类分析
"""

# 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)

print(data.head())

# 2、基本数据处理
# 2.1、缺失值处理
data = data.replace(to_replace="?", value=np.nan)
data = data.dropna()

# 2.2 确定特征值,目标值
x = data.iloc[:, 1:10]
print("x.head():", x.head())
y = data["Class"]
print("y.head():\n", y.head())

# 2.3 分割数据
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.fit_transform(x_test)

# 4.机器学习 - 模型训练(逻辑回归)
estimator = LogisticRegression()
estimator.fit(x_train, y_train)

# 5.模型评估
# 5.1 准确率
ret = estimator.score(x_test, y_test)
print("准确率为:\n", ret)

# 5.2 预测值
y_pre = estimator.predict(x_test)
print("模型预测值为:\n", y_pre)


4、示例运行结果:

运行结果.png

总结:

上一篇下一篇

猜你喜欢

热点阅读