【发布时间】:2015-10-21 06:31:17
【问题描述】:
给定 2 个数组:一个用于主数据集,第二个作为引用主数据集的分组索引列表。我正在寻找从给定索引数据生成新数组的最快方法?
这是我目前从双键列表生成 2 个数组的解决方案:
# Lets make a large point cloud with 1 million entries and a list of random paired indices
import numpy as np
COUNT = 1000000
POINT_CLOUD = np.random.rand(COUNT,3) * 100
INDICES = (np.random.rand(COUNT,2)*COUNT).astype(int) # (1,10),(233,12),...
# Split into sublists, np.squeeze is needed here because i don't want arrays of single elements.
LIST1 = POINT_CLOUD[np.squeeze(INDICES[:,[0]])]
LIST2 = POINT_CLOUD[np.squeeze(INDICES[:,[1]])]
这行得通,但它有点慢,而且它只适用于生成 2 个列表,如果有一个可以处理任何大小的索引组的解决方案(例如:((1,2,3,4) ,(8,4,5,3),...)
类似:
# PSEUDO CODE using quadruple keys
INDICES = (np.random.rand(COUNT,4)*COUNT).astype(int)
SPLIT = POINT_CLOUD[<some pythonic magic>[INDICES]]
SPLIT[0] = np.array([points from INDEX #1])
SPLIT[1] = np.array([points from INDEX #2])
SPLIT[2] = np.array([points from INDEX #3])
SPLIT[3] = np.array([points from INDEX #4])
【问题讨论】:
标签: python-2.7 numpy