将numpy矩阵转换为python数组

将numpy矩阵转换为python数组,python,arrays,numpy,Python,Arrays,Numpy,有没有其他更好的方法可以将numpy矩阵转换为python数组 >>> import numpy >>> import array >>> b = numpy.matrix("1.0 2.0 3.0; 4.0 5.0 6.0", dtype="float16") >>> print(b) [[ 1. 2. 3.] [ 4. 5. 6.]] >>> a = array.array("f") >

有没有其他更好的方法可以将numpy矩阵转换为python数组

>>> import numpy
>>> import array
>>> b = numpy.matrix("1.0 2.0 3.0; 4.0 5.0 6.0", dtype="float16")
>>> print(b)
[[ 1.  2.  3.]
 [ 4.  5.  6.]]
>>> a = array.array("f")
>>> a.fromlist((b.flatten().tolist())[0])
>>> print(a)
array('f', [1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
您可以转换为并使用
.ravel()
.flant()
生成其展开版本。这也可以通过简单地使用函数本身来实现,因为它在引擎盖下执行这两个操作。最后,像这样使用它-

a = array.array('f',np.ravel(b))
样本运行-

In [107]: b
Out[107]: 
matrix([[ 1.,  2.,  3.],
        [ 4.,  5.,  6.]], dtype=float16)

In [108]: array.array('f',np.ravel(b))
Out[108]: array('f', [1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
以下是一个例子:

>>> x = np.matrix(np.arange(12).reshape((3,4))); x
matrix([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]])
>>> x.tolist()
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]