Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

python 3.x - How to define a function on numpy array that uses array indexes to lookup a dictionary?

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think there is no need to create dictionary from 1-d array you can directly transform the arr by taking the outer product then you can divide the matrix by this transformed arr to get the final result:

mat / (arr[:, None] * arr)

array([[0.04 , 0.02 , 0.01 ],
       [0.08 , 0.04 , 0.02 ],
       [0.06 , 0.03 , 0.015]])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...