python生成等距数组_python:生成间距不均匀的数组

I would like to generate an array bounded by a lower and upper value with n elements similar to:

def my_lin(lb, ub, steps):

dx = (ub-lb) / (steps-1)

return [lb + i*dx for i in range(steps)]

my_lin(0,10,11)

But I would like to have more values closer to the lower value. Some kind of harmonic spacing. I do not want to have a logarithmic spacing.

I guess it is rather simple but I cannot figure it out. Any help is highly appreciated.

EDIT:

I came up with following quick solution:

def harm_series(lb,n):

return [float(lb)/float(i) for i in range(1,n) ]

解决方案

Find a function f which has your desired spacing within [0,1] (note that f(0)==0 and f(1)==1 should hold) and then apply f(i*dx/(ub-lb))*(ub-lb).

I personally like power functions for this but there is many more.

For example, try this:

def my_lin(lb, ub, steps, spacing=1.1):

span = (ub-lb)

dx = 1.0 / (steps-1)

return [lb + (i*dx)**spacing*span for i in range(steps)]

lin = my_lin(0, 10, 11)

print lin

# visualize the spacing

print ''.join(' '*int(2*a)+'*' for a in lin)

Prints:

[0.0, 0.7943282347242814, 1.702679845041569, 2.6597044516956405, 3.6497741462219233, 4.665164957684037, 5.701201299034059, 6.754727665838724, 7.823462148343428, 8.905673323855929, 10.0]

* * * * * * * * * * *

Now with the parameter spacing it works like this:

The closer you get to zero, the more your values gather around ub

The farther you go away from 1 into the other direction, the more they gather around lb

If spacing is exactly 1, they are equally spaced

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值