Home > Blockchain >  numpy reshape cannot do (a,b,c) to (a,b,c,1)?
numpy reshape cannot do (a,b,c) to (a,b,c,1)?

Time:01-26

I reshape from the tensor from (a,b,c) to (a,b,c,1), but it turns out (a,1,b,c) using numpy. anybody knows why? or is there any way to get (a,b,c,1) from (a,b,c)?

CodePudding user response:

You can use

your_array = np.expand_dims(your_array, axis=3)

CodePudding user response:

You can simply add 1 to arr.shape tuple:

arr = np.arange(20).reshape(5,2,2)
arr.shape  = (1,)
print(arr.shape)

Output:

(5, 2, 2, 1)

CodePudding user response:

I would suggest to use np.newaxis like the example below:

tensor1 = np.array([[[1,2],[3,4],[5,6]],[[7,8],[9,10],[11,12]]])
print(tensor1.shape)

output: (2, 3, 2)

tensor2 = tensor1[..., np.newaxis]
print(tensor2.shape)

output: (2, 3, 2, 1)

  •  Tags:  
  • Related