NumPy stands for Numerical Python. It is a Python library, mostly used for working on high level mathematical calculations using arrays and matrices.
Before going any further let’s learn a bit about arrays.
An array is a grid of values, split into rows and columns. Each data in the array is called an element. So, array is an ordered collection of elements with every element being of the same data type.
Here I’m using Jupiter Notebook to install and run NumPy library.
Installation
pip install numpy
Importing
Once you successfully install the library, it’s time to import it
import numpy
Types of Arrays
Arrays start from 0-D, 1-D, 2-D and the list goes on. Hence the name n-D array/multi-dimensional array
Here ‘n’ can be any whole number, D stands for Dimension.
0-D Array
0-D arrays are also called as Scalars.
To that matter every element in an array is a scalar by itself.
numpy.array(100)
For our convenience, instead of importing and addressing as numpy we can address it as a shortcut shown below:
import numpy as np
1-D Array
Has only one axis
Also called as uni-dimensional array
Passing a simple list into an array
np.array([2,4,6,8,10])
The above also stands as a simple example of passing a list as an input to an array. The other ways of array creation are covered later in the post.
2-D Array
Has two axes
Also called as multi-dimensional array
This is an array of arrays, which has 1-D array as its elements.
arr_2D = np.array( [[1, 2, 3], [4, 5, 6]] )
print(arr_2D)
3-D Array
This has 2-D arrays as its elements. So, this is a combination of two or more 2D arrays
arr_3D = np.array( [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]] )
print(arr_3D)
Now, let’s check the dimensions of some already created above arrays (arr_2D, arr_3D)
print("2D array dimension: ", arr_2D.ndim)
print("3D array dimension:", arr_3D.ndim)
Creation of n-D array
As one last example, let’s see how to create a n-D array.
arr_nD = np.array([1, 2, 3, 4], ndmin=5)
print(arr_nD)
print('This is a', arr_nD.ndim , 'D array')
Different ways of creating arrays
Other ways of converting a list into an array is:
lst = [2,4,6,8,10]
np.array(lst)
np.asarray(lst)
The primary difference between np.array( ) and np.asarray( ) is:
np.array() will make a duplicate of the original object which means this will create a new copy in memory
lst = [2,4,6,8,10]
print("lst", lst)
old_mem_arr = np.array(lst)
print("old_mem_arr", old_mem_arr)
new_mem_arr = np.array(old_mem_arr)
print("new_mem_arr", new_mem_arr)
new_mem_arr[0] = 1
print("lst", lst)
print("old_mem_arr",old_mem_arr)
print("new_mem_arr", new_mem_arr)
Whereas, numpy.asarray() would mirror the changes in the original object and thus doesn’t create a new copy in memory. So, the changes made in one array are reflected into the other but doesn’t affect the list.
lst = [2,4,6,8,10]
print("lst", lst)
old_mem_arr = np.asarray(lst)
print("old_mem_arr", old_mem_arr)
new_mem_arr = np.asarray(old_mem_arr)
print("new_mem_arr", new_mem_arr)
old_mem_arr[0] = 1
print("lst", lst)
print("old_mem_arr", old_mem_arr)
print("new_mem_arr", new_mem_arr)
Creating from a tuple
np.array((1 , 3, 2))
Creating from all zeros, a (2,3) array
np.zeros((2, 3))
By default, the data type is float, we can explicitly change it using the ‘dtype’, which is shown under the Basic properties of arrays section.
Creating from all ones, a (2,3) array
np.ones((2, 3))
Creating a (2,3) array from a constant value like 5
np.full( (2,3), 5 )
Basic properties of arrays
arr = np.array([[ 1, 2, 3], [ 4, 5, 6]])
print("Type: ", type(arr))
print("Data type: ", arr.dtype)
print("Shape: ", arr.shape)
print("Size: ", arr.size)
type tells about type of an object. Here arr is an ndarray
Numpy Data Types
Along with all the basic data types like
Integer (i),
float (f) ,
string (s),
boolean (b)
Numpy also supports some special data types like:
datetime (M),
timedelta (m),
unicode string (U),
unsigned integer (u),
complex float (c),
object (O),
fixed memory (v)
dtype – datatype – tells us the type of data.
color_arr = np.array(['Green', 'Pink', 'Blue'])
print(color_arr.dtype)
Here we see the output as <U5, because, of all the elements in the color_array, the element ‘Green’ has maximum length of 5 characters and every other element’s length is less than 5 so <U5
Now, let’s add another element ‘Violet’ and see the result.
color_arr = np.array(['Green', 'Pink', 'Blue', ’Violet’])
print(color_arr.dtype)
As the length of the element ‘Violet’ is 6, the output is <U6.
Specifying the data type
arr = np.array([2,4,6,8,10], dtype='float')
arr.dtype
arr = np.array([2,4,6,8,10], dtype='i')
print(arr.dtype)
arr = np.array([2,4,6,8,10], dtype='i4')
print(arr.dtype)
arr = np.array([2,4,6,8,10], dtype='i8')
arr.dtype
Using ‘astype()’ to create a copy of an array
arr = np.array([2.0,4.1,6.6,8.5,10.9])
changed_arr = arr.astype(int)
print('arr', arr)
print('changed_arr', changed_arr)
changed_arr.dtype
Try to convert a string to a int
color_arr = np.array(['Green', 'Pink', 'Blue', 'Violet'], dtype = 'int')
color_arr
Shape of the array is a tuple, which gives the information about the number of elements present in each dimension.
arr = np.array([[ 1, 2, 3], [ 4, 5, 6]])
In the above example, the shape is (2,3)
As 2 and 3 are two different elements, it states that number of dimensions are 2.
The first dimension (usually referred as x-axis, rows) has 2 elements and
The second dimension (can refer as y-axis, columns) has 3 elements.
arr = np.array( [[ 1, 2, 3], [ 4, 5, 6], [7,8,9]] )
Here, the shape is (3,3), ndim = 2
Reshape
Let’s reshape this from (3,3) to (4,2)
Because 9 elements can be adjusted in a (4,2) array
Instead, we can try (9,1) or (1,9) formats
Reshaping 1D to 3D array
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
arr.reshape(2,1,6)
Reshaping with an unknown dimension
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8,9,10,11,12])
arr.reshape(2, 2, -1)
I am only concerned about the two axes (2,2) and the third can be of any order in that case I use (-1) at the third point.
Reshape a multi-dimensional array into a 1-D array. This is called Flattening.
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1,3,5],[6,7,3]]])
arr.reshape(-1)
Size tells the number of elements in that array.
arr = np.array([[ 1, 2, 3], [ 4, 5, 6]])
arr.size
To get the size of rows and columns individually use the below code.
arr = np.array([[ 1, 2, 3], [ 4, 5, 6]])
print("Size of rows", np.size(arr, 0))
print("Size of Columns", np.size(arr, 1))
Basic Operations on arrays
Addition, Subtraction, Multiplication, Cubing, Transpose
arr = np.array([[ 1, 2, 3], [ 4, 5, 6]])
print ("Add 5 to all elements:", arr+5)
print ("Subtract 10 from each element:", arr-10)
print ("Multiply each element by 25:", arr*25)
print ("cube of each element:", arr**3, "\n")
print ("Original array:\n", arr)
print ("Transpose of the array:\n", arr.T)
Indexing
In Numpy, the positive index starts from 0 and the Negative index starts from -1 same as in Python.
1-D array
arr = np.array([1, 2, 3, 4,5])
print(arr[2])
print(arr[2] + arr[4])
2-D array
arr = np.array([[0,1],[2,3],[4,5]])
arr[2][1]
3-D array
arr = np.array([[[1,2,3,4]], [[5,6,7,8]],[[9,10,11,12]]])
print(arr)
arr[1,0,1]
Negative Indexing
arr = np.array([[[1,2,3,4]], [[5,6,7,8]],[[9,10,11,12]]])
print(arr)
arr[-1,0,-1]
Slicing
arr = np.array([1, 2, 3, 4, 5, 6, 7,8])
print('1:5 slicing - ', arr[1:5])
print(':5 slicing - ', arr[:5])
print('1: slicing - ', arr[1:])
print(':: slicing - ', arr[::])
print('::-1 slicing - ', arr[::-1])
print('-1:-5 slicing - ', arr[-5:-1])
Step size slicing
arr = np.array([1, 2, 3, 4, 5, 6, 7,8])
print('::2 slicing - ',arr[::2])
print('::3 slicing - ',arr[::3])
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print('arr[0,0:2] slicing - ',arr[0,0:2 ])
print('arr[1,0:2] slicing - ',arr[1,0:2 ])
print('arr[0:1,0:3 ] slicing - ',arr[0:1,0:3 ])
print('\n arr[0:2,0:3 ] slicing - \n ',arr[0:2,0:3 ])
Official site for Numpy - https://numpy.org/