0% found this document useful (0 votes)
8 views55 pages

Python AI & DS Numpy

NumPy is an open-source Python library designed for numerical computing, providing support for multi-dimensional arrays and a variety of mathematical functions. It is widely used in data analysis, machine learning, and scientific computing due to its efficiency and compatibility with other libraries. Key features include array manipulation, fast computation, and the ability to perform arithmetic operations on arrays.

Uploaded by

shreejitmondal06
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views55 pages

Python AI & DS Numpy

NumPy is an open-source Python library designed for numerical computing, providing support for multi-dimensional arrays and a variety of mathematical functions. It is widely used in data analysis, machine learning, and scientific computing due to its efficiency and compatibility with other libraries. Key features include array manipulation, fast computation, and the ability to perform arithmetic operations on arrays.

Uploaded by

shreejitmondal06
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON AI & Object Oriented

DS Programming
WHAT IS NUMPY?
NumPy, short for Numerical Python, is an open-source Python library. It supports
multi-dimensional arrays (matrices) and provides a wide range of mathematical
functions for array operations. It is used in scientific computing, and in areas like
data analysis, machine learning, etc.

Numeric, the ancestor of NumPy, was developed by Jim Hugunin. Another


package Numarray was also developed, having some additional functionalities.
In 2005, Travis Oliphant created NumPy package by incorporating the features
of Numarray into Numeric package. There are many contributors to this open
source project.
WHY TO USE NUMPY?
The following are some of the key reasons to use NumPy:
NumPy provides various math functions for calculations like
addition, algebra, and data analysis.
NumPy provides various objects representing arrays and
multi-dimensional arrays which can be used to handle large data
such as images, sounds, etc.
NumPy also works with other libraries like SciPy (for scientific
computing), Pandas (for data analysis), and scikit-learn (for
machine learning).
NumPy is fast and reliable, which makes it a great choice for
numerical computing in Python.
NUMPY APPLICATIONS
Data Analysis: In Data analysis, while handling data, we can create data (in the form of
array objects), filter the data, and perform various operations such as mean, finding
the standard deviations, etc.
Machine Learning & AI: Popular machine learning tools
like TensorFlow and PyTorch use NumPy to manage input data, handle model
parameters, and process the output values.
Array Manipulation: NumPy allows you to create, resize, slice, index, stack, split, and
combine arrays.
Finance & Economics: NumPy is used for financial analysis, including portfolio
optimization, risk assessment, time series analysis, and statistical modelling.
Image & Signal Processing: NumPy helps process and analyze images and signals for
various applications.
Data Visualization: NumPy independently does not create visualizations, but it works
with libraries like Matplotlib and Seaborn to generate charts and graphs from
numerical data.
WHY IS NUMPY FASTER THAN
LISTS?
• NumPy arrays are stored at one continuous place in memory
unlike lists, so processes can access and manipulate them very
efficiently.
• This behavior is called locality of reference in computer science.
• This is the main reason why NumPy is faster than lists. Also it is
optimized to work with latest CPU architectures.
• Python lists do not store the actual values of their elements
directly in one continuous block of memory. Instead, a Python list
stores a contiguous array of references (pointers) to the actual
objects that represent the list's elements. These individual objects
(like integers, strings, or other lists) can be located anywhere in
WHICH LANGUAGE IS NUMPY
WRITTEN IN?
• NumPy is a Python library and is written partially in Python.
• Most of the parts that require fast computation are
written in C or C++.
NUMPY ARRAY EXAMPLE
NUMPY ARRAY EXAMPLE
TUPLE TO ARRAY
• When you create a NumPy array from a tuple that contains
different data types, NumPy will default to the most general
data type.
TUPLE TO ARRAY
• When you create a NumPy array from a tuple that contains
different data types, NumPy will default to the most general
data type.
Dimensions in Arrays
A 0-dimensional NumPy array, also known as a scalar array,
0D array represents a single value. It is the most basic type of NumPy
array.
Dimensions in Arrays
A 1-dimensional NumPy array is a fundamental data structure in the
NumPy library, representing a sequence of elements arranged in a
1D array single line.
Dimensions in Arrays

2D array

Each row must have same Error: T he requested arr ay has an


number of columns i nhom ogeneous shape
DIMENSIONS IN ARRAYS : 3D ARRAY
CHECK NUMBER OF
DIMENSIONS?
ARRAY INDEXING
• You can access an array element by referring to its index number.

• The indexes in NumPy arrays start with 0, meaning that the first
element has index 0, and the second has index 1 etc.
ARRAY INDEXING
ACCESS ELEMENTS OF 2D
ARRAY

Output:
ACCESS ELEMENTS OF 2D
ARRAY

• ar [Link] retu rn s a tu pl e of i nteger s

• Each integer (n and m)


