可以使用.重塑(-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是什么意思?
可以使用.重塑(-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是什么意思?
当前回答
这仅仅意味着您不确定您可以给出多少行或列,并且您要求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]]
其他回答
根据文档:
Newshape: int或int的元组 新形状应与原形状兼容。如果一个 整数,则结果将是该长度的一维数组。一个形状 维度可以是-1。在本例中,该值是从 数组的长度和剩余维度。
这仅仅意味着您不确定您可以给出多少行或列,并且您要求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]]
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))
输出:(1维柱状阵列)
array([[0],
[1],
[2],
[3],
[4],
[5],
[6],
[7],
[8],
[9]])
or
b = np.arange(10).reshape((1,-1))
输出:(是一个1维行数组)
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
-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].
摘要(基本使用)
一次重塑两个连续的维度,而且只能重塑两个。这样就更容易理解了。 如果要重塑非连续维度,则在重塑前进行转置 可能还有更高级的用法,但这是我设法理解重塑()正在做什么的唯一方法。