NumPy reshape中的-1是什么意思
NumPy是一个用于数值计算的Python库,提供了高效的数组操作,numpy.reshape()是一个用于改变数组形状的函数,其中-1表示一个被推断的维度。
在使用数组时,我们经常需要修改数组的形状,但这需要先复制数据,然后将其排列成所需的形状,这需要时间。幸运的是,Python有一个叫做reshape()的函数可以帮助我们。
示例1:在Numpy中找到未知维度
我们在不知道数组的维度时,只允许有一个”未知”维度。
这意味着在使用reshape方法时,我们不需要为其中的一个维度提供一个数字。我们使用NumPy通过传递-1来计算未知维度。
参数值(2, 2, -1)表示输出数组的期望形状是2行、2列,并且剩余的维度根据输入数组的大小自动推断。
下面的程序展示了在NumPy数组的reshape()函数中使用-1的方法。
# importing numpy module
import numpy as np
# creating a numpy array
inputArray = np.array([15, 16, 17, 18, 19, 20, 21, 22])
# reshaping the input array into a 3-dimensional array with 2 rows, 2 columns, and an automatically inferred depth
outputArray = inputArray.reshape(2, 2, -1)
# printing the resultant array
print(outputArray)
输出
[[[15 16]
[17 18]]
[[19 20]
[21 22]]]
示例2:使用-1作为未知列数值
以下程序演示了如何使用-1表示未知的列数值。
# importing numpy module
import numpy as np
# creating a numpy array
inputArray = np.array([15, 16, 17, 18, 19, 20, 21, 22])
# reshaping the input array into a 3-dimensional array with 2 rows, an automatically inferred middle dimension, and 4 columns
outputArray = inputArray.reshape(2, -1, 4)
# printing the resultant array
print(outputArray)
输出
[[[15 16 17 18]]
[[19 20 21 22]]]
示例3:使用-1作为未知行数值
在下面给出的示例中,形状被指定为(-1, 4, 2),这表示第一个维度将根据输入数组的大小自动推断。输出是一个具有所需形状的3维数组。然后使用reshape()函数将inputArray重塑为一个3维数组。它有2行,自动推断的中间维度(使用-1),和4列。-1参数允许numpy根据输入数组的大小和给定的维度自动计算推断维度的大小。
# importing numpy module
import numpy as np
# creating a numpy array
inputArray = np.array([15, 16, 17, 18, 19, 20, 21, 22])
# reshaping the input array into a 3-dimensional array with an automatically inferred first dimension, 4 columns, and 2 depth elements
outputArray = inputArray.reshape(-1, 4, 2)
# printing the resultant array
print(outputArray)
输出
执行上述程序后,将生成以下输出结果
[[[15 16]
[17 18]
[19 20]
[21 22]]]
示例4:使用-1表示多个未知维度
在这个例子中,我们使用了-1表示多个未知维度。因此出现了错误。
# importing numpy module
import numpy as np
# creating a numpy array
inputArray = np.array([15, 16, 17, 18, 19, 20, 21, 22])
# reshaping the input array into a 3-dimensional array with 2 as the first dimension and an automatically inferred second and third dimension
outputArray = inputArray.reshape(2, -1, -1)
# printing the resultant array
print(outputArray)
输出
Traceback (most recent call last):
File "/home/cg/root/85901/main.py", line 8, in <module>
outputArray = inputArray.reshape(2, -1, -1)
ValueError: can only specify one unknown dimension
使用reshape()函数对Numpy数组进行扁平化
使用reshape()函数将3D数组扁平化为1D数组
以下程序通过使用reshape()函数将输入的3D数组扁平化为1D数组,返回扁平化后的1维数组。
# importing numpy module
import numpy as np
# creating a 3D array
inputArray = np.array([[1, 3, 5],
[7, 9, 11],
[13, 15, 17]])
# flattening 3D input array to 1-dimension using reshape() function
outputArray = inputArray.reshape(-1)
# printing the resultanat flattened array
print("Output flattened array:\n", outputArray)
输出
Output flattened array:
[ 1 3 5 7 9 11 13 15 17]
将二维数组平铺成一维数组使用reshape()函数
以下程序通过使用reshape()函数将输入的二维数组平铺成一维数组,并返回平铺后的一维数组
# importing numpy module
import numpy as np
# creating a 2D array
inputArray = np.array([[15, 16, 17], [18, 19, 20]])
# flattening 2D input array to 1-dimension using reshape() function
outputArray = inputArray.reshape(-1)
# printing the resultant flattened array
print("Output flattened array:\n", outputArray)
输出
Output flattened array:
[15 16 17 18 19 20]
结论
总而言之,NumPy的reshape()函数中的-1被用于表示未知的维度。它允许NumPy根据其他维度和输入数组的大小自动计算该维度的大小。这个特性在重新整形具有已知维度的数组或将多维数组展平为一维数组时特别有用。通过利用-1,我们可以简化重新整形的过程并避免手动计算的需求。