问题
数据中包含较多0值,类似于包含较大噪声,对结果产生较大影响
目标
对数据进行清洗,在进行其他数据清洗操作的基础上,实现删除数据中包含较多0值的行
可类比推广到删除其他
代码实现
data = data[np.sum(data == 0, axis=1) < data.shape[-1] * 0.6, :]
- 首先,使用
np.sum(data == 0, axis=1)
统计每行数据中含0的数量 - 保留含0量少于所有数据60%的行,下面这行代码的输出为bool向量
np.sum(data == 0, axis=1) < data.shape[-1] * 0.6
示例
data=np.array([[0,0,0,0,0,0,0,0,0,0],[1,1,1,1,1,1,1,0,1,1],[2,2,2,0,0,0,0,2,2,2],[3,3,3,3,3,3,3,3,3,3],[4,0,4,0,4,0,4,0,4,0],[5,5,5,0,0,0,0,0,0,0],[6,6,0,0,0,0,0,0,6,6]])
print(data.shape)
print(np.sum(data == 0, axis=1) )
print(np.sum(data == 0, axis=1) < data.shape[-1] * 0.6)
data = data[np.sum(data == 0, axis=1) < data.shape[-1] * 0.6, :]
print(data)
print(data.shape)
输出结果
# 原始数据大小
(7, 10)
# 每行含0的数量
[10 1 4 0 5 7 6]
# 每行含0量是都小于设定值
[False True True True True False False]
# 清洗完的数据
[[1 1 1 1 1 1 1 0 1 1][2 2 2 0 0 0 0 2 2 2][3 3 3 3 3 3 3 3 3 3][4 0 4 0 4 0 4 0 4 0]]
# 清洗完的数据大小
(4, 10)