首页 > 解决方案 > 点的聚类采样

问题描述

考虑范围 [0,100]。假设我想从这个范围内采样 N 个点,但我想将我的样本“聚集”在 50 左右(或范围内的任何数字),并且越接近 0 或 100,采样的点就越少。

我怎样才能在 Python 中做到这一点?

谢谢

标签: python

解决方案


如果你的字面意思是指数级地减少点,一个便宜的解决方案可以像这样工作:

根据指数分布对值进行采样,将中心移至 50 并随机改变分布的方向:

import numpy as np

n = 1000
offset = 50
spread = np.random.exponential(scale=np.log(offset), size=n)
direction = (-1)**np.random.randint(2, size=n)

x = offset + spread * direction
# Trim the outliers
x = x[(x <= 100) & (x >= 0)]

推荐阅读