numpy reshape -1

>>> import numpy as np
>>> x = np.arange(12)
>>> x
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
>>> x.reshape(2,3,2,1)
array([[[[ 0],
         [ 1]],

        [[ 2],
         [ 3]],

        [[ 4],
         [ 5]]],


       [[[ 6],
         [ 7]],

        [[ 8],
         [ 9]],

        [[10],
         [11]]]])
>>> x.reshape(2,2,1,3)
array([[[[ 0,  1,  2]],

        [[ 3,  4,  5]]],


       [[[ 6,  7,  8]],

        [[ 9, 10, 11]]]])
>>> x.reshape(1,3,2,2)
array([[[[ 0,  1],
         [ 2,  3]],

        [[ 4,  5],
         [ 6,  7]],

        [[ 8,  9],
         [10, 11]]]])
>>>
>>> x
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
>>> y= x.reshape(1,3,2,2)
>>> y
array([[[[ 0, 1],
[ 2, 3]],

[[ 4, 5],
[ 6, 7]],

[[ 8, 9],
[10, 11]]]])
>>> y.shape
(1, 3, 2, 2)
>>> y.shape[0]
1
>>> y.reshape(y.shape[0],-1)
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]])
>>> y
array([[[[ 0, 1],
[ 2, 3]],

[[ 4, 5],
[ 6, 7]],

[[ 8, 9],
[10, 11]]]])
>>> y.reshape(1,-1)
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]])
>>> y.reshape(2,-1)
array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11]])
>>> y.reshape(3,-1)
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> y.reshape(4,-1)
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
>>> y.reshape(10,-1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: cannot reshape array of size 12 into shape (10,newaxis)
>>> y.reshape(5,-1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: cannot reshape array of size 12 into shape (5,newaxis)
>>> y.shape
(1, 3, 2, 2)
>>>

  

  

原文地址:https://www.cnblogs.com/morganh/p/8616612.html