I have a large numpy matrix 'mat' of size (150,000 * 150,000). I'm trying to apply a function on each element of this numpy array. The function uses a dictionary whose key range from 0 to 149,999:
Step1:
Converting the 1-d array to a dictionary
dict1 = dict(enumerate(arr)) # arr is a 1d array of size (150,000*1)
step 2
define the function:
def find_res(mat): #Psuedo code
for each element 'e' in the array, obtain the row index i and column index j
return e/(dict1[i]*dict1[j])
Vectorizing the function and finding results
vfunc = np.vectorize(find_res)
res = vfunc(mat)
I'm wondering how to actually define the find_res function. Maybe their's a better way to do this.
Sample data
arr = np.array([5,10,20])
dict1 = dict(enumerate(arr))
print(dict1)
-> {0:5, 1:10, 2:20}
print(mat)
->
[[1 1 1]
[4 4 4]
[6 6 6]]
output:
print(vfunc(mat))
->
[[0.04 0.02 0.01]
[0.08 0.04 0.02 ]
[0.06 0.03 0.015]]
The mat size is 3*3 in here, but the original mat size is 150000 * 150000
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…