Numpy 矩阵到数组 [英] Numpy matrix to array

查看:28
本文介绍了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 = matrix([[1], [2], [3], [4]]),我想得到 A = array([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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