扁平化 一个NumPy数组的列表

扁平化 一个NumPy数组的列表

前提是Flatten()和Ravel()Numpy函数之间的区别,Python中的numpy.ravel()。

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

扁平化NumPy数组列表是指将多维的NumPy数组合并成一个数组或列表,下面是一个例子

Numpy数组的列表:

[array([[ 0.00353654]]),
array([[ 0.00353654]]),
array([[ 0.00353654]]),
array([[ 0.00353654]]),
array([[ 0.00353654]]),
array([[ 0.00353654]]),
array([[ 0.00353654]]),
array([[ 0.00353654]]),
array([[ 0.00353654]]),
array([[ 0.00353654]]),
array([[ 0.00353654]]),
array([[ 0.00353654]]),
array([[ 0.00353654]])]

扁平化Numpy数组:

Array([ 0.00353654, 0.00353654, 0.00353654, 0.00353654, 0.00353654。
0.00353654, 0.00353654, 0.00353654, 0.00353654, 0.00353654,
0.00353654, 0.00353654, 0.00353654])

方法 1
使用numpy的连接方法

# 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())

输出 :

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

方法 2
使用numpy的flatten方法

# 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]

方法 3
使用numpy的ravel方法

# 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]

方法 4
使用numpy的reshape方法

# 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]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Numpy 数组操作