異常値を除去するPythonのデータセットの手法

Pythonでは異常値をデータセットから削除するには以下のメソッドを利用できます。

  1. 条件文でデータをフィルタリング: 条件文を設定することでデータの異常性を判定し、正常なデータを抽出します。例えば、あるしきい値以上の外れ値を除去したい場合、以下のコードを使用します。
data = [1, 2, 3, 4, 5, 100, 6, 7, 8, 200]
threshold = 10
cleaned_data = [x for x in data if x <= threshold]
  1. 統計手法を用いてデータをフィルターする: データの統計的な特性(平均値や標準偏差など)を計算し、それによってデータが異常かどうかを判断し、正常なデータをスクリーニングする。例えば、平均値からずれた異常なデータを排除する場合は、以下のコードを使用できる:
import numpy as np
data = [1, 2, 3, 4, 5, 100, 6, 7, 8, 200]
mean = np.mean(data)
std = np.std(data)
threshold = 2.0
cleaned_data = [x for x in data if abs(x - mean) <= threshold * std]
  1. 標準化スコア
from scipy import stats
data = [1, 2, 3, 4, 5, 100, 6, 7, 8, 200]
threshold = 2.0
z_scores = stats.zscore(data)
cleaned_data = [x for x, z in zip(data, z_scores) if abs(z) <= threshold]

具体的なニーズやデータの特徴によって適切な方法を選択して異常データを削除します。

コメントを残す 0

Your email address will not be published. Required fields are marked *


广告
広告は10秒後に閉じます。
bannerAds