【python数据分析】Numpy 的 6 种高效函数

本文介绍了Numpy库中六个提升数据分析效率的函数:argpartition用于快速找到数组中最大的几个元素;allclose检查两个数组在给定公差内的相似度;clip限制数组值在指定范围内;extract根据条件提取数组元素;where返回满足条件的数组元素索引或替换值;percentile计算数组元素的百分位数。这些函数能有效优化数据分析过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们都知道,NumpyPython 环境下的扩展程序库,支持大量的维度数组和矩阵运算;在日常的数据分析中都发挥着重要作用,如果没有 Numpy 的支持,数据分析将变得异常困难。但有时我们需要加快数据分析的速度,有什么办法可以帮助到我们吗?

在本文中,我为大家介绍6 种 Numpy 函数。这些高效的函数会令数据分析更为容易、便捷。


Numpy 的 6 种高效函数

Numpy 是用于科学计算的 Python 语言扩展包,通常包含强大的 N 维数组对象、复杂函数、用于整合 C/C++和 Fortran 代码的工具以及有用的线性代数、傅里叶变换和随机数生成能力。

除了上面这些明显的用途,Numpy 还可以用作通用数据的高效多维容器(container),定义任何数据类型。这使得 Numpy 能够实现自身与各种数据库的无缝、快速集成。

接下来一一解析 6 种 Numpy 函数。

argpartition()

借助于 argpartition()Numpy 可以找出 N 个最大数值的索引,也会将找到的这些索引输出。然后我们根据需要对数值进行排序。

x = np.array(\[12, 10, 12, 0, 6, 8, 9, 1, 16, 4, 6, 0\])  
index\_val = np.argpartition(x, -4)\[-4:\]  
index\_val  

output

array(\[1, 8, 2, 0\], dtype=int64)np.sort(x\[index\_val\])  
array(\[10, 12, 12, 16\])  

allclose()

allclose() 用于匹配两个数组,并得到布尔值表示的输出。如果在一个公差范围内(within a tolerance)两个数组不等同,则 allclose() 返回 False。该函数对于检查两个数组是否相似非常有用。

array1 = np.array(\[0.12,0.17,0.24,0.29\])  
array2 = np.array(\[0.13,0.19,0.26,0.31\])# with a tolerance of 0.1, it should return False:  
np.allclose(array1,array2,0.1)  

output

False  

又例如

np.allclose(array1,array2,0.2)  

output

False  

Clip()

Clip() 使得一个数组中的数值保持在一个区间内。有时,我们需要保证数值在上下限范围内。为此,我们可以借助 Numpyclip() 函数实现该目的。给定一个区间,则区间外的数值被剪切至区间上下限(interval edge)

x = np.array(\[3, 17, 14, 23, 2, 2, 6, 8, 1, 2, 16, 0\])  
np.clip(x,2,5)  

output

array(\[3, 5, 5, 5, 2, 2, 5, 5, 2, 2, 5, 2\])  

extract()

顾名思义,extract() 是在特定条件下从一个数组中提取特定元素。借助于 extract(),我们还可以使用 andor 等条件。

# Random integers  
array = np.random.randint(20, size=12)  
array  

output

array(\[ 0,  1,  8, 19, 16, 18, 10, 11,  2, 13, 14,  3\])#  Divide by 2 and check if remainder is 1  

又例如

cond = np.mod(array, 2)==1  
cond  

output

array(\[False,  True, False,  True, False, False, False,  True, False, True, False,  True\])# Use extract to get the values  

又例如

np.extract(cond, array)  

output

array(\[ 1, 19, 11, 13,  3\])# Apply condition on extract directly  

又例如

np.extract(((array < 3) | (array > 15)), array)  

output

array(\[ 0,  1, 19, 16, 18,  2\])  

where()

Where() 用于从一个数组中返回满足特定条件的元素。比如,它会返回满足特定条件的数值的索引位置。Where()SQL 中使用的 where condition 类似,如以下示例所示:

y = np.array(\[1,5,6,8,1,7,3,6,9\])# Where y is greater than 5, returns index position  
np.where(y>5)  

output

array(\[2, 3, 5, 7, 8\], dtype=int64),)# First will replace the values that match the condition,   

又例如

# second will replace the values that does not  
np.where(y>5, "Hit", "Miss")  

output

array(\['Miss', 'Miss', 'Hit', 'Hit', 'Miss', 'Hit', 'Miss', 'Hit', 'Hit'\],dtype='<U4')  

percentile()

Percentile() 用于计算特定轴方向上数组元素的第 n 个百分位数。

a = np.array(\[1,5,6,8,1,7,3,6,9\])  
print("50th Percentile of a, axis = 0 : ",  np.percentile(a, 50, axis =0))  

output

50th Percentile of a, axis = 0 :  6.0  

又例如

b = np.array(\[\[10, 7, 4\], \[3, 2, 1\]\])  
print("30th Percentile of b, axis = 0 : ",  np.percentile(b, 30, axis =0))  

output

30th Percentile of b, axis = 0 :  \[5.1 3.5 1.9\]  

这就是 Numpy 扩展包的 6 种高效函数,相信会为你带来帮助。

😝朋友们如果有需要的话,可以V扫描下方二维码免费领取🆓

学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!

#### **一、Python学习路线**

image-20230619144606466

python学习路线图1

二、Python基础学习
1. 开发工具

2. 学习笔记

在这里插入图片描述

3. 学习视频

在这里插入图片描述

三、Python小白必备手册

图片

四、数据分析全套资源

在这里插入图片描述

五、Python面试集锦
1. 面试资料

在这里插入图片描述

在这里插入图片描述

2. 简历模板

在这里插入图片描述

** 因篇幅有限,仅展示部分资料,添加上方即可获取**
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值