使用 Python 進行機器學習的帕金森病預測
帕金森病是一種影響全球數百萬人神經退行性疾病,早期和準確的診斷對於有效治療至關重要,這可以透過使用 Python 中的機器學習輕鬆實現。
本文探討了機器學習技術在使用 UCI 機器學習儲存庫中的資料集預測帕金森病中的應用。透過使用隨機森林分類器演算法,我們演示瞭如何利用 Python 分析和預處理資料、訓練預測模型以及進行準確預測。
使用 Python 進行機器學習的帕金森病預測
我們已使用 Jupyter notebook 在本文中執行程式碼。
以下是我們將遵循的使用 Python 進行機器學習的帕金森病預測的步驟:
步驟 1:匯入必要的庫
示例
import pandas as pd import numpy as np import matplotlib.pyplot as plt from sklearn.metrics import accuracy_score, confusion_matrix from sklearn.preprocessing import StandardScaler from sklearn.decomposition import PCA from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestClassifier
步驟 2:載入帕金森病資料集
程式使用 pd.read_csv() 函式從 'parkinsons.csv' 檔案讀取資料集,並將其儲存在 data 變數中。
示例
# Load the Parkinson's Disease dataset data = pd.read_csv('parkinsons.csv')
步驟 3:資料清洗
下面的程式使用 drop() 函式從資料集中刪除 'name' 列,並將修改後的資料集重新分配給 data 變數。
示例
# Data cleaning data = data.drop('name', axis=1) # Remove the 'name' column
步驟 4:資料預處理
下面的程式使用 drop() 函式將特徵 (X) 與目標變數 (y) 分開,並將它們分配給相應的變數。
示例
# Data preprocessing X = data.drop('status', axis=1) # Features y = data['status'] # Target variable
步驟 5:資料分析
下面的程式提供了有關資料集的資訊:
資料集的形狀(行數和列數)使用 data.shape 列印。
分別使用 len(data[data['status'] == 1]) 和 len(data[data['status'] == 0]) 顯示患有帕金森病的樣本數和健康樣本數。
使用 data.describe() 列印資料集的摘要。
示例
print("Data Shape:", data.shape) print("Parkinson's Disease Samples:", len(data[data['status'] == 1])) print("Healthy Samples:", len(data[data['status'] == 0])) print("\nData Summary:") print(data.describe())
輸出
Data Shape: (195, 23) Parkinson's Disease Samples: 147 Healthy Samples: 48 Data Summary: MDVP:Fo(Hz) MDVP:Fhi(Hz) MDVP:Flo(Hz) MDVP:Jitter(%) \ count 195.000000 195.000000 195.000000 195.000000 mean 154.228641 197.104918 116.324631 0.006220 std 41.390065 91.491548 43.521413 0.004848 min 88.333000 102.145000 65.476000 0.001680 25% 117.572000 134.862500 84.291000 0.003460 50% 148.790000 175.829000 104.315000 0.004940 75% 182.769000 224.205500 140.018500 0.007365 max 260.105000 592.030000 239.170000 0.033160 MDVP:Jitter(Abs) MDVP:RAP MDVP:PPQ Jitter:DDP MDVP:Shimmer \ count 195.000000 195.000000 195.000000 195.000000 195.000000 mean 0.000044 0.003306 0.003446 0.009920 0.029709 std 0.000035 0.002968 0.002759 0.008903 0.018857 min 0.000007 0.000680 0.000920 0.002040 0.009540 25% 0.000020 0.001660 0.001860 0.004985 0.016505 50% 0.000030 0.002500 0.002690 0.007490 0.022970 75% 0.000060 0.003835 0.003955 0.011505 0.037885 max 0.000260 0.021440 0.019580 0.064330 0.119080 max 0.685151 0.825288 -2.434031 0.450493 3.671155 0.527367 [8 rows x 23 columns]
步驟 6:資料視覺化
直方圖使用 plt.show() 顯示。
示例
# Data visualization data.hist(figsize=(12, 12)) plt.tight_layout() plt.show()
輸出
步驟 7:資料縮放
下面的程式使用 StandardScaler() 縮放特徵,該函式透過減去均值並縮放至單位方差來標準化特徵。縮放後的特徵儲存在 X_scaled 變數中。
示例
scaler = StandardScaler() X_scaled = scaler.fit_transform(X)
步驟 8:降維
它使用 PCA(n_components=2) 將特徵減少到兩個主要成分。減少後的特徵儲存在 X_pca 變數中。
示例
pca = PCA(n_components=2) X_pca = pca.fit_transform(X_scaled)
步驟 9:將資料集拆分為訓練集和測試集
下面的程式使用 train_test_split() 將資料集拆分為訓練集和測試集。
示例
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)
步驟 10:建立一個名為隨機森林分類器的分類器
下面的程式使用 RandomForestClassifier() 建立隨機森林分類器的例項。
訓練模型
示例
rf_classifier = RandomForestClassifier() # Train the model rf_classifier.fit(X_train, y_train)
輸出
RandomForestClassifier()
步驟 11:對測試集進行預測
計算模型的準確率
示例
# Make predictions on the test set y_pred = rf_classifier.predict(X_test) accuracy = accuracy_score(y_test, y_pred) print("\nAccuracy:", accuracy)
輸出
Accuracy: 0.9230769230769231
程式透過將預測標籤 (y_pred) 與實際標籤 (y_test) 進行比較來計算模型的準確率。
步驟 12:混淆矩陣
它使用 sklearn.metrics 中的 confusion_matrix() 函式並將混淆矩陣分配給 cm 變數。
示例
cm = confusion_matrix(y_test, y_pred) print("\nConfusion Matrix:") print(cm)
輸出
Confusion Matrix: [[ 5 2] [ 1 31]]
結論
總之,本文介紹了一種使用 Python 進行帕金森病預測的機器學習方法。透過利用隨機森林分類器演算法和綜合資料集,我們證明了機器學習在準確預測帕金森病存在方面的有效性。
結果突出了這種方法在協助醫療保健專業人員進行早期診斷和干預方面的潛力,從而改善患者預後。