Python | Numpy | np.newaxis有什么作用?[np.newaxis,:][:,np.newaxis][np.newaxis,:,np.newaxis]
Posted 人工智能博士
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python | Numpy | np.newaxis有什么作用?[np.newaxis,:][:,np.newaxis][np.newaxis,:,np.newaxis]相关的知识,希望对你有一定的参考价值。
理解一下:
np.newaxis是用来增加一个维度,比如原来是1维的,我可以用它变成2维,甚至3维的!
import numpy as np
x=np.array([1,2,3,4,5,6,7,8,9,10])
print(x)
print(x.shape)
[ 1 2 3 4 5 6 7 8 9 10] (10,)
import numpy as np
x=np.array([1,2,3,4,5,6,7,8,9,10])
y = x[np.newaxis,:]
print(y)
print(y.shape)
[[ 1 2 3 4 5 6 7 8 9 10]] (1, 10)
import numpy as np
x=np.array([1,2,3,4,5,6,7,8,9,10])
y = x[:,np.newaxis]
print(y)
print(y.shape)
[[ 1] [ 2] [ 3] [ 4] [ 5] [ 6] [ 7] [ 8] [ 9] [10]] (10, 1)
import numpy as np
x=np.array([1,2,3,4,5,6,7,8,9,10])
y = x[np.newaxis,:,np.newaxis]
print(y)
print(y.shape)
[[[ 1] [ 2] [ 3] [ 4] [ 5] [ 6] [ 7] [ 8] [ 9] [10]]] (1, 10, 1)
以上是关于Python | Numpy | np.newaxis有什么作用?[np.newaxis,:][:,np.newaxis][np.newaxis,:,np.newaxis]的主要内容,如果未能解决你的问题,请参考以下文章