如何在NumPy中将矩阵压缩成数组/集最快?

时间:2014-03-07 03:55:54

标签: python arrays numpy matrix set

如何将像mat0 = np.array([[1,2,3],[4,5,6],[7,8,9]])这样的矩阵展平为像arr0 = np.array([1,2,3,4,5,6,7,8,9])这样的数组?或者也许是一套?

1 个答案:

答案 0 :(得分:1)

使用reshape

>>> import numpy as np
>>> mat0 = np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> mat0.reshape(-1)
array([1, 2, 3, 4, 5, 6, 7, 8, 9])

ravel

>>> mat0.ravel()
array([1, 2, 3, 4, 5, 6, 7, 8, 9])