Python basics
  • Tutorial
  • Exercices
  • About
  1. Advanced
  2. 10-Numpy
  • Introduction
    • Python
  • Fundamentals
    • 0-Basic Syntax
    • 1-Variables
    • 2-Data types
    • 3-Numbers
    • 4-Strings
    • 5-Lists
    • 6-Booleans
    • 7-Loops
    • 8-Functions
    • 9-Dictionary
  • Advanced
    • 10-Numpy
    • 11-Matplotlib
    • 12-DataFrame
  • CLI
    • Exercices
    • Solutions
  • Git
    • Git introduction
    • Git branch
    • Git exercices
  • SQL
    • SQL Exercices
    • SQL Solutions

On this page

  • Installation
  • Importation
  • Create array
  • Dimensions
    • 0-D arrays
    • 1-D arrays
    • 2-D arrays
    • 3-D arrays
  • Access
    • Slicing
  • Reshape
  • Filtering
  1. Advanced
  2. 10-Numpy

10-Numpy

Notebook

Link to the Google Colaboratory notebook: 10-Numpy

NumPy is a Python library used for working with arrays.

It stands for Numerical Python.

NumPy aims to provide an array object that is up to 50x faster than traditional Python lists.

Arrays are very frequently used in data science, where speed and resources are very important.

Installation

You can install numpy using the pip command in a terminal:

pip install numpy
Notebook

If you’re using a notebook, the installation process is a little different: you need to add a ! before the pip:

!pip install numpy

Importation

Once NumPy is installed, import it in your applications by adding the import keyword:

import numpy as np

arr = np.array([0, 1, 2, 3, 4, 5])
print(arr)
[0 1 2 3 4 5]

Create array

We can create a NumPy ndarray object by using the array() function.

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

print(arr)
print(type(arr))
[1 2 3 4 5]
<class 'numpy.ndarray'>

The creation of an array should be done with specification of the dimension (but it can after be reshaped, but not extended).

Common approaches is to initialised with either the zeros or ones command:

import numpy as np

arr = np.zeros((2, 2)) # Create an array that will have 2 row and 2 columns

new_arr = arr.reshape(1, 4) # We can reshape it after
print(arr)
print(new_arr)
[[0. 0.]
 [0. 0.]]
[[0. 0. 0. 0.]]
import numpy as np

arr = 17 * np.ones((2, 2)) # Create an array that will 17 as values and of size 2x2

print(arr)
[[17. 17.]
 [17. 17.]]

Dimensions

0-D arrays

0-D arrays, or Scalars, are the elements in an array. Each value in an array is a 0-D array.

import numpy as np

arr = np.array(42)
print(arr)
42

1-D arrays

An array that has 0-D arrays as its elements is called uni-dimensional or 1-D array.

import numpy as np

arr = np.array([1,2,3,4,5])
print(arr)
[1 2 3 4 5]

2-D arrays

An array that has 1-D arrays as its elements is called a 2-D array.

These are often used to represent matrix or 2nd order tensors.

import numpy as np

arr = np.array([ [1,2,3,4,5], [6,7,8,9,10]])
print(arr)
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]]

3-D arrays

An array that has 2-D arrays (matrices) as its elements is called 3-D array.

These are often used to represent a 3rd order tensor.

import numpy as np

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

print(arr)
[[[1 2 3]
  [4 5 6]]

 [[1 2 3]
  [4 5 6]]]

You can get the number of dimension using the .ndim suffix:

import numpy as np

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

print(arr.ndim)
3

NumPy arrays have an attribute called shape that returns a tuple with each index having the number of corresponding elements.

print(arr.shape)
(2, 2, 3)

Access

To access elements from 2-D arrays we can use comma separated integers representing the dimension and the index of the element.

Think of 2-D arrays like a table with rows and columns, where the dimension represents the row and the index represents the column.

import numpy as np

arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])

print('2nd element on 1st row: ', arr[0, 1])
2nd element on 1st row:  2

To access elements from 3-D arrays we can use comma separated integers representing the dimensions and the index of the element.

import numpy as np

arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])

print(arr[0, 1, 2])
6

Slicing

Slicing in python means taking elements from one given index to another given index.

We pass slice instead of index like this: [start:end].

We can also define the step, like this: [start:end:step].

import numpy as np

arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])

print(arr[0:2, 1:, -1])
[[ 6]
 [12]]

Reshape

Reshaping means changing the shape of an array.

The shape of an array is the number of elements in each dimension.

By reshaping we can add or remove dimensions or change number of elements in each dimension.

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])

newarr = arr.reshape(2, 3, 2)

print(newarr)
[[[ 1  2]
  [ 3  4]
  [ 5  6]]

 [[ 7  8]
  [ 9 10]
  [11 12]]]

If we don’t know one of the dimension, we can use the -1 in the dimension:

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
new_arr = arr.reshape(1, -1) # It will reshape with 1 row and the rest in columns
print(new_arr.shape)
(1, 9)

Filtering

Getting some elements out of an existing array and creating a new array out of them is called filtering.

In NumPy, you filter an array using a boolean index list.

If the value at an index is True that element is contained in the filtered array, if the value at that index is False that element is excluded from the filtered array.

import numpy as np

arr = np.array([41, 42, 43, 44])

x = [True, False, True, False]
newarr = arr[x]
print(newarr)
[41 43]

We can filter directly with array conditions:

import numpy as np

arr = np.array([41, 42, 43, 44])

filter_arr = arr > 42

newarr = arr[filter_arr]

print(filter_arr)
print(newarr)
[False False  True  True]
[43 44]
9-Dictionary
11-Matplotlib
 

Copyright 2023, Clément Bernard