英文:
Compare number in numpy array
问题
假设我有一个数组,其中存储了总共 800,000 个类型为 'int8' 的整数。我想比较数组中每对数字,如果这两个位置上的数字相等,结果应为 1,如果这两个位置上的数字不相等,结果应为 0。结果应存储在一个大小为 (800000,800000)、类型为 'int8' 的数组中。有哪些高效的方法可以在优化速度和最小化内存使用的情况下计算这个结果?
示例:
数组 = [52, 41, 62 , 52]
结果
[1,0,0,1
0,1,0,0
0,0,1,0
1,0,0,1]
英文:
Suppose I have an array that stores a total of 800,000 integers of type 'int8'. I want to compare every pair of numbers in the array, and the result should be 1 if the numbers in those two positions are equal, and 0 if the numbers in those two positions are not equal. The results should be stored in an array of size (800000,800000) of type 'int8'. What are some efficient ways to compute this while optimizing speed and minimizing memory usage?
Ex.
array = [52, 41, 62 , 52]
result
[1,0,0,1
0,1,0,0
0,0,1,0
1,0,0,1]
答案1
得分: 1
建立一个映射:值 -> 索引,其中每个值指向一个包含该值的索引数组。在线性时间内解析你的数组并填充你的映射。
现在,你已经拥有了一切必要的东西来轻松打印你的数组,但你的存储只有线性的(通过打印或存储为 n^2 矩阵是 O(n^2),不能更高效)。
例如:
数组 = [52, 41, 62 , 52]
映射 = {52 => [0, 3],
41 => [1],
62 => [2]}
要打印,遍历你的映射中的索引数组。给定的索引数组将生成与数组中的值一样多的行,数组中的每个值都为1,其他值都为0。输出放在与数组中的索引对应的行中。
例如:
52 => [0, 3] 给我们行 [1, 0, 0, 1],这将是第0行和第3行。
41 => [1] 给我们行 [0, 1, 0, 0],这将是第1行。
62 => [2] 给我们行 [0, 0, 1, 0],这将是第2行。
英文:
Built a map: val -> indices, where each value points to an array of the indices that hold that value. Parse your array and fill your map in linear time.
Now, you have everything you need to easily print your array, but you're storage is only linear (through printing or storing as an n^2 matrix is O(n^2), which can't be made more efficient).
E.g.
array = [52, 41, 62 , 52]
map = {52 => [0, 3],
41 => [1],
62 => [2]}
To print, go through the arrays of indices of your map. A given index array will generate as many rows as there are values in the array, with a 1 for each value in the array and a 0 for each other value. The output goes in the rows corresponding to the indices in the array.
E.g.
52 => [0, 3] gives us the row [1, 0, 0, 1], which will be the 0th and 3rd row.
41 => [1] gives us the row [0, 1, 0, 0], which will be the 1st row
62 => [2] gives us the row [0, 0, 1, 0], which will be the 2nd row.
答案2
得分: 0
同意评论中的看法,取决于您想要实现什么,您可能需要重新考虑这个问题,但这里是您提出的具体问题的解决方案,并参考了[此答案][1]:
import numpy as np
tst_arr = np.array([1,2,3,1]).astype('int8')
## 创建所有排列的数组
perm_arr = np.empty((tst_arr.size, tst_arr.size, 2), dtype=tst_arr.dtype)
perm_arr[..., 0] = tst_arr[:, None]
perm_arr[..., 1] = tst_arr
## 创建二进制数组以检查值是否相等
check_arr = np.zeros((tst_arr.size, tst_arr.size)).astype('int8')
check_arr = np.where(perm_arr[:,:,0]==perm_arr[:,:,1], 1, 0)
print(check_arr)
[[1 0 0 1]
[0 1 0 0]
[0 0 1 0]
[1 0 0 1]]
[1]: https://stackoverflow.com/questions/27323448/numpy-array-to-permutation-matrix
英文:
Agreed with the comments that depending on what you are trying to accomplish you may want to rethink the problem, but here is a solution to the specific question you asked, and borrowing from this answer:
import numpy as np
tst_arr = np.array([1,2,3,1]).astype('int8')
## create array of all permutations
perm_arr = np.empty((tst_arr.size, tst_arr.size, 2), dtype=tst_arr.dtype)
perm_arr[..., 0] = tst_arr[:, None]
perm_arr[..., 1] = tst_arr
## create binary array that checks if values are equal
check_arr = np.zeros((tst_arr.size, tst_arr.size)).astype('int8')
check_arr = np.where(perm_arr[:,:,0]==perm_arr[:,:,1], 1, 0)
print(check_arr)
[[1 0 0 1]
[0 1 0 0]
[0 0 1 0]
[1 0 0 1]]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论