python: generate unevenly spaced array

匿名 (未验证) 提交于 2019-12-03 01:39:01

问题:

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

回答1:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!