为了账号安全,请及时绑定邮箱和手机立即绑定

使用 `numpy.take` 随机选择 2d 点

使用 `numpy.take` 随机选择 2d 点

慕码人8056858 2023-05-16 09:54:22
问题设置:points- 2Dnumpy.array的长度N。centroids-numpy.array我从算法中得到的2D K-Means,长度k< N。作为算法的质心初始化例程MLE,我想points从centroids.所需输出:形状为 (N, 2) 的A numpy.array ,由从中随机选择的 2D 点组成centroids我的努力:我试过将numpy.take与 一起使用,numpy.random.choice如中所示Code 1,但它没有返回所需的输出。代码 1:import numpy as npa = np.random.randint(1, 10, 10).reshape((5, 2))idx = np.random.choice(5, 20)np.take(a, idx)Out: array([6, 2, 3, 3, 8, 2, 5, 2, 6, 3, 3, 8, 6, 6, 6, 6, 8, 2, 6, 5])从numpy.take 文档页面我了解到它从展平数组中选择项目,这不是我需要的。对于如何完成此任务的任何想法,我将不胜感激。在此先感谢您的帮助。
查看完整描述

2 回答

?
烙印99

TA贡献1829条经验 获得超13个赞

一种方法是对索引进行采样,然后使用它来索引的第一个维度centroids:


idx = np.random.choice(np.arange(len(centroids)), size=len(a))


out = centroids[idx]


查看完整回答
反对 回复 2023-05-16
?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

是:


a = np.random.randint(1, 10, 10).reshape((5, 2))

n_sampled_points = 20

a[np.random.randint(0, a.shape[0], n_sampled_points)]

干杯。


查看完整回答
反对 回复 2023-05-16
  • 2 回答
  • 0 关注
  • 105 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信