📜  重塑 NumPy 数组

📅  最后修改于: 2022-05-13 01:54:24.136000             🧑  作者: Mango

重塑 NumPy 数组

NumPy是一个通用的数组处理包。它提供了一个高性能的多维数组对象,以及用于处理这些数组的工具。它是使用Python进行科学计算的基础包。 Numpy 基本上用于创建 n 维数组。
重塑 numpy 数组只是意味着改变给定数组的形状,形状基本上告诉数组的元素数量和维度,通过重塑数组我们可以添加或删除维度或更改每个维度中的元素数量。
为了重塑一个 numpy 数组,我们对给定的数组使用 reshape 方法。

注意:我们也可以使用 np.reshape(array, shape) 命令来重塑数组
重塑:一维到二维
在这个例子中,我们将把形状为 (1, n) 的一维数组重塑为形状为 (N, M) 的二维数组,这里 M 应该等于 n/N,因为 N 应该是 n 的因数。

Python3
# importing numpy
import numpy as np
 
# creating a numpy array
array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
 
# printing array
print("Array : " + str(array))
 
# length of array
n = array.size
 
# N-D array N dimension
N = 4
 
# calculating M
M = n//N
 
# reshaping numpy array
# converting it to 2-D from 1-D array
reshaped1 = array.reshape((N, M))
 
# printing reshaped array
print("First Reshaped Array : ")
print(reshaped1)
 
# creating another reshaped array
reshaped2 = np.reshape(array, (2, 8))
 
# printing reshaped array
print("Second Reshaped Array : ")
print(reshaped2)


Python3
# importing numpy
import numpy as np
 
# creating a numpy array
array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
 
# printing array
print("Array : " + str(array))
 
 
# reshaping numpy array
# converting it to 3-D from 1-D array
reshaped = array.reshape((2, 2, 4))
 
# printing reshaped array
print("Reshaped 3-D Array : ")
print(reshaped)


Python3
# importing numpy
import numpy as np
 
# creating a numpy array
array = np.array([[1, 2, 3],
                 [4, 5, 6],
                 [7, 8, 9]])
 
# printing array
print(" 2-D Array : ")
print(array)
 
 
# reshaping numpy array
# converting it to 1-D from 2-D array
reshaped = array.reshape((9))
 
# or we can use unknown dimension
# reshaped = array.reshape((-1))
 
# printing reshaped array
print("Reshaped 1-D Array : ")
print(reshaped)


Python3
# importing numpy
import numpy as np
 
# creating a numpy array
array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
 
# printing array
print("Array : " + str(array))
 
 
# reshaping numpy array
# converting it to 3-D from 1-D array
reshaped1 = array.reshape((2, 2, -1))
 
# printing reshaped array
print("First Reshaped Array : ")
print(reshaped1)
 
 
# converting it to 2-D array
reshaped2 = array.reshape((4, -1))
 
# printing reshaped array
print("Second Reshaped Array : ")
print(reshaped2)


Python3
# importing numpy
import numpy as np
 
# creating a numpy array
array = np.array([[1, 2, 3],
                 [4, 5, 6],
                 [7, 8, 9]])
 
# printing array
print(" 2-D Array : ")
print(array)
 
 
# reshaping numpy array
# converting it to 1-D from 2-D array
# reshaping it into 1, 5
reshaped = array.reshape((1, 5))
 
# or we can use
 
# printing reshaped array
print("Reshaped 1-D Array : ")
print(reshaped)


输出 :

Array : [ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16]
First Reshaped Array : 
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [13 14 15 16]]
Second Reshaped Array : 
[[ 1  2  3  4  5  6  7  8]
 [ 9 10 11 12 13 14 15 16]]

重塑:1-D 到 3-D
在本文中,我们将看到如何将 1-D 数组重塑为 3-D 维数组。 3-D 阵列是 2-D 阵列的 1-D 阵列。

Python3

# importing numpy
import numpy as np
 
# creating a numpy array
array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
 
# printing array
print("Array : " + str(array))
 
 
# reshaping numpy array
# converting it to 3-D from 1-D array
reshaped = array.reshape((2, 2, 4))
 
# printing reshaped array
print("Reshaped 3-D Array : ")
print(reshaped)

输出 :

Array : [ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16]
Reshaped 3-D Array : 
[[[ 1  2  3  4]
  [ 5  6  7  8]]

 [[ 9 10 11 12]
  [13 14 15 16]]]

将 ND 重塑为一维阵列
在此示例中,我们将看到如何重塑 2-D 或 3-D 数组以形成 1-D 数组。我们也可以使用 reshape(-1) 来做到这一点,这里 -1 是未知维度。

Python3

# importing numpy
import numpy as np
 
# creating a numpy array
array = np.array([[1, 2, 3],
                 [4, 5, 6],
                 [7, 8, 9]])
 
# printing array
print(" 2-D Array : ")
print(array)
 
 
# reshaping numpy array
# converting it to 1-D from 2-D array
reshaped = array.reshape((9))
 
# or we can use unknown dimension
# reshaped = array.reshape((-1))
 
# printing reshaped array
print("Reshaped 1-D Array : ")
print(reshaped)

输出 :

2-D Array : 
[[1 2 3]
 [4 5 6]
 [7 8 9]]
Reshaped 1-D Array : 
[[1 2 3 4 5 6 7 8 9]]

使用未知维度重塑
我们可以重塑一个数组,虽然我们不知道所有的新维度,使用 -1 作为维度之一,但我们应该知道所有其他维度以使用未知维度

Python3

# importing numpy
import numpy as np
 
# creating a numpy array
array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
 
# printing array
print("Array : " + str(array))
 
 
# reshaping numpy array
# converting it to 3-D from 1-D array
reshaped1 = array.reshape((2, 2, -1))
 
# printing reshaped array
print("First Reshaped Array : ")
print(reshaped1)
 
 
# converting it to 2-D array
reshaped2 = array.reshape((4, -1))
 
# printing reshaped array
print("Second Reshaped Array : ")
print(reshaped2)

输出 :

Array : [ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16]
First Reshaped Array : 
[[[ 1  2  3  4]
  [ 5  6  7  8]]

 [[ 9 10 11 12]
  [13 14 15 16]]]
Second Reshaped Array : 
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [13 14 15 16]]

整形过程中出现错误
当我们尝试将数组重塑为数学上不可能的形状时,会产生值错误,表示无法重塑数组。例如,当我们尝试将具有 4 个元素的一维数组重塑为维度 (3, 3) 的二维数组时,这是不可能的,因为新数组需要 9 个元素

Python3

# importing numpy
import numpy as np
 
# creating a numpy array
array = np.array([[1, 2, 3],
                 [4, 5, 6],
                 [7, 8, 9]])
 
# printing array
print(" 2-D Array : ")
print(array)
 
 
# reshaping numpy array
# converting it to 1-D from 2-D array
# reshaping it into 1, 5
reshaped = array.reshape((1, 5))
 
# or we can use
 
# printing reshaped array
print("Reshaped 1-D Array : ")
print(reshaped)
ValueError: cannot reshape array of size 9 into shape (1, 5)