OKPEDIA PYTHON EN

How to Multiply Matrices in Python

The dot function of the numpy library allows you to multiply two arrays in python through the product rows by columns.

import numpy as np
np.dot (m, n)

The arguments m and n are two matrix objects or vectors, previously defined with the array function.

The dot() function returns the product row by column of arrays.

An example of a product line by column

Example

The dot function requires importing the numpy library into the python interpreter

import numpy as np

Give two arrays A and B defined with the array function.

A = np.array ([[1,2], [3,4], [5,6]])
B = np.array ([[1,2,3], [3,4,5]])

The number of rows in the first array must be equal to the number of columns in the second array.

The product is calculated row by column using the dot () function.

np.dot (A, B)

This is the result in output of the function

array ([[7, 10, 13],
[15, 22, 29],
[23, 34, 45]])

The output matrix is ​​the result of the product between the rows of A for the rows of B.

the product row by column between two matrices

https://how.okpedia.org/en/python/how-to-multiply-two-arrays-matrix-in-python


Report us an error or send a suggestion to improve this page



FacebookTwitterLinkedinLinkedin