What does -1 mean in numpy reshape?

use*_*504 354 python numpy

A numpy matrix can be reshaped into a vector using reshape function with parameter -1. But I don't know what -1 means here.

For example:

a = numpy.matrix([[1, 2, 3, 4], [5, 6, 7, 8]])
b = numpy.reshape(a, -1)
Run Code Online (Sandbox Code Playgroud)

The result of b is: matrix([[1, 2, 3, 4, 5, 6, 7, 8]])

有谁知道-1在这里意味着什么?并且似乎python赋予-1几个含义,例如:array[-1]表示最后一个元素.你能解释一下吗?

Jul*_*med 472

满足提供新形状的标准是"新形状应与原始形状兼容"

numpy允许我们给出一个新的形状参数为-1(例如:(2,-1)或(-1,3)但不是(-1,-1)).它只是意味着它是一个未知的维度,我们想要numpy来弄清楚它.numpy将通过查看 "数组的长度和剩余维度"来确定它并确保它满足上述标准

现在看看这个例子.

z = np.array([[1, 2, 3, 4],
         [5, 6, 7, 8],
         [9, 10, 11, 12]])
z.shape
(3, 4)
Run Code Online (Sandbox Code Playgroud)

现在尝试重塑(-1).结果新形状为(12,)并与原始形状兼容(3,4)

z.reshape(-1)
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])
Run Code Online (Sandbox Code Playgroud)

现在尝试重塑(-1,1).我们已将列提供为1但行未知.所以我们得到结果新的形状为(12,1).与原始形状兼容(3,4)

z.reshape(-1,1)
array([[ 1],
   [ 2],
   [ 3],
   [ 4],
   [ 5],
   [ 6],
   [ 7],
   [ 8],
   [ 9],
   [10],
   [11],
   [12]])
Run Code Online (Sandbox Code Playgroud)

新形状为(-1,2).行未知,第2列.我们得到结果新形状为(6,2)

z.reshape(-1, 2)
array([[ 1,  2],
   [ 3,  4],
   [ 5,  6],
   [ 7,  8],
   [ 9, 10],
   [11, 12]])
Run Code Online (Sandbox Code Playgroud)

现在试图将列保持为未知.新形状为(1,-1).即,行为1,列未知.我们得到结果新形状为(1,12)

z.reshape(1,-1)
array([[ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12]])
Run Code Online (Sandbox Code Playgroud)

新形状(2,-1).第2行,列未知.我们得到结果新形状为(2,6)

z.reshape(2, -1)
array([[ 1,  2,  3,  4,  5,  6],
   [ 7,  8,  9, 10, 11, 12]])
Run Code Online (Sandbox Code Playgroud)

新形状为(3,-1).第3行,列未知.我们得到结果新形状为(3,4)

z.reshape(3, -1)
array([[ 1,  2,  3,  4],
   [ 5,  6,  7,  8],
   [ 9, 10, 11, 12]])
Run Code Online (Sandbox Code Playgroud)

最后,如果我们尝试将两个维度都提供为未知,即新形状为(-1,-1).它会抛出一个错误

z.reshape(-1, -1)
ValueError: can only specify one unknown dimension
Run Code Online (Sandbox Code Playgroud)

  • 这个答案比接受的答案更清楚-1对数组实际做了什么,并显示了例子. (24认同)
  • 这个解释应该是公认的答案. (5认同)
  • 这个答案包含很多例子,但没有列出-1用简单的英语做什么.重塑数组时,新形状必须包含与旧形状相同数量的元素,这意味着两个形状的尺寸的乘积必须相等.当使用-1时,对应于-1的维度将是原始数组的维度除以给予"reshape"的维度的乘积,以便保持相同数量的元素. (5认同)
  • 在我看来,接受的答案和这个答案都有帮助,而接受的答案更简单,我更喜欢更简单的答案 (2认同)
  • 形状 (12, 1) 如何与形状 (3,4) “兼容”? (2认同)
  • @Vijender我猜这意味着元素数量相同但轴不同 - 即 12x1 == 3x4? (2认同)

Anu*_*pta 72

用于重塑数组.

假设我们有一个尺寸为2 x 10 x 10的三维数组:

r = numpy.random.rand(2, 10, 10) 
Run Code Online (Sandbox Code Playgroud)

现在我们要重塑为5 X 5 x 8:

numpy.reshape(r, shape=(5, 5, 8)) 
Run Code Online (Sandbox Code Playgroud)

会做的.

请注意,一旦修复第一个dim = 5和第二个dim = 5,您就不需要确定第三个维度.为了帮助你的懒惰,python提供了-1的选项:

numpy.reshape(r, shape=(5, 5, -1)) 
Run Code Online (Sandbox Code Playgroud)

会给你一个shape =(5,5,8)的数组.

同样,

numpy.reshape(r, shape=(50, -1)) 
Run Code Online (Sandbox Code Playgroud)

会给你一个shape =(50,4)的数组

您可以在http://anie.me/numpy-reshape-transpose-theano-dimshuffle/了解更多信息.


fal*_*tru 57

根据the documentation:

newshape:int或int of int

