肿瘤预测

import pandas as pd
import numpy as np
#1.读取数据
path="https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data"

column_name = ['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(path,names=column_name) 


#2.缺失值处理
    #1)替换成np.nan
data=data.replace(to_replace="?",value=np.nan)
    #2)删除缺失样本,原地删除
data.dropna(inplace=True)
    
data.isnull().any() #不存在缺失值
Sample code number             False
Clump Thickness                False
Uniformity of Cell Size        False
Uniformity of Cell Shape       False
Marginal Adhesion              False
Single Epithelial Cell Size    False
Bare Nuclei                    False
Bland Chromatin                False
Normal Nucleoli                False
Mitoses                        False
Class                          False
dtype: bool
from sklearn.model_selection import train_test_split
x=data.iloc[:,1:-1]
y=data["Class"]
x_train,x_test,y_train,y_test=train_test_split(x,y)
#4.标准化
from sklearn.preprocessing import StandardScaler
transfer=StandardScaler()
x_train=transfer.fit_transform(x_train)
x_test=transfer.transform(x_test)
from sklearn.linear_model import LogisticRegression
#5预估器流程.
estimator=LogisticRegression()
estimator.fit(x_train,y_train)

LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
                   intercept_scaling=1, l1_ratio=None, max_iter=100,
                   multi_class='auto', n_jobs=None, penalty='l2',
                   random_state=None, solver='lbfgs', tol=0.0001, verbose=0,
                   warm_start=False)
#逻辑回归的模型参数:回归系数和偏置
estimator.coef_ #回归系数
array([[ 1.34866956,  0.39407465,  0.95414338,  0.52087229, -0.30587845,
         1.18209596,  0.94562307,  0.95800325,  0.58231319]])
estimator.intercept_ #偏置
array([-1.14018688])
#6.模型评估
#方法1:直接比对真实值和预测值
y_predict=estimator.predict(x_test)
print("y_predict\n",y_predict)
print("直接比对真实值和预测值:\n",y_test==y_predict)
#方法2:计算准确率
score=estimator.score(x_test,y_test)
print("准确率:\n",score)

y_predict
 [2 2 4 2 2 2 4 4 4 2 4 4 2 4 4 4 2 2 2 4 2 2 2 4 2 2 2 2 2 2 2 4 2 2 2 2 4
 2 4 4 2 2 2 2 2 4 2 2 2 4 2 2 2 4 2 2 2 4 4 2 2 4 2 4 4 2 2 4 2 4 4 2 4 2
 4 4 4 4 4 2 4 4 4 2 2 2 2 4 2 2 2 2 2 4 2 4 2 2 2 2 2 4 4 2 2 4 2 2 2 4 2
 4 4 4 2 2 4 2 4 2 4 4 4 4 4 4 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 4 4 4 2 2 2
 4 2 4 4 4 2 4 4 2 4 2 4 4 2 2 4 2 2 2 2 2 2 2]
直接比对真实值和预测值:
 93     True
165    True
670    True
380    True
385    True
       ... 
291    True
370    True
332    True
632    True
492    True
Name: Class, Length: 171, dtype: bool
准确率:
 0.9707602339181286
#7.查看精确率和召回率,F1-score
from sklearn.metrics import classification_report
report=classification_report(y_test,y_predict,labels=[2,4],target_names=['良性','恶性'])
print(report)
              precision    recall  f1-score   support

          良性       0.96      0.99      0.98       102
          恶性       0.98      0.94      0.96        69

    accuracy                           0.97       171
   macro avg       0.97      0.97      0.97       171
weighted avg       0.97      0.97      0.97       171
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值