cancer_demo.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. # In[33]:
  4. import pandas as pd
  5. import numpy as np
  6. import os
  7. import urllib
  8. # In[34]:
  9. # 1、读取数据
  10. data_url = "https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data"
  11. if not os.path.exists("breast-cancer-wisconsin.data"):
  12. urllib.request.urlretrieve(data_url, "breast-cancer-wisconsin.data")
  13. column_name = ['Sample code number', 'Clump Thickness', 'Uniformity of Cell Size', 'Uniformity of Cell Shape',
  14. 'Marginal Adhesion', 'Single Epithelial Cell Size', 'Bare Nuclei', 'Bland Chromatin',
  15. 'Normal Nucleoli', 'Mitoses', 'Class']
  16. data = pd.read_csv("breast-cancer-wisconsin.data", names=column_name)
  17. # In[35]:
  18. data.head()
  19. # In[36]:
  20. # 2、缺失值处理
  21. # 1)替换-》np.nan
  22. data = data.replace(to_replace="?", value=np.nan)
  23. # 2)删除缺失样本
  24. data.dropna(inplace=True)
  25. # In[37]:
  26. data.isnull().any() # 不存在缺失值
  27. # In[38]:
  28. # 3、划分数据集
  29. from sklearn.model_selection import train_test_split
  30. # In[39]:
  31. data.head()
  32. # In[40]:
  33. # 筛选特征值和目标值
  34. x = data.iloc[:, 1:-1]
  35. y = data["Class"]
  36. # In[41]:
  37. x.head()
  38. # In[42]:
  39. y.head()
  40. # In[43]:
  41. x_train, x_test, y_train, y_test = train_test_split(x, y)
  42. # In[44]:
  43. x_train.head()
  44. # In[45]:
  45. # 4、标准化
  46. from sklearn.preprocessing import StandardScaler
  47. # In[46]:
  48. transfer = StandardScaler()
  49. x_train = transfer.fit_transform(x_train)
  50. x_test = transfer.transform(x_test)
  51. # In[47]:
  52. x_train
  53. # In[48]:
  54. from sklearn.linear_model import LogisticRegression
  55. # In[49]:
  56. # 5、预估器流程
  57. estimator = LogisticRegression()
  58. estimator.fit(x_train, y_train)
  59. # In[50]:
  60. # 逻辑回归的模型参数:回归系数和偏置
  61. estimator.coef_
  62. # In[51]:
  63. estimator.intercept_
  64. # In[52]:
  65. # 6、模型评估
  66. # 方法1:直接比对真实值和预测值
  67. y_predict = estimator.predict(x_test)
  68. print("y_predict:\n", y_predict)
  69. print("直接比对真实值和预测值:\n", y_test == y_predict)
  70. # 方法2:计算准确率
  71. score = estimator.score(x_test, y_test)
  72. print("准确率为:\n", score)
  73. # In[53]:
  74. # 查看精确率、召回率、F1-score
  75. from sklearn.metrics import classification_report
  76. # In[54]:
  77. report = classification_report(y_test, y_predict, labels=[2, 4], target_names=["良性", "恶性"])
  78. # In[55]:
  79. print(report)
  80. # In[56]:
  81. y_test.head()
  82. # In[57]:
  83. # y_true:每个样本的真实类别,必须为0(反例),1(正例)标记
  84. # 将y_test 转换成 0 1
  85. y_true = np.where(y_test > 3, 1, 0) # 3以上为恶性1(正例),否则为良性0(反例)
  86. # In[58]:
  87. y_true
  88. # In[59]:
  89. from sklearn.metrics import roc_auc_score
  90. # In[60]:
  91. roc_auc_score(y_true, y_predict)