Data Whale第20期组队学习 Pandas学习—缺失数据


Python中有三种缺失值(空值):

类型说明
NaNpandas使用浮点值NaN(Not a Number)表示缺失数值数据
NApandas将缺失值表示为NA(not available),主要表示为np.nan
NonePython内置的None值,如创建一个空的列表list=[]

一、缺失值的统计和删除

1.1 统计缺失信息

缺失数据可以使用 isna 或 isnull (两个函数等价)来查看每个单元格是否缺失,结合 mean 可以计算出每列缺失值的比例。

import pandas as pd
import numpy as np
df = pd.read_csv('D:/binchen/txzq/data/learn_pandas.csv',usecols = ['Grade', 'Name', 'Gender', 'Height','Weight', 'Transfer'])
print("df.isna().head()=",df.isna().head()) # 查看每个单元格是否缺失
# df.isna().head()=    Grade   Name  Gender  Height  Weight  Transfer
# 0  False  False   False   False   False     False
# 1  False  False   False   False   False     False
# 2  False  False   False   False   False     False
# 3  False  False   False    True   False     False
# 4  False  False   False   False   False     False
print("df.isna().mean()=\n",df.isna().mean()) # 查看缺失的比例
# df.isna().mean()=
#  Grade       0.000
# Name        0.000
# Gender      0.000
# Height      0.085
# Weight      0.055
# Transfer    0.060
# dtype: float64
print("df.isnull().head()=\n",df.isnull().head()) #查看每个单元格是否缺失
print("df.isnull().mean()=\n",df.isnull().mean()) # 查看缺失的比例

想要查看某一列缺失或者非缺失的行,可以利用 Series 上的 isna 或者 notna 进行布尔索引。

# 查看身高缺失的行
print("df[df.Height.isna()].head()=\n",df[df.Height.isna()].head())
# df[df.Height.isna()].head()=
#          Grade          Name  Gender  Height  Weight Transfer
# 3   Sophomore  Xiaojuan Sun  Female     NaN    41.0        N
# 12     Senior      Peng You  Female     NaN    48.0      NaN
# 26     Junior     Yanli You  Female     NaN    48.0        N
# 36   Freshman  Xiaojuan Qin    Male     NaN    79.0        Y
# 60   Freshman    Yanpeng Lv    Male     NaN    65.0        N
print("df[df.Transfer.isna()].head()=\n",df[df.Transfer.isna()].head())
# df[df.Transfer.isna()].head()=
#       Grade           Name  Gender  Height  Weight Transfer
# 12  Senior       Peng You  Female     NaN    48.0      NaN
# 21  Senior  Xiaopeng Shen    Male   166.0    62.0      NaN
# 69  Junior    Chunquan Xu  Female   162.1    54.0      NaN
# 84  Junior     Yanjuan Lv  Female   159.3    49.0      NaN
# 87  Senior      Feng Yang  Female   167.0    52.0      NaN

同时对几个列,检索出全部为缺失或者至少有一个缺失或者没有缺失的行,可以使用 isna, notna 和 any, all 的组合。

# 对身高、体重和转系情况这3列分别进行这三种情况的检索
sub_s=df[['Height', 'Weight', 'Transfer']]
print("df[sub_s.isna().all(1)]=\n",df[sub_s.isna().all(1)]) # 全部缺失
# df[sub_s.isna().all(1)]=
#        Grade          Name Gender  Height  Weight Transfer
# 102  Junior  Chengli Zhao   Male     NaN     NaN      NaN
print("df[sub_s.isna().any(1)].head()=\n",df[sub_s.isna().any(1)].head())# 至少有一个缺失
# df[sub_s.isna().any(1)].head()=
#          Grade           Name  Gender  Height  Weight Transfer
# 3   Sophomore   Xiaojuan Sun  Female     NaN    41.0        N
# 9      Junior        Juan Xu  Female   164.8     NaN        N
# 12     Senior       Peng You  Female     NaN    48.0      NaN
# 21     Senior  Xiaopeng Shen    Male   166.0    62.0      NaN
# 26     Junior      Yanli You  Female     NaN    48.0        N
print("df[sub_s.notna().all(1)].head()=\n",df[sub_s.notna().all(1)].head()) # 没有缺失
# df[sub_s.notna().all(1)].head()=
#         Grade            Name  Gender  Height  Weight Transfer
# 0   Freshman    Gaopeng Yang  Female   158.9    46.0        N
# 1   Freshman  Changqiang You    Male   166.5    70.0        N
# 2     Senior         Mei Sun    Male   188.9    89.0        N
# 4  Sophomore     Gaojuan You    Male   174.0    74.0        N
# 5   Freshman     Xiaoli Qian  Female   158.0    51.0        N

