Python Numpy 矩阵到数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3337301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Numpy matrix to array
提问by yassin
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.
我正在使用 numpy。我有一个有 1 列和 N 行的矩阵,我想从 N 个元素中获取一个数组。
For example, if i have M = matrix([[1], [2], [3], [4]]), I want to get A = array([1,2,3,4]).
例如,如果我有M = matrix([[1], [2], [3], [4]]),我想得到A = array([1,2,3,4])。
To achieve it, I use A = np.array(M.T)[0]. Does anyone know a more elegant way to get the same result?
为了实现它,我使用A = np.array(M.T)[0]. 有谁知道获得相同结果的更优雅的方法?
Thanks!
谢谢!
采纳答案by Joe Kington
If you'd like something a bit more readable, you can do this:
如果你想要一些更具可读性的东西,你可以这样做:
A = np.squeeze(np.asarray(M))
Equivalently, you could also do: A = np.asarray(M).reshape(-1), but that's a bit less easy to read.
同样,您也可以这样做:A = np.asarray(M).reshape(-1),但这不太容易阅读。
回答by Pierre GM
Or you could try to avoid some temps with
或者你可以尝试避免一些临时工
A = M.view(np.ndarray)
A.shape = -1
回答by mvu
A, = np.array(M.T)
depends what you mean by elegance i suppose but thats what i would do
我想取决于你所说的优雅是什么意思,但这就是我要做的
回答by bubble
You can try the following variant:
您可以尝试以下变体:
result=np.array(M).flatten()
回答by hpaulj
回答by Kevad
np.array(M).ravel()
If you care for speed; But if you care for memory:
如果你在意速度;但如果你关心内存:
np.asarray(M).ravel()
回答by oracleyue
First, Mv = numpy.asarray(M.T), which gives you a 4x1 but 2D array.
首先,Mv = numpy.asarray(M.T),它为您提供了一个 4x1 的二维数组。
Then, perform A = Mv[0,:], which gives you what you want. You could put them together, as numpy.asarray(M.T)[0,:].
然后,执行A = Mv[0,:],它给你你想要的。你可以把它们放在一起,作为numpy.asarray(M.T)[0,:].
回答by Siraj S.
This will convert the matrix into array
这会将矩阵转换为数组
A = np.ravel(M).T
回答by Siddharth Satpathy
ravel()and flatten()functions from numpy are two techniques that I would try here. I will like to add to the posts made by Joe, Siraj, bubbleand Kevad.
来自 numpy 的ravel()和flatten()函数是我在这里尝试的两种技术。我想对Joe、Siraj、bubble和Kevad发表的帖子进行补充。
Ravel:
拉威尔:
A = M.ravel()
print A, A.shape
>>> [1 2 3 4] (4,)
Flatten:
压平:
M = np.array([[1], [2], [3], [4]])
A = M.flatten()
print A, A.shape
>>> [1 2 3 4] (4,)
numpy.ravel()is faster, since it is a library level function which does not make any copy of the array. However, any change in array A will carry itself over to the original array M if you are using numpy.ravel().
numpy.ravel()更快,因为它是一个库级函数,不会复制任何数组。但是,如果您使用的是numpy.ravel().
numpy.flatten()is slower than numpy.ravel(). But if you are using numpy.flatten()to create A, then changes in A will not get carried over to the original array M.
numpy.flatten()比 慢numpy.ravel()。但是,如果您使用的numpy.flatten()是创建 A,则 A 中的更改将不会延续到原始数组 M。
numpy.squeeze()and M.reshape(-1)are slower than numpy.flatten()and numpy.ravel().
numpy.squeeze()并且M.reshape(-1)比numpy.flatten()和慢numpy.ravel()。
%timeit M.ravel()
>>> 1000000 loops, best of 3: 309 ns per loop
%timeit M.flatten()
>>> 1000000 loops, best of 3: 650 ns per loop
%timeit M.reshape(-1)
>>> 1000000 loops, best of 3: 755 ns per loop
%timeit np.squeeze(M)
>>> 1000000 loops, best of 3: 886 ns per loop

