python从2D数组到3D坐标数组

时间:2017-06-08 08:08:14

标签: python arrays performance numpy

我有一个名为a的mXn numpy数组: 我想编写一个函数,它返回一个大小为(3,mxn)的数组,该数组包含第一个数组中每对(x,y)的对应值。

import numpy as np

m=5
n=10
a = np.random.random((m, n))    
x = np.random.random((m, 1))                          # x coordinates
y = np.random.random((1, n))                          # y coordinates


b = np.empty((3, m*n))                # array to store coordinates
k=0
for i in range (0,m):
    for j in range (0,n):
        b[0,k] = a[i,0]
        b[1,k] = a[0,j]
        b[2,k] = a[i,j]
        k=k+1

这似乎运行正常,但有更快或更好的编码方式来做到这一点?

1 个答案:

答案 0 :(得分:3)

步骤:

  • 初始化3D数组,以使mn分开。这让我们可以播放价值。

  • 沿着第一个输出轴索引前三个元素,并将适当的元素关闭a,并确保这些形状是可广播的。

  • 将输出重新整形为2D

这就是所有游戏都在这里!这是矢量化实现 -

b_out = np.empty((3, m,n),dtype=a.dtype)  # 1. Initialize 
b_out[0] = a[:,0,None]                    # 2. Assign
b_out[1] = a[0]
b_out[2] = a
b_out.shape = (3,m*n)                     # 3. Reshape back to 2D

运行时测试

方法 -

def loopy_app(a):
    m,n = a.shape
    b = np.empty((3, m*n),dtype=a.dtype)
    k=0
    for i in range (0,m):
        for j in range (0,n):
            b[0,k] = a[i,0]
            b[1,k] = a[0,j]
            b[2,k] = a[i,j]
            k=k+1
    return b

def vectorized_app(a):
    b_out = np.empty((3, m,n),dtype=a.dtype)  
    b_out[0] = a[:,0,None]
    b_out[1] = a[0]
    b_out[2] = a
    b_out.shape = (3,m*n)
    return b_out

计时 -

In [194]: m=5
     ...: n=10
     ...: a = np.random.random((m, n))
     ...: 

In [195]: %timeit loopy_app(a)
     ...: %timeit vectorized_app(a)
     ...: 
10000 loops, best of 3: 28.2 µs per loop
100000 loops, best of 3: 2.48 µs per loop

In [196]: m=50
     ...: n=100
     ...: a = np.random.random((m, n))
     ...: 

In [197]: %timeit loopy_app(a)
     ...: %timeit vectorized_app(a)
     ...: 
100 loops, best of 3: 2.56 ms per loop
100000 loops, best of 3: 6.31 µs per loop

In [198]: 2560/6.31
Out[198]: 405.7052297939778

400x+ 加速大型数据集,更多关于大型数据集!