機器學習 - 低方差過濾



低方差過濾器是一種用於機器學習的特徵選擇技術,用於識別並從資料集中刪除低方差特徵。此技術用於透過減少用於訓練模型的特徵數量來提高模型的效能,並去除幾乎沒有或沒有區分能力的特徵。

低方差過濾器透過計算資料集中每個特徵的方差並刪除方差低於某個閾值的特徵來工作。這樣做是因為方差低的特徵幾乎沒有或沒有區分能力,並且不太可能用於預測目標變數。

實施低方差過濾器的步驟如下:

  • 計算資料集中每個特徵的方差。

  • 為特徵的方差設定閾值。

  • 刪除方差低於閾值的特徵。

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

示例

以下是在 Python 中實施低方差過濾器的示例:

# Importing the necessary libraries
import pandas as pd
import numpy as np

# Load the diabetes dataset
diabetes = pd.read_csv(r'C:\Users\Leekha\Desktop\diabetes.csv')

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

# Compute the variance of each feature
variances = np.var(X, axis=0)

# Set the threshold for the variance of the features
threshold = 0.1

# Find the indices of the low variance features
low_var_indices = np.where(variances < threshold)

# Remove the low variance features from the dataset
X_filtered = np.delete(X, low_var_indices, axis=1)

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

輸出

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

Shape of the filtered dataset: (768, 8)

低方差過濾器的優點

以下是使用低方差過濾器的優點:

  • 減少過擬合 - 低方差過濾器可以透過去除對預測目標變數貢獻不大的特徵來幫助減少過擬合。

  • 節省計算資源 - 由於特徵較少,因此減少了訓練機器學習模型所需的計算資源。

  • 提高模型效能 - 透過去除低方差特徵,低方差過濾器可以提高機器學習模型的效能。

  • 簡化模型 - 由於特徵較少,因此模型更容易解釋和理解。

低方差過濾器的缺點

以下是使用低方差過濾器的缺點:

  • 資訊丟失 - 低方差過濾器可能導致資訊丟失,因為它會刪除可能包含重要資訊的特徵。

  • 影響非線性關係 - 低方差過濾器假設特徵之間的關係是線性的。對於特徵之間關係是非線性的資料集,它可能無法正常工作。

  • 對因變數的影響 - 刪除低方差特徵有時會對因變數產生負面影響,尤其是在這些特徵對於預測因變數很重要的情況下。

  • 選擇偏差 - 如果低方差過濾器刪除了對預測因變數很重要的特徵,則可能會引入選擇偏差。

廣告