可以使用.重塑(-1)将2D数组重新塑造为1D数组。 例如:

>>> 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是什么意思?


当前回答

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)

其他回答

"在所有其他维度都已指定的情况下,推断出这个维度"

显式地,这将使-1维度成为原始数组的维度积与新指定的dim的维度积的商。如果不是整数,则返回错误。

例如,对于一个形状数组(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))

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

-1对应行或列的未知计数。 我们可以把它看成x(未知)X是用原始数组中的元素数除以-1的有序对的另一个值得到的。

例子:

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


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

-1代表“未知维度”,可以从另一个维度推断出来。 在这种情况下,如果你这样设置你的矩阵:

a = numpy.matrix([[1, 2, 3, 4], [5, 6, 7, 8]])

像这样修改你的矩阵:

b = numpy.reshape(a, -1)

它将调用一些对矩阵a的默认操作,这将返回一个1-d numpy数组/矩阵。

然而,我不认为使用这样的代码是一个好主意。为什么不试试呢:

b = a.reshape(1, -1)

它会给你同样的结果,而且更容易让读者理解:将b设置为a的另一种形状。对于a,我们不知道它应该有多少列(设置为-1!),但我们想要一个一维数组(设置第一个参数为1!)

直到读了这篇文章,我才明白np. remodeling()是做什么的。

从机械上看,重塑()的功能很清楚。但我们如何解读重塑前后的数据呢?

我所缺少的是:

当我们训练机器学习模型时,数组的嵌套层具有精确定义的含义。

这意味着,在重塑操作有任何意义之前,必须敏锐地意识到以下两点:

它所操作的数据(重塑输入的样子) 算法/模型期望重塑后的数据是什么样子(重塑后的输出是什么样子)

例如:

外部数组包含观测值/行。内部数组包含列/特性。这导致了两种特殊情况,即我们要么有一个对一个特征的多个观察的数组,要么有一个对多个特征的单一观察。

更高级的例子: 请看这个stackoverflow问题


编辑:增加了更详细的例子,见下文。

场景

我们有以下三组/份:

(图中为1组)

所有东西都被平化了,所以emb(3个src节点,emb_size=32)是torch。大小([32])。和,emb的6 tgt节点火炬。大小([6 32])

Goal

我们希望重塑数据,以便每个src对应2个tgt节点,因此我们这样做:

现在,对于第i个src节点,我们有:

source_embs[我:] 对应的target_embs[i,:,:] 这就是关键所在:数据现在被整齐地组织起来了,如果没有重塑,我们就不能做这个简单的索引。

细节

查看target_embs的形状:

before reshaping, shape is [6,32] we start from rightmost dim, dim1=32, it isn't changed in the reshape, so ignore we view shape as [6,*], and now the rightmost dim is dim0=6, almost like ignore dim1, and view it as [6] When we reshape [6] into [3,2], we always look at the rightmost dim first, so we take 2 elements, then change row, then 2 element then change row and so on As prior knowledge, we know [6,*] corresponds to [src1_tgt1, src1_tgt2, src2_tgt1, src2_tgt2, src3_tgt1, src3_tgt2] (this input has to be in this format, or else we need to rearrange the input into this format) hence we know output is formatted correctly: [3,2] will correspond to what we want: [[src1_tgt1,src1_tgt2],[src2_tgt1, src2_tgt2],[src3_tgt1, src3_tgt2]] So reshaping [6,32] into [3,2,32] is now complete what if we want to reshape [6,32] into [4,3,16]? torch can do this, because the index match up, but the result is useless to our purposes what if we want to have [32,2,3] in the end instead of [3,2,32]? Do we just do reshape(input6x32,(32,2,3))? No. Because the data will be scrambled and will be meaningless. What we can do is to get to [3,2,32] first, and then use transpose() into [32,2,3].

摘要(基本使用)

一次重塑两个连续的维度,而且只能重塑两个。这样就更容易理解了。 如果要重塑非连续维度,则在重塑前进行转置 可能还有更高级的用法,但这是我设法理解重塑()正在做什么的唯一方法。

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)