Matrix operations and functions on two-dimensional arrays
Basic matrix operations form the backbone of quite a few statistical analyses—for example, neural networks. In this section, we will be covering some of the most used operations and functions on 2D arrays:
- Addition
- Multiplication by scalar
- Matrix arithmetic
- Matrix-matrix multiplication
- Matrix inversion
- Matrix transposition
In the following sections, we will look into the methods of implementing each of them in Python using SciPy/NumPy.
How to do it…
Let's look the the different methods.
Matrix addition
In order to understand how matrix addition is done, we will first initialize two arrays:
# Initializing an array x = np.array([[1, 1], [2, 2]]) y = np.array([[10, 10], [20, 20]])
Similar to what we saw in a previous chapter, we initialize a 2 x 2 array by using the np.array
function.
There are two methods by which we can add two arrays.
Method 1
A simple addition of the two arrays x
and y
can be performed as follows:
x+y
Note that x
evaluates to:
[[1...