iT邦幫忙

2021 iThome 鐵人賽

DAY 12
0
AI & Data

AI平台初學者工作坊: 從training、tracking到serving系列 第 12

心血管疾病資料集的資料清理

這一篇我們來說明處理心血管疾病資料集的notebook內容, 這份notebook可以從下方連結下載:
https://github.com/masonwu1762/ithome-ironman

  • notebook: cardiovascular_disease_prediction_notebook.ipynb
  • dataset: cardio_train.csv

請將notebook與csv檔上傳到jupyterhub, 並打開notebook.
以下說明notebook的內容

修改後的notebook內容是為了接續後續整合MLFlow與Seldon

安裝所需的package

請開啟terminal執行下方指令安裝所需的package

pip install pandas numpy seaborn matplotlib

notebook內容說明

  • 首先匯入所需要的package

    import pandas as pd
    import numpy as np
    import seaborn as sns
    import matplotlib.pyplot as plt
    
  • 讀取dataset資料

    # read the csv file 
    cardio_df = pd.read_csv("cardio_train.csv", sep=";")
    
  • 看一下前5筆資料長什麼樣子

    cardio_df.head()
    
  • 把id欄位拿掉, 因為在訓練時這個欄位是沒有用的

    # Drop id
    
    cardio_df = cardio_df.drop(columns = 'id')
    
  • 轉換年齡單位為

    # since the age is given in days, we convert it into years
    
    cardio_df['age'] = cardio_df['age']/365
    
  • 再來看一次前5筆的資料長怎麼樣

    cardio_df.head()
    
  • 確認有沒有空值.

    # checking the null values
    cardio_df.isnull().sum()
    
  • 看一下資料集的基本統計數值

    # Checking the dataframe information
    
    cardio_df.info()
    
  • 顯示每個欄位的統計值

    # Statistical summary of the dataframe
    cardio_df.describe()
    
  • 畫出pairplot
    用來畫出兩兩特徵之間的關係

    sns.pairplot(cardio_df)
    
  • 設定資料集之中的答案欄位特徵欄位
    答案欄位是指我們要推論的結果, 在這裡是指罹患心血管疾病的機率
    特徵欄位是指我們用來推估罹患心血管疾病機率的相關資料

    # split the dataframe into target and features
    
    df_target = cardio_df['cardio']
    df_features = cardio_df.drop(columns =['cardio'])
    
  • 確認一下Dataset有哪些欄位

    cardio_df.columns
    
  • 確認一下特徵欄位有幾筆資料與欄位數

    df_features.shape
    
  • 確認特徵欄位的欄位數量

    df_features.columns
    
  • 確認答案欄位的筆數

    df_target.shape
    
  • 將資料區分為訓練資料與測試資料,以8:2進行區分

    #spliting the data in to test and train sets
    from sklearn.model_selection import train_test_split
    
    X_train, X_test, y_train, y_test = train_test_split(df_features, df_target, test_size = 0.2)
    
  • 看一下訓練資料的筆數與欄位數量

    X_train.shape
    
  • 看一下訓練資料的欄位名稱

    X_train.columns
    
  • 看一下訓練資料的筆數

    y_train.shape
    
  • 看一下測試資料的筆數

    X_test.shape
    
  • 安裝xgboost
    接下來會使用xgboost執行訓練, 因此需要安裝xgboost

    # install xgboost
    
    !pip install xgboost
    
  • 呼叫xgboost分類演算法並執行訓練

    from xgboost import XGBClassifier
    
    # model = XGBClassifier(learning_rate=0.01, n_estimators=100, objective='binary:logistic')
    model = XGBClassifier()
    
    model.fit(X_train, y_train)
    
  • 以測試資料進行預測

    # make predictions on test data
    
    predict = model.predict(X_test)
    
  • 印出預測結果

    predict
    
  • 印出測試資料的metrics

    print("Precision = {}".format(precision_score(y_test, predict)))
    print("Recall = {}".format(recall_score(y_test, predict)))
    print("Accuracy = {}".format(accuracy_score(y_test, predict)))
    
  • 將訓練好的model另存為檔案
    要儲存xgboost的model檔可以呼叫 save_mode 這個function. 如果附檔名是.json則會將model檔儲存為json格式, 你可以用編輯器打開內容.
    如果附檔名不是.json, 例如下方範例是.pkl檔, 則會將model檔儲存為二進位檔(pickle). 之後我們部署model時會使用 .pkl 檔, 這部份等到seldon時再說明.
    這時你就先把 bst_save_model.pkl下載到本機了, 留著備用.

    model.save_model('bst_save_model.json')
    model.save_model('bst_save_model.pkl')
    

到這裡, 我們已經訓練完成心血管疾病資料集, 並且取得model檔. 下一篇我們來說明把MLFlow功能加進來的內容.


上一篇
範例(二)預測心血管疾病的可能性
下一篇
心血管疾病notebook使用MLFlow做記錄
系列文
AI平台初學者工作坊: 從training、tracking到serving30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言