numpy reshape 中的 -1 是什么意思?
- 2025-02-05 13:23:00
- admin 原创
- 90
问题描述:
可以使用 将二维数组重塑为一维数组.reshape(-1)
。例如:
>>> a = numpy.array([[1, 2, 3, 4], [5, 6, 7, 8]])
>>> a.reshape(-1)
array([[1, 2, 3, 4, 5, 6, 7, 8]])
通常,array[-1]
表示最后一个元素。但是 -1 在这里是什么意思?
解决方案 1:
提供新形状需要满足的标准是“新形状应与原始形状兼容”
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)
现在尝试用 (-1) 重塑。结果新形状为 (12,) 并且与原始形状 (3,4) 兼容
z.reshape(-1)
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
现在尝试用 (-1, 1) 重塑。我们将列指定为 1,但将行指定为未知。因此,我们得到的结果新形状为 (12, 1)。再次与原始形状 (3,4) 兼容
z.reshape(-1,1)
array([[ 1],
[ 2],
[ 3],
[ 4],
[ 5],
[ 6],
[ 7],
[ 8],
[ 9],
[10],
[11],
[12]])
以上与numpy
建议/错误消息一致,用于reshape(-1,1)
单个功能;即单列
array.reshape(-1, 1)
如果您的数据具有单一特征,请使用以下方法重塑数据
新形状为 (-1, 2)。行未知,列 2。我们得到结果新形状为 (6, 2)
z.reshape(-1, 2)
array([[ 1, 2],
[ 3, 4],
[ 5, 6],
[ 7, 8],
[ 9, 10],
[11, 12]])
现在尝试将列保持为未知。新形状为 (1,-1)。即行为 1,列未知。我们得到结果新形状为 (1, 12)
z.reshape(1,-1)
array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]])
以上与numpy
建议/错误消息一致,用于reshape(1,-1)
单个样本;即单行
如果数据
array.reshape(1, -1)
包含单个样本,则使用
新形状 (2, -1)。第 2 行,列未知。我们得到结果新形状为 (2,6)
z.reshape(2, -1)
array([[ 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12]])
新形状为 (3, -1)。第 3 行,列未知。我们得到结果新形状为 (3,4)
z.reshape(3, -1)
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
最后,如果我们尝试将两个维度都设置为未知,即新形状为 (-1,-1)。它将抛出一个错误
z.reshape(-1, -1)
ValueError: can only specify one unknown dimension
解决方案 2:
假设我们有一个尺寸为 2 x 10 x 10 的三维数组:
r = numpy.random.rand(2, 10, 10)
现在我们要将其重塑为 5 X 5 x 8:
numpy.reshape(r, shape=(5, 5, 8))
将会完成这项工作。
请注意,一旦确定了第一维dim = 5
和第二维dim = 5
,就不需要确定第三维了。为了帮助你懒惰,Numpy 提供了使用以下选项-1
:
numpy.reshape(r, shape=(5, 5, -1))
将为您提供一个数组shape = (5, 5, 8)
。
同样地,
numpy.reshape(r, shape=(50, -1))
将为您提供一个形状为 (50, 4) 的数组
您可以在http://anie.me/numpy-reshape-transpose-theano-dimshuffle/阅读更多
解决方案 3:
根据the documentation
:
newshape:整数或整数元组
新形状应与原始形状兼容。如果是整数,则结果将是该长度的一维数组。一个形状维度可以是-1。在这种情况下,该值是根据数组的长度和剩余维度推断出来的。
解决方案 4:
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)
输出:
matrix([[1, 2, 3, 4, 5, 6, 7, 8]])
可以用另一个例子来更准确地解释这一点:
b = np.arange(10).reshape((-1,1))
输出:(是一维柱状数组)
array([[0],
[1],
[2],
[3],
[4],
[5],
[6],
[7],
[8],
[9]])
或者
b = np.arange(10).reshape((1,-1))
输出:(是一维行数组)
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
解决方案 5:
这仅仅意味着您不确定可以给出多少行或多少列,并且您要求 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)
输出 :-
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]]
解决方案 6:
代表-1
“未知维度”,可以从另一个维度推断出来。在这种情况下,如果您像这样设置矩阵:
a = numpy.matrix([[1, 2, 3, 4], [5, 6, 7, 8]])
像这样修改矩阵:
b = numpy.reshape(a, -1)
它将调用矩阵的一些默认操作a
,这些操作将返回一维numpy
数组/矩阵。
但是,我认为使用这样的代码不是一个好主意。为什么不试试呢:
b = a.reshape(1, -1)
它会给你相同的结果,而且读者更容易理解:设置b
为的另一种形状a
。对于a
,我们不知道它应该有多少列(将其设置为 -1!),但我们想要一个一维数组(将第一个参数设置为 1!)。
解决方案 7:
转换的最终结果是最终数组中元素的数量与初始数组或数据框的数量相同。
-1 对应行或列的未知计数。我们可以将其视为x
(未知)。x
是通过将原始数组中的元素数量除以有序对的另一个值为 -1 的值得到的。
例子:
12 个元素reshape(-1,1)
对应于具有 =12/1=12 行和 1 列的数组x
。
12 个元素对应 1 行、 =12/1=12 列reshape(1,-1)
的数组。x
解决方案 8:
长话短说:您设置一些维度,然后让 NumPy 设置剩余的维度。
(userDim1, userDim2, ..., -1) -->>
(userDim1, userDim1, ..., TOTAL_DIMENSION - (userDim1 + userDim2 + ...))
解决方案 9:
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)
解决方案 10:
np.reshape()
直到读了这篇文章,我才明白什么是。
从机制上看,这很清楚reshape()
。但我们如何解释重塑前后的数据?
我缺少的是:
当我们训练机器学习模型时,数组的嵌套级别具有精确定义的含义。
这意味着重塑操作在具有任何意义之前必须敏锐地意识到以下两点:
它所操作的数据(重塑输入的样子)
算法/模型期望重塑后的数据是什么样的(重塑后的输出是什么样的)
例如:
外部数组包含观测/行。内部数组包含列/特征。当我们有一个仅包含一个特征的多个观测的数组或多个特征的单个观测时,这会导致两种特殊情况。
有关更高级的示例:请参阅此 stackoverflow 问题
编辑:添加了更详细的示例,见下文。
设想
我们有以下3 组/副本:
(图示为1组)
一切都是扁平的,因此 3 个 src 节点的 emb,其 emb_size=32,是torch.Size([3, 32])
。并且,6 个 tgt 节点的 embtorch.Size([6, 32])
目标
我们希望重塑数据,以便每个 src 对应 2 个 tgt 节点,因此我们这样做:
现在,对于第 i 个 src 节点,我们有:
source_embs[i,:]
与相应的
target_embs[i,:,:]
这就是重点:数据现在被整齐地组织起来了,如果不重塑,我们就无法进行这种简单的索引。
细节
查看形状target_embs
:
重塑之前,形状是
[6,32]
我们从最右边的暗淡开始,
dim1=32
它在重塑时不会改变,所以忽略我们将形状视为
[6,*]
,现在最右边的dim是dim0=6
,几乎就像忽略dim1,并将其视为[6]
当我们重塑
[6]
成时[3,2]
,我们总是先看最右边的暗淡,所以我们取2
元素,然后改变行,然后2
元素,然后改变行等等作为先验知识,我们知道
[6,*]
对应于[src1_tgt1, src1_tgt2, src2_tgt1, src2_tgt2, src3_tgt1, src3_tgt2]
(此输入必须采用这种格式,否则我们需要将输入重新排列为这种格式)因此我们知道输出格式正确:
[3,2]
将对应于我们想要的:[[src1_tgt1,src1_tgt2],[src2_tgt1, src2_tgt2],[src3_tgt1, src3_tgt2]]
因此重塑
[6,32]
现已[3,2,32]
完成如果我们想重塑
[6,32]
成什么样[4,3,16]
?torch 可以做到这一点,因为索引匹配,但结果对我们的目的毫无用处如果我们想要的
[32,2,3]
是 而不是[3,2,32]
,该怎么办?我们这样做吗reshape(input6x32,(32,2,3))
?不。因为数据会变得混乱,毫无意义。我们可以做的是先得到[3,2,32]
,然后transpose()
使用[32,2,3]
。
摘要(基本用法)
每次重塑 2 个连续的维度,且只能重塑 2 个。这样就更容易理解了。
如果要重塑非连续维度,则在重塑之前进行转置
可能还有更高级的用法,但这是我设法理解
reshape()
正在做什么的唯一方法。
解决方案 11:
当你使用 -1 (或任何其他负整数,我做了这个测试 kkk)
b = numpy.reshape(a, -1)
您只是说要numpy.reshape
自动计算向量的大小(行 x 列)并将其重新定位为具有该维度的 1-D 向量。此命令很有趣,因为它会自动为您完成此操作。如果您想通过输入正整数值将向量重塑为 1-D,reshape
则只有正确输入值“行 x 列”时,该命令才会起作用。所以能够输入负整数会使过程更容易,您知道的。
解决方案 12:
“在所有其他维度都已指定的情况下推断这个维度。”
明确地说,这将使-1
维度成为原始数组的维度乘积与新指定的维度乘积的商。如果这不是整数,它将返回错误。
例如,对于形状为的数组(2,3,5)
,以下内容都是等效的:
a = np.random.rand(2, 3, 5)
np.reshape(a, (-1, 2, 5))
np.reshape(a, ( 3, -1, 5))
np.reshape(a, ( 3, 2, -1))