import numpy as np
arr = np.array([0, 1, 2, 3, 4, 5])
print(arr)[0 1 2 3 4 5]
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.
You can install numpy using the pip command in a terminal:
pip install numpy
If you’re using a notebook, the installation process is a little different: you need to add a ! before the pip:
!pip install numpy
Once NumPy is installed, import it in your applications by adding the import keyword:
We can create a NumPy ndarray object by using the array() function.
[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:
0-D arrays, or Scalars, are the elements in an array. Each value in an array is a 0-D array.
An array that has 0-D arrays as its elements is called uni-dimensional or 1-D array.
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.
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.
[[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]]
You can get the number of dimension using the .ndim suffix:
3
NumPy arrays have an attribute called shape that returns a tuple with each index having the number of corresponding elements.
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 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].
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:
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: