What does -1 mean in numpy reshape?
Posted xyq_idata
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了What does -1 mean in numpy reshape?相关的知识,希望对你有一定的参考价值。
The criterion to satisfy for providing the new shape is that ‘The new shape should be compatible with the original shape‘
numpy allow us to give one of new shape parameter as -1 (eg: (2,-1) or (-1,3) but not (-1, -1)). It simply means that it is an unknown dimension and we want numpy to figure it out. And numpy will figure this by looking at the ‘length of the array and remaining dimensions‘ and making sure it satisfies the above mentioned criteria
Now see the example.
z = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]])
z.shape
(3, 4)
Now trying to reshape with (-1) . Result new shape is (12,) and is compatible with original shape (3,4)
z.reshape(-1)
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
Now trying to reshape with (-1, 1) . We have provided column as 1 but rows as unknown . So we get result new shape as (12, 1).again compatible with original shape(3,4)
z.reshape(-1,1)
array([[ 1],
[ 2],
[ 3],
[ 4],
[ 5],
[ 6],
[ 7],
[ 8],
[ 9],
[10],
[11],
[12]])
New shape as (-1, 2). row unknown, column 2. we get result new shape as (6, 2)
z.reshape(-1, 2)
array([[ 1, 2],
[ 3, 4],
[ 5, 6],
[ 7, 8],
[ 9, 10],
[11, 12]])
以上是关于What does -1 mean in numpy reshape?的主要内容,如果未能解决你的问题,请参考以下文章
What does axis/axes mean in Numpy
What does a (+) sign mean in an Oracle SQL WHERE clause?
Helix, what does it mean for Sitecore CMS development?
(译)What does explicit keyword mean?
空间复杂度是什么?What does ‘Space Complexity’ mean? ------geeksforgeeks 翻译