類別資料的考量

Python 的探索性資料分析

George Boorman

Curriculum Manager, DataCamp

為什麼做 EDA?

  • 偵測模式與關係

 

 

  • 產生問題或假設

 

 

  • 為機器學習準備資料

紅色霓虹燈中的問號

1 Image credit: https://unsplash.com/@simonesecci
Python 的探索性資料分析

具代表性的資料

  • 樣本需能代表母體

例如:

  • 美國的教育與收入
    • 不能用法國的資料

美國國旗

法國國旗

1 Image credits: https://unsplash.com/@cristina_glebova; https://unsplash.com/@nimbus_vulpis
Python 的探索性資料分析

類別類別(Classes)

  • 類別 = 標籤

 

  • 調查大眾對婚姻的看法
    • 婚姻狀況
      • 單身
      • 已婚
      • 離婚
Python 的探索性資料分析

類別不平衡

樣本中各婚姻狀況的長條圖:離婚 700、單身 250、已婚 50

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% 的印度國內航班目的地是 Delhi
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 的探索性資料分析

交叉列聯表

呼叫 pd-dot-crosstab

pd.crosstab(
Python 的探索性資料分析

選擇索引

選擇作為索引的欄位

pd.crosstab(planes["Source"],
Python 的探索性資料分析

選擇欄位

選擇欄位

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)
Banglore Delhi 4232.21
Banglore New Delhi 12114.56
Chennai Kolkata 3859.76
Delhi Cochin 9987.63
Kolkata Banglore 9654.21
Mumbai Hyderabad 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)
Banglore Delhi 4232.21 4823.0
Banglore New Delhi 12114.56 10976.50
Chennai Kolkata 3859.76 3850.0
Delhi Cochin 9987.63 10260.0
Kolkata Banglore 9654.21 9345.0
Mumbai Hyderabad 3431.97 3342.0
Python 的探索性資料分析

一起來練習吧!

Python 的探索性資料分析

Preparing Video For Download...