您可以使用Numpy线性空间在指定的时间间隔内得到均匀间隔的数字。
$ import numpy as np
$ np.linspace(0,10,5)
>>> array([ 0. , 2.5, 5. , 7.5, 10. ])然而,我想要在我的间隔的开始和结束时取样更多的数字。例如,如果我的间隔是[0-10],我想要5个样本。一个很好的样本是:
>>> array([0, 1, 5, 9, 10])我知道有人可能会说有很多方法可以对这个空间进行示例,例如:[0, 0.5, 5, 9.5, 10]是另一个很好的示例。我不介意如何取样,我只对抽样方法感兴趣,这些方法在我的样本空间开始和结束时返回更多的样本。
一种解决方案是从高斯分布中抽取指数,如果你得到一个接近分布平均值的数字,你就会在样本空间的开始或结束处画一个数字。然而,这个方法看起来比它所需要的要复杂得多,而且你也不能保证得到好的样本。
有谁知道在样本空间开始和结束时生成样本的好方法吗?
发布于 2019-07-18 20:32:20
这将为您提供更多的示例,以获得intervall的end:
np.sqrt(np.linspace(0,100,5))
array([ 0. , 5. , 7.07106781, 8.66025404, 10. ])您可以选择一个更高的指数,以获得更频繁的空隙走向终点。
为了获得更多的样本到 shift 和 end的中间空间,使原始的直线空间对称于0,然后将其移动。
通用函数:
def nonlinspace(xmin, xmax, n=50, power=2):
'''Intervall from xmin to xmax with n points, the higher the power, the more dense towards the ends'''
xm = (xmax - xmin) / 2
x = np.linspace(-xm**power, xm**power, n)
return np.sign(x)*abs(x)**(1/power) + xm + xmin示例:
>>> nonlinspace(0,10,5,2).round(2)
array([ 0. , 1.46, 5. , 8.54, 10. ])
>>> nonlinspace(0,10,5,3).round(2)
array([ 0. , 1.03, 5. , 8.97, 10. ])
>>> nonlinspace(0,10,5,4).round(2)
array([ 0. , 0.8, 5. , 9.2, 10. ])发布于 2019-07-19 00:37:46
您可以重新调整tanh以获得具有可调块性的序列:
import numpy as np
def sigmoidspace(low,high,n,shape=1):
raw = np.tanh(np.linspace(-shape,shape,n))
return (raw-raw[0])/(raw[-1]-raw[0])*(high-low)+low
# default shape parameter
sigmoidspace(1,10,10)
# array([ 1. , 1.6509262 , 2.518063 , 3.60029094, 4.8461708 ,
# 6.1538292 , 7.39970906, 8.481937 , 9.3490738 , 10. ])
# small shape parameter -> almost linear points
sigmoidspace(1,10,10,0.01)
# array([ 1. , 1.99995391, 2.99994239, 3.99995556, 4.99998354,
# 6.00001646, 7.00004444, 8.00005761, 9.00004609, 10. ])
# large shape paramter -> strong clustering towards the ends
sigmoidspace(1,10,10,10)
# array([ 1. , 1.00000156, 1.00013449, 1.01143913, 1.87995338,
# 9.12004662, 9.98856087, 9.99986551, 9.99999844, 10. ])https://stackoverflow.com/questions/57102014
复制相似问题