numpy.ndarray.flatten(order=‘C’)
把多维数组"扁平化"为一个一维向量,其过程是把该数组按照order
指定的顺序遍历一遍,并把结果储存为一维向量.
Parameters
order {‘C’, ‘F’, ‘A’, ‘K’}, optional
‘C’ (Default) means to flatten in row-major (C-style) order.
‘F’ means to flatten in column-major (Fortran- style) order.
‘A’ means to flatten in column-major order if a is Fortran contiguous in memory, row-major order otherwise.
‘K’ means to flatten a in the order the elements occur in memory. The default is ‘C’.
示例
a = np.array([[1,2], [3,4]])
print(a.flatten())
结果
[1 2 3 4]
相当于
print(a.reshape(1,a.size))