3
import numpy
A = numpy.array([
  [0,1,1],
  [2,2,0],
  [3,0,3]
])

B = numpy.array([
  [1,1,1],
  [2,2,2],
  [3,2,9],
  [4,4,4],
  [5,9,5]
])

Dimension of A: N * N(3*3)

Dimension of B: K * N(5*3)

Expected result is: C = [ A * B[0], A * B[1], A * B[2], A * B[3], A * B[4]] (Dimension of C is also 5*3)

I am new to numpy and not sure how to perform this operation without using for loops.

Thanks!

Paul H
  • 65,268
  • 20
  • 159
  • 136
Phoebe
  • 121
  • 7
  • 1
    In your example, what does `A * B[0]` evaluate to? – Paul H Oct 29 '19 at 05:36
  • 1
    Should the output shape be `(5, 3, 3)` – U13-Forward Oct 29 '19 at 05:37
  • Read [here](https://stackoverflow.com/questions/34142485/difference-between-numpy-dot-and-python-3-5-matrix-multiplication) too, maybe it will help to clarify how it works. – Denis Rasulev Oct 29 '19 at 05:41
  • Basically, it is just A*BT. So np.matmul(A,B.transpose()) will give what you want – Mayan Oct 29 '19 at 05:41
  • A * B[0] should evaluate to [[0,1,1],[2,2,0],[3,0,3]] * [1,1,1].T – Phoebe Oct 29 '19 at 05:42
  • 1
    Which is? I'm asking you to do math by hand and provide your expected output – Paul H Oct 29 '19 at 05:43
  • @Paul H really sorry for the confusion and thank you for asking. I was not being clear when I asked the question. By looking at pok fung Chan's answer, Output:[[2 4 6], [ 4 8 12],[11 10 36],[ 8 16 24],[14 28 30] is exactly what I want. – Phoebe Oct 29 '19 at 06:04

2 Answers2

5

By the math you provide, I think you are evaluating A times B transpose. If you want the resultant matrix to have the size 5*3, you can transpose it (equivalent to numpy.matmul(B.transpose(),A)).

import numpy
A = numpy.array([
  [0,1,1],
  [2,2,0],
  [3,0,3]
])

B = numpy.array([
  [1,1,1],
  [2,2,2],
  [3,2,9],
  [4,4,4],
  [5,9,5]
])

print(numpy.matmul(A,B.transpose()))
output :array([[ 2,  4, 11,  8, 14],
               [ 4,  8, 10, 16, 28],
               [ 6, 12, 36, 24, 30]])

for i in range(5):
    print (numpy.matmul(A,B[i]))
Output:
[2 4 6]
[ 4  8 12]
[11 10 36]
[ 8 16 24]
[14 28 30]
Mayan
  • 492
  • 4
  • 11
0

You can move forward like this:

import numpy as np

matrix_a = np.array([
    [0, 1, 1],
    [2, 2, 0],
    [3, 0, 3]
])

matrix_b = np.array([
    [1, 1, 1],
    [2, 2, 2],
    [3, 2, 9],
    [4, 4, 4],
    [5, 9, 5]
])

Remember: For matrix multiplication , Order of first Column of matrix-A == Order of first row of matrix-B - Such as: B -> (3, 3) == (3, 5), to get order of column and row of matrices, you can use:

rows_of_second_matrix = matrix_b.shape[0]
columns_of_first_matrix = matrix_a.shape[1]

Here, you can check whether Order of first Column of matrix-A == Order of first row of matrix-B or not. If order is not same then go for transpose of matrix-B, else simply multiply.

if columns_of_first_matrix != rows_of_second_matrix:

    transpose_matrix_b = np.transpose(matrix_b)

    output_1 = np.dot(matrix_a, transpose_matrix_b)
    print('Shape of dot product:', output_1.shape)
    print('Dot product:\n {}\n'.format(output_1))

    output_2 = np.matmul(matrix_a, transpose_matrix_b)
    print('Shape of matmul product:', output_2.shape)
    print('Matmul product:\n {}\n'.format(output_2))

    # In order to obtain -> Output_Matrix of shape (5, 3), Again take transpose

    output_matrix = np.transpose(output_1)
    print("Shape of required matrix: ", output_matrix.shape)

else:
    output_1 = np.dot(matrix_a, matrix_b)
    print('Shape of dot product:', output_1.shape)
    print('Dot product:\n {}\n'.format(output_1))

    output_2 = np.matmul(matrix_a, matrix_b)
    print('Shape of matmul product:', output_2.shape)
    print('Matmul product:\n {}\n'.format(output_2))

    output_matrix = output_2
    print("Shape of required matrix: ", output_matrix.shape)

Output:

   - Shape of dot product: (3, 5)
    Dot product:
     [[ 2  4 11  8 14]
     [ 4  8 10 16 28]
     [ 6 12 36 24 30]]

    - Shape of matmul product: (3, 5)
    Matmul product:
     [[ 2  4 11  8 14]
     [ 4  8 10 16 28]
     [ 6 12 36 24 30]]

    - Shape of required matrix:  (5, 3)
Muhammad Usman Bashir
  • 1,441
  • 2
  • 14
  • 43