Numpy - Basics
Numpy - Basics
# NumPy
NumPy is a Python library used for working with arrays.
It has support for large, multi-dimensional arrays and matrices,
along with a large collection of high-level mathematical functions to opera
In [ ]:
# import numpy
-> By adding the import keyword
-> Create an alias with the as keyword while importing
In [4]:
import numpy as np
In [ ]:
# Create a NumPy ndarray Object:
In [6]:
import numpy as np
print(arr1)
print(type(arr1))
print(arr2)
print(type(arr2))
[1 2 3 4 5]
<class 'numpy.ndarray'>
[10 9 8 7 6]
<class 'numpy.ndarray'>
In [ ]:
# Dimensions in Arrays
A dimension in arrays is one level of array depth (nested arrays).
In [ ]:
# 0-D Arrays
0-D arrays, or Scalars, are the elements in an array.
Each value in an array is a 0-D array.
In [7]:
import numpy as np
arr = np.array(42)
print(arr)
42
In [ ]:
# 1-D Arrays
An array that has 0-D arrays as its elements is called uni-dimensional or 1
In [9]:
#Create a 1-D array containing the values 1,2,3,4,5:
import numpy as np
print(arr)
[1 2 3 4 5]
In [ ]:
# 2-D Arrays
An array that has 1-D arrays as its elements is called a 2-D array.
In [10]:
#Create a 2-D array containing two arrays with the values 1,2,3 and 4,5,6:
import numpy as np
print(arr)
[[1 2 3]
[4 5 6]]
In [ ]:
# 3-D arrays
An array that has 2-D arrays (matrices) as its elements is called 3-D array
In [11]:
#Create a 3-D array with two 2-D arrays, both containing two arrays with th
import numpy as np
print(arr)
[[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]]
In [ ]:
# Check Number of Dimensions?
NumPy Arrays provides the ndim attribute that returns an integer that tells
the array have.
In [13]:
#Check how many dimensions the arrays have:
import numpy as np
a = np.array(42)
b = np.array([1, 2, 3, 4, 5])
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(a.ndim)
print(b.ndim)
print(c.ndim)
print(d.ndim)
0
1
2
3
In [ ]:
# Higher Dimensional Arrays
An array can have any number of dimensions.
When the array is created, you can define the number of dimensions by using
In [14]:
#Create an array with 5 dimensions and verify that it has 5 dimensions:
import numpy as np
print(arr)
print('number of dimensions :', arr.ndim)
[[[[[1 2 3 4]]]]]
number of dimensions : 5
In [ ]:
# NumPy Array Indexing
Access Array Elements
Array indexing is the same as accessing an array element.
The indexes in NumPy arrays start with 0, meaning that the first element ha
and the second has index 1 etc.
In [15]:
#Get the first element from the following array:
import numpy as np
print(arr[0])
1
2
7
In [ ]:
# Access 2-D Arrays
To access elements from 2-D arrays we can use comma separated integers repr
and the index of the element.
In [1]:
import numpy as np
In [ ]:
# Access 3-D Arrays
To access elements from 3-D arrays we can use comma separated integers repr
and the index of the element.
In [16]:
#Access the third element of the second array of the first array:
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(arr[0, 1, 2])
In [ ]:
# Negative Indexing
Use negative indexing to access an array from the end.
In [20]:
#Print the last element from the 2nd dim:
import numpy as np
In [ ]:
# NumPy Array Slicing
Slicing arrays
Slicing in python means taking elements from one given index to another giv
In [19]:
#Slice elements from index 1 to index 5 from the following array:
import numpy as np
#The result includes the start index, but excludes the end index.
print(arr[1:5])
#Slice elements from index 4 to the end of the array:
print(arr[4:])
#Negative Slicing
#Use the minus operator to refer to an index from the end:
#Slice from the index 3 from the end to index 1 from the end:
print(arr[-3:-1])
#STEP
#Use the step value to determine the step of the slicing:
#Return every other element from index 1 to index 5:
print(arr[1:5:2])
[2 3 4 5]
[5 6 7]
[1 2 3 4]
[5 6]
[2 4]
[1 3 5 7]
import numpy as np
print(arr[1, 1:4])
#From both elements, slice index 1 to index 4 (not included), this will re
print(arr[0:2, 1:4])
[7 8 9]
[3 8]
[[2 3 4]
[7 8 9]]
In [ ]:
# Data Types in NumPy
NumPy has some extra data types, and refer to data types with one character
Below is a list of all data types in NumPy and the characters used to repre
i - integer
b - boolean
u - unsigned integer
f - float
c - complex float
M - datetime
S - string
U - unicode string
In [ ]:
# Checking the Data Type of an Array
The NumPy array object has a property called dtype that returns the data ty
In [2]:
#Get the data type of an array object:
import numpy as np
print(arr.)
int32
In [34]:
#Get the data type of an array containing strings:
import numpy as np
print(arr.dtype)
<U7
In [ ]:
# Creating Arrays With a Defined Data Type
We use the array() function to create arrays,
this function can take an optional argument: dtype that allows us to define
of the array elements:
In [4]:
#Create an array with data type string:
import numpy as np
print(arr)
print(arr.dtype)
[b'1' b'2' b'3' b'4']
|S1
In [ ]:
# Converting Data Type on Existing Arrays
The best way to change the data type of an existing array,
is to make a copy of the array with the astype() method.
The astype() function creates a copy of the array,
and allows you to specify the data type as a parameter.
The data type can be specified using a string, like 'f' for float,
'i' for integer etc. or you can use the data type directly like float for f
In [4]:
#Change data type from float to integer by using 'i' as parameter value:
import numpy as np
newarr = arr.astype('i')
print(arr)
print(arr.dtype)
print(newarr)
print(newarr.dtype)
In [6]:
#Change data type from float to integer by using int as parameter value:
import numpy as np
newarr = arr.astype(int)
print(newarr)
print(newarr.dtype)
[1 2 3]
int32
In [ ]:
# NumPy Array Copy vs View
The Difference Between Copy and View
The main difference between a copy and a view of an array is that the copy
and the view is just a view of the original array.
The copy owns the data and any changes made to the copy will not affect ori
and any changes made to the original array will not affect the copy.
The view does not own the data and any changes made to the view will affect
and any changes made to the original array will affect the view.
In [7]:
#Make a copy, change the original array, and display both arrays:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
x = arr.copy()
arr[0] = 42
print(arr)
print(x)
#The copy SHOULD NOT be affected by the changes made to the original array
[42 2 3 4 5]
[1 2 3 4 5]
In [8]:
#Make a view, change the original array, and display both arrays:
import numpy as np
print(arr)
print(x)
#The view SHOULD be affected by the changes made to the original array.
[42 2 3 4 5]
[42 2 3 4 5]
In [9]:
#Make a view, change the view, and display both arrays:
import numpy as np
print(arr)
print(x)
#The original array SHOULD be affected by the changes made to the view.
[31 2 3 4 5]
[31 2 3 4 5]
In [ ]:
# Shape of an Array
The shape of an array is the number of elements in each dimension.
In [1]:
#Print the shape of a 2-D array:
import numpy as np
print(arr.shape)
(2, 4)
In [2]: #Create an array with 5 dimensions using ndmin using a vector with values 1
import numpy as np
print(arr)
print('shape of array :', arr.shape)
[[[[[1 2 3 4]]]]]
shape of array : (1, 1, 1, 1, 4)
In [ ]:
# Reshaping arrays
Reshaping means changing the shape of an array.
In [1]:
#Reshape From 1-D to 2-D
#Convert the following 1-D array with 12 elements into a 2-D array.
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
newarr = arr.reshape(4, 3)
print(newarr)
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
In [7]:
#Reshape From 1-D to 3-D
#Convert the following 1-D array with 12 elements into a 3-D array.
import numpy as np
newarr = arr.reshape(2, 3, 2)
print(newarr)
#print(arr)
[[[ 1 2]
[ 3 4]
[ 5 6]]
[[ 7 8]
[ 9 10]
[11 12]]]
In [9]:
#Flattening the arrays
#Flattening array means converting a multidimensional array into a 1D array
newarr = arr.reshape(-1)
print(newarr)
#print(arr)
[1 2 3 4 5 6]
In [ ]:
# Iterating Arrays
Iterating means going through elements one by one.
In [10]:
#Iterate on the elements of the following 1-D array:
import numpy as np
for x in arr:
print(x)
1
2
3
In [11]:
#Iterate on the elements of the following 2-D array:
import numpy as np
for x in arr:
print(x)
[1 2 3]
[4 5 6]
In [13]:
#Iterate on each scalar element of the 2-D array:
import numpy as np
for x in arr:
for y in x:
print(y)
1
2
3
4
5
6
In [12]: #Iterate down to the scalars:
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
for x in arr:
for y in x:
for z in y:
print(z)
1
2
3
4
5
6
7
8
9
10
11
12
In [ ]:
# Joining NumPy Arrays
Joining means putting contents of two or more arrays in a single array.
In [17]:
#Join two arrays
import numpy as np
print(arr)
[1 2 3 4 5 6]
In [2]:
#Join two 2-D arrays along rows (axis=1):
import numpy as np
print(arr)
[[1 2 5 6]
[3 4 7 8]]
In [13]:
#Join two 2-D arrays (axis=0):
import numpy as np
print(arr)
[[1 2]
[3 4]
[5 6]
[7 8]]
In [14]:
import numpy as np
print(arr)
[[[1 2]
[5 6]]
[[3 4]
[7 8]]]
In [18]:
import numpy as np
print(arr)
[[[1 2]
[3 4]]
[[5 6]
[7 8]]]
In [15]:
#Stacking Along Rows
#NumPy provides a helper function: hstack() to stack along rows.
import numpy as np
[[1 2 5 6]
[3 4 7 8]]
In [19]:
#Stacking Along Columns
#NumPy provides a helper function: vstack() to stack along columns.
import numpy as np
print(arr)
[[1 2]
[3 4]
[5 6]
[7 8]]
In [20]:
#Stacking Along Height (depth)
#NumPy provides a helper function: dstack() to stack along height, which is
import numpy as np
print(arr)
[[[1 5]
[2 6]]
[[3 7]
[4 8]]]
In [ ]:
Splitting NumPy Arrays
Splitting is reverse operation of Joining.
Joining merges multiple arrays into one and Splitting breaks one array into
In [23]:
#Split the array in 3 parts:
import numpy as np
newarr = np.array_split(arr, 3)
print(newarr)
#The return value is an array containing three arrays.
#If the array has less elements than required, it will adjust from the end
In [24]:
#Split the array in 4 parts:
import numpy as np
newarr = np.array_split(arr, 4)
print(newarr)
In [ ]:
Splitting 2-D Arrays
Use the same syntax when splitting 2-D arrays.
Use the array_split() method, pass in the array you want to split and the n
Example
In [25]:
#Split the 2-D array into three 2-D arrays.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15
newarr = np.array_split(arr, 3)
print(newarr)
[array([[1, 2, 3],
[4, 5, 6]]), array([[ 7, 8, 9],
[10, 11, 12]]), array([[13, 14, 15],
[16, 17, 18]])]
Searching Arrays
You can search an array for a certain value, and return the indexes that get a match.
In [27]:
#Find the indexes where the value is 4:
import numpy as np
x = np.where(arr == 4)
print(x)
import numpy as np
x = np.where(arr%2 == 0)
print(x)
In [2]:
#Find the indexes where the values are odd:
import numpy as np
x = np.where(arr%2 == 1)
print(x)
Sorting Arrays
Sorting means putting elements in an ordered sequence.
Ordered sequence is any sequence that has an order corresponding to elements, like
numeric or alphabetical, ascending or descending.
The NumPy ndarray object has a function called sort(), that will sort a specified array.
In [23]:
#Sort the array:
import numpy as np
print(np.sort(arr))
#This method returns a copy of the array, leaving the original array unchan
[0 1 2 3]
In [24]:
#Sort the array alphabetically:
import numpy as np
print(np.sort(arr))
In [25]:
#Sort a boolean array:
import numpy as np
print(np.sort(arr))
In [26]:
#If you use the sort() method on a 2-D array, both arrays will be sorted:
import numpy as np
print(np.sort(arr))
[[2 3 4]
[0 1 5]]
In [ ]:
Some of the useful aggregate functions
In [3]:
# import NumPy library
import numpy as np
In [4]:
# import NumPy library
import numpy as np
max_value = np.max(np_array)
In [5]:
# import NumPy library
import numpy as np
min_value = np.min(np_array)
In [24]:
# import NumPy library
import numpy as np
sum_value = np.sum(np_array)
In [ ]: