numpy的矩阵阵列 [英] Numpy matrix to array
本文介绍了numpy的矩阵阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用numpy的。我有1列和N行的矩阵,我想从N个元素得到一个数组。
I am using numpy. I have a matrix with 1 column and N rows and I want to get an array from with N elements.
例如,如果我有 M =矩阵([1],[2],[3] [4])
,我想获得 A =阵列([1,2,3,4])
。
For example, if i have M = matrix([[1], [2], [3], [4]])
, I want to get A = array([1,2,3,4])
.
要实现它,我用 A = np.array(M.T)[0]
。有谁知道一个更优雅的方式来获得同样的结果?
To achieve it, I use A = np.array(M.T)[0]
. Does anyone know a more elegant way to get the same result?
谢谢!
推荐答案
如果你想要一些更可读的,你可以这样做:
If you'd like something a bit more readable, you can do this:
A = np.squeeze(np.asarray(M))
等价地,你也可以这样做: A = np.asarray(M).reshape(-1)
,但是这是一个有点不太容易阅读
Equivalently, you could also do: A = np.asarray(M).reshape(-1)
, but that's a bit less easy to read.
这篇关于numpy的矩阵阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文