1.2 删除缺失信息

数据处理中经常需要根据缺失值的大小、比例或其他特征来进行行样本或列特征的删除, pandas 中提供了 dropna 函数来进行操作。

dropna 的主要参数为轴方向 axis (默认为0,即删除行)、删除方式 how 、删除的非缺失值个数阈值 thresh ( 非缺失值 没有达到这个数量的相应维度会被删除)、备选的删除子集 subset ,其中 how 主要有 any 和 all 两种参数可以选择。

# 可以使用布尔索引来完成上述的两个操作
res = df.loc[df[['Height', 'Weight']].notna().all(1)]
print("res.shape=",res.shape)
res = df.loc[:, ~(df.isna().sum()>15)]
print("res.head()=\n",res.head())
# res.head()=
#         Grade            Name  Gender  Weight Transfer
# 0   Freshman    Gaopeng Yang  Female    46.0        N
# 1   Freshman  Changqiang You    Male    70.0        N
# 2     Senior         Mei Sun    Male    89.0        N
# 3  Sophomore    Xiaojuan Sun  Female    41.0        N
# 4  Sophomore     Gaojuan You    Male    74.0        N

二、缺失值的填充和插值

2.1 利用fillna进行填充

在 fillna 中有三个参数是常用的: value、 method和 limit 。其中, value 为填充值,可以是标量,也可以是索引到元素的字典映射; method 为填充方法,有用前面的元素填充 ffill 和用后面的元素填充 bfill 两种类型, limit 参数表示连续缺失值的最大填充次数。

Ser = pd.Series([np.nan, 1, np.nan, np.nan, 2, np.nan],list('aaabcd'))
print("Ser=",Ser)
# Ser= a    NaN
# a    1.0
# a    NaN
# b    NaN
# c    2.0
# d    NaN
# dtype: float64
print("Ser.fillna(method='ffill')=",Ser.fillna(method='ffill'))# 用前面的值向后填充
# Ser.fillna(method='ffill')= a    NaN
# a    1.0
# a    1.0
# b    1.0
# c    2.0
# d    2.0
# dtype: float64
print("Ser.fillna(method='ffill', limit=1)=\n",Ser.fillna(method='ffill', limit=1) )
# Ser.fillna(method='ffill', limit=1)=
#  a    NaN
# a    1.0
# a    1.0
# b    NaN
# c    2.0
# d    2.0
# dtype: float64
print("Ser.fillna(Ser.mean())=\n",Ser.fillna(Ser.mean()) )# value为标量)
# Ser.fillna(Ser.mean())=
#  a    1.5
# a    1.0
# a    1.5
# b    1.5
# c    2.0
# d    1.5
# dtype: float64
print("Ser.fillna({'a': 100, 'd': 200})=\n",Ser.fillna({'a': 100, 'd': 200}))
# Ser.fillna({'a': 100, 'd': 200})=
#  a    100.0
# a      1.0
# a    100.0
# b      NaN
# c      2.0
# d    200.0
# dtype: float64
# 为了更加合理地填充,需要先进行分组后再操作
# 根据年级进行身高的均值填充
print("df.groupby('Grade')['Height'].transform(lambda x: x.fillna(x.mean())).head()=\n",
      df.groupby('Grade')['Height'].transform(lambda x: x.fillna(x.mean())).head())