represents
• the size of the array
• along that dimension.
Output:
ACCESS ELEMENTS OF 2D
ARRAY
Output:
NEGATIVE INDEXING
• A negati v e i ndex means counti ng from th e end of the array .
• - 1 i s th e l ast el ement, - 2 i s the second- l ast, and so on.
SLICING ARRAYS
• S l ici ng in py thon means tak ing el em ents from one gi v en i ndex to
anoth er gi v en i ndex .
• W e pass sli ce i nstead of index - - - > [ : ]
• W e can al so define th e step, l i k e thi s - - - > [ : : ]

• I f w e don't pass star t i t’s consi dered 0


• I f w e don't pass end i ts consi dered l ength of arr ay in th at di mensi on
• I f w e don't pass step, i t’s consi dered 1
SLICING ARRAYS

Output:
SLICING ARRAYS

• Array negative indexing with slicing


• Arr [-3:] will print all the elements starting from last third
elements
SLICING ARRAYS
SLICING ARRAYS

Output:
SLICING ARRAYS

Output:
NUMPY ARRAY RESHAPING
• 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
• Change number of elements in each dimension.
RESHAPE FROM 1D TO 2D
Convert the following 1D array with 9 elements into a 2D array.
The outermost dimension will have 3 arrays, each with 3 elements:
RESHAPE FROM 1D TO 2D
□ Reshaping is only
possible when the
total number of
elements in the
original array
matches the total
number of elements
required by the new
shape.
RESHAPE FROM 1D TO 3D
The array is reshaped into a 3D
array with shape (2,3,2) means
that

• It has 2 blocks
• Each containing a 3x2 matrix
• The first block contains the 3x2 matrix:
[[1, 2], [3, 4], [5, 6]]
• The second block contains the 3x2
matrix: [[7, 8], [9, 10], [11, 12]]
ITERATING ARRAYS
ITERATING 2D ARRAYS

• Here x refers to
each row of the 2D
array
ITERATING 2D ARRAYS

• Here x refers to each


row of the 2D array

• Y refers to each
element of current row
ITERATING 3D ARRAYS
ITERATING ARRAYS USING
• In basic for loops, iterating through each scalar of an array we
need to use n for loops.
• This is difficult for arrays with very high dimensionality.
ITERATING ARRAYS USING

nditer() is a powerful and flexible iterator in NumPy that allows you


to iterate over arrays in a variety of ways. It is particularly useful
when working with multi-dimensional arrays, as it can iterate over
each element efficiently, handle multiple arrays simultaneously, and
even control the order in which the array elements are accessed.
NUMPY ARITHMETIC
OPERATIONS
1. Addition of Arrays
2. Subtraction of Arrays
3. Multiplication of Arrays
4. Division of Arrays
5. Exponentiation (Power)
6. Modulus Operation
NUMPY ARITHMETIC
OPERATIONS
ADDITION OF ARRAYS
Corresponding elements of two arrays are added together
using [Link]() function.

Output
SUBTRACTION OF ARRAYS
Corresponding elements of two arrays are subtracted together using
[Link]() function.

Output
MULTIPLICATION OF ARRAYS
Corresponding elements of two arrays are multiplied together using
[Link]() function.

Output
DIVISION OF ARRAYS
Division is performed element-wise using
the [Link]() function. This divides each element of the first
array by the corresponding element in the second array.

Output
EXPONENTIATION (POWER)
It allows us to raise each element in an array to a specified
power. In NumPy, this can be done using
the [Link]() function.

Output
MODULUS OPERATION
It finds the remainder when one number is divided by another. In NumPy, you can
use the [Link]() function to calculate the modulus element-wise between
two arrays.

Output
NUMPY JOINING ARRAY
• Joining means putting contents of two or more arrays in a single
array.
• We pass a sequence of arrays that we want to join to the
concatenate() function, along with the axis. If axis is not explicitly
passed, it is taken as 0.
NUMPY JOINING ARRAY
• Concatenate the arrays vertically, along the first axis (the rows)
NUMPY JOINING ARRAY
• Concatenate the arrays horizontally.
• Along the 2nd axis (the columns) side by side.
JOINING ARRAYS USING STACK
FUNCTIONS
• Stacking is same as concatenation, the only
difference is that stacking is done along a
new axis.

• We can concatenate two 1-D arrays along the


second axis which would result in putting
them one over the other, ie. stacking.

• We pass a sequence of arrays that we want


to join to the stack() method along with the
axis.

• If axis is not explicitly passed it is taken as 0.


STACKING ALONG ROWS
STACKING ALONG COLUMNS
STACKING ALONG HEIGHT (DEPTH)
NUMPY SPLITTING ARRAY
• Splitting breaks one array into multiple.
• We use array_split() for splitting arrays.
• We pass it the array we want to split and the number of splits.
SPLITTING 2-D ARRAYS
STUDY LINK
A Visual Intro to NumPy and Data Representation – Jay Alammar –
Visualizing machine learning one concept at a time.

You might also like