可以使用.重塑(-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是什么意思?
当前回答
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)
其他回答
假设我们有一个三维数组,大小为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/上阅读更多
当你使用-1(或任何其他负整数,我做这个测试kkk)在
b = numpy.reshape(a, -1)
你说的只是麻木的人。重塑自动计算向量的大小(行x列),并将其重新定位到具有该维度的1-D向量。这个命令很有趣,因为它会自动为您执行。如果您想通过输入一个正整数值来将向量重塑为1-D,那么重塑命令只有在正确输入值“rows x columns”时才会起作用。所以输入一个负整数会让这个过程更简单。
转换的最终结果是最终数组中的元素数量与初始数组或数据帧的元素数量相同。
-1对应行或列的未知计数。 我们可以把它看成x(未知)X是用原始数组中的元素数除以-1的有序对的另一个值得到的。
例子:
12个元素与重塑(-1,1)对应的数组x=12/1=12行1列。
12个元素具有重塑(1,-1)对应于一个有1行和x=12/1=12列的数组。
这仅仅意味着您不确定您可以给出多少行或列,并且您要求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]])