python - 从不同大小的较小数组构造单个 numpy 数组

标签 python arrays optimization numpy

我有一组值,x。给定“开始”和“停止”索引,我需要使用 x 的子数组构造一个数组 y

import numpy as np
x = np.arange(20)
start = np.array([2, 8, 15])
stop = np.array([5, 10, 20])
nsubarray = len(start)

我希望 y 的位置:

y = array([ 2,  3,  4,  8,  9, 15, 16, 17, 18, 19])

(实际上我使用的数组要大得多)。

构造 y 的一种方法是使用列表理解,但之后需要将列表展平:

import itertools as it
y = [x[start[i]:stop[i]] for i in range(nsubarray)]
y = np.fromiter(it.chain.from_iterable(y), dtype=int)

我发现使用 for 循环实际上更快:

y = np.empty(sum(stop - start), dtype = int)
a = 0
for i in range(nsubarray):
    b = a + stop[i] - start[i]
    y[a:b] = x[start[i]:stop[i]]
    a = b

我想知道是否有人知道我可以优化它的方法?非常感谢!

编辑

以下始终测试:

import numpy as np
import numpy.random as rd
import itertools as it


def get_chunks(arr, start, stop):
    rng = stop - start
    rng = rng[rng!=0]      #Need to add this in case of zero sized ranges
    np.cumsum(rng, out=rng)
    inds = np.ones(rng[-1], dtype=np.int)
    inds[rng[:-1]] = start[1:]-stop[:-1]+1
    inds[0] = start[0]
    np.cumsum(inds, out=inds)
    return np.take(arr, inds)


def for_loop(arr, start, stop):
    y = np.empty(sum(stop - start), dtype = int)
    a = 0
    for i in range(nsubarray):
        b = a + stop[i] - start[i]
        y[a:b] = arr[start[i]:stop[i]]
        a = b
    return y

xmax = 1E6
nsubarray = 100000
x = np.arange(xmax)
start = rd.randint(0, xmax - 10, nsubarray)
stop = start + 10

结果是:

In [379]: %timeit np.hstack([x[i:j] for i,j in it.izip(start, stop)])
1 loops, best of 3: 410 ms per loop

In [380]: %timeit for_loop(x, start, stop)
1 loops, best of 3: 281 ms per loop

In [381]: %timeit np.concatenate([x[i:j] for i,j in it.izip(start, stop)])
10 loops, best of 3: 97.8 ms per loop

In [382]: %timeit get_chunks(x, start, stop)
100 loops, best of 3: 16.6 ms per loop

最佳答案

这有点复杂,但相当快。基本上我们所做的是基于向量加法创建索引列表,并使用 np.take 而不是任何 python 循环:

def get_chunks(arr, start, stop):
     rng = stop - start
     rng = rng[rng!=0]      #Need to add this in case of zero sized ranges
     np.cumsum(rng, out=rng)
     inds = np.ones(rng[-1], dtype=np.int)
     inds[rng[:-1]] = start[1:]-stop[:-1]+1
     inds[0] = start[0]
     np.cumsum(inds, out=inds)
     return np.take(arr, inds)

检查它是否返回正确的结果:

xmax = 1E6
nsubarray = 100000
x = np.arange(xmax)
start = np.random.randint(0, xmax - 10, nsubarray)
stop = start + np.random.randint(1, 10, nsubarray)

old = np.concatenate([x[b:e] for b, e in izip(start, stop)])
new = get_chunks(x, start, stop)
np.allclose(old,new)
True

一些时间:

%timeit np.hstack([x[i:j] for i,j in zip(start, stop)])
1 loops, best of 3: 354 ms per loop

%timeit np.concatenate([x[b:e] for b, e in izip(start, stop)])
10 loops, best of 3: 119 ms per loop

%timeit get_chunks(x, start, stop)
100 loops, best of 3: 7.59 ms per loop

关于python - 从不同大小的较小数组构造单个 numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22326882/

相关文章:

c++ - 如何验证接收到的字节数组的长度,它不是空终止的?

python - 如何在类实例上正确执行算术?例如。对 House 类实例列表的 n_people 属性求和

python - 如何获取当前模块中某个类的所有实例

python - 打破全局条件

编译器警告将结构数组(结构本身的成员)传递给函数

arrays - 如何快速获取二维数组中的第一个维度

python - 按行索引 NumPy 数组

python - 如何在 Scikit-Learn (sklearn) 中将 `log_loss` 中的 `GridSearchCV` 与多类标签一起使用?

c# - 如何在不并行化的情况下使循环更快

python - 如何从sqlalchemy中的查询中获取列表