python - 点的聚类采样
问题描述
考虑范围 [0,100]。假设我想从这个范围内采样 N 个点,但我想将我的样本“聚集”在 50 左右(或范围内的任何数字),并且越接近 0 或 100,采样的点就越少。
我怎样才能在 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)]
推荐阅读
- google-cloud-platform - google cloud firestore datastore模式下实体组对事务的影响
- javascript - html表格水平滚动启用特定列
- c# - .Net Core System.Text.Json 以最小日期时间和 UTC 偏移量失败
- twilio - Twilio 出站 IVR
- sql - 如何根据条件合并两个 SQL Server 表?
- .net - 不可为空的日期时间(不)相等与空编译和运行?
- pdf - 如何自动将特定工作表(即“SheetA”)作为 PDF 导出到 Google Drive?
- r - 为什么 gsub 会自动将 Factor 更改为 Character
- mysql - 如何在 Django 中修复“截断不正确的整数值:'UUID'”
- java - 当我们使用 WebElement 的 obj 方法 .getCssValue("font-family") 时,TestNG 的 SoftAsert.assertEquals(actual,expected) 方法无法正常运行