# df.groupby('Grade')['Height'].transform(lambda x: x.fillna(x.mean())).head()=
#  0    158.900000
# 1    166.500000
# 2    188.900000
# 3    163.075862
# 4    174.000000
# Name: Height, dtype: float64


2.2 插值函数

对于 interpolate 而言,除了插值方法(默认为 linear 线性插值)之外,有与 fillna 类似的两个常用参数,一个是控制方向的 limit_direction ,另一个是控制最大连续缺失值插值个数的 limit 。其中,限制插值的方向默认为 forward ,这与 fillna 的 method 中的 ffill 是类似的,若想要后向限制插值或者双向限制插值可以指定为 backward 或 both 。

Ser1 = pd.Series([np.nan, np.nan, 1,np.nan, np.nan, np.nan,2, np.nan, np.nan])
print("Ser1.values=",Ser1.values)
# Ser1.values= [nan nan  1. nan nan nan  2. nan nan]
# 在默认线性插值法下分别进行 backward 和双向限制插值,同时限制最大连续条数为1
res = Ser1.interpolate(limit_direction='backward', limit=1)
print("res.values=",res.values)
# res.values= [ nan 1.   1.    nan  nan 1.75 2.    nan  nan]
res = Ser1.interpolate(limit_direction='both', limit=1)
print("res.values=",res.values)
# res.values= [ nan 1.   1.   1.25  nan 1.75 2.   2.    nan]
# 第二种常见的插值是最近邻插补,即缺失值的元素和离它最近的非缺失值元素一样
print("Ser1.interpolate('nearest').values=\n",Ser1.interpolate('nearest').values)
# Ser1.interpolate('nearest').values=
#  [nan nan  1.  1.  1.  2.  2. nan nan]
# 索引插值根据索引大小进行线性插值
# 构造不等间距的索引进行演示
Ser2 = pd.Series([0,np.nan,10],index=[0,1,10])
print("Ser2=",Ser2)
# Ser2= 0      0.0
# 1      NaN
# 10    10.0
# dtype: float64
print("Ser2.interpolate()=\n",Ser2.interpolate())
# Ser2.interpolate()=
#  0      0.0
# 1      5.0
# 10    10.0
# dtype: float64
print("Ser2.interpolate(method='index')=",Ser2.interpolate(method='index'))
# Ser2.interpolate(method='index')= 0      0.0
# 1      1.0
# 10    10.0
# dtype: float64
Ser3= pd.Series([0,np.nan,10],index=pd.to_datetime(['20200101','20200102','20200111']))
print("Ser3=",Ser3)
# Ser3= 2020-01-01     0.0
# 2020-01-02     NaN
# 2020-01-11    10.0
# dtype: float64
print("Ser3.interpolate()=",Ser3.interpolate())
# Ser3.interpolate()= 2020-01-01     0.0
# 2020-01-02     5.0
# 2020-01-11    10.0
# dtype: float64
print("Ser3.interpolate(method='index'))=\n",
      Ser3.interpolate(method='index'))
# Ser3.interpolate(method='index'))=
#  2020-01-01     0.0
# 2020-01-02     1.0
# 2020-01-11    10.0
# dtype: float64

三、Nullable类型

3.1 缺失记号及其缺陷

在 python 中的缺失值用 None 表示,该元素除了等于自己本身之外,与其他任何元素不相等。

