coding=utf-8
“”"
author:lei
function: 逻辑回归做二分类
“”"
import pandas as pd
from sklearn.linear_model import LogisticRegression
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import classification_report
def logistic():
“”"
逻辑回归做二分类进行癌症预测
:return:
“”"
column = ["{}".format(i) for i in range(11)]
# 读取数据
data = pd.read_csv("./data/breast-cancer-wisconsin.data")
# 缺失值进行处理
data = data.replace(to_replace="?", value=np.nan)
data = data.dropna()
# print(data)
# 进行数据的分割
x_train, x_test, y_train, y_test = train_test_split(data[column[1:10]], data[column[10]], test_size=0.25)
# 进行标准化处理
std = StandardScaler()
x_train = std.fit_transform(x_train)
x_test = std.transform(x_test)
# 逻辑回归预测
lg = LogisticRegression(C=1.0)
lg.fit(x_train, y_train)
print(lg.coef_)
y_predict = lg.predict(x_test)
print("准确率:", lg.score(x_test, y_test))
print("召回率:", classification_report(y_test, y_predict, labels=[2, 4], target_names=["良性", "恶性"]))
if name == ‘main’:
logistic()