机器学习—数据清洗总结

需要清洗数据的主要类型:

残缺数据、错误数据、重复数据

数据清洗方法:

(1)不完整数据:经验推导,平均最大最下,甚至概率估计,或建立回归模型,插值法等 (2)错误值检测,异常点检测,偏差分析,规则库等 (3)重复值,将重复记录合并、清楚

数据清洗的评价标准

(1)可信性,可信性包括精确性、完整性、一致性、有效性、唯一性等指标。

(1)精确性:描述数据是否与其对应的客观实体的特征相一致。 (2)完整性:描述数据是否存在缺失记录或缺失字段。 (3)一致性:描述同一实体的同一属性的值在不同的系统是否一致。 (4)有效性:描述数据是否满足用户定义的条件或在一定的域值范围内。 (5)唯一性:描述数据是否存在重复记录。

(2) 数据的可用性

数据的可用性考察指标主要包括时间性和稳定性。 (1)时间性:描述数据是当前数据还是历史数据。 (2)稳定性:描述数据是否是稳定的,是否在其有效期内。

常见的数据清洗算法(属性清洗算法) 空缺值的清洗 对于空缺值的清洗可以采取忽略元组,人工填写空缺值,使用一个全局变量填充空缺值,使用属性的平均值、中问值、最大值、最小值或更为复杂的概率统计函数值来填充空缺值。

噪声数据的清洗 分箱(Binning),通过考察属性值的周围值来平滑属性的值。属性值被分布到一些等深或等宽的“箱”中,用箱中属性值的平均值或中值来替换“箱”中的属性值;计算机和人工检查相结合,计算机检测可疑数据,然后对它们进行人工判断;使用简单规则库检测和修正错误;使用不同属性间的约束检测和修正错误;使用外部数据源检测和修正错误。

不一致数据的清洗 对于有些事务,所记录的数据可能存在不一致。有些数据不一致,可以使用其他材料人工加以更正。例如,数据输入时的错误可以使用纸上的记录加以更正。知识工程工具

也可以用来检测违反限制的数据。例如,知道属性间的函数依赖,可以查找违反函数依赖的值。此外,数据集成也可能产生数据不一致。

重复数据的清洗 目前消除重复记录的基本思想是“排序和合并”,先将数据库中的记录排序,然后通过比较邻近记录是否相似来检测记录是否重复。

消除重复记录的算法主要有:优先队列算法,近邻排序算法(Sorted—Neighborhood Method),多趟近邻排序(Multi—Pass Sorted—Neighborhood)。

Series对象-类似于数组的对象

In [46]:

import pandas as pd
object=pd.Series([2,5,8,9])
print(object)
#结果中包含一列数据和一列标签
print(object.values) #对应的数据值
print(object.index)#标签或索引值
#按自己的想法构建标签
object=pd.Series([2,5,8,9],index=['a','b','c','d'])
print(object)

#对序进行运算,保留满足要求的数据
print(object[object>5])
#把Series当成一个字典,使用in进行判断
print('a' in object)
print('8' in object)
0    2
1    5
2    8
3    9
dtype: int64
[2 5 8 9]
RangeIndex(start=0, stop=4, step=1)
a    2
b    5
c    8
d    9
dtype: int64
c    8
d    9
dtype: int64
True
False

In [52]:

#DataFrame数据框,一种数据结构
data={'year':[2000,2001,2002,2003],'income':[3000,3500,4500,6000]}
data=pd.DataFrame(data)
print(data)
#标签重置
data1=pd.DataFrame(data,columns=['year','income','outcome'],index=['a','b','c','d'])
print(data1)
   year  income
0  2000    3000
1  2001    3500
2  2002    4500
3  2003    6000
   year  income  outcome
a   NaN     NaN      NaN
b   NaN     NaN      NaN
c   NaN     NaN      NaN
d   NaN     NaN      NaN

In [61]:

data1['money']=np.arange(4)
print(data1)
#重建索引
data2=data1.reindex(['a','b','c','d','e'])
print(data2)
#索引删除以及过滤的方法
print(data1.drop(['a']))
print(data1[data1['money']>2])

