遺漏值

用 Python 拿下 Kaggle 競賽

Yauhen Babakhin

Kaggle Grandmaster

遺漏值

ID 類別特徵 數值特徵 二元目標
1 A 5.1 1
2 B 7.2 0
3 C 3.4 0
4 A NaN 1
5 NaN 2.6 0
6 A 5.3 0
用 Python 拿下 Kaggle 競賽

插補遺漏值

 

數值資料

  • 平均/中位數插補
ID 類別特徵 數值特徵 二元目標
1 A 5.1 1
2 B 7.2 0
3 C 3.4 0
4 A NaN 1
5 NaN 2.6 0
6 A 5.3 0
用 Python 拿下 Kaggle 競賽

插補遺漏值

 

數值資料

  • 平均/中位數插補
  • 常數插補
ID 類別特徵 數值特徵 二元目標
1 A 5.1 1
2 B 7.2 0
3 C 3.4 0
4 A 4.72 1
5 NaN 2.6 0
6 A 5.3 0
用 Python 拿下 Kaggle 競賽

插補遺漏值

 

數值資料

  • 平均/中位數插補
  • 常數插補
ID 類別特徵 數值特徵 二元目標
1 A 5.1 1
2 B 7.2 0
3 C 3.4 0
4 A -999 1
5 NaN 2.6 0
6 A 5.3 0
用 Python 拿下 Kaggle 競賽

插補遺漏值

 

數值資料

  • 平均/中位數插補
  • 常數插補

類別資料

  • 以最常見類別插補
ID 類別特徵 數值特徵 二元目標
1 A 5.1 1
2 B 7.2 0
3 C 3.4 0
4 A -999 1
5 NaN 2.6 0
6 A 5.3 0
用 Python 拿下 Kaggle 競賽

插補遺漏值

 

數值資料

  • 平均/中位數插補
  • 常數插補

類別資料

  • 以最常見類別插補
  • 新增類別插補
ID 類別特徵 數值特徵 二元目標
1 A 5.1 1
2 B 7.2 0
3 C 3.4 0
4 A -999 1
5 A 2.6 0
6 A 5.3 0
用 Python 拿下 Kaggle 競賽

插補遺漏值

 

數值資料

  • 平均/中位數插補
  • 常數插補

類別資料

  • 以最常見類別插補
  • 新增類別插補
ID 類別特徵 數值特徵 二元目標
1 A 5.1 1
2 B 7.2 0
3 C 3.4 0
4 A -999 1
5 MISS 2.6 0
6 A 5.3 0
用 Python 拿下 Kaggle 競賽

找出遺漏值

df.isnull().head(1)
         ID       cat       num    target
0     False     False     False     False
df.isnull().sum()
ID        0
cat       1
num       1
target    0
用 Python 拿下 Kaggle 競賽

數值遺漏值

# Import SimpleImputer
from sklearn.impute import SimpleImputer

# Different types of imputers mean_imputer = SimpleImputer(strategy='mean') constant_imputer = SimpleImputer(strategy='constant', fill_value=-999)
# Imputation df[['num']] = mean_imputer.fit_transform(df[['num']])
用 Python 拿下 Kaggle 競賽

類別遺漏值

# Import SimpleImputer
from sklearn.impute import SimpleImputer

# Different types of imputers
frequent_imputer = SimpleImputer(strategy='most_frequent')
constant_imputer = SimpleImputer(strategy='constant', fill_value='MISS')

# Imputation df[['cat']] = constant_imputer.fit_transform(df[['cat']])
用 Python 拿下 Kaggle 競賽

一起來練習吧!

用 Python 拿下 Kaggle 競賽

Preparing Video For Download...