新形状应与原始形状兼容.如果是整数,则结果将是该长度的1-D数组.一个形状尺寸可以是-1.在这种情况下,该值是从数组长度和剩余维度推断出来的.

  • -1让numpy为您确定结果矩阵中未知的列数或行数.注意:未知应该是列或行,而不是两者. (3认同)

小智 14

numpy.reshape(a,newshape,order {})查看以下链接以获取更多信息. https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html

对于下面的示例,您提到输出将结果向量解释为单行.( - 1)表示行数为1.如果

a = numpy.matrix([[1, 2, 3, 4], [5, 6, 7, 8]])
b = numpy.reshape(a, -1)
Run Code Online (Sandbox Code Playgroud)

输出:

矩阵([[1,2,3,4,5,6,7,8]])

这可以通过另一个例子更精确地解释:

b = np.arange(10).reshape((-1,1))
Run Code Online (Sandbox Code Playgroud)

输出:(是一维柱状数组)

阵列([[0],

   [1],
   [2],
   [3],
   [4],
   [5],
   [6],
   [7],
   [8],
   [9]])
Run Code Online (Sandbox Code Playgroud)

b = np.arange(10).reshape((1,-1))

输出:(是一维行数组)

数组([[0,1,2,3,4,5,6,7,8,9]])


lon*_*olf 14

这只是意味着您不确定可以提供多少行或列数,并且您要求 numpy 建议一些列或行数以进行重塑。

numpy 为 -1 https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html提供了最后一个例子

检查以下代码及其输出以更好地了解(-1):

代码:-

import numpy
a = numpy.matrix([[1, 2, 3, 4], [5, 6, 7, 8]])
print("Without reshaping  -> ")
print(a)
b = numpy.reshape(a, -1)
print("HERE We don't know about what number we should give to row/col")
print("Reshaping as (a,-1)")
print(b)
c = numpy.reshape(a, (-1,2))
print("HERE We just know about number of columns")
print("Reshaping as (a,(-1,2))")
print(c)
d = numpy.reshape(a, (2,-1))
print("HERE We just know about number of rows")
print("Reshaping as (a,(2,-1))")
print(d)
Run Code Online (Sandbox Code Playgroud)

输出:-

Without reshaping  -> 
[[1 2 3 4]
 [5 6 7 8]]
HERE We don`t know about what number we should give to row/col
Reshaping as (a,-1)
[[1 2 3 4 5 6 7 8]]
HERE We just know about number of columns
Reshaping as (a,(-1,2))
[[1 2]
 [3 4]
 [5 6]
 [7 8]]
HERE We just know about number of rows
Reshaping as (a,(2,-1))
[[1 2 3 4]
 [5 6 7 8]]
Run Code Online (Sandbox Code Playgroud)


小智 14

转换的最终结果是最终数组中的元素数与初始数组或数据帧中的元素数相同。

-1 对应于行或列的未知计数。我们可以将其视为x(未知)。x是通过将原始数组中的元素数除以带有 -1 的有序对的另一个值而获得的。

例子:

12 个元素reshape(-1,1)对应于一个x=12/1=12 行和 1 列的数组。


12 个元素reshape(1,-1)对应于 1 行x=12/1=12 列的数组。


小智 12

这很容易理解."-1"代表"未知维度",可以从另一个维度进行推测.在这种情况下,如果您将矩阵设置为:

a = numpy.matrix([[1, 2, 3, 4], [5, 6, 7, 8]])
Run Code Online (Sandbox Code Playgroud)

像这样修改你的矩阵:

b = numpy.reshape(a, -1)
Run Code Online (Sandbox Code Playgroud)

它会将一些失败的操作称为矩阵a,它将返回1-d numpy array/martrix.

但是,我不认为使用这样的代码是个好主意.为什么不尝试:

b = a.reshape(1,-1)
Run Code Online (Sandbox Code Playgroud)

它会给你相同的结果,让读者更清楚地理解:将b设置为另一种形状.对于a,我们不应该有多少列(将其设置为-1!),但我们需要一维数组(将第一个参数设置为1!).


She*_*zod 10

import numpy as np
x = np.array([[2,3,4], [5,6,7]]) 

# Convert any shape to 1D shape
x = np.reshape(x, (-1)) # Making it 1 row -> (6,)

# When you don't care about rows and just want to fix number of columns
x = np.reshape(x, (-1, 1)) # Making it 1 column -> (6, 1)
x = np.reshape(x, (-1, 2)) # Making it 2 column -> (3, 2)
x = np.reshape(x, (-1, 3)) # Making it 3 column -> (2, 3)

# When you don't care about columns and just want to fix number of rows
x = np.reshape(x, (1, -1)) # Making it 1 row -> (1, 6)
x = np.reshape(x, (2, -1)) # Making it 2 row -> (2, 3)
x = np.reshape(x, (3, -1)) # Making it 3 row -> (3, 2)
Run Code Online (Sandbox Code Playgroud)


Sha*_*ani 8

长话短说:你设置了一些尺寸,让NumPy设置其余的尺寸.

(userDim1, userDim2, ..., -1) -->>

(userDim1, userDim1, ..., TOTAL_DIMENSION - (userDim1 + userDim2 + ...))
Run Code Online (Sandbox Code Playgroud)