python swapaxes函数怎么用

版权声明:belongs to tony2278 https://blog.csdn.net/tony2278/article/details/89216705
#就是轴的互换,如下例,b是2*4的ndarray,把0轴(2)和一轴(4)换了,就成了4*2的ndarray
b = array([[0, 1, 2, 3],
       [4, 5, 6, 7]])
np.swapaxes(b,0,1)
b
>>>array([[0, 4],
       [1, 5],
       [2, 6],
       [3, 7]])

From: https://zhidao.baidu.com/question/308918610789546324.html

Numpy实现增加数组维度

方法一:用None作为索引

a = np.array([1,2,3]) # a.shape = (3,)
a = a[None,:] # a.shape =(1,3)

方法二:用np.newaxis

a = np.array([1,2,3]) # a.shape = (3,)
a = a[np.newaxis,:] # a.shape =(1,3)

From:【Python】Numpy实现增加数组维度

eg

import numpy as np

a = np.array([1,2,3,4,5]) # a.shape = (3,)
print(a)
a = a[np.newaxis,:] # a.shape =(1,3)
print(a)
a = a[np.newaxis,:] # a.shape =(1,3)
print(a)
[1 2 3 4 5]
[[1 2 3 4 5]]
[[[1 2 3 4 5]]]

猜你喜欢

转载自blog.csdn.net/tony2278/article/details/89216705