块矩阵到数组

问题:块矩阵到数组

我正在使用numpy。我有一个具有1列和N行的矩阵,并且我想从中获得具有N个元素的数组。

例如,如果我有M = matrix([[1], [2], [3], [4]]),我想得到A = array([1,2,3,4])

为此,我使用A = np.array(M.T)[0]。有谁知道一种更优雅的方式来获得相同的结果?

谢谢!

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.

For example, if i have M = matrix([[1], [2], [3], [4]]), I want to get 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?

Thanks!


回答 0

如果您想让内容更具可读性,可以执行以下操作:

A = np.squeeze(np.asarray(M))

同样,您也可以执行以下操作:A = np.asarray(M).reshape(-1),但是它不太容易阅读。

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.


回答 1


回答 2

A, = np.array(M.T)

我想,这取决于您所说的优雅的意思,但这就是我会做的

A, = np.array(M.T)

depends what you mean by elegance i suppose but thats what i would do


回答 3

您可以尝试以下变体:

result=np.array(M).flatten()

You can try the following variant:

result=np.array(M).flatten()

回答 4

np.array(M).ravel()

如果您在乎速度;但是,如果您关心内存:

np.asarray(M).ravel()
np.array(M).ravel()

If you care for speed; But if you care for memory:

np.asarray(M).ravel()

回答 5

或者您可以尝试避免与

A = M.view(np.ndarray)
A.shape = -1

Or you could try to avoid some temps with

A = M.view(np.ndarray)
A.shape = -1

回答 6

第一, Mv = numpy.asarray(M.T)您会得到一个4×1但2D的数组。

然后,执行A = Mv[0,:],这将为您提供所需的内容。您可以将它们放在一起,如numpy.asarray(M.T)[0,:]

First, Mv = numpy.asarray(M.T), which gives you a 4×1 but 2D array.

Then, perform A = Mv[0,:], which gives you what you want. You could put them together, as numpy.asarray(M.T)[0,:].


回答 7

这会将矩阵转换为数组

A = np.ravel(M).T

This will convert the matrix into array

A = np.ravel(M).T

回答 8

numpy的ravel()flatten()函数是我将在此处尝试的两种技术。我想补充一下JoeSirajbubbleKevad的帖子

拉威尔:

A = M.ravel()
print A, A.shape
>>> [1 2 3 4] (4,)

展平:

M = np.array([[1], [2], [3], [4]])
A = M.flatten()
print A, A.shape
>>> [1 2 3 4] (4,)

numpy.ravel()更快,因为它是库级别的函数,不会复制任何数组。但是,如果使用,则数组A中的任何更改都会将其自身带到原始数组M中numpy.ravel()

numpy.flatten()比慢numpy.ravel()。但是,如果你使用的是numpy.flatten()创建一个,然后改变了一个将不会延续到原来的列M

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

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, bubble and 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.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.squeeze() and M.reshape(-1) are slower than numpy.flatten() and 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