2 回答

TA貢獻1773條經驗 獲得超3個贊
您正在查看的優化需要存儲p不同的n x n矩陣。如果您確實想嘗試一下,我可能會使用 scipy 的 C 擴展中稀疏矩陣中內置的所有功能。
import numpy as np
from scipy import sparse
arr = sparse.random(100,10000, format="csr", density=0.01)
xxt = arr @ arr.T
p_comps = [arr[:, i] @ arr.T[i, :] for i in range(arr.shape[1])]
def calc_weights(xxt, thetas, p_comps):
xxt = xxt.copy()
xxt.data = np.zeros(xxt.data.shape, dtype=xxt.dtype)
for i, t in enumerate(thetas):
xxt += (p_comps[i] * t)
return xxt
W = calc_weights(xxt, np.ones(10000), p_comps)
>>>(xxt.A == W.A).all()
True
這真的不太可能在 python 中很好地實現。在 C 語言中執行此操作可能會更幸運,或者使用對元素進行操作的嵌套循環編寫一些東西,并且可以使用 numba 進行 JIT 編譯。

TA貢獻1802條經驗 獲得超10個贊
第一個簡單的解決方案是注意輸出矩陣是對稱的:
n = X.shape[0]
intersection_dict = {}
for i in range(n):
for j in range(i,n): #note the edit here
indices = np.intersect1d(X[i].indices, X[j].indices)
intersection_dict[(i,j)] = indices
這將使您的計算量減少不到 2 倍
添加回答
舉報