#对DataFrame进行排序

data=pd.DataFrame(np.arange(10).reshape((2,5)),index=['c','a'],
                  columns=['one','four','two','three','five'])
print(data)
#默认对行进行排序
print(data.sort_index())

#默认对列进行排序
print(data.sort_index(axis=1))
   year  income  outcome  money
a   NaN     NaN      NaN      0
b   NaN     NaN      NaN      1
c   NaN     NaN      NaN      2
d   NaN     NaN      NaN      3
   year  income  outcome  money
a   NaN     NaN      NaN    0.0
b   NaN     NaN      NaN    1.0
c   NaN     NaN      NaN    2.0
d   NaN     NaN      NaN    3.0
e   NaN     NaN      NaN    NaN
   year  income  outcome  money
b   NaN     NaN      NaN      1
c   NaN     NaN      NaN      2
d   NaN     NaN      NaN      3
   year  income  outcome  money
d   NaN     NaN      NaN      3
   one  four  two  three  five
c    0     1    2      3     4
a    5     6    7      8     9
   one  four  two  three  five
a    5     6    7      8     9
c    0     1    2      3     4
   five  four  one  three  two
c     4     1    0      3    2
a     9     6    5      8    7

汇总及统计描述

In [69]:

data=pd.DataFrame(np.arange(10).reshape((2,5)),index=['c','a'],
columns=['one','four','two','three','five'])
print(data)
#统计描述
print("一次性输出多个描述性指标")
print(data.describe())
#对各个指标下的数据求和
print(data.sum())
#对个索引求和
print(data.sum(axis=1))

#求各个指标的
print("各指标最小值")
print(data.min())
print("各指标方差var")
print(data.var())
print("各指标或各维度的众数")
print(data.mode())
print("非空元素计算")
print(data.count())
   one  four  two  three  five
c    0     1    2      3     4
a    5     6    7      8     9
一次性输出多个描述性指标
            one      four       two     three      five
count  2.000000  2.000000  2.000000  2.000000  2.000000
mean   2.500000  3.500000  4.500000  5.500000  6.500000
std    3.535534  3.535534  3.535534  3.535534  3.535534
min    0.000000  1.000000  2.000000  3.000000  4.000000
25%    1.250000  2.250000  3.250000  4.250000  5.250000
50%    2.500000  3.500000  4.500000  5.500000  6.500000
75%    3.750000  4.750000  5.750000  6.750000  7.750000
max    5.000000  6.000000  7.000000  8.000000  9.000000
one       5
four      7
two       9
three    11
five     13
dtype: int64
c    10
a    35
dtype: int64
各指标最小值
one      0
four     1
two      2
three    3
five     4
dtype: int32
各指标方差var
one      12.5
four     12.5
two      12.5
three    12.5
five     12.5
dtype: float64
各指标或各维度的众数
   one  four  two  three  five
0    0     1    2      3     4
1    5     6    7      8     9
非空元素计算
one      2
four     2
two      2
three    2
five     2
dtype: int64

相关系数与协方差

In [72]:

data=pd.DataFrame(np.random.random(20).reshape((4,5)),index=['c','a','b','c'],
                  columns=['one','four','two','three','five'])
print(data)
#输出one和three的相关系数
print(data.one.corr(data.three))
#输出one和three的协方差
print(data.corrwith(data.one))
        one      four       two     three      five
c  0.194879  0.329147  0.967115  0.674350  0.275277
a  0.426067  0.840322  0.224656  0.937782  0.611792
b  0.196726  0.897771  0.260533  0.663372  0.207867
c  0.039813  0.977257  0.514693  0.134688  0.272775
0.9341174441749929
one      1.000000
four    -0.106747
two     -0.402354
three    0.934117
five     0.835847
dtype: float64

成员信息统计相关

In [79]:

data=pd.Series(['a','a','b','b','b','c','d','d'])
#
print(data.unique())
print(data.isin(['b']))
print(pd.value_counts(data.values,sort=False))

