简体   繁体   中英

How to multiply elements in an array with each elements in another array using Python

I need help for my project. I have two arrays wherein I have to multiply the elements of array1 for each elements in array2 .

As an example,

pop_i = [[1, 0, 1]
         [0, 0, 1]
         [1, 1, 0]]

r_q = [[3, 5, 2], [5, 4, 3], [5, 2, 2]] 

What I did first is to arrange r_q to become the array that I wanted.

# simply arranging the values by means of transposition or using zip
r_q = [[3, 5, 5], [5, 4, 2], [2, 3, 2]]

What I need to do now is to multiply the elements in r_q with each elements in pop_i, like:

r_10 = [3, 5, 5] * [1, 0, 1]
r_11 = [3, 5, 5] * [0, 0, 1]
r_12 = [3, 5, 5] * [1, 1, 0]

r_20 = [5, 4, 2] * [1, 0, 1]
r_21 = [5, 4, 2] * [0, 0, 1]
r_22 = [5, 4, 2] * [1, 1, 0]

r_30 = [2, 3, 2] * [1, 0, 1]
r_31 = [2, 3, 2] * [0, 0, 1]
r_32 = [2, 3, 2] * [1, 1, 0]

Afterwards, get their sums.

# r_1_sum = [3*1 + 5*0 + 5*1, 3*0 + 5*0 + 5*1, 3*1 + 5*1 + 5*0] and so on...

r_1_sum = [8, 5, 8]
r_2_sum = [7, 2, 9]
r_3_sum = [4, 2, 5]

I am having a hard time multiplying r_q with each elements in pop_i. So far, my code looks like this:

def fitness_score(g, u):
   # arrange resource demand of r_q 
   result = numpy.array([lst for lst in zip(*r_q)])

   # multiply elements in r_q with each elements in pop_i
   for i in range(0, len(result)):
      multiplied_output = numpy.multiply(result[i], pop_i)
   print(multiplied_output)

   for x in in range(0, len(multiplied_output)):
      final = numpy.sum(multiplied_output[x])

But I keep getting answer for the last index in r_q. I think the multiplication part is wrong. Any help/suggestion would be very much appreciated. Thank you so much!

Assuming,

pop_i = [[1, 0, 1],[0, 0, 1],[1, 1, 0]]
r_q = [[3, 5, 2], [5, 4, 3], [5, 2, 2]] 

Use:

matrix = []
for row in zip(*r_q):
    temp = []
    for col in zip(*pop_i):
        temp.append(sum([x*y for x, y in zip(row, col)]))
    matrix.append(temp)

r_1_sum, r_2_sum, r_3_sum = matrix

Or, better use the numpy approach,

import numpy as np

a1 = np.array(pop_i)
a2 = np.array(r_q)
a = a1 @ a2
r_1_sum, r_2_sum, r_3_sum = a.T.tolist()

Result:

[8, 5, 8] # r_1_sum
[7, 2, 9] # r_2_sum
[4, 2, 5] # r_3_sum

You can get the desired result by using numpy dot function.

import numpy as np

pop_i = np.array([[1, 0, 1],[0, 0, 1],[1, 1, 0]])

r_q = np.array([[3, 5, 2], [5, 4, 3], [5, 2, 2]])

result = np.dot(np.transpose(r_q), pop_i)

Refer numpy.dot documentation.

Sample code from the link for reference:

# Program to multiply two matrices using nested loops
# 3x3 matrix
X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]
# 3x4 matrix
Y = [[5,8,1,2],
    [6,7,3,0],
    [4,5,9,1]]
# result is 3x4
result = [[0,0,0,0],
         [0,0,0,0],
         [0,0,0,0]]

# iterate through rows of X
for i in range(len(X)):
   # iterate through columns of Y
   for j in range(len(Y[0])):
       # iterate through rows of Y
       for k in range(len(Y)):
           result[i][j] += X[i][k] * Y[k][j]

for r in result:
   print(r)

Check if this link helps you: https://www.programiz.com/python-programming/examples/multiply-matrix . (It doesn't have the direct answer, but uses the same logic that you are trying.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM