可以使用.重塑(-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允许我们给出一个新的形状参数为-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建议/错误消息一致,对单个特性使用重塑(-1,1);即单柱
使用数组重塑数据。如果数据只有单一特征,则重塑(- 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建议/错误消息一致,对单个样本使用重塑(1,-1);即单行
使用数组重塑数据。如果包含单个样本,则重塑(1,-1)
新形状(2,-1)。第二行,未知列。结果是(2,6)
z.reshape(2, -1)
array([[ 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12]])
新形状为(3,-1)。第三行,未知列。结果是(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
其他回答
这仅仅意味着您不确定您可以给出多少行或列,并且您要求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允许我们给出一个新的形状参数为-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建议/错误消息一致,对单个特性使用重塑(-1,1);即单柱
使用数组重塑数据。如果数据只有单一特征,则重塑(- 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建议/错误消息一致,对单个样本使用重塑(1,-1);即单行
使用数组重塑数据。如果包含单个样本,则重塑(1,-1)
新形状(2,-1)。第二行,未知列。结果是(2,6)
z.reshape(2, -1)
array([[ 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12]])
新形状为(3,-1)。第三行,未知列。结果是(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
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]])
假设我们有一个三维数组,大小为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))
会得到一个数组shape = (50,4)
你可以在http://anie.me/numpy-reshape-transpose-theano-dimshuffle/上阅读更多
根据文档:
Newshape: int或int的元组 新形状应与原形状兼容。如果一个 整数,则结果将是该长度的一维数组。一个形状 维度可以是-1。在本例中,该值是从 数组的长度和剩余维度。