flatten():降至一维。
from numpy import * x = mat(arange(12).reshape((3,4))) print(x.flatten(),type(x.flatten())) y=arange(12).reshape((3,4)) print(y.flatten(),type(y.flatten()))
输出:
[[ 0 1 2 3 4 5 6 7 8 9 10 11]] <class 'numpy.matrixlib.defmatrix.matrix'> [ 0 1 2 3 4 5 6 7 8 9 10 11] <class 'numpy.ndarray'>
A:矩阵转化为数组
from numpy import * a=[[1,2],[3,4]] b=mat(a) print(b.flatten(),type(b.flatten())) print(b.flatten().A,type(b.flatten().A)) print(b.flatten().A[0])
输出:
[[1 2 3 4]] <class 'numpy.matrixlib.defmatrix.matrix'> [[1 2 3 4]] <class 'numpy.ndarray'> [1 2 3 4]