print("None == None:",None == None)
# None == None: True
print("None == False:",None == False)
# None == False: False
print("None == []:",None == [])
# None == []: False
print("None == '':",None == '')
# None == '': False
"""
在 numpy 中利用 np.nan 来表示缺失值,该元素除了不和其他任何元素相等之外,和自身的比较结果也返回 False 
"""
print("np.nan == np.nan:",np.nan == np.nan)
# np.nan == np.nan: False
print("np.nan == None:",np.nan == None)
# np.nan == None: False
print("np.nan == False:",np.nan == False)
# np.nan == False: False
"""
虽然在对缺失序列或表格的元素进行比较操作的时候, np.nan 的对应位置会返回 False ,
但是在使用 equals 函数进行两张表或两个序列的相同性检验时,会自动跳过两侧表都是缺失值的位置,直接返回 True
"""
S1=pd.Series([1,6,np.nan,9])
S2=pd.Series([1,6,2,9])
S3=pd.Series([1,6,np.nan,9])
print("S1==1:",S1==1)
# S1==1: 0     True
# 1    False
# 2    False
# 3    False
# dtype: bool
print("S1.equals(S2):",S1.equals(S2))
# S1.equals(S2): False
print("S1.equals(S3):",S1.equals(S3))
# S1.equals(S3): True
"""
在时间序列的对象中, pandas 利用 pd.NaT 来指代缺失值,它的作用和 np.nan 是一致的
"""
print("pd.to_timedelta(['30s', np.nan]) =",pd.to_timedelta(['30s', np.nan]) )
# pd.to_timedelta(['30s', np.nan]) = TimedeltaIndex(['0 days 00:00:30', NaT], dtype='timedelta64[ns]', freq=None)
print("pd.to_datetime(['20200101', np.nan])=",pd.to_datetime(['20200101', np.nan]))
# pd.to_datetime(['20200101', np.nan])= DatetimeIndex(['2020-01-01', 'NaT'], dtype='datetime64[ns]', freq=None)

在 pandas 中可以看到 object 类型的对象,而 object 是一种混杂对象类型,如果出现了多个类型的元素同时存储在 Series 中,它的类型就会变成 object 。

print("pd.Series([1, 'two',np.nan,2.0])=\n",pd.Series([1, 'two',np.nan,2.0]))
# pd.Series([1, 'two',np.nan,2.0])=
#  0      1
# 1    two
# 2    NaN
# 3      2
# dtype: object
print("pd.Series([1, np.nan]).dtype=",pd.Series([1, np.nan]).dtype)
# pd.Series([1, np.nan]).dtype= float64
print("pd.Series([True, False, np.nan]).dtype=", pd.Series([True, False, np.nan]).dtype)
# pd.Series([True, False, np.nan]).dtype= object

3.2 Nullable类型的性质

从字面意义上看 Nullable 就是可空的,言下之意就是序列类型不受缺失值的影响。

print("pd.Series([np.nan, 1,3], dtype = 'Int64')=",pd.Series([np.nan, 1,3], dtype = 'Int64'))
# pd.Series([np.nan, 1,3], dtype = 'Int64')= 0    <NA>
# 1       1
# 2       3
# dtype: Int64
print("pd.Series([False,np.nan, True], dtype = 'boolean')=",
      pd.Series([False,np.nan, True], dtype = 'boolean'))
# pd.Series([False,np.nan, True], dtype = 'boolean')= 0    False
# 1     <NA>
# 2     True
# dtype: boolean
print("pd.Series(['Hello',np.nan, 'my_str'], dtype = 'string')=",
      pd.Series(['Hello',np.nan, 'my_str'], dtype = 'string'))
# pd.Series(['Hello',np.nan, 'my_str'], dtype = 'string')= 0     Hello
# 1      <NA>
# 2    my_str
# dtype: string
"""
在 Int 的序列中,返回的结果会尽可能地成为 Nullable 的类型
"""
print("pd.Series([np.nan, 0], dtype = 'Int64') + 2020:",pd.Series([np.nan, 0], dtype = 'Int64') + 2020)
# pd.Series([np.nan, 0], dtype = 'Int64') + 2020: 0    <NA>
# 1    2020
# dtype: Int64
print("pd.Series([np.nan, 0], dtype = 'Int64') == 0:",pd.Series([np.nan, 0], dtype = 'Int64') == 0)
# pd.Series([np.nan, 0], dtype = 'Int64') == 0: 0    <NA>
# 1    True
# dtype: boolean
print("pd.Series([np.nan, 9], dtype = 'Int64') * 0.5:",pd.Series([np.nan, 9], dtype = 'Int64') * 0.5)
# pd.Series([np.nan, 9], dtype = 'Int64') * 0.5: 0    NaN
# 1    4.5
# dtype: float64

