Matrix operations

Apart from element-wise calculations using the np.dot() function, you can also apply multiplications to your two-dimensional arrays based on matrix calculations, such as vector-matrix and matrix-matrix multiplications:

In: import numpy as np    M = np.arange(5*5, dtype=float).reshape(5,5)     MOut: array([[  0.,   1.,   2.,   3.,   4.],            [  5.,   6.,   7.,   8.,   9.],            [ 10.,  11.,  12.,  13.,  14.],            [ 15.,  16.,  17.,  18.,  19.],            [ 20.,  21.,  22.,  23.,  24.]])

As an example, we will create a 5 x 5 two-dimensional array of ordinal numbers from 0 to 24:

  1. We will define a vector of coefficients and an array column stacking the vector and its reverse:
In: coefs = np.array([1., 0.5, 0.5, 0.5, 0.5])    coefs_matrix = np.column_stack((coefs,coefs[::-1]))    print (coefs_matrix) ...

Get Python Data Science Essentials - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.