Dali工具箱🕶5——the use of numpy

"np.where, np.argmax, np.argmin, np.eye, np.seed, np.vstack, np.hstack, torch.sum, squeeze "

Posted by fuhao7i on April 17, 2021

1. Boolean indexes

1
2
3
4
5
6
7
8
9
10
11
12
13
import numpy as np

a = np.arange(12).reshape(3,4)

print('-' * 20)
print(a)
print('-' * 20)
# 数组a中所有大于5的位置为True, 其余为False
print(a>5)
print('-' * 20)
# 将数组a中所有大于5的位置赋值为999
a[a>5]=999
print(a)
--------------------
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
-------a > 5-------
[[False False False False]
 [False False  True  True]
 [ True  True  True  True]]
-------a[a>5]=999-------
[[  0   1   2   3]
 [  4   5 999 999]
 [999 999 999 999]]

2. 三元运算符

np.where

1
2
3
4
5
6
7
8
9
import numpy as np

a = np.arange(12).reshape(3,4)

print('-' * 20)
print(a)
print('-' * 20)
# a中大于5的位置为999, 其余为111
np.where(a>5, 999, 111)
--------------------
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
-------np.where-------
array([[111, 111, 111, 111],
       [111, 111, 999, 999],
       [999, 999, 999, 999]])

3. clip

1
2
3
4
5
6
7
8
9
import numpy as np

a = np.arange(12).reshape(3,4)

print('-' * 20)
print(a)
print('-' * 20)
# a小于4的全都赋值为4,大于8的全都赋值为8
a.clip(4, 8)
--------------------
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
-------clip-------
array([[4, 4, 4, 4],
       [4, 5, 6, 7],
       [8, 8, 8, 8]])

4. stack

np.vstack() 列拼接 and np.hstack() 行拼接

1
2
3
4
5
6
7
8
9
10
11
12
import numpy as np

t1 = np.arange(12).reshape(3,4)
t2 = np.arange(12).reshape(3,4)

print('-' * 20)
print(t1)
print('-' * 20)
print(t2)
print('-' * 20)
np.vstack((t1,t2))
np.hstack((t1,t2))
--------------------
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
--------------------
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
-------列拼接-------
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
-------行拼接-------
array([[ 0,  1,  2,  3,  0,  1,  2,  3],
       [ 4,  5,  6,  7,  4,  5,  6,  7],
       [ 8,  9, 10, 11,  8,  9, 10, 11]])

5. 行列交换

1
2
3
4
5
6
7
8
9
10
11
12
import numpy as np

t1 = np.arange(12).reshape(3,4)

print('-' * 20)
print(t1)
print('-' * 20)
t1[[1,2],:] = t1[[2,1],:] # 行交换
print(t1)
print('-' * 20)
t1[:,[0,2]] = t1[:,[2,0]] # 交换
print(t1)
-------原始t1-------
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
-------行交换-------
[[ 0  1  2  3]
 [ 8  9 10 11]
 [ 4  5  6  7]]
-------列交换-------
[[ 2  1  0  3]
 [10  9  8 11]
 [ 6  5  4  7]]

6. squeeze and unsqueeze

squeeze作用: 从数组的形状中删除单维度条目,即把shape中为1的维度去掉

1
2
3
4
5
import numpy as np

a  = np.arange(10).reshape(1,10)
print(a)
print(a.shape)
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])

(1, 10)
1
2
3
b = np.squeeze(a)
print(b)
print(b.shape)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

(10,)

对维度不为1的维度没有用

unsqueeze作用: 增加一个维度

例如: a 的维度为(3, 4), a.unsqueeze(1) 就是在第二维增加一个维度使其变为(3, 1, 4)

7. numpy的copy和view

a = b 完全不复制, a和b相互影响

1
2
3
4
5
6
7
8
9
10
11
12
13
import numpy as np

a = np.arange(12).reshape(3,4)

print('-' * 20)
print(a)
print('-' * 20)
b = a
b[b>5] = 999
print(a)
print('-' * 20)
a[a<2] = 111
print(b)
-------初始的a-------
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
-------改变b看a的值-------
[[  0   1   2   3]
 [  4   5 999 999]
 [999 999 999 999]]
-------改变a看b的值-------
[[111 111   2   3]
 [  4   5 999 999]
 [999 999 999 999]]

a=b[:], 视图的操作,一种切片,会创建新的对象a, 但是a的数据完全由b保管, 他们两个的数据变化是一致的,和上个操作a=b一样,都是相互影响

a = b.copy()复制,a和b互不影响

8. torch.sum

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import torch

a = torch.tensor([[[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]], [[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]]])
b = torch.tensor([[[[1,0,0],[0,1,0]],[[0,0,1],[0,1,0]]], [[[1,0,0],[0,1,0]],[[0,0,1],[0,1,0]]]])
print(a)
print('-'*20)
print(a.shape)
print('-'*20)
print(torch.sum(a, 0))
print('-'*20)
print(torch.sum(a, 1))
print('-'*20)
print(torch.sum(a, [0,1]))
print('='*20)
print(a*b)
print('='*30)
print(torch.sum(a*b, 0))
print(torch.sum(a*b, [0,1]))
tensor([[[[ 1,  2,  3],
          [ 4,  5,  6]],

         [[ 7,  8,  9],
          [10, 11, 12]]],


        [[[ 1,  2,  3],
          [ 4,  5,  6]],

         [[ 7,  8,  9],
          [10, 11, 12]]]])
--------------------
torch.Size([2, 2, 2, 3])
--------------------
tensor([[[ 2,  4,  6],
         [ 8, 10, 12]],

        [[14, 16, 18],
         [20, 22, 24]]])
--------------------
tensor([[[ 8, 10, 12],
         [14, 16, 18]],

        [[ 8, 10, 12],
         [14, 16, 18]]])
--------------------
tensor([[16, 20, 24],
        [28, 32, 36]])
====================
tensor([[[[ 1,  0,  0],
          [ 0,  5,  0]],

         [[ 0,  0,  9],
          [ 0, 11,  0]]],


        [[[ 1,  0,  0],
          [ 0,  5,  0]],

         [[ 0,  0,  9],
          [ 0, 11,  0]]]])
==============================
tensor([[[ 2,  0,  0],
         [ 0, 10,  0]],

        [[ 0,  0, 18],
         [ 0, 22,  0]]])
tensor([[ 2,  0, 18],
        [ 0, 32,  0]])

9. sundry

  • np.argmax(t, axis=0) 获取数组t第0维最大值的位置
  • np.argmin(t, axis=0) 获取数组t第0维最小值的位置
  • np.zeros((3,4)) 创建全0数组
  • np.ones((3,4)) 创建全1数组
  • np.eye(3) 创建一个对角线全为1,其余全为0的正方形矩阵
  • np.seed(s) 随机数种子,s是给定的种子值。因为计算机生成的是伪随机数,所以通过设定相同的随机数种子,可以每次生成相同的随机数