numpy函数总结

2021年11月23日 阅读数:3
这篇文章主要向大家介绍numpy函数总结,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

1.stackspa

两个以上(n)相同array进行堆叠,n为axis的维度。code

做用:用于Faster RCNN不一样ratio的anchor生成。blog

a = np.array([[0, 0, 0], [4, 4, 4], [8, 8, 8]])
b = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]])
c = np.stack([a, b], axis=1)
print(c, c.shape)

输出:
[[[0 0 0]
  [1 1 1]]

 [[4 4 4]
  [2 2 2]]

 [[8 8 8]
  [3 3 3]]] (3, 2, 3)

 

2. meshgridio

a = np.array([1, 2, 3])
b = np.array([[4, 5, 6], [7, 8, 9]])
c, d = np.meshgrid(a, b)
print(c, c.shape)
print(d, d.shape)

输出:
[[1 2 3]
 [1 2 3]
 [1 2 3]
 [1 2 3]
 [1 2 3]
 [1 2 3]] (6, 3)
[[4 4 4]
 [5 5 5]
 [6 6 6]
 [7 7 7]
 [8 8 8]
 [9 9 9]] (6, 3)

 

3. flattenast

a = np.array([[1, 2, 3], [4, 5, 6]])
b = a.flatten()
print(b)

输出:
[1 2 3 4 5 6]

 

4. broadcast_toclass

将array复制指定的倍数。grid

a = np.array([1, 2, 3])
b = np.broadcast_to(a, (3, 3))
print(b)

输出:
[[1 2 3]
 [1 2 3]
 [1 2 3]]