機器學習 - 缺失值比率



缺失值比率是一種機器學習中使用的特徵選擇技術,用於識別和刪除資料集中的缺失值比例較高的特徵。此技術用於透過減少用於訓練模型的特徵數量來提高模型效能,並避免由缺失值引起的偏差問題。

缺失值比率的工作原理是計算資料集每個特徵的缺失值百分比,並刪除缺失值百分比超過特定閾值的特徵。之所以這樣做,是因為缺失值百分比高的特徵可能對預測目標變數沒有用,並可能在模型中引入偏差。

實施缺失值比率的步驟如下:

  • 計算資料集中每個特徵的缺失值百分比。

  • 為特徵的缺失值百分比設定閾值。

  • 刪除缺失值百分比高於閾值的特徵。

  • 使用剩餘特徵訓練機器學習模型。

示例

以下是如何在 Python 中實現缺失值比率的示例:

# Importing the necessary libraries
import numpy as np

# Load the diabetes dataset
diabetes = np.genfromtxt(r'C:\Users\Leekha\Desktop\diabetes.csv', delimiter=',')

# Define the predictor variables (X) and the target variable (y)
X = diabetes[:, :-1]
y = diabetes[:, -1]

# Compute the percentage of missing values for each feature
missing_percentages = np.isnan(X).mean(axis=0)

# Set the threshold for the percentage of missing values for the features
threshold = 0.5

# Find the indices of the features with a missing value percentage
# above the threshold
high_missing_indices = [i for i, percentage in enumerate(missing_percentages) if percentage > threshold]

# Remove the high missing value features from the dataset
X_filtered = np.delete(X, high_missing_indices, axis=1)

# Print the shape of the filtered dataset
print('Shape of the filtered dataset:', X_filtered.shape)

以上程式碼對糖尿病資料集執行缺失值比率,並刪除缺失值百分比高於閾值的特徵。

輸出

執行此程式碼時,將產生以下輸出:

Shape of the filtered dataset: (769, 8)

缺失值比率的優點

以下是使用缺失值比率的優點:

  • 節省計算資源 - 減少特徵數量後,訓練機器學習模型所需的計算資源減少了。

  • 提高模型效能 - 透過刪除缺失值百分比高的特徵,缺失值比率可以提高機器學習模型的效能。

  • 簡化模型 - 特徵減少後,模型更容易解釋和理解。

  • 減少偏差 - 透過刪除缺失值百分比高的特徵,缺失值比率可以減少模型中的偏差。

缺失值比率的缺點

以下是使用缺失值比率的缺點:

  • 資訊丟失 - 缺失值比率可能導致資訊丟失,因為它刪除了可能包含重要資訊的特徵。

  • 影響非缺失資料 - 刪除缺失值百分比高的特徵有時會對非缺失資料產生負面影響,尤其是在這些特徵對於預測因變數很重要的情況下。

  • 對因變數的影響 - 刪除缺失值百分比高的特徵有時會對因變數產生負面影響,尤其是在這些特徵對於預測因變數很重要的情況下。

  • 選擇偏差 - 如果缺失值比率刪除了對預測因變數很重要的特徵,則可能引入選擇偏差。

廣告