标签: 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])这样的数组?或者也许是一套?
mat0 = np.array([[1,2,3],[4,5,6],[7,8,9]])
arr0 = np.array([1,2,3,4,5,6,7,8,9])
答案 0 :(得分:1)
使用reshape:
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:
ravel
>>> mat0.ravel() array([1, 2, 3, 4, 5, 6, 7, 8, 9])