如何基于箱子创建矩阵?

时间:2017-09-28 14:13:02

标签: python arrays numpy bins

我有一组范围从3到27的值,它们有20个有限值:

A = [(0,21),(1,12),(2,15),(3,3),(4,21),(5,15),(6,27),(7,21),(8,9),(9,27),(10,12),(11,9),(12,12),(13,3),(14,9),(15,12),(16,6),(17,3),(18,9),(19,15)]

我想学习如何创建一个具有9个bin的numpy数组,每个bin对于给定的元组[1]具有-1和+1整数的范围,3的范围为3的倍数27(但这应该可以与整数和范围的任何其他组合互换)。最后,我希望能够创建一个看起来像这样的矩阵:

[[0,0,0,0,0,0,1,0,0],
 [0,0,0,1,0,0,0,0,0],
 [0,0,0,0,1,0,0,0,0],
 [1,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,1,0,0],
 [0,0,0,0,1,0,0,0,0],
 [0,0,0,0,0,0,0,0,1],
 ....]

我正在阅读numpy如何(num, bins) = histogram(x, bins=None, range=None),但我不太清楚该怎么做。

我原以为我必须遍历'A'来获取唯一值('a'),然后通过(a-1,a + 1)进行范围,以得到我想要的箱数只到len(unique_values)。但后来我迷路了。谁能指导我?

1 个答案:

答案 0 :(得分:2)

这是np.searchsorted / np.digitize -

的一种方式
bins = np.arange(3,28,3)
ar = np.asarray(A)[:,1] # or np.array([i[1] for i in A])
ids = np.searchsorted(bins, ar) # or np.digitize(ar,bins)-1
out = (ids[:,None] == np.arange(9)).astype(int)

获取最终输出的最后一步可以替换为array-initialization -

out = np.zeros((len(ids), 9),dtype=int)
out[np.arange(len(ids)), ids] = 1

如果元组中的第一个元素不是按顺序排列的,我们可能希望使用它们来索引到行 -

out[np.asarray(A)[:,0], ids] = 1

示例运行 -

In [205]: A
Out[205]: 
[(0, 21),
 (1, 12),
 (2, 15),
 (3, 3),
 (4, 21),
 (5, 15),
 (6, 27),
 (7, 21),
 (8, 9),
 (9, 27),
 (10, 12),
 (11, 9),
 (12, 12),
 (13, 3),
 (14, 9),
 (15, 12),
 (16, 6),
 (17, 3),
 (18, 9),
 (19, 15)]

In [206]: out[:7] # first 7 rows of output
Out[206]: 
array([[0, 0, 0, 0, 0, 0, 1, 0, 0],
       [0, 0, 0, 1, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 0, 0, 0, 0],
       [1, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 1, 0, 0],
       [0, 0, 0, 0, 1, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 1]])