【发布时间】:2018-06-01 07:47:16
【问题描述】:
假设我想要一个大小为(n,m) 的numpy 数组,其中n 非常大,但有很多重复,即。 0:n1 相同,n1:n2 相同等(与n2%n1!=0,即不是定期间隔)。有没有办法在查看整个数组的同时为每个重复项只存储一组值?
示例:
unique_values = np.array([[1,1,1], [2,2,2] ,[3,3,3]]) #these are the values i want to store in memory
index_mapping = np.array([0,0,1,1,1,2,2]) # a mapping between index of array above, with array below
unique_values_view = np.array([[1,1,1],[1,1,1],[2,2,2],[2,2,2],[2,2,2], [3,3,3],[3,3,3]]) #this is how I want the view to look like for broadcasting reasons
我打算将数组(视图)乘以其他大小为(1,m) 的数组,然后取这个乘积的点积:
other_array1 = np.arange(unique_values.shape[1]).reshape(1,-1) # (1,m)
other_array2 = 2*np.ones((unique_values.shape[1],1)) # (m,1)
output = np.dot(unique_values_view * other_array1, other_array2).squeeze()
输出是一个长度为n的一维数组。
【问题讨论】:
-
您打算如何使用输出?仅供参考:第一阶段的视图,不保证以后不会强制复制。
-
@Divakar 真的。我计划与另一个形状为
(1,m)的数组相乘,然后将点积与另一个数组一起存储。主要考虑因素是将数组安装到内存中。如果强制复制,我可以分块完成最后一步 -
或者可以添加一些更通用的最小样本,比如
index_mapping具有更大的数字范围,unique_values具有随机数? -
“相同”是指它们是整数吗?或者只是非常接近的浮动? (即你能做按位等价吗)
-
@M.T Yakym Pirozhenko 的解决方案似乎适用于列出的最小案例。但同样,我看不出通用案例是什么。
标签: python numpy array-broadcasting