我尝试用数组中的数据用元组键更新dict:
myarray = np.array([[0, 0], # 0, 1
[0, 1],
[1, 1], # 1, 2
[1, 2], # 1, 3
[2, 2],
[1, 3]]
) # a lot of this with shape~(10e6, 2)
dict_with_tuples_key = {(0, 1): 1,
(3, 7): 1} # ~10e6 keys
使用数组存储dict值(感谢@MSeifert),我们得到:
def convert_dict_to_darray(dict_with_tuples_key, myarray):
idx_max_array = np.max(myarray, axis=0)
idx_max_dict = np.max(dict_with_tuples_key.keys(), axis=0)
lens = np.max([list(idx_max_array), list(idx_max_dict)], axis=0)
xlen, ylen = lens[0] + 1, lens[1] + 1
darray = np.zeros((xlen, ylen)) # Empty array to hold all indexes in myarray
for key, value in dict_with_tuples_key.items():
darray[key] = value
return darray
@njit
def update_darray(darray, myarray):
elements = myarray.shape[0]
for i in range(elements):
darray[myarray[i][0]][myarray[i][1]] += 1
return darray
def darray_to_dict(darray):
updated_dict = {}
keys = zip(*map(list, np.nonzero(darray)))
for x, y in keys:
updated_dict[(x, y)] = darray[x, y]
return updated_dict
darray = convert_dict_to_darray(dict_with_tuples_key, myarray)
darray = update_darray(darray, myarray)
我得到了所需的确切结果:
# print darray_to_dict(darray)
# {(0, 1): 2.0,
# (0, 0): 1.0,
# (1, 1): 1.0,
# (2, 2): 1.0,
# (1, 2): 1.0,
# (1, 3): 1.0,
# (3, 7): 1.0, }
对于小矩阵,它工作得很好,@njit工作得很快,
但是…
创建巨大的空
darray = np.zeros((xlen, ylen))
不适合内存。我们如何避免分配一个非常稀疏的数组,并且只以坐标格式存储非空值,比如稀疏矩阵? 最佳答案
Usedok_matrix
fromscipy
;adock_matrix
是基于键的稀疏矩阵字典它们允许您以增量方式构建稀疏矩阵,并且不会分配不适合您计算机内存的巨大空darray = np.zeros((xlen, ylen))
。
唯一要做的更改是从scipy导入正确的模块,并更改函数darray
中convert_dict_to_darray
的定义。
它将如下所示:
from scipy.sparse import dok_matrix
def convert_dict_to_darray(dict_with_tuples_key, myarray):
idx_max_array = np.max(myarray, axis=0)
idx_max_dict = np.max(dict_with_tuples_key.keys(), axis=0)
lens = np.max([list(idx_max_array), list(idx_max_dict)], axis=0)
xlen, ylen = lens[0] + 1, lens[1] + 1
darray = dok_matrix( (xlen, ylen) )
for key, value in dict_with_tuples_key.items():
darray[key[0], key[1]] = value
return darray