当前位置:网站首页>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:]) # 输出后两列

最后再对比一下数据清洗前清洗后,虽然本次数据量很小,但是也有非常多种的清洗方式,熟悉了这些方式,以后对于更加庞大的数据也是手到擒来,让自己的数据干净又卫生。
边栏推荐
- Runnable是否可以中断
- Briefly describe the working principle of kept
- Super simple and fully automated generation super signature system (cloud Xiaoduo minclouds.com cloud service instance), free application in-house test app distribution and hosting platform, maintenan
- Getting started with webgl (4)
- [Data Mining] Visual Pattern Mining: Hog Feature + cosinus Similarity / K - means Clustering
- Getting started with webgl (3)
- Unity之ASE实现卡通火焰
- STM32F103C8T6 PWM驱动舵机(SG90)
- Cocos creator collision and collision callback do not take effect
- 【數字IC驗證快速入門】26、SystemVerilog項目實踐之AHB-SRAMC(6)(APB協議基本要點)
猜你喜欢

Starting from 1.5, build a microservice framework link tracking traceid

2022全开源企业发卡网修复短网址等BUG_2022企业级多商户发卡平台源码

Vertex shader to slice shader procedure, varying variable
![[quick start of Digital IC Verification] 24. AHB sramc of SystemVerilog project practice (4) (AHB continues to deepen)](/img/cf/45775b712f60869186a25d3657ee1b.png)
[quick start of Digital IC Verification] 24. AHB sramc of SystemVerilog project practice (4) (AHB continues to deepen)

20th anniversary of agile: a failed uprising

【数字IC验证快速入门】20、SystemVerilog学习之基本语法7(覆盖率驱动...内含实践练习)
使用Scrapy框架爬取网页并保存到Mysql的实现

OpenGL's distinction and understanding of VAO, VBO and EBO

How to create Apple Developer personal account P8 certificate

webgl_ Enter the three-dimensional world (1)
随机推荐
Actually changed from 408 to self proposition! 211 North China Electric Power University (Beijing)
Unity's ASE achieves full screen sand blowing effect
TypeScript 发布 4.8 beta 版本
Cocos uses custom material to display problems
How to release NFT in batches in opensea (rinkeby test network)
What is Base64?
Create lib Library in keil and use lib Library
Wechat applet 01
大表delete删数据导致数据库异常解决
[data mining] visual pattern mining: hog feature + cosine similarity /k-means clustering
Detailed explanation of Cocos creator 2.4.0 rendering process
【兰州大学】考研初试复试资料分享
How to understand that binary complement represents negative numbers
The download button and debug button in keil are grayed out
Use cpolar to build a business website (2)
After UE4 is packaged, mesh has no material problem
摘抄的只言片语
postman生成时间戳,未来时间戳
Async and await
【数字IC验证快速入门】22、SystemVerilog项目实践之AHB-SRAMC(2)(AMBA总线介绍)