1. Introduction toNumPy
What is NumPy?
Importance in Data Science and Machine Learning
2. Core Features of NumPy
Arrays and Matrices
Mathematical Operations
Broadcasting
OUTLINE
INTRODUCTION TO NUMPY
WHATIS NUMPY :
Def: NumPy is the abrivation of (Numerical Python) which
is a powerful python library for numerical computing .
Why power full library?
1. Mathematical function numpy give wide range of built
in functions like arthematic,trigonometric ,statistic.
2. Efficient: It allows for fast, vectorized operations on
arrays
5.
Continue….
e.g: vectorization refersto the ability to perform operations on
entire arrays (vectors) without the need for writing explicit loops
(like for loop)
CODE :
import numpy as np
# Create a NumPy array
arr = np.array([1, 2, 3, 4, 5])
# Perform vectorized operations
6.
Continue ….
arr_plus_10 =arr + 10 # Add 10 to each
element
arr_squared = arr ** 2 # Square each
element
arr_times_2 = arr * 2 # Multiply each
element by 2
# Print the results
print("Array + 10:", arr_plus_10)
print("Array squared:", arr_squared)
print("Array * 2:", arr_times_2)
7.
Continue…..
3. Multi-dimensional:You can work with arrays of any dimension (1D, 2D,
3D, etc.).
Importance of numpy in machine learning and data scienece
NumPy provides fast and memory-efficient operations for large datasets
using arrays.
E.g. Numpy works on parallelsim.
Machine learning models often involve working with arrays and matrices.
NumPy simplifies these operations with:
Matrix multiplication (dot)
Transposition (T) # changing of row to column and vice versa
8.
Continue ………
DataPreprocessing
NumPy is essential for preparing data before feeding it into ML
models, such as:
Feature scaling (normalization and standardization).
Missing value handling (replacing NaN with values)
Core for Data Science Tasks
Statistical analysis (mean, median, standard deviation).
Manipulating multidimensional arrays.
Creating datasets for visualization
9.
Core Features ofNumPy
1. Arrays and Matrices
Explanation:
NumPy introduces the ndarray, a powerful n-dimensional array object
that is more efficient than Python's lists.
Matrix is two dimensional array.
RULE FOR DOT PRODUCT
In dot product the column of first Array and row
of second Array is must
same so then dot product is applied on it .
10.
Continue….
Why It’sImportant:
Arrays are used to represent data points, features, and target
labels in machine learning.
Matrices are crucial for linear algebra operations like dot products,
solving systems of equations, etc.
2.Broadcasting
Explanation:
Broadcasting allows NumPy to perform operations on arrays with
different shapes and sizes.
11.
It automaticallystretches or replicates smaller arrays to match the shape of larger arrays
during computations.
RULES FOR BROADCASTING
Compare Shapes from Right to Left
NumPy compares the
dimensions of the arrays starting from the trailing (rightmost) dimensions .
RULE: Compare the shapes of the arrays from right to left.
If the dimensions are the same or one of them is 1, broadcasting can
happen.
12.
Continue……
EXAMPLE
import numpy asnp
# Define arrays
a = np.array([[1, 2, 3],
[4, 5, 6]]) # Shape: (2, 3)
b = np.array([10, 20, 30]) # Shape: (3)
13.
Continue…..
# Addarrays (broadcasting)
result = a + b
print(result)
Result:
[[11 22 33]
[14 25 36]]
14.
Continue…..
Example 2:Incompatible Shapes
a = np.array([1, 2, 3]) # Shape: (3,)
b = np.array([[10, 20]]) # Shape: (1, 2)
result = a + b # Error:
Form the right most broadcasting is perform on those arrays which
has same dimension or has 1 in there dimension
Contiue….
After broadcasting
ResultA*B =shape (2,1,3,4)
In compatibale shapes
A= shape (3,4,5)
B= shape (2,3)
After broadcasting
result = error
because From the rightmost
dimension
18.
Continue…..
a has(5) and b has (4); they don’t match.
Broadcasting fails because no dimension is 1 or
compatible
3.MATHMATICAL OPERATIONS OF NUMPY
numpy provide a large amount of built in function which works fast in
data science and machine learning .
19.
Continue…..
For machinelearning task
Matrix multiplication (dot)
Transposition (T)
numpy provide a large amount of built in functions for
preprocessing
For Data Science Tasks
Tasks like:
20.
Continue……
Statistical analysis(mean, median, standard deviation).
Manipulating multidimensional arrays.
Creating datasets for visualization
NUMPY ARRAYS
A NumPy array is a powerful data
structure in the NumPy library, used to store and manipulate
largeamounts of numerical data efficiently.
21.
Numpy arrays continue…….
WHYNUMPY ARRAY CONSUME LESS MEMORY
NumPy arrays consume less memory and allow faster
data manipulation.
NumPy arrays consume less memory because they store
data in contiguous memory blocks with a fixed data
type for all elements. In contrast, Python lists store
references to objects, which requires additional
memory for metadata and object pointers
22.
Continue…….
Basics:
NumPyarrays are like Python lists but much faster and support
advanced operations.
They are homogeneous, meaning all elements must be of the same
data type.
BENEFITS :
They allow element-wise operations (e.g., addition, subtraction).
They consume less memory compared to Python lists.
NumPy arrays support multi-dimensional data.
23.
Continue…..
Types of Arrays
1DArrays:
A single row of data (like a list).
CODE
arr_1d = np.array([1, 2, 3, 4])
print(arr_1d) # Output: [1 2 3 4]
2D Arrays:
Rows and columns (like a matrix).
24.
Continue…….
CODE
arr_2d =np.array([[1, 2], [3, 4]])
print(arr_2d)
3D Arrays:
A collection of matrices (like a cube).
CODE
arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr_3d)
Indexing, Slicing
Indexing.
Access elements using their position(indices ).
CODE FOR 1D ARRAY
arr = np.array([10, 20, 30, 40])
print(arr[2]) # Output: 30
# print(arr) # for all element
For multi-dimensional arrays
CODE FOR 2D ARRAY
arr_2d = np.array([[1, 2, 3],
[4, 5, 6]])
27.
Continue….
print(arr_2d[1, 2])# Output: 6
# acess the rows and column
all_rows_col = arr_2d[:,:]
print(all_rows_col)
#acess all columns for specific row
row_2 = arr_2d[2, :] # Access the 2nd row
# and all columns
28.
Continue…..
ACCESS ALLROW FOR SPECIFIC COLUMN:
column2 = arr[:, 2]
print(column2)
more on indexing
ARR = arr[1:4,1:4] # from row 1 to 3 rows and column
ARR = arr[:2,:] # it give row 0 and 1 row and
# all columns
29.
Continue……
3D ArrayIndexing
A 3D array can be thought of as a collection of 2D arrays
(matrices) stacked along a third dimension.
INDEXING
arry_3d = [
[[11,22,33]
[12,13,14 ],
[[9, 2, 5 ]
[3, 4, 6 ]]]
30.
Continue……
Print(arry_3d)
For eachelement
arr_3d_ele = arr_3d(0,1,2) # 14
print( arr_3d_ele)
for num of rows and columns
x = arr_3d[0, : , :] # give all rows
and column on 0 dimension
31.
Continue….
x= arr_3d[0,0:2 , 0:2] # give 0 num dimension, (0
, 1 row and
print(x) # index (0,1)
columns
x= arr_3d[0:2 , 0:3 , 0:2] # includes (0,
and 1 ) dimension and (0 #
to 2 row and (0 to 1 index) column
32.
Continue……
Reshaping Arraysin NumPy
Reshaping in NumPy allows you to change the shape
(number of rows and columns) of an array without changing its
data. This is useful when you need to transform arrays for
operations, modeling, or visualization.
RESHAPE 1D TO 2D ARRAY
import numpy as np
# Original array (1D)
Arrays functions
Function Description
np.array()
Createan array from a list or
tuple
np.zeros(shape) Create an array filled with zeros
np.ones(shape) Create an array filled with ones
np.full(shape, fill)
Create an array filled with
specific value