Outline
• Introduction to NumPy
• Creating Arrays Using Lists
• Create Arrays with NumPy
• Array Slicing
• Array Reshaping
• Math with NumPy I
• Combining to arrays
• Adding elements to arrays
• Inserting Elements in to arrays
• Deleting elements from arrays
• Finding Unique elements and Sorting
• Math's with NumPy II
• Analysing data across arrays
• NumPy exercises
What is NumPy?
• NumPy is one of the main libraries to use for scientific computing in Python.
• It allows you to work with high-performance multidimensional array objects and it
also provides tools for working with these arrays.
• Arrays can have more than 3 dimensions but in this guide, we will only work with 2
dimensions.
• 2-dimensional arrays can be seen as matrices. This is a convenient way of storing and
manipulating multivariate data with python.
What are arrays?
To get started we will first have to
import the library:
Syntax:
import numpy as np
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Creating Arrays Using Lists
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Example :1
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Example 2
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Excercises
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Solution 1 & 2
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Datatypes in arrays
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Solution 1
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Excercises
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Edit Elements in Arrays
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Solution 1
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Shape of the arrays
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Creating arrays with NumPy functions
• This will be useful for when you need to create
large arrays and you don't want to type in
each entry one by one as lists.
• Note: The NumPy functions below are all part
of the NumPy package.
Downloaded by Arun Kumar
(aruncit17@gmail.com)
np.zeros : This function creates a new array filled with zeroes of given shape and type
(optional). The codes below will return an array with 2 rows and 2 columns of zeros.
Downloaded by Arun Kumar
(aruncit17@gmail.com)
np.ones : This function creates a new array filled with ones of given shape
and type (optional). The codes below will return an array with 2 rows and 2
columns of ones.
Downloaded by Arun Kumar
(aruncit17@gmail.com)
np.arange : This function creates an array of evenly spaced values within a
defined interval according to the number of specified arguments. It is
similar to Python's built-in function range().
Downloaded by Arun Kumar
(aruncit17@gmail.com)
np.linspace
Similar to np.arange, this function creates an array of evenly spaced values. However, the difference is that
you can specify number of elements instead of an interval or step.
If you do not specify the sample size, it will print the default of 50 values.
Downloaded by Arun Kumar
(aruncit17@gmail.com)
np.full
Downloaded by Arun Kumar
(aruncit17@gmail.com)
np.eye
Downloaded by Arun Kumar
(aruncit17@gmail.com)
np.diag
Downloaded by Arun Kumar
(aruncit17@gmail.com)
np.random.random
Downloaded by Arun Kumar
(aruncit17@gmail.com)
np.random.randomint
Downloaded by Arun Kumar
(aruncit17@gmail.com)
What is array slicing?
• Array slicing is similar to list slicing in Python. Array indexing also begins from 0.
However, since arrays can be multidimensional, we have to specify the slice for
each dimension.
• As we are mainly working with 2 dimensional arrays in this guide, we need to
specify the row and column like what we do in a matrix. We can select a single
element from an array or even a subarray containing multiple rows and columns of
the original array.
• To do array slicing, we use square brackets
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Syntax
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Why do we need to reshape arrays?
• There may be various reasons to reshape arrays. One of these reasons could be
when you want to add an array to another - they need to be of the same size.
• Recall that the shape of an array is the number of elements in each dimension.
• Before reshaping arrays, we have to make sure that the existing array size can
indeed match its new size.
• This means that a 1D array with 11 elements cannot be reshaped into 2D array
with 2 rows because 11 cannot be divided by 2 (1 row will have 6 and another row
will have 5 elements).
To reshape arrays, we can use the .reshape.
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Excercises
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Math functions with NumPy
• More often than not, data analysis will involve some form of mathematical
calculations.
• Having a large data set also means that you would not want to have to
manually count each data point.
• What you can do is to perform mathematical calculations using arrays.
• Basic mathematical functions on arrays are element wise operators.
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)
Downloaded by Arun Kumar
(aruncit17@gmail.com)