#缺失值处理
data=pd.Series(['a','a','b',np.nan,'b','c',np.nan,'d'])
print(data.isnull())
#根据标签对缺失值进行过滤
print(data.dropna())
#用指定的方法填充缺失值
print(data.ffill())
#用0进行填充
print(data.fillna(0))
['a' 'b' 'c' 'd']
0    False
1    False
2     True
3     True
4     True
5    False
6    False
7    False
dtype: bool
c    1
d    2
a    2
b    3
dtype: int64
0    False
1    False
2    False
3     True
4    False
5    False
6     True
7    False
dtype: bool
0    a
1    a
2    b
4    b
5    c
7    d
dtype: object
0    a
1    a
2    b
3    b
4    b
5    c
6    c
7    d
dtype: object
0    a
1    a
2    b
3    0
4    b
5    c
6    0
7    d
dtype: object

数据合并

In [17]:

import numpy as np
import pandas as pd
import pandas as pd
import numpy as np
df1 = pd.DataFrame({'key':['b','b','a','c','a','a','b'],'data1': range(7)})
df1

Out[17]:

 

 keydata1
0b0
1b1
2a2
3c3
4a4
5a5
6b6

In [18]:

df2 = pd.DataFrame({'key':['a','b','d'],'data2':range(3)})
df2

Out[18]:

 

 keydata2
0a0
1b1
2d2

In [16]:

pd.merge(df1,df2)

Out[16]:

 

 keydata1data2
0b01
1b11
2b61
3a20
4a40
5a50

In [20]:

#重叠数据合并
data3=pd.DataFrame({'level':['a','b','c','d'],'number':[1,3,5,np.nan]})
data4=pd.DataFrame({'level':['a','b','c','e'],'number2':[2,np.nan,6,10]})
print(data3.combine_first(data4))
  level  number  number2
0     a     1.0      2.0
1     b     3.0      NaN
2     c     5.0      6.0
3     d     NaN     10.0

数据重塑和轴向旋转

数据重塑主要使用reshape函数,旋转主要使用unstack和stack两个函数

In [21]:

data=pd.DataFrame(np.arange(12).reshape(3,4),columns=['a','b','c','d'],index=['wang','li','zhang'])
print(data)
       a  b   c   d
wang   0  1   2   3
li     4  5   6   7
zhang  8  9  10  11

In [22]:

#数据旋转
print(data.unstack())
a  wang      0
   li        4
   zhang     8
b  wang      1
   li        5
   zhang     9
c  wang      2
   li        6
   zhang    10
d  wang      3
   li        7
   zhang    11
dtype: int32

数据转换

In [28]:

#删除重复行数据
data=pd.DataFrame({'a':[1,3,3,4],'b':[1,3,3,5]})
print(data)
print("重复行检测:")
print(data.duplicated())
print("用drop_duplicates去除重复行")
print(data.drop_duplicates())
print("数据替换结果")
print(data.replace(1,2))#第一行全部替换成了2
   a  b
0  1  1
1  3  3
2  3  3
3  4  5
重复行检测:
0    False
1    False
2     True
3    False
dtype: bool
用drop_duplicates去除重复行
   a  b
0  1  1
1  3  3
3  4  5
数据替换结果
   a  b
0  2  2
1  3  3
2  3  3
3  4  5

数据分段

In [34]:

data=[11,15,18,120,25,26,27,24]
bins=[15,20,25]
print(data)
print(pd.cut(data,bins)) #bins相邻数据给出了段的描述
[11, 15, 18, 120, 25, 26, 27, 24]
[NaN, NaN, (15, 20], NaN, (20, 25], NaN, NaN, (20, 25]]
Categories (2, interval[int64]): [(15, 20] < (20, 25]]

排列和采样

In [37]:

data=np.random.permutation(5)
print(data)#5以内的数,随机排序
#对数据进行采样
df=pd.DataFrame(np.arange(12).reshape(4,3))
samp=np.random.permutation(3)
print(df)
print(samp)
[2 0 3 4 1]
   0   1   2
0  0   1   2
1  3   4   5
2  6   7   8
3  9  10  11
[1 2 0]
  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值