カテゴリデータで考慮すべき点

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

George Boorman

Curriculum Manager, DataCamp

EDAを行う理由

  • パターンや関係の検出
  • 問いや仮説を立てる
  • 機械学習に向けたデータの準備

Question mark in a red neon light

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

代表性のあるデータ

  • サンプルは母集団を適切に代表している

たとえば:

  • 米国における教育レベルと所得の関係
    • フランス居住者のデータは使えない

USA flag

France flag

1 Image credits: https://unsplash.com/@cristina_glebova; https://unsplash.com/@nimbus_vulpis
Pythonによる探索的データ分析

カテゴリデータのクラス

  • クラス = ラベル
  • 結婚に対する人々の意識を調査
    • 婚姻状況
      • 未婚
      • 離婚
Pythonによる探索的データ分析

クラス不均衡

bar plot showing counts of marital statuses in a sample - 700 divorced, 250 single, and 50 married

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

クラスの頻度

print(planes["Destination"].value_counts())
Cochin       4391
Banglore     2773
Delhi        1219
New Delhi     888
Hyderabad     673
Kolkata       369
Name: Destination, dtype: int64
Pythonによる探索的データ分析

クラスの相対頻度

  • インド国内線の40%がデリー行き
planes["Destination"].value_counts(normalize=True)
Cochin       0.425773
Banglore     0.268884
Delhi        0.118200
New Delhi    0.086105
Hyderabad    0.065257
Kolkata      0.035780
Name: Destination, dtype: float64
  • サンプルは母集団(インド国内線)を適切に代表しているか?
Pythonによる探索的データ分析

クロス集計

Call pd-dot-crosstab

pd.crosstab(
Pythonによる探索的データ分析

インデックスを選択

Select column to use as the index

pd.crosstab(planes["Source"],
Pythonによる探索的データ分析

列を選択

Select the column

pd.crosstab(planes["Source"], planes["Destination"])
Pythonによる探索的データ分析

クロス集計

Destination  Banglore  Cochin  Delhi  Hyderabad  Kolkata  New Delhi
Source                                                             
Banglore            0       0   1199          0        0        868
Chennai             0       0      0          0      364          0
Delhi               0    4318      0          0        0          0
Kolkata          2720       0      0          0        0          0
Mumbai              0       0      0        662        0          0
Pythonによる探索的データ分析

クロス集計の拡張

Source Destination Median Price (IDR)
バンガロール デリー 4232.21
バンガロール ニューデリー 12114.56
チェンナイ コルカタ 3859.76
デリー コーチン 9987.63
コルカタ バンガロール 9654.21
ムンバイ ハイデラバード 3431.97
Pythonによる探索的データ分析

pd.crosstab()による集約値

pd.crosstab(planes["Source"], planes["Destination"],

values=planes["Price"], aggfunc="median")
Destination  Banglore   Cochin   Delhi  Hyderabad  Kolkata  New Delhi
Source                                                               
Banglore          NaN      NaN  4823.0        NaN      NaN    10976.5
Chennai           NaN      NaN     NaN        NaN   3850.0        NaN
Delhi             NaN  10262.0     NaN        NaN      NaN        NaN
Kolkata        9345.0      NaN     NaN        NaN      NaN        NaN
Mumbai            NaN      NaN     NaN     3342.0      NaN        NaN
Pythonによる探索的データ分析

サンプルと母集団の比較

Source Destination Median Price (IDR) Median Price (dataset)
バンガロール デリー 4232.21 4823.0
バンガロール ニューデリー 12114.56 10976.50
チェンナイ コルカタ 3859.76 3850.0
デリー コーチン 9987.63 10260.0
コルカタ バンガロール 9654.21 9345.0
ムンバイ ハイデラバード 3431.97 3342.0
Pythonによる探索的データ分析

お疲れさまでした!

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

Preparing Video For Download...