Python -1 在 numpy reshape 中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18691084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does -1 mean in numpy reshape?
提问by user2262504
A numpy matrix can be reshaped into a vector using reshape function with parameter -1. But I don't know what -1 means here.
可以使用参数为 -1 的 reshape 函数将 numpy 矩阵重新整形为向量。但我不知道 -1 在这里是什么意思。
For example:
例如:
a = numpy.matrix([[1, 2, 3, 4], [5, 6, 7, 8]])
b = numpy.reshape(a, -1)
The result of b
is: matrix([[1, 2, 3, 4, 5, 6, 7, 8]])
结果b
是:matrix([[1, 2, 3, 4, 5, 6, 7, 8]])
Does anyone know what -1 means here?
And it seems python assign -1 several meanings, such as: array[-1]
means the last element. Can you give an explanation?
有谁知道-1在这里是什么意思?似乎python分配了-1几个含义,例如:array[-1]
表示最后一个元素。你能给出解释吗?
回答by falsetru
According to the documentation
:
newshape : int or tuple of ints
The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.
newshape : int 或 int 元组
新形状应与原始形状兼容。如果是整数,则结果将是该长度的一维数组。一个形状维度可以是-1。在这种情况下,该值是从数组的长度和剩余维度推断出来的。
回答by Dinesh Kumar
numpy.reshape(a,newshape,order{}) check the below link for more info. https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html
numpy.reshape(a,newshape,order{}) 查看以下链接了解更多信息。 https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html
for the below example you mentioned the output explains the resultant vector to be a single row.(-1) indicates the number of rows to be 1. if the
对于您提到的以下示例,输出将结果向量解释为单行。(-1) 表示行数为 1。如果
a = numpy.matrix([[1, 2, 3, 4], [5, 6, 7, 8]])
b = numpy.reshape(a, -1)
output:
输出:
matrix([[1, 2, 3, 4, 5, 6, 7, 8]])
矩阵([[1, 2, 3, 4, 5, 6, 7, 8]])
this can be explained more precisely with another example:
这可以用另一个例子更准确地解释:
b = np.arange(10).reshape((-1,1))
output:(is a 1 dimensional columnar array)
输出:(是一维列数组)
array([[0],
数组([[0],
[1],
[2],
[3],
[4],
[5],
[6],
[7],
[8],
[9]])
b = np.arange(10).reshape((1,-1))
b = np.arange(10).reshape((1,-1))
output:(is a 1 dimensional row array)
输出:(是一维行数组)
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
数组([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
回答by F0rge1cE
It is fairly easy to understand. The "-1" stands for "unknown dimension" which can should be infered from another dimension. In this case, if you set your matrix like this:
这很容易理解。“-1”代表“未知维度”,可以从另一个维度推断出来。在这种情况下,如果您像这样设置矩阵:
a = numpy.matrix([[1, 2, 3, 4], [5, 6, 7, 8]])
Modify your matrix like this:
像这样修改你的矩阵:
b = numpy.reshape(a, -1)
It will call some deafult operations to the matrix a, which will return a 1-d numpy array/martrix.
它将对矩阵 a 调用一些默认操作,这将返回一个一维 numpy 数组/矩阵。
However, I don't think it is a good idea to use code like this. Why not try:
但是,我认为使用这样的代码不是一个好主意。为什么不试试:
b = a.reshape(1,-1)
It will give you the same result and it's more clear for readers to understand: Set b as another shape of a. For a, we don't how much columns it should have(set it to -1!), but we want a 1-dimension array(set the first parameter to 1!).
它会给你相同的结果,读者更容易理解:将 b 设置为 a 的另一种形状。对于a,我们不知道它应该有多少列(将其设置为-1!),但我们想要一个一维数组(将第一个参数设置为1!)。
回答by Julu Ahamed
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
numpy 允许我们将新的形状参数之一指定为 -1(例如:(2,-1) 或 (-1,3) 但不是 (-1, -1))。它只是意味着它是一个未知的维度,我们希望 numpy 找出它。numpy 将通过查看 “数组的长度和剩余维度”并确保它满足上述标准来计算这一点
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)
现在尝试用 (-1) 重塑。结果新形状是 (12,) 并且与原始形状 (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)
现在尝试用 (-1, 1) 重塑。我们提供 column 作为 1 但行作为 unknown 。所以我们得到结果新形状为 (12, 1). 再次与原始形状兼容 (3,4)
z.reshape(-1,1)
array([[ 1],
[ 2],
[ 3],
[ 4],
[ 5],
[ 6],
[ 7],
[ 8],
[ 9],
[10],
[11],
[12]])
The above is consistent with numpy
advice/error message, to use reshape(-1,1)
for a single feature; i.e. single column
以上与numpy
建议/错误消息一致,reshape(-1,1)
用于单个功能;即单列
Reshape your data using
array.reshape(-1, 1)
if your data has a single feature
array.reshape(-1, 1)
如果您的数据具有单一特征,则使用重塑您的数据
New shape as (-1, 2). row unknown, column 2. we get result new shape as (6, 2)
新形状为 (-1, 2)。行未知,第 2 列。我们得到结果新形状为 (6, 2)
z.reshape(-1, 2)
array([[ 1, 2],
[ 3, 4],
[ 5, 6],
[ 7, 8],
[ 9, 10],
[11, 12]])
Now trying to keep column as unknown. New shape as (1,-1). i.e, row is 1, column unknown. we get result new shape as (1, 12)
现在试图将列保持为未知。新形状为 (1,-1)。即,行为 1,列未知。我们得到结果新形状为 (1, 12)
z.reshape(1,-1)
array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]])
The above is consistent with numpy
advice/error message, to use reshape(1,-1)
for a single sample; i.e. single row
以上与numpy
建议/错误信息一致,reshape(1,-1)
用于单个样本;即单行
Reshape your data using
array.reshape(1, -1)
if it contains a single sample
使用
array.reshape(1, -1)
if 包含单个样本来重塑您的数据
New shape (2, -1). Row 2, column unknown. we get result new shape as (2,6)
新形状 (2, -1)。第 2 行,列未知。我们得到结果新形状为 (2,6)
z.reshape(2, -1)
array([[ 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12]])
New shape as (3, -1). Row 3, column unknown. we get result new shape as (3,4)
新形状为 (3, -1)。第 3 行,列未知。我们得到结果新形状为 (3,4)
z.reshape(3, -1)
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
And finally, if we try to provide both dimension as unknown i.e new shape as (-1,-1). It will throw an error
最后,如果我们尝试将两个维度都提供为未知,即新形状为 (-1,-1)。它会抛出错误
z.reshape(-1, -1)
ValueError: can only specify one unknown dimension
回答by Anuj Gupta
Used to reshape an array.
用于重塑数组。
Say we have a 3 dimensional array of dimensions 2 x 10 x 10:
假设我们有一个尺寸为 2 x 10 x 10 的 3 维数组:
r = numpy.random.rand(2, 10, 10)
Now we want to reshape to 5 X 5 x 8:
现在我们要重塑为 5 X 5 x 8:
numpy.reshape(r, shape=(5, 5, 8))
will do the job.
会做的工作。
Note that, once you fix first dim = 5 and second dim = 5, you don't need to determine third dimension. To assist your laziness, python gives the option of -1:
请注意,一旦您修复了第一个 dim = 5 和第二个 dim = 5,您就不需要确定第三维。为了帮助你的懒惰,python 提供了 -1 的选项:
numpy.reshape(r, shape=(5, 5, -1))
will give you an array of shape = (5, 5, 8).
会给你一个形状数组 = (5, 5, 8)。
Likewise,
同样地,
numpy.reshape(r, shape=(50, -1))
will give you an array of shape = (50, 4)
会给你一个形状数组 = (50, 4)
You can read more at http://anie.me/numpy-reshape-transpose-theano-dimshuffle/
您可以在http://anie.me/numpy-reshape-transpose-theano-dimshuffle/阅读更多信息
回答by Shayan Amani
Long story short: you set some dimensions and let NumPy set the remaining(s).
长话短说:您设置一些维度,然后让 NumPy 设置剩余的维度。
(userDim1, userDim2, ..., -1) -->>
(userDim1, userDim1, ..., TOTAL_DIMENSION - (userDim1 + userDim2 + ...))
回答by lonewolf
It simply means that you are not sure about what number of rows or columns you can give and you are asking numpy to suggest number of column or rows to get reshaped in.
这只是意味着您不确定可以提供多少行或列数,并且您要求 numpy 建议要重新调整的列数或行数。
numpy provides last example for -1 https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html
numpy 为 -1 https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html提供了最后一个例子
check below code and its output to better understand about (-1):
检查以下代码及其输出以更好地了解(-1):
CODE:-
代码:-
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)
OUTPUT :-
输出 :-
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]]
回答by Sherzod
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)
回答by deepjyoti22
The final outcome of the conversion is that the number of elements in the final array is same as that of the initial array or data frame.
转换的最终结果是最终数组中的元素数与初始数组或数据帧中的元素数相同。
-1 corresponds to the unknown count of the row or column. we can think of it as x
(unknown). x
is obtained by dividing the umber of elements in the original array by the other value of the ordered pair with -1.
-1 对应于行或列的未知计数。我们可以将其视为x
(未知)。x
是通过将原始数组中的元素数量除以带有 -1 的有序对的另一个值而获得的。
Examples
例子
12 elements with reshape(-1,1) corresponds to an array with x
=12/1=12 rows and 1 column.
具有 reshape(-1,1) 的 12 个元素对应于具有x
=12/1=12 行和 1 列的数组。
12 elements with reshape(1,-1) corresponds to an array with 1 row and x
=12/1=12 columns.
具有 reshape(1,-1) 的 12 个元素对应于具有 1 行和x
=12/1=12 列的数组。