| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- #!/usr/bin/env python
- # coding: utf-8
- # In[33]:
- import pandas as pd
- import numpy as np
- import os
- import urllib
- # In[34]:
- # 1、读取数据
- data_url = "https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data"
- if not os.path.exists("breast-cancer-wisconsin.data"):
- urllib.request.urlretrieve(data_url, "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("breast-cancer-wisconsin.data", names=column_name)
- # In[35]:
- data.head()
- # In[36]:
- # 2、缺失值处理
- # 1)替换-》np.nan
- data = data.replace(to_replace="?", value=np.nan)
- # 2)删除缺失样本
- data.dropna(inplace=True)
- # In[37]:
- data.isnull().any() # 不存在缺失值
- # In[38]:
- # 3、划分数据集
- from sklearn.model_selection import train_test_split
- # In[39]:
- data.head()
- # In[40]:
- # 筛选特征值和目标值
- x = data.iloc[:, 1:-1]
- y = data["Class"]
- # In[41]:
- x.head()
- # In[42]:
- y.head()
- # In[43]:
- x_train, x_test, y_train, y_test = train_test_split(x, y)
- # In[44]:
- x_train.head()
- # In[45]:
- # 4、标准化
- from sklearn.preprocessing import StandardScaler
- # In[46]:
- transfer = StandardScaler()
- x_train = transfer.fit_transform(x_train)
- x_test = transfer.transform(x_test)
- # In[47]:
- x_train
- # In[48]:
- from sklearn.linear_model import LogisticRegression
- # In[49]:
- # 5、预估器流程
- estimator = LogisticRegression()
- estimator.fit(x_train, y_train)
- # In[50]:
- # 逻辑回归的模型参数:回归系数和偏置
- estimator.coef_
- # In[51]:
- estimator.intercept_
- # In[52]:
- # 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)
- # In[53]:
- # 查看精确率、召回率、F1-score
- from sklearn.metrics import classification_report
- # In[54]:
- report = classification_report(y_test, y_predict, labels=[2, 4], target_names=["良性", "恶性"])
- # In[55]:
- print(report)
- # In[56]:
- y_test.head()
- # In[57]:
- # y_true:每个样本的真实类别,必须为0(反例),1(正例)标记
- # 将y_test 转换成 0 1
- y_true = np.where(y_test > 3, 1, 0) # 3以上为恶性1(正例),否则为良性0(反例)
- # In[58]:
- y_true
- # In[59]:
- from sklearn.metrics import roc_auc_score
- # In[60]:
- roc_auc_score(y_true, y_predict)
|