Numpy 矩阵到数组

发布于 2024-09-11 10:41:04 字数 242 浏览 3 评论 0原文

我正在使用numpy。我有一个 1 列 N 行的矩阵,我想从中获取一个包含 N 个元素的数组。

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

为了实现它,我使用 A = np.array(MT)[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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

早乙女 2024-09-18 10:41:04

如果你想要更具可读性的东西,你可以这样做:

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.

白况 2024-09-18 10:41:04

您可以尝试以下变体:

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

You can try the following variant:

result=np.array(M).flatten()
追星践月 2024-09-18 10:41:04
A, = np.array(M.T)

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

A, = np.array(M.T)

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

摇划花蜜的午后 2024-09-18 10:41:04
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()
并安 2024-09-18 10:41:04

或者你可以尝试避免一些临时情况

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
一瞬间的火花 2024-09-18 10:41:04

首先,Mv = numpy.asarray(MT),它为您提供一个 4x1 的二维数组。

然后,执行 A = Mv[0,:],这会给你你想要的。您可以将它们放在一起,如 numpy.asarray(MT)[0,:]。

First, Mv = numpy.asarray(M.T), which gives you a 4x1 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,:].

万水千山粽是情ミ 2024-09-18 10:41:04

这会将矩阵转换为数组

A = np.ravel(M).T

This will convert the matrix into array

A = np.ravel(M).T
他不在意 2024-09-18 10:41:04

ravel()flatten() 来自 numpy 的函数我将在这里尝试两种技术。我想添加 JoeSiraj气泡Kevad< /a>.

Ravel:

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() 更快,因为它是一个库级函数,不制作数组的任意副本。但是,如果您使用numpy.ravel(),则数组 A 中的任何更改都会转移到原始数组 M 中。

numpy.flatten()numpy.ravel() 慢。但是,如果您使用 numpy.flatten() 来创建 A,则 A 中的更改将不会转移到原始数组 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
缱倦旧时光 2024-09-18 10:41:04

来的有点晚了,希望对大家有帮助

np.array(M.flat)

Came in a little late, hope this helps someone,

np.array(M.flat)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文