当前位置:网站首页>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:]) # 输出后两列
最后再对比一下数据清洗前清洗后,虽然本次数据量很小,但是也有非常多种的清洗方式,熟悉了这些方式,以后对于更加庞大的数据也是手到擒来,让自己的数据干净又卫生。
边栏推荐
- Excerpted words
- What is Base64?
- How to understand that binary complement represents negative numbers
- HPDC smart base Talent Development Summit essay
- 【数字IC验证快速入门】23、SystemVerilog项目实践之AHB-SRAMC(3)(AHB协议基本要点)
- 2. 堆排序『较难理解的排序』
- Oracle control file loss recovery archive mode method
- Pat grade a 1103 integer factorizatio
- Pit avoidance: description of null values in in and not in SQL
- Cocos uses custom material to display problems
猜你喜欢
15. Using the text editing tool VIM
Unity之ASE实现卡通火焰
2. 堆排序『较难理解的排序』
HW primary flow monitoring, what should we do
Actually changed from 408 to self proposition! 211 North China Electric Power University (Beijing)
2. Heap sort "hard to understand sort"
webgl_ Enter the three-dimensional world (1)
Asynchronous application of generator function
unnamed prototyped parameters not allowed when body is present
Virtual memory, physical memory /ram what
随机推荐
Implementation of crawling web pages and saving them to MySQL using the scrapy framework
A need to review all the knowledge, H5 form is blocked by the keyboard, event agent, event delegation
Virtual memory, physical memory /ram what
[quick start for Digital IC Validation] 26. Ahb - sramc (6) for system verilog project practice (Basic Points of APB Protocol)
webgl_ Enter the three-dimensional world (2)
Vertex shader to slice shader procedure, varying variable
【数字IC验证快速入门】22、SystemVerilog项目实践之AHB-SRAMC(2)(AMBA总线介绍)
【数据挖掘】视觉模式挖掘:Hog特征+余弦相似度/k-means聚类
STM32F103C8T6 PWM驱动舵机(SG90)
[quick start of Digital IC Verification] 25. AHB sramc of SystemVerilog project practice (5) (AHB key review, key points refining)
./ Functions of configure, make and make install
Pit avoidance: description of null values in in and not in SQL
After UE4 is packaged, mesh has no material problem
postman生成时间戳,未来时间戳
Actually changed from 408 to self proposition! 211 North China Electric Power University (Beijing)
[follow Jiangke University STM32] stm32f103c8t6_ PWM controlled DC motor_ code
The rebound problem of using Scrollview in cocos Creator
Runnable是否可以中断
The download button and debug button in keil are grayed out
[server data recovery] data recovery case of raid failure of a Dell server