扁平化numpy数组,还能保留值位置的索引? [英] Flatten numpy array but also keep index of value positions?
问题描述
我有几个2D numpy数组(矩阵),我想将其转换为包含数组值的向量和包含每个行/列索引的向量.
I have several 2D numpy arrays (matrix) and for each one I would like to convert it to vector containing the values of the array and a vector containing each row/column index.
例如,我可能有一个像这样的数组:
For example I might have an array like this:
x = np.array([[3, 1, 4],
[1, 5, 9],
[2, 6, 5]])
我基本上想要这些值
[3, 1, 4, 1, 5, 9, 2, 6, 5]
及其位置
[[0,0], [0,1], [0,2], [1,0], [1,1], [1,2], [2,0], [2,1], [2,2]]
我的最终目标是将它们像这样的列放入pandas DataFrame中:
My end goal is to put these into a pandas DataFrame as columns like this:
V | x | y
--+---+---
3 | 0 | 0
1 | 0 | 1
4 | 0 | 2
1 | 1 | 0
5 | 1 | 1
9 | 1 | 2
6 | 2 | 0
5 | 2 | 1
3 | 2 | 2
其中V是值,x是行位置(索引),y是列位置(索引).
where V is the value, x is the row position (index), and y is the column position (index).
我认为我可以一起破解一些东西,但我正在尝试找到一种有效的方法来完成此任务,而不是四处摸索.例如,我知道我可以使用类似x.reshape(x.size, 1)
的值来获取值,并且可以尝试从x.shape
创建索引列,但是似乎应该有更好的方法.
I think I can hack something together but I'm trying to find the efficient way of doing this rather than fumbling around. For example I know I can get the values using something like x.reshape(x.size, 1)
and that I could try to create the index columns from x.shape
, but there seems like there should be a better way.
推荐答案
I don't know if it's most efficient, but numpy.meshgrid
is designed for this:
x = np.array([[3, 1, 4],
[1, 5, 9],
[2, 6, 5]])
XX,YY = np.meshgrid(np.arange(x.shape[1]),np.arange(x.shape[0]))
table = np.vstack((x.ravel(),XX.ravel(),YY.ravel())).T
print table
这将产生:
[[3 0 0]
[1 1 0]
[4 2 0]
[1 0 1]
[5 1 1]
[9 2 1]
[2 0 2]
[6 1 2]
[5 2 2]]
然后我认为df = pandas.DataFrame(table)
将为您提供所需的数据框.
Then I think df = pandas.DataFrame(table)
will give you your desired data frame.
这篇关于扁平化numpy数组,还能保留值位置的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!