当前位置:网站首页>numpy--数据清洗
numpy--数据清洗
2022-07-07 13:26:00 【madkeyboard】
数据清洗
脏数据
通常我们获得的数据不可能都是百分百无错误的,一般都会存在一些问题,比如数据值缺失、数据值异常大或小、格式错误和非独立数据错误。
通过下面代码生动生成一组脏数据,如图所示在最后一行输出了一个dtype = object。(这个dtype就是numpy数据的类型,这里要注意,如果dtype = object说明list直接转换过来的数据是无法直接参与计算的,只有int和float这样的数据类型才能参与计算)
raw_data = [
["Name", "StudentID", "Age", "AttendClass", "Score"],
["小明", 20131, 10, 1, 67],
["小花", 20132, 11, 1, 88],
["小菜", 20133, None, 1, "98"],
["小七", 20134, 8, 1, 110],
["花菜", 20134, 98, 0, None],
["刘欣", 20136, 12, 0, 12]
]
data = np.array(raw_data)
print(data)

数据预处理
pre_data = []
for i in range(len(raw_data)):
if i == 0: # 去掉第一行字符串
continue
pre_data.append(raw_data[i][1:]) # 去掉第一列名字
data = np.array(pre_data,dtype=np.float) # 这里之所以用float是因为数据中含有None,只有float才能转换None
print(data)

清洗数据
把不合逻辑的数据都清洗掉,在之前输入的数据中,第一列有明显的学号重复,这就不合逻辑的数据。np.unique()让数据具有唯一性,并且在使用过程中还可以看到重复的数据到底重复了多少次。如下图所示,20134出现了两次,那么我们就可以清楚的知道20135没有录上,之后就可以对数据进行更正。
fcow = data[:,0] # 取第一列的所有学号
print(fcow)
unique, counts = np.unique(fcow,return_counts=True) # return_counts展示数据的重复次数
print("清洗后的数据:",unique)
print("数据重复的次数:",counts)

在看第二列数据,首先能直观的看到有数据缺失,那么对于这个缺失的数据,我们可以通过求已有数据的平均值来进行补充
is_nan = np.isnan(data[:,1]) # 找到第二列为none的数据
nan_idx = np.argwhere(is_nan)
print("下标:",nan_idx,"为none")
mean_age = data[~np.isnan(data[:,1]), 1].mean() # ~取反,isnan返回的是一个布尔值,这里选择的是布尔值为false(不为None),然后求平均
print("平均年龄:",mean_age)

看到这里就疑惑了,小学生的平均年龄有28?通过观察数据,有一个数据为98,明显就错了,这就是异常数据。所以我们需要删除这两个错误数据,然后用剩下数据的平均来代替它们。
normal_idx = ~np.isnan(data[:,1]) & (data[:,1] < 13) # 找到第二列为none的数据
print("(flase)为需要更改的数据:",normal_idx)
mean_age = data[normal_idx,1].mean() # ~取反,isnan返回的是一个布尔值,这里选择的是布尔值为false(不为None),然后求平均
print("平均年龄:",mean_age)
data[~normal_idx,1] = mean_age
print("清洗后的数据:",np.floor(data[:,1])) # 年龄没有小数,向下再取整

最后再看后两列的数据,这里0和1代表上课与否,当没有上课时不可能有成绩,最后一行存在问题。还有小学总分一般都是100,有超出100的存在说明是异常数据。
data = np.array(pre_data,dtype=np.float64) # 这里之所以用float是因为数据中含有None,只有float才能转换None
data[data[:,2] == 0,3] = np.nan # 没上课的通通没有成绩nan
data[:,3] = np.clip(data[:,3], 0, 100) # 把不再合理范围内的分数裁剪掉
print(data[:,2:]) # 输出后两列

最后再对比一下数据清洗前清洗后,虽然本次数据量很小,但是也有非常多种的清洗方式,熟悉了这些方式,以后对于更加庞大的数据也是手到擒来,让自己的数据干净又卫生。
边栏推荐
- Actually changed from 408 to self proposition! 211 North China Electric Power University (Beijing)
- 微信小程序 01
- Getting started with webgl (2)
- #HPDC智能基座人才发展峰会随笔
- HPDC smart base Talent Development Summit essay
- 20th anniversary of agile: a failed uprising
- MySQL bit type resolution
- Getting started with webgl (1)
- Keil5 does not support online simulation of STM32 F0 series
- How to release NFT in batches in opensea (rinkeby test network)
猜你喜欢

Typescript release 4.8 beta

Keil5 does not support online simulation of STM32 F0 series

Use cpolar to build a business website (2)

Actually changed from 408 to self proposition! 211 North China Electric Power University (Beijing)

Ue4/ue5 multi thread development attachment plug-in download address

The difference between full-time graduate students and part-time graduate students!

Syntax of generator function (state machine)

写一篇万字长文《CAS自旋锁》送杰伦的新专辑登顶热榜

【數據挖掘】視覺模式挖掘:Hog特征+餘弦相似度/k-means聚類

【数字IC验证快速入门】20、SystemVerilog学习之基本语法7(覆盖率驱动...内含实践练习)
随机推荐
Stm32f103c8t6 PWM drive steering gear (sg90)
Oracle控制文件丢失恢复归档模式方法
#HPDC智能基座人才发展峰会随笔
【數字IC驗證快速入門】20、SystemVerilog學習之基本語法7(覆蓋率驅動...內含實踐練習)
Excerpted words
【數據挖掘】視覺模式挖掘:Hog特征+餘弦相似度/k-means聚類
STM32F103C8T6 PWM驱动舵机(SG90)
[deep learning] semantic segmentation experiment: UNET network /msrc2 dataset
2022全开源企业发卡网修复短网址等BUG_2022企业级多商户发卡平台源码
How to build your own super signature system (yunxiaoduo)?
Virtual memory, physical memory /ram what
VS2005 strange breakpoint is invalid or member variable value cannot be viewed
Cut ffmpeg as needed, and use emscripten to compile and run
大表delete删数据导致数据库异常解决
[markdown grammar advanced] make your blog more exciting (IV: set font style and color comparison table)
Unity's ASE realizes cartoon flame
Use of SVN
Typescript release 4.8 beta
Write a ten thousand word long article "CAS spin lock" to send Jay's new album to the top of the hot list
【數字IC驗證快速入門】26、SystemVerilog項目實踐之AHB-SRAMC(6)(APB協議基本要點)