逻辑回归实例--乳腺癌肿瘤预测
生活随笔
收集整理的這篇文章主要介紹了
逻辑回归实例--乳腺癌肿瘤预测
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 0.前言
- 1.導入數據
- 2.數據預處理
- 3.準備訓練測試數據
- 4. 標準化數據
- 5.邏輯回歸模型
- 6.性能分析
- 7.十折交叉驗證
0.前言
- 環境:Python3.6.5
- 編譯器:jupyter notebook
- 代碼|數據:微信公眾號(明天依舊可好)中回復:第6天
1.導入數據
這篇文章中我們將預測乳腺癌腫瘤的良\惡性。
import pandas as pdcolumn_names = ['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'] dataFrame = pd.read_csv("../data/breast-cancer-wisconsin.data",names = column_names) dataFrame.head()2.數據預處理
import numpy as np # 將?替換成標準缺失值表示 dataFrame = dataFrame.replace('?',value = np.nan) # 丟棄帶有缺失值的數據 dataFrame = dataFrame.dropna(how='any') dataFrame.shape3.準備訓練測試數據
from sklearn.model_selection import train_test_splitX_train,X_test,y_train,y_test = train_test_split(dataFrame[column_names[1:10]],dataFrame[column_names[10]],test_size=0.25,random_state=2)4. 標準化數據
# 標準化數據,保證每個維度的特征數據方差為1,均值為0。使得預測結果不會被某些維度過大的特征值而主導 from sklearn.preprocessing import StandardScaler sc = StandardScaler() X_train = sc.fit_transform(X_train) X_test = sc.transform(X_test)5.邏輯回歸模型
from sklearn.linear_model import LogisticRegression lr = LogisticRegression() lr.fit(X_train, y_train)6.性能分析
lr.score(X_test,y_test) """ 輸出: 0.9473684210526315 """7.十折交叉驗證
from sklearn.model_selection import KFoldscore = 0 #進行十折交叉驗證 kf = KFold(n_splits=10)for train_index, test_index in kf.split(X):X_train, X_test = X.iloc[train_index], X.iloc[test_index]y_train, y_test = y.iloc[train_index], y.iloc[test_index]lr.fit(X_train, y_train)score += lr.score(X_test,y_test)print("模型的平均精確率為:",score/10) """ 輸出: 模型的平均精確率為: 0.9620204603580564 """【機器學習100天目錄】
【機器學習第3天:預測汽車的燃油效率】
【機器學習第4天:預測1立方米混凝土抗壓強度】
【機器學習第5天:邏輯回歸】
如有錯誤歡迎指教,有問題的也可以加入QQ群(1149530473)向我提問,關注微信公眾號(明天依舊可好)和我同步學習。
總結
以上是生活随笔為你收集整理的逻辑回归实例--乳腺癌肿瘤预测的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 专题一:预处理数据(使用sklearn-
- 下一篇: K折交叉验证(StratifiedKFo