为什么会有缺失值?

Python 中的机器学习特征工程

Robert O'Callaghan

Director of Data Science, Ordergroove

数据缺口的来源

  • 数据采集不当
  • 采集与管理错误
  • 有意省略数据
  • 数据转换可能产生缺失
Python 中的机器学习特征工程

为何重视?

  • 一些模型无法处理缺失数据(Null/NaN)
  • 缺失可能指向更广泛的数据问题
  • 缺失本身可作为有用特征
Python 中的机器学习特征工程

发现缺失值

print(df.info())
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 999 entries, 0 to 998
Data columns (total 12 columns):
 #   Column                      Non-Null Count  Dtype  
 --  ------                      --------------  -----  
 0   SurveyDate                  999 non-null    object 
...  ...                         ...             ...
 8   StackOverflowJobsRecommend  487 non-null    float64
 9   VersionControl              999 non-null    object 
 10  Gender                      693 non-null    object 
 11  RawSalary                   665 non-null    object 
dtypes: float64(2), int64(2), object(8)
memory usage: 93.7+ KB
Python 中的机器学习特征工程

查找缺失值

print(df.isnull())
   StackOverflowJobsRecommend  VersionControl  ... \ 
0                        True           False  ...
1                       False           False  ...
2                       False           False  ...
3                        True           False  ...
4                       False           False  ...

   Gender  RawSalary
0   False       True
1   False      False
2    True       True
3   False      False
4   False      False
Python 中的机器学习特征工程

查找缺失值

print(df['StackOverflowJobsRecommend'].isnull().sum())
512
Python 中的机器学习特征工程

查找非缺失值

print(df.notnull())
   StackOverflowJobsRecommend  VersionControl  ... \
0                       False            True  ...
1                        True            True  ...
2                        True            True  ...
3                       False            True  ...
4                        True            True  ...

   Gender  RawSalary
0    True      False
1    True       True
2   False      False
3    True       True
4    True       True
Python 中的机器学习特征工程

现在去查找缺失值!

Python 中的机器学习特征工程

Preparing Video For Download...