numpy一些函数的使用记录

1、np.where()

np.where(condition)
当np.where仅仅含有condition没有x/y时,属于条件选择
例如:
一维数组

>>>a=np.array([1,2,3])
挑选数组中大于2元素
>>>b = np.where(a>2)
>>>b
(array([2], dtype=int64),)
结果显示为一个元组,因数组只有一位,因此元组仅有第一个元素,表示行index

二维数组

>>>a=np.array([[1,2,3],[4,5,6]])
# 筛选二维数组中大于3的元素
>>>index = np.where(a>3)
>>>index
(array([1, 1, 1], dtype=int64), array([0, 1, 2], dtype=int64))
从结果看,得到的是一个元组,分别表示满足筛选条件的数组的行列的index

np.where(condition,x,y)
当np.where既含有condition,又含有x,y的时候,可以理解为条件选择,为真的时候值为x,为假的时候为值为y

>>>a=np.array([[1,12,5,9],[17,10,8,6],[60,7,38,17]])
>>>a
array([[ 1, 12,  5,  9],
       [17, 10,  8,  6],
       [60,  7, 38, 17]])
>>>b = np.where(a>10,a,-10*a)
>>>b
array([[ -10,   12,  -50,  -90],
       [  17, -100,  -80,  -60],
       [  60,  -70,   38,   17]])

2、np.squeeze()

针对多维数组,去除冗余维度(特指维度为1的)
函数接口:
squeeze(a, axis=None):

>>>a = np.array([[1,2,3],[4,5,6]]).reshape(-1,2,3)
>>>a
array([[[1, 2, 3],
        [4, 5, 6]]])
>>>a.shape
(1, 2, 3)
>>>b = a.squeeze(0)
>>>b 
array([[1, 2, 3],
       [4, 5, 6]])
>>>b.shape
(2,3)
 

3、np.concatenate()

针对ndarray的数组,依照某一维度进行拼接,除去拼接的维度,其余维度均需相同
函数接口:
concatenate(arrays,axis=None, out=None, *, dtype=None, casting=None):

>>>a = np.array([[1,2,3],[4,5,6]]).reshape(-1,2,3)
>>>a
array([[[1, 2, 3],
        [4, 5, 6]]])
>>>a.shape
(1, 2, 3)
>>>b =a.squeeze(0)
>>>b.shape
(2, 3)
>>>c = np.concatenate([a,b],axis=0)
Traceback (most recent call last):
  File "D:\Users\Administrator\anaconda3\envs\base_env\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "<__array_function__ internals>", line 180, in concatenate
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 3 dimension(s) and the array at index 1 has 2 dimension(s)
>>>b = b[np.newaxis,...]
>>>b.shape
(1,2,3)
>>>c = np.concatenate([a,b],axis=0)
>>>c.shape
(2, 2, 3)

4、np.argmin()与np.argmax()

针对ndarray数组,按照某一特定维度求取最大最小值;
针对图像或者二维数组,因ndarray的shape一般为(h,w,c),因此可以简单记忆维度0对应列方向,维度1对应行方向。
函数接口:
argmin(a, axis=None, out=None, *, keepdims=np._NoValue):
argmax(a, axis=None, out=None, *, keepdims=np._NoValue):

>>>a =  np.array([[1,8,9],[4,5,6]])
>>>a
array([[1, 8, 9],
       [4, 5, 6]])
>>>np.argmin(a,0)
array([0, 1, 1], dtype=int64)
>>>np.argmin(a,1)
array([0, 0], dtype=int64)
>>>np.argmax(a,0)
array([1, 0, 0], dtype=int64)
>>>np.argmax(a,1)
array([2, 2], dtype=int64)

5、np.repeat()

针对ndarray数组,按照某一特定维度进行维度上元素的repeat
函数接口:
repeat(a, repeats, axis=None):

>>>a = np.array([[1,8,9],[4,5,6]])
>>>b = np.repeat(a,axis=0,repeats=2)
>>>b.shape
(4,3)
>>>b
array([[1, 8, 9],
       [1, 8, 9],
       [4, 5, 6],
       [4, 5, 6]])
>>>b = np.repeat(b,axis=1,repeats=2)
>>>b
array([[1, 1, 8, 8, 9, 9],
       [1, 1, 8, 8, 9, 9],
       [4, 4, 5, 5, 6, 6],
       [4, 4, 5, 5, 6, 6]])
>>>b.shape
(4,6)

6、np.mean()

针对某一特定维度求取平均值
函数接口:
mean(a,axis=None,dtype=None,out=None, keepdims=np._NoValue, *,where=np._NoValue):

>>>a=np.array([[1,12,5,9],[17,10,8,6]])
array([[ 1, 12,  5,  9],
       [17, 10,  8,  6]])
>>>np.mean(a,axis=0)
array([ 9. , 11. ,  6.5,  7.5])
>>>np.mean(a,axis=1)
array([ 6.75, 10.25])

7、np.median()

针对数组的某一特定维度求取中位数
函数接口:
median(a, axis=None, out=None,overwrite_input=False, keepdims=False)
参数说明:
a:输入数组;
axis:指定维度;
out:用于放置结果的数组
overwrite_input :bool型的参数,默认为Flase。如果为True那么将直接在数组内存中计算,计算之后原数组没办法保存,优点在于节省内存资源,Flase则相反;
keepdims:bool型的参数,默认为Flase

注意:
当选定维度上的数据个数为奇数时,求取的是按照从大到小排序的中位数
当选定维度上的数据个数为偶数时,求取的是按照从大到小排序的中间两个数的平均值

>>>a=np.array([[1,12,5,9],[17,10,8,6],[60,7,38,17]])
>>>a
array([[ 1, 12,  5,  9],
       [17, 10,  8,  6],
       [60,  7, 38, 17]])
>>>np.median(a,axis=0)
array([17., 10.,  8.,  9.]) 
>>>np.median(a,axis=1)
array([ 7. ,  9. , 27.5])

持续更新中

–END–

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI小花猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值