CHAPTER 3: NUMPY
Introduc on to NumPy
What is NumPy?
NumPy stands for Numerical Python.
It is one of the most important libraries in Python used for working with numerical data.
Before NumPy, programmers used Python Lists to store data.
Example:
marks = [85, 90, 78, 95, 88]
This works well for small amounts of data.
But in Data Analy cs and Data Science, we o en work with:
Thousands of rows
Millions of records
Large datasets
Python Lists become slower when handling large data.
To solve this problem, NumPy provides a special data structure called Array.
NumPy Arrays:
Use less memory
Work faster
Support mathema cal opera ons
Handle large datasets efficiently
Because of these advantages, NumPy is widely used in:
Data Analy cs
Data Science
Machine Learning
Ar ficial Intelligence
Impor ng NumPy
Before using NumPy, it must be imported.
import numpy as np
Explana on
numpy → library name
np → alias (short name)
Instead of wri ng:
[Link]()
we can write:
[Link]()
which is shorter and easier.
1. NUMPY ARRAYS
What is an Array?
An Array is a collec on of similar types of data stored together.
Think of an array as a more powerful and faster version of a Python List.
Example:
Python List
marks = [85, 90, 78, 95]
NumPy Array
import numpy as np
marks = [Link]([85, 90, 78, 95])
Both store mul ple values.
The difference is that NumPy Arrays are op mized for numerical opera ons.
Crea ng a NumPy Array
import numpy as np
arr = [Link]([10,20,30,40,50])
print(arr)
Output
[10 20 30 40 50]
Checking Array Type
print(type(arr))
Output
<class '[Link]'>
Explana on
ndarray means:
N-Dimensional Array
This is the main object used in NumPy.
Array Arithme c
One major advantage of arrays is mathema cal opera ons.
Example
import numpy as np
arr = [Link]([10,20,30])
print(arr + 10)
Output
[20 30 40]
Explana on
10 is added to every element automa cally.
Python Lists cannot do this directly.
2. ARANGE()
What is arange()?
arange() is used to generate numbers automa cally within a range.
Instead of manually typing values, NumPy creates them for us.
Syntax
[Link](start, stop, step)
Parameters
start → star ng value
stop → ending value (not included)
step → gap between values
Example 1
import numpy as np
arr = [Link](1,11)
print(arr)
Output
[ 1 2 3 4 5 6 7 8 9 10]
Explana on
Starts at 1 and stops before 11.
Example 2
arr = [Link](0,20,2)
print(arr)
Output
[ 0 2 4 6 8 10 12 14 16 18]
Explana on
Step size is 2.
Only even numbers are generated.
Example 3
arr = [Link](5)
print(arr)
Output
[0 1 2 3 4]
Explana on
If only one value is given, NumPy starts from 0.
3. LINSPACE()
What is linspace()?
linspace() creates equally spaced numbers between two values.
Unlike arange(), we specify how many values we need.
NumPy automa cally calculates the spacing.
Syntax
[Link](start, stop, number_of_values)
Example 1
import numpy as np
arr = [Link](1,10,5)
print(arr)
Output
[ 1. 3.25 5.5 7.75 10. ]
Explana on
We requested:
Start = 1
End = 10
Total values = 5
NumPy automa cally distributed 5 values equally.
Example 2
arr = [Link](0,100,11)
print(arr)
Output
[ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.]
Difference Between arange() and linspace()
arange()
[Link](0,10,2)
Output
[0 2 4 6 8]
Gap is specified.
linspace()
[Link](0,10,5)
Output
[ 0. 2.5 5. 7.5 10. ]
Number of values is specified.
4. RESHAPE()
What is reshape()?
reshape() changes the shape of an array.
Some mes data is stored in one row.
But we may want it in mul ple rows and columns.
reshape() helps us reorganize the data.
Example
import numpy as np
arr = [Link](1,10)
print(arr)
Output
[1 2 3 4 5 6 7 8 9]
Convert to 3×3 Matrix
arr = [Link](3,3)
print(arr)
Output
[[1 2 3]
[4 5 6]
[7 8 9]]
Explana on
3 rows × 3 columns = 9 values
Since array contains 9 values, reshape works perfectly.
Example
arr = [Link](1,13)
arr = [Link](3,4)
print(arr)
Output
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
5. INDEXING AND SLICING
Why Indexing?
Indexing is used to access a specific element from an array.
1D Array Indexing
import numpy as np
arr = [Link]([10,20,30,40,50])
print(arr[0])
Output
10
print(arr[3])
Output
40
Nega ve Indexing
print(arr[-1])
Output
50
Array Slicing
Slicing means selec ng mul ple elements.
Example
arr = [Link]([10,20,30,40,50])
print(arr[1:4])
Output
[20 30 40]
Explana on
Starts from index 1.
Stops before index 4.
2D Array Indexing
arr = [Link]([
[1,2,3],
[4,5,6],
[7,8,9]
])
print(arr[1,2])
Output
Explana on
Row 1 → [4,5,6]
Column 2 → 6
2D Array Slicing
print(arr[:,1])
Output
[2 5 8]
Explana on
Select all rows.
Take column 1.
6. AGGREGATION FUNCTIONS
What are Aggrega on Func ons?
Aggrega on Func ons summarize data into a single value.
Instead of checking every value manually, NumPy calculates results quickly.
Common func ons:
sum()
mean()
max()
min()
sum()
Purpose
Calculates total of all values.
Example
import numpy as np
arr = [Link]([10,20,30,40])
print([Link](arr))
Output
100
mean()
Purpose
Calculates average.
Formula
Average = Sum of values / Number of values
Example
arr = [Link]([10,20,30,40])
print([Link](arr))
Output
25.0
Explana on
(10 + 20 + 30 + 40) / 4
= 25
max()
Purpose
Finds the largest value.
Example
arr = [Link]([10,50,30,80])
print([Link](arr))
Output
80
min()
Purpose
Finds the smallest value.
Example
arr = [Link]([10,50,30,80])
print([Link](arr))
Output
10
Aggrega on on 2D Arrays
arr = [Link]([
[10,20,30],
[40,50,60]
])
print([Link](arr))
Output
210
Row-wise Sum
print([Link](arr, axis=1))
Output
[ 60 150]
Explana on
Row 1:
10 + 20 + 30 = 60
Row 2:
40 + 50 + 60 = 150
Column-wise Sum
print([Link](arr, axis=0))
Output
[50 70 90]
Explana on
Column 1:
10 + 40 = 50
Column 2:
20 + 50 = 70
Column 3:
30 + 60 = 90
Summary
Arrays
Store mul ple values efficiently and perform fast numerical opera ons.
arange()
Creates values using start, stop, and step.
linspace()
Creates equally spaced values by specifying the number of values required.
reshape()
Changes the dimensions of an array.
Indexing
Accesses a specific element.
Slicing
Accesses mul ple elements.
Aggrega on Func ons
Used to summarize data.
[Link]() → Total
[Link]() → Average
[Link]() → Largest value
[Link]() → Smallest value
These concepts form the founda on of NumPy and are heavily used in Data Analy cs, Data
Science, Machine Learning, and Ar ficial Intelligence.