Machine Learning | 基于逻辑回归做二分类进行癌症预测
【摘要】 导入包
import pandas as pdimport numpy as npfrom sklearn.datasets import load_bostonfrom sklearn.linear_model import LinearRegression, SGDRegressor, Ridge, LogisticRegressionfrom sklearn.mod...
导入包
-
import pandas as pd
-
import numpy as np
-
from sklearn.datasets import load_boston
-
from sklearn.linear_model import LinearRegression, SGDRegressor, Ridge, LogisticRegression
-
from sklearn.model_selection import train_test_split
-
from sklearn.preprocessing import StandardScaler
-
from sklearn.metrics import mean_squared_error, classification_report
-
from sklearn.externals import joblib
构造列标签名字
column = ['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("breast-cancer-wisconsin.csv", names=column)
-
data.head()
缺失值进行处理
-
data = data.replace(to_replace='?', value=np.nan)
-
-
data = data.dropna()
数据的分割
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_)
-
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
-
intercept_scaling=1, max_iter=100, multi_class='warn',
-
n_jobs=None, penalty='l2', random_state=None, solver='warn',
-
tol=0.0001, verbose=0, warm_start=False)
-
[[ 1.60392495 -0.11066665 0.93702846 1.01160157 -0.31111269 1.20876603
-
1.20701977 1.04581779 0.81269039]]
-
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=["良性", "恶性"]))
文章来源: drugai.blog.csdn.net,作者:DrugAI,版权归原作者所有,如需转载,请联系作者。
原文链接:drugai.blog.csdn.net/article/details/102001398
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)