可以使用.重塑(-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.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]])
其他回答
根据文档:
Newshape: int或int的元组 新形状应与原形状兼容。如果一个 整数,则结果将是该长度的一维数组。一个形状 维度可以是-1。在本例中,该值是从 数组的长度和剩余维度。
-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!)
这仅仅意味着您不确定您可以给出多少行或列,并且您要求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]]
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(或任何其他负整数,我做这个测试kkk)在
b = numpy.reshape(a, -1)
你说的只是麻木的人。重塑自动计算向量的大小(行x列),并将其重新定位到具有该维度的1-D向量。这个命令很有趣,因为它会自动为您执行。如果您想通过输入一个正整数值来将向量重塑为1-D,那么重塑命令只有在正确输入值“rows x columns”时才会起作用。所以输入一个负整数会让这个过程更简单。