Introduction to matrices with Numpy
Tags: python, beginners, matrix
Published: 2020-01-07 10:06:22

Why learn scientific programming with  Python? Python today has an incredible amount of library just for scientific support. And if you want to go with python for your scientific stuff, then it's a good idea.
So, let's start.
#Table Of Contents
-[What is a matrice ?](#matrice)
-[What is Numpy?](#numpy)
-[Define an Array](#definearray)
    -[1D array](#1Darray)
    -[2D array](#2Darray)
-[Usable methods](#methods)
    -[numpy.size() method](#numpysize)
    -[numpy.shape() method](#numpyshape)
-[Slicing](#slicing)
Before rushing into programming, I will make a quick reminder of matrice and their mathematical syntax. 
##What is a matrice ?<a name="matrice"></a>
A matrice is a rectangular array of numbers, symbols or other mathematical objects arranged in rows and columns.
The mathematical syntax is quite simple : 
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/sx33pt0slot84zlwz712.JPG)
You just have to retain that they are organized in rows and columns (m x n). It's mostly because of their structure that they are very interesting.
Examples of matrices

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/rlhwgxvhi4kt7j3hg8xs.JPG)
##What is Numpy?<a name="numpy"></a>
Numpy is a mathematic module for Python mostly written in C to make sure that the precompiled mathematical and numerical functions and functionalities of Numpy guarantee great execution speed. 
Numpy enriches Python with data structures concepts and implementations of multi-dimensional arrays and matrices. It even supports huge matrices and arrays, knows as "Big data".
To install it just do
```bash
pip3 install numpy
```

##Define a Matrice <a name="definearray"></a>
Just to be clear, Numpy creates arrays that can serve as vectors or matrices. I will use the term array with Numpy.
First start your python interpreter and import the module
```python
import numpy as np
```
To create arrays, we are going to use np.array()
###1D array<a name="1Darray"></a>
To create a 1D array, you just need to pass the list of numbers as arguments to <code>np.array()</code>.
```python
>>> a = np.array([1,2,3])
>>> a
array([1, 2, 3])
>>> 
```
Now do 
```python
>>> type(a)
numpy.ndarray
```
<code>a</code> is simply a ndarray object, totally different from a list object.

###2D array<a name="2Darray"></a>
To create a 2D array, you need to pass a list of lists of numbers as arguments to <code>np.array()</code>
```python
>>> b = np.array([[1,2,3],[4,5,6]])
>>> b
array([[1, 2, 3],
       [4, 5, 6]])
>>> c = np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> c
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
```

##Usable methods<a name="methods"></a>
Now let's talk about usables methods for your arrays. 
###numpy.size() method<a name="numpysize"></a>
This method helps get the numbers of element in an array
```python
>>> np.size(a)
3
>>> np.size(c)
9
```
###numpy.shape() method<a name="numpyshape"></a>
This method return the size (m*n) of an array.
```python
>>> np.shape(c)
(3,3)
>>> np.shape(a)
(,3)
>>> np.shape(b)
(2,3)
```
##Slicing<a name="slicing"></a>
Sometimes while working with arrays, we just need a part to work on. With the slicing concept in Python, we can easily handle this task. Slicing in Python means "extract a part of an array, list or tuples".
It consists in indicating in brackets the indices of the beginning and the end of the slice, the indices separated by <code>:</code>.
Let's suppose we want only the two last numbers of array <code>a</code>. The syntax will look like <code>arr[start:end]</code>
```python
>>> a = np.array([12, 25, 34, 56, 87])
>>> a
array([12, 25, 34, 56, 87])
>>> a[1:3]
array([25, 34])
```
Notice that : 
- Array always start with indice 0. So, a[0] will return 12</li>
- When you are doing slicing, the number identified by the second indice is not included.

Let's see slicing with 2D arrays
```python
>>> b
array([[1, 2, 3],
       [4, 5, 6]])
>>> b[0,1]
2
>>> b[0:1,0:2]
array([[1, 2]])
>>> b[0:2,0:]
array([[1, 2, 3],
       [4, 5, 6]])
```
Slicing with 2D arrays is a little different. We have just use a syntax like <code>arr[start:end,start:end]

##Conclusion
That's all for introduction to matrices with Python. We will talk about more concepts like addition and multiplication, and how to resolve equations with numpy in the next article. 

Documentations for further reads (RECOMMENDED):
<ul>
<li><a href="https://www.pythoninformer.com/python-libraries/numpy/index-and-slice/">Indexing and slicing numpy arrays</a></li>
<li><a href="https://numpy.org/">Numpy Official Website</a></li>
<li><a href="https://www.mathsisfun.com/algebra/matrix-introduction.html">Matrices</a></li>
<ul>