Mangabo Kolawole
Posted on January 7, 2020
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 ?
-What is Numpy?
-Define an Array
-1D array
-2D array
-Usable methods
-numpy.size() method
-numpy.shape() method
-Slicing
Before rushing into programming, I will make a quick reminder of matrice and their mathematical syntax.
What is a matrice ?
A matrice is a rectangular array of numbers, symbols or other mathematical objects arranged in rows and columns.
The mathematical syntax is quite simple :
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
What is Numpy?
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
pip3 install numpy
Define a Matrice
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
import numpy as np
To create arrays, we are going to use np.array()
1D array
To create a 1D array, you just need to pass the list of numbers as arguments to np.array()
.
>>> a = np.array([1,2,3])
>>> a
array([1, 2, 3])
>>>
Now do
>>> type(a)
numpy.ndarray
a
is simply a ndarray object, totally different from a list object.
2D array
To create a 2D array, you need to pass a list of lists of numbers as arguments to np.array()
>>> 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
Now let's talk about usables methods for your arrays.
numpy.size() method
This method helps get the numbers of element in an array
>>> np.size(a)
3
>>> np.size(c)
9
numpy.shape() method
This method return the size (m*n) of an array.
>>> np.shape(c)
(3,3)
>>> np.shape(a)
(,3)
>>> np.shape(b)
(2,3)
Slicing
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 :
.
Let's suppose we want only the two last numbers of array a
. The syntax will look like arr[start:end]
>>> 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
- When you are doing slicing, the number identified by the second indice is not included.
Let's see slicing with 2D arrays
>>> 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 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):
Posted on January 7, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.