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
264 views
in Technique[技术] by (71.8m points)

python 3.x - Creating a numpy matrix from a 1dim numpy array by performing computation on itself?

I have a numpy array 'arr' of shape (100000,). I need to create a numpy matrix 'res_matrix' of shape 100000X100000 such that

for i in range(res_matrix.shape[0]):
    for j in range(res_matrix.shape[1]):
        res_matrix[i][j]= arr[i]*arr[j]

Sample input/output


arr=[1 2 4]


Output:
res_matrix:

[[1 2 4]
 [2 4 18]
 [4 8 16]]

Is there way to do vectorize this operation, to reduce the computation time of calculating 00000X100000 in loop?


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

1 Reply

0 votes
by (71.8m points)

There are a few ways you can get an outer multiplication.

arr = np.array([1,2,4])

#Using Multiply outer
print(np.multiply.outer(arr, arr)) #As suggested by Warren

#Using broadcasting
print(arr[:,None] * arr[None,:]) #(3,1) * (1,3)
[[ 1  2  4]
 [ 2  4  8]
 [ 4  8 16]]

[[ 1  2  4]
 [ 2  4  8]
 [ 4  8 16]]

? Note, the output is still a very large matrix for storing in memory. Depending on what you need it for, I would advise considering something like a generator function. Let me know how you are going to use this matrix and I could suggest more memory efficient methods.


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

...