📜  展平 NumPy 数组列表

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

展平 NumPy 数组列表

Flatten() 和 Ravel() Numpy 函数之间的先决条件差异, Python中的 numpy.ravel() ,

在本文中,我们将看到如何展平 numpy 数组列表。 NumPy是Python编程语言的库,增加了对大型、多维数组和矩阵的支持,以及对这些数组进行操作的大量高级数学函数集合。

Flatten a list of NumPy array 意思是将多维 NumPy 数组组合成一个数组或列表,下面是例子

方法一
使用 numpy 的连接方法

Python3
# importing numpy as np
import numpy as np
 
# list of numpy array
list_array = [np.array([[1]]),
               np.array([[2]]),
               np.array([[3]]),
               np.array([[4]]),
               np.array([[5]]),
               np.array([[6]]),
               np.array([[7]]),
               np.array([[8]]),
               np.array([[9]]),
               np.array([[10]]),
               np.array([[11]]),
               np.array([[12]]),
               np.array([[13]]),
               np.array([[14]]),
               np.array([[15]]),
               np.array([[16]])]
 
# concatenating all the numpy array
flatten = np.concatenate(list_array)
 
# printing the ravel flatten array
print(flatten.ravel())


Python3
# importing numpy as np
import numpy as np
 
# list of numpy array
list_array = [np.array([[1]]),
               np.array([[2]]),
               np.array([[3]]),
               np.array([[4]]),
               np.array([[5]]),
               np.array([[6]]),
               np.array([[7]]),
               np.array([[8]]),
               np.array([[9]]),
               np.array([[10]]),
               np.array([[11]]),
               np.array([[12]]),
               np.array([[13]]),
               np.array([[14]]),
               np.array([[15]]),
               np.array([[16]])]
 
# flatten the numpy array
flatten = np.array(list_array).flatten()
 
# printing the flatten array
print(flatten)


Python3
# importing numpy as np
import numpy as np
 
# list of numpy array
list_array = [np.array([[1]]),
               np.array([[2]]),
               np.array([[3]]),
               np.array([[4]]),
               np.array([[5]]),
               np.array([[6]]),
               np.array([[7]]),
               np.array([[8]]),
               np.array([[9]]),
               np.array([[10]]),
               np.array([[11]]),
               np.array([[12]]),
               np.array([[13]]),
               np.array([[14]]),
               np.array([[15]]),
               np.array([[16]])]
 
# flatten the numpy array using ravel method
flatten = np.array(list_array).ravel()
 
# printing the flatten array
print(flatten)


Python3
# importing numpy as np
import numpy as np
 
# list of numpy array
list_array = [np.array([[1]]),
               np.array([[2]]),
               np.array([[3]]),
               np.array([[4]]),
               np.array([[5]]),
               np.array([[6]]),
               np.array([[7]]),
               np.array([[8]]),
               np.array([[9]]),
               np.array([[10]]),
               np.array([[11]]),
               np.array([[12]]),
               np.array([[13]]),
               np.array([[14]]),
               np.array([[15]]),
               np.array([[16]])]
 
# flatten the numpy array using reshape method
flatten = np.array(list_array).reshape(-1)
 
# printing the flatten array
print(flatten)


输出 :

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

方法二
使用 numpy 的 flatten 方法

蟒蛇3

# importing numpy as np
import numpy as np
 
# list of numpy array
list_array = [np.array([[1]]),
               np.array([[2]]),
               np.array([[3]]),
               np.array([[4]]),
               np.array([[5]]),
               np.array([[6]]),
               np.array([[7]]),
               np.array([[8]]),
               np.array([[9]]),
               np.array([[10]]),
               np.array([[11]]),
               np.array([[12]]),
               np.array([[13]]),
               np.array([[14]]),
               np.array([[15]]),
               np.array([[16]])]
 
# flatten the numpy array
flatten = np.array(list_array).flatten()
 
# printing the flatten array
print(flatten)

输出 :

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

方法三
使用 numpy 的 ravel 方法

蟒蛇3

# importing numpy as np
import numpy as np
 
# list of numpy array
list_array = [np.array([[1]]),
               np.array([[2]]),
               np.array([[3]]),
               np.array([[4]]),
               np.array([[5]]),
               np.array([[6]]),
               np.array([[7]]),
               np.array([[8]]),
               np.array([[9]]),
               np.array([[10]]),
               np.array([[11]]),
               np.array([[12]]),
               np.array([[13]]),
               np.array([[14]]),
               np.array([[15]]),
               np.array([[16]])]
 
# flatten the numpy array using ravel method
flatten = np.array(list_array).ravel()
 
# printing the flatten array
print(flatten)

输出 :

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

方法四
使用 numpy 的 reshape 方法

蟒蛇3

# importing numpy as np
import numpy as np
 
# list of numpy array
list_array = [np.array([[1]]),
               np.array([[2]]),
               np.array([[3]]),
               np.array([[4]]),
               np.array([[5]]),
               np.array([[6]]),
               np.array([[7]]),
               np.array([[8]]),
               np.array([[9]]),
               np.array([[10]]),
               np.array([[11]]),
               np.array([[12]]),
               np.array([[13]]),
               np.array([[14]]),
               np.array([[15]]),
               np.array([[16]])]
 
# flatten the numpy array using reshape method
flatten = np.array(list_array).reshape(-1)
 
# printing the flatten array
print(flatten)

输出 :

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