DEV Community

Cover image for Matrix multiplication in Python
Vidyasagar SC Machupalli
Vidyasagar SC Machupalli

Posted on Edited on

Matrix multiplication in Python

Learn how to do matrix multiplication in Python using @ operator.

@ operator is supported in Python version 3.5 and above. The operator works on ndarrays

# Matrix multiplication using @
import numpy as np
import random

# Generate a list
X = [[i for i in random.sample(range(0, 10), 3)] for i in range(3)]
Y = [[i for i in random.sample(range(0, 10), 3)] for i in range(3)]

# You can use numpy
X = np.array(X)
Y = np.array(Y)

Z = X @ Y
print(Z)

Enter fullscreen mode Exit fullscreen mode

@vidyasagar Machupalli

Top comments (0)