2015-06-26 39 views
1

我有几个二维numpy阵列(矩阵),我想将它转换为包含数组值和包含每个行/列索引的向量的向量。平坦numpy数组,但也保持价值位置的指数?

比如我可能有一个这样的数组:

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]] 

我的最终目标是把这些像熊猫这样的DataFrame这样的列:

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是列位置(索引)。

我想我可以一起破解一些东西,但我试图找到这样做的有效方式,而不是摸索。例如,我知道我可以使用类似x.reshape(x.size, 1)的值来获取值,并且我可以尝试从x.shape创建索引列,但似乎应该有更好的方法。

+0

我想'reshape'执行时间不变并且为了创建索引,你只需要一个'for'。 – Tempux

+0

你是什么意思只有一个'for'?我明白,“重塑”的效率大致如此。 – user12202013

回答

6

我不知道这是否是最有效的,但numpy.meshgrid是专为这样的:

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)会给你想要的数据帧。

0

您可以简单地使用循环。

x = np.array([[3, 1, 4], 
       [1, 5, 9], 
       [2, 6, 5]]) 
values = [] 
coordinates = [] 
data_frame = [] 
for v in xrange(len(x)): 
    for h in xrange(len(x[v])): 
     values.append(x[v][h]) 
     coordinates.append((h, v)) 
     data_frame.append(x[v][h], h, v) 
     print '%s | %s | %s' % (x[v][h], v, h) 
0

你可以试试这个使用itertools

import itertools 
import numpy as np 
import pandas as pd 

def convert2dataframe(array): 
    a, b = array.shape 
    x, y = zip(*list(itertools.product(range(a), range(b)))) 
    df = pd.DataFrame(data={'V':array.ravel(), 'x':x, 'y':y}) 
    return df 

这适用于任何形状的阵列,不一定方阵。

0

另一种方式:

arr = np.array([[3, 1, 4], 
       [1, 5, 9], 
       [2, 6, 5]]) 

# build out rows array 
x = np.arange(arr.shape[0]).reshape(arr.shape[0],1).repeat(arr.shape[1],axis=1) 
# build out columns array 
y = np.arange(arr.shape[1]).reshape(1,arr.shape[0]).repeat(arr.shape[0],axis=0) 

# combine into table 
table = np.vstack((arr.reshape(arr.size),x.reshape(arr.size),y.reshape(arr.size))).T 
print table 
1

你也可以让熊猫做的工作适合你,因为你将在一个数据帧使用它:

x = np.array([[3, 1, 4], 
       [1, 5, 9], 
       [2, 6, 5]]) 
df=pd.DataFrame(x) 
#unstack the y columns so that they become an index then reset the 
#index so that indexes become columns. 
df=df.unstack().reset_index() 
df 

    level_0 level_1 0 
0  0  0 3 
1  0  1 1 
2  0  2 2 
3  1  0 1 
4  1  1 5 
5  1  2 6 
6  2  0 4 
7  2  1 9 
8  2  2 5 

#name the columns and switch the column order 
df.columns=['x','y','V'] 
cols = df.columns.tolist() 
cols = cols[-1:] + cols[:-1] 
df = df[cols] 
df 

    V x y 
0 3 0 0 
1 1 0 1 
2 2 0 2 
3 1 1 0 
4 5 1 1 
5 6 1 2 
6 4 2 0 
7 9 2 1 
8 5 2 2 
+0

Wha?你可以这样做?! – user12202013