如何以递减方式拉伸numpy数组的特定项目? [英] How to stretch specific items of numpy array with decrement?

查看:96
本文介绍了如何以递减方式拉伸numpy数组的特定项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出边界值 k ,是否有矢量化的方法,可以用从 n-1 n > k ?例如,如果 k 为0,我想将 np.array([3,4,2,2,1,3,1])替换为 np.array([2,1,0,3,2,1,0,1,0,1,0,0,2,1,0,0]).输入数组的每个项目都大于 k .

我尝试将 np.repeat np.cumsum 组合使用,但似乎是回避性的解决方案:

  x = np.array([3,4,2,2,1,3,1])y = np.repeat(x,x)t = -np.ones(y.shape [0])t [np.r_ [0,np.cumsum(x)[:-1]]] = x-1np.cumsum(t) 

还有其他方法吗?我希望像 np.add.reduceat 的逆一样的东西能够将整数广播为递减的序列,而不是使它们最小化.

解决方案

这是另一种使用数组分配的方法来跳过重复部分-

  def func1(a):l = a.sum()out = np.full(l,-1,dtype = int)out [0] = a [0] -1idx = a.cumsum()[:-1]out [idx] = a [1:]-1返回out.cumsum() 

基准化

 #OP的解决方案定义OP(x):y = np.repeat(x,x)t = -np.ones(y.shape [0],dtype = int)t [np.r_ [0,np.cumsum(x)[:-1]]] = x-1返回np.cumsum(t) 

使用

扩展为将 k 作为arg

  def func1(a,k):l = a.sum()+ len(a)*(-k)out = np.full(l,-1,dtype = int)out [0] = a [0] -1idx =(a-k).cumsum()[:-1]out [idx] = a [1:]-1-k返回out.cumsum() 

样品运行-

 在[120]中:Out [120]:数组([3,4,2,2,1,3,1])在[121]中:func1(a,k = -1)出[121]:数组([2,1,0,-1,3,2,1,0,-1,1,0,-1,1,0,-1,0,-1,2,1,0,-1,0,-1]) 

Given boundary value k, is there a vectorized way to replace each number n with consecutive descending numbers from n-1 to k? For example, if k is 0 the I'd like to replace np.array([3,4,2,2,1,3,1]) with np.array([2,1,0,3,2,1,0,1,0,1,0,0,2,1,0,0]). Every item of input array is greater than k.

I have tried combination of np.repeat and np.cumsum but it seems evasive solution:

x = np.array([3,4,2,2,1,3,1])
y = np.repeat(x, x)
t = -np.ones(y.shape[0])
t[np.r_[0, np.cumsum(x)[:-1]]] = x-1
np.cumsum(t)

Is there any other way? I expect smth like inverse of np.add.reduceat that is able to broadcast integers to decreasing sequences instead of minimizing them.

解决方案

Here's another way with array-assignment to skip the repeat part -

def func1(a):
    l = a.sum()
    out = np.full(l, -1, dtype=int)
    out[0] = a[0]-1
    idx = a.cumsum()[:-1]
    out[idx] = a[1:]-1
    return out.cumsum()

Benchmarking

# OP's soln
def OP(x):
    y = np.repeat(x, x)
    t = -np.ones(y.shape[0], dtype=int)
    t[np.r_[0, np.cumsum(x)[:-1]]] = x-1
    return np.cumsum(t)

Using benchit package (few benchmarking tools packaged together; disclaimer: I am its author) to benchmark proposed solutions.

import benchit

a = np.array([3,4,2,2,1,3,1])
in_ = [np.resize(a,n) for n in [10, 100, 1000, 10000]]
funcs = [OP, func1]
t = benchit.timings(funcs, in_)
t.plot(logx=True, save='timings.png')

Extend to take k as arg

def func1(a, k):
    l = a.sum()+len(a)*(-k)
    out = np.full(l, -1, dtype=int)
    out[0] = a[0]-1
    idx = (a-k).cumsum()[:-1]
    out[idx] = a[1:]-1-k
    return out.cumsum()

Sample run -

In [120]: a
Out[120]: array([3, 4, 2, 2, 1, 3, 1])

In [121]: func1(a, k=-1)
Out[121]: 
array([ 2,  1,  0, -1,  3,  2,  1,  0, -1,  1,  0, -1,  1,  0, -1,  0, -1,
        2,  1,  0, -1,  0, -1])

这篇关于如何以递减方式拉伸numpy数组的特定项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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