分类数据的注意事项

Python 中的探索性数据分析

George Boorman

Curriculum Manager, DataCamp

为何进行 EDA?

  • 发现模式与关系

 

 

  • 生成问题或假设

 

 

  • 为机器学习准备数据

红色霓虹灯中的问号

1 Image credit: https://unsplash.com/@simonesecci
Python 中的探索性数据分析

具有代表性的数据

  • 样本应能代表总体

例如:

  • 美国教育 vs 收入
    • 不能用法国数据

美国国旗

法国国旗

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

分类类别

  • 类别 = 标签

 

  • 调查人们对婚姻的态度
    • 婚姻状况
      • 单身
      • 已婚
      • 离婚
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 中的探索性数据分析

样本 vs. 总体对比

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 中的探索性数据分析

Ayo berlatih!

Python 中的探索性数据分析

Preparing Video For Download...