对于 boolean 类型的序列而言,其和 bool 序列的行为主要有两点区别,第一点是带有缺失的布尔列表无法进行索引器中的选择,而 boolean 会把缺失值看作 False ;第二点是在进行逻辑运算时, bool 类型在缺失处返回的永远是 False ,而 boolean 会根据逻辑运算是否能确定唯一结果来返回相应的值。那什么叫能否确定唯一结果呢?举个简单例子: True | pd.NA 中无论缺失值为什么值,必然返回 True ; False | pd.NA 中的结果会根据缺失值取值的不同而变化,此时返回 pd.NA ; False & pd.NA 中无论缺失值为什么值,必然返回 False 。

在 Int 的序列中,返回的结果会尽可能地成为 Nullable 的类型
"""
print("pd.Series([np.nan, 0], dtype = 'Int64') + 2020:",pd.Series([np.nan, 0], dtype = 'Int64') + 2020)
# pd.Series([np.nan, 0], dtype = 'Int64') + 2020: 0    <NA>
# 1    2020
# dtype: Int64
print("pd.Series([np.nan, 0], dtype = 'Int64') == 0:",pd.Series([np.nan, 0], dtype = 'Int64') == 0)
# pd.Series([np.nan, 0], dtype = 'Int64') == 0: 0    <NA>
# 1    True
# dtype: boolean
print("pd.Series([np.nan, 9], dtype = 'Int64') * 0.5:",pd.Series([np.nan, 9], dtype = 'Int64') * 0.5)
# pd.Series([np.nan, 9], dtype = 'Int64') * 0.5: 0    NaN
# 1    4.5
# dtype: float64
s = pd.Series(['W', 'O','R','L','D'])
s_bool = pd.Series([True, np.nan,False,np.nan,True])
s_boolean = pd.Series([True, np.nan,False,np.nan,True]).astype('boolean')
print("s[s_boolean]:",s[s_boolean])
# s[s_boolean]: 0    W
# 4    D
# dtype: object
print("s_boolean & True:",s_boolean & True)
# s_boolean & True: 0     True
# 1     <NA>
# 2    False
# 3     <NA>
# 4     True
# dtype: boolean
print("s_boolean | True:",s_boolean | True)
# s_boolean | True: 0    True
# 1    True
# 2    True
# 3    True
# 4    True
# dtype: boolean
print("~s_boolean:",~s_boolean)
# ~s_boolean: 0    False
# 1     <NA>
# 2     True
# 3     <NA>
# 4    False
# dtype: boolean
# 一般在实际数据处理时,可以在数据集读入后,先通过 convert_dtypes 转为 Nullable 类型
df = pd.read_csv('D:/binchen/txzq/data/learn_pandas.csv')
df = df.convert_dtypes()
print("df.dtypes=",df.dtypes)
# df.dtypes= School          string
# Grade           string
# Name            string
# Gender          string
# Height         float64
# Weight           Int64
# Transfer        string
# Test_Number      Int64
# Test_Date       string
# Time_Record     string
# dtype: object

3.3 缺失数据的计算和分组

当调用函数 sum, prob 使用加法和乘法的时候,缺失数据等价于被分别视作0和1,即不改变原来的计算结果。

Ser4=pd.Series([2,3,np.nan,4,5])
print("Ser4.sum()=",Ser4.sum())
# Ser4.sum()= 14.0
print("Ser4.prod()=",Ser4.prod())
# Ser4.sum()= 14.0
"""
当进行单个标量运算的时候,除了 np.nan ** 0 和 1 ** np.nan 这两种情况为确定的值之外,
所有运算结果全为缺失( pd.NA 的行为与此一致 ),并且 np.nan 在比较操作时一定返回 False ,
而 pd.NA 返回 pd.NA 
"""
print( np.nan == 0,pd.NA == 0,np.nan > 0,pd.NA > 0)
# False <NA> False <NA>
print(np.nan + 1,np.log(np.nan),np.add(np.nan, 1),np.nan ** 0)
# nan nan nan 1.0
print(pd.NA ** 0,1 ** np.nan,1 ** pd.NA)
# 1 1.0 1
"""
diff, pct_change 这两个函数虽然功能相似,但是对于缺失的处理不同,
前者凡是参与缺失计算的部分全部设为了缺失值,而后者缺失值位置会被设为 0% 的变化率
"""
print("Ser4.diff()=",Ser4.diff())
# Ser4.diff()= 0    NaN
# 1    1.0
# 2    NaN
# 3    NaN
# 4    1.0
# dtype: float64
print("Ser4.pct_change()=",Ser4.pct_change())
# Ser4.pct_change()= 0         NaN
# 1    0.500000
# 2    0.000000
# 3    0.333333
# 4    0.250000
# dtype: float64

对于一些函数而言,缺失可以作为一个类别处理,例如在 groupby, get_dummies 中可以设置相应的参数来进行增加缺失类别。

df_nan = pd.DataFrame({'category':['a','a','b',np.nan,np.nan],'value':[1,3,5,7,9]})
print("df_nan=",df_nan)
# df_nan=   category  value
# 0        a      1
# 1        a      3
# 2        b      5
# 3      NaN      7
# 4      NaN      9
print(" df_nan.groupby('category',dropna=False)['value'].mean()=",
      df_nan.groupby('category',dropna=False)['value'].mean())
#  df_nan.groupby('category',dropna=False)['value'].mean()= category
# a      2
# b      5
# NaN    8
# Name: value, dtype: int64
print("pd.get_dummies(df_nan.category, dummy_na=True)=",
      pd.get_dummies(df_nan.category, dummy_na=True))
# pd.get_dummies(df_nan.category, dummy_na=True)=    a  b  NaN
# 0  1  0    0
# 1  1  0    0
# 2  0  1    0
# 3  0  0    1
# 4  0  0    1

参考文献

1、https://datawhalechina.github.io/joyful-pandas/build/html/%E7%9B%AE%E5%BD%95/ch7.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
鲸鱼优化(Whale Optimization Algorithm)是一种模拟鲸鱼群体行为的优化算法,灵感来自于鲸鱼群体中个体之间的协同行动。该算法结合了全局搜索和局部搜索的优点,能够有效地寻找复杂问题的最优解。 深度学习是一种基于人工神经网络的机器学习方法,通过多层次的神经元结构进行模式识别和数据分析。而极限学习机(Extreme Learning Machine)是一种以单隐层前馈神经网络为基础的机器学习算法,其强调快速训练和相对较少的参数调整。 鲸鱼优化-delm深度学习极限学习机结合了鲸鱼优化和深度学习极限学习机的特点,具有以下优势: 首先,鲸鱼优化算法通过模拟鲸鱼群体的行为,能够在解空间中进行全局搜索,从而找到问题的全局最优解或者接近最优解的解。这对于复杂的深度学习模型参数优化非常有帮助。 其次,极限学习机作为深度学习的一种变种,具有快速训练和较少参数调整的特点。这意味着鲸鱼优化-delm深度学习极限学习机在解决深度学习问题时,具有更高的效率和更好的可扩展性。 最后,鲸鱼优化-delm深度学习极限学习机的结合,能够同时发挥两者的优点,既能进行全局搜索,又能实现快速训练和调整参数。这使得算法在解决复杂问题时更加灵活和高效。 综上所述,鲸鱼优化-delm深度学习极限学习机是一种能够有效解决深度学习问题的优化算法,其集成了鲸鱼优化和极限学习机的特点,具有全局搜索和快速训练的优势,对于提高深度学习模型的性能和效率具有重要意义。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值