外れ値への対処

Pythonによる探索的データ分析

George Boorman

Curriculum Manager, DataCamp

外れ値とは?

  • 他のデータ点から大きく離れた観測値
    • 住宅価格の中央値:$400,000
    • 外れ値となる住宅価格:$5,000,000
  • 値が異なる理由を考える:
    • 所在地、寝室数、全体の広さなど

Large house with a swimming pool

1 画像クレジット: https://unsplash.com/@ralphkayden
Pythonによる探索的データ分析

記述統計量を使う

print(salaries["Salary_USD"].describe())
count       518.000
mean     104905.826
std       62660.107
min        3819.000
25%       61191.000
50%       95483.000
75%      137496.000
max      429675.000
Name: Salary_USD, dtype: float64
Pythonによる探索的データ分析

四分位範囲を使う

四分位範囲(IQR)

  • IQR = 75パーセンタイル − 25パーセンタイル
Pythonによる探索的データ分析

箱ひげ図におけるIQR

sns.boxplot(data=salaries,
            y="Salary_USD")
plt.show()

Box plot of salaries for data professionals, showing the 25th percentile at the bottom of the box, the 50th percentile as the middle line, the 75th percentile at the top of the box, and outliers as diamonds outside of the box

Pythonによる探索的データ分析

四分位範囲を使う

四分位範囲(IQR)

  • IQR = 75パーセンタイル − 25パーセンタイル
  • 上側の外れ値 > 75パーセンタイル + (1.5 * IQR)
  • 下側の外れ値 < 25パーセンタイル − (1.5 * IQR)
Pythonによる探索的データ分析

閾値の特定

# 75th percentile
seventy_fifth = salaries["Salary_USD"].quantile(0.75)

# 25th percentile twenty_fifth = salaries["Salary_USD"].quantile(0.25)
# Interquartile range salaries_iqr = seventy_fifth - twenty_fifth
print(salaries_iqr)
76305.0
Pythonによる探索的データ分析

外れ値の特定

# Upper threshold
upper = seventy_fifth + (1.5 * salaries_iqr)

# Lower threshold lower = twenty_fifth - (1.5 * salaries_iqr)
print(upper, lower)
251953.5 -53266.5
Pythonによる探索的データ分析

データを絞り込む

salaries[(salaries["Salary_USD"] < lower) | (salaries["Salary_USD"] > upper)] \

[["Experience", "Employee_Location", "Salary_USD"]]
        Experience    Employee_Location    Salary_USD
29      Mid           US                   429675.0
67      Mid           US                   257805.0
80      Senior        US                   263534.0
83      Mid           US                   429675.0
133     Mid           US                   403895.0
410     Executive     US                   309366.0
441     Senior        US                   362837.0
445     Senior        US                   386708.0
454     Senior        US                   254368.0
Pythonによる探索的データ分析

なぜ外れ値を確認するのか?

  • 外れ値は極端な値

    • データ全体を適切に代表していない可能性がある
  • 平均や標準偏差に影響を与える

  • 統計検定や機械学習モデルでは、正規分布に従うデータが必要になることが多い

Pythonによる探索的データ分析

外れ値にはどう対処する?

考えるべきこと:

  • なぜこれらの外れ値が存在するのか?
    • 上位の役職や国によっては給与が高くなる
    • データセットに残すことを検討する
  • データは正確か?
    • データ収集時に誤りがあった可能性はないか?
      • その場合は削除する
Pythonによる探索的データ分析

外れ値の削除

no_outliers = salaries[(salaries["Salary_USD"] > lower) & (salaries["Salary_USD"] < upper)]
print(no_outliers["Salary_USD"].describe())
count       509.000000
mean     100674.567780
std       53643.050057
min        3819.000000
25%       60928.000000
50%       95483.000000
75%      134059.000000
max      248257.000000
Name: Salary_USD, dtype: float64
Pythonによる探索的データ分析

給与の分布

Histogram of salaries after replacing outliers with the median, with extreme values from around 250000 to 450000 dollars

Histogram of salaries after replacing outliers with the median, which almost resembles a normal distribution

Pythonによる探索的データ分析

お疲れさまでした!

Pythonによる探索的データ分析

Preparing Video For Download...