Numpy
Definition:
● NumPy, short for Numerical Python, is a powerful Python library for numerical
computations.
● It's widely used in scientific computing, data analysis, machine learning, and
engineering because of its ability to efficiently process large, multi-dimensional
arrays and matrices.
● NumPy provides an extensive collection of mathematical functions to operate on
these arrays, making complex calculations much faster and more concise than using
pure Python.
Key Concepts and Features
1. Arrays: The core of NumPy is the ndarray (n-dimensional array), which allows
efficient storage and manipulation of large datasets. Unlike Python lists, NumPy
arrays are homogenous, meaning they contain elements of a single data type, which
allows for optimized performance.
2. Vectorization: One of NumPy’s strengths is its support for vectorized operations.
Instead of using loops, you can perform element-wise operations on entire arrays at
once, which makes the code faster and cleaner.
3. Broadcasting: This is a powerful feature that allows you to perform operations on
arrays of different shapes and sizes by automatically “broadcasting” smaller arrays across
larger ones in a compatible manner.
4.Mathematical Functions: NumPy provides a wide array of functions for linear algebra,
statistics, Fourier transformations, and random number generation.
5.Indexing and Slicing: Similar to lists, you can access and modify subsets of a NumPy
array using indices and slices. NumPy also supports advanced indexing and Boolean
masking, which allows for more complex data selection.
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.
● NumPy is a Python library and is written partially in Python, but
most of the parts that require fast computation are written in C or
C++.
Advantages of NumPy over python Nested List
● Performance (Speed)
∙ NumPy: Written in C, it uses optimized libraries and vectorized operations, making
computations much faster.
∙ Nested Lists: Operations on nested lists are executed in Python's interpreted loops,
which are much slower.
Memory Efficiency
● NumPy: Stores data in contiguous blocks of memory with a fixed type, leading to
much smaller memory footprints.
● Nested Lists: Each element is a Python object (which has additional metadata like
type information and reference counts), making it more memory-intensive.
Ease of Computation
• NumPy: Supports vectorized operations (e.g., addition, multiplication) without
explicit loops.
• Nested Lists: Require explicit looping to perform operations.
Multidimensional Arrays
•NumPy: Provides efficient tools to handle and manipulate multidimensional arrays.
•Nested Lists: Working with nested lists for multidimensional data is
cumbersome and error-prone.
Broadcasting
•NumPy: Automatically expands arrays of different shapes for
element-wise operations.
•Nested Lists: We must explicitly write logic to handle different shapes.
Installation of NumPy
● pip install numpy
Note:
NumPy is usually imported under the ’np’ alias
In python alias are an alternate name for referring to the same
thing
Checking numpy version
import numpy as np
print(np.__version__)
o/p:
1.26.4
Creating a one-dimensional NumPy array
● The One-dimensional array contains elements only in one dimension. In other
words, the shape of the NumPy array should contain only one value in the tuple..
● Create 1-D NumPy Array using Array() Function
import numpy as np
# creating the list
list = [100, 200, 300, 400]
# creating 1-d array
n = np.array(list)
print(n)
OUTPUT:[100 200 300 400]
Create a 1-d array using Arange function
● Arange function creates an array with values in a specific interval, with a specified
step size.
● Parameters:
start:The starting value of the sequence.
stop:The end value of the sequence (exclusive).
step:The spacing between consecutive values. Default is 1.
Use Case: Useful when you want a range of values with a specific interval (like a
Python range function
import numpy as np
# creating 1-d array
x = np.arange(3, 10, 2)
print(x)
OUTPUT:
[3 5 7 9]
Create a 1-d array using linspace function
∙ Purpose: Creates an array with a specified number of evenly spaced values between a start and an
end value.
∙ Parameters:
o start: The starting value of the sequence.
o stop: The end value of the sequence.
o num: Number of points to generate (default is 50).
o endpoint: If True (default), the stop value is included. If False, the stop value is excluded.
∙ Use Case: Useful when you want a specific number of points between two values.
import numpy as np
x = np.linspace(3, 10, 3)
print(x)
OUTPUT:
[ 3. 6.5 10. ]
Some More examples
import numpy as np
# From a list
arr_from_list = np.array([1, 2, 3, 4, 5])
# From a tuple
arr_from_tuple = np.array((1, 2, 3, 4, 5))
print("Array from list:", arr_from_list)
print("Array from tuple:", arr_from_tuple)
o/p:
Array from list: [1 2 3 4 5]
Array from tuple: [1 2 3 4 5]
# Using arange
arr_arange = np.arange(5) # Array from 0 to 4
# Using linspace
arr_linspace = np.linspace(0, 1, 5) # 5 evenly spaced
values from 0 to 1
# Using random
arr_random = np.random.rand(5) # Array of 5 random floats
between 0 and 1
print("Array from list:", arr)
print("Array with arange:", arr_arange)
print("Array with linspace:", arr_linspace)
print("Array of random floats:", arr_random)
Array from list: [1 2 3 4 5]
Array with arange: [0 1 2 3 4]
Array with linspace: [0. 0.25 0.5 0.75 1. ]
OUTPUT
Array of random floats: [0.75762723 0.21806401 0.08906999 0.5627405
0.85459441]
Shape of an Array
● The shape of an array is the number of elements in each dimension.
● NumPy arrays have an attribute called shape that returns a tuple with each
index having the number of corresponding elements.
○ import numpy as np
○ arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
○ print(arr.shape)
OUTPUT:(2,4)
● The example above returns (2, 4), which means that the array has 2
dimensions,where the first dimension has 2 elements and the second has 4.
● ndim() method returns the number of
dimensions of an array.
import numpy as np
arr=np.array([1,2,3,4])
print(arr.ndim)
OUTPUT:1
import numpy as np
arr1=np.array([[1,2,3,4],[5,6,7,8]])
arr2=np.array([[[1],[3]],[[5],[7]]])
print(arr1.ndim)
print(arr2.ndim)
OUTPUT: 2
3
import numpy as np
arr = np.array([1, 2, 3, 4,5,6,7], ndmin=5)
print(arr)
print('shape of array :', arr.shape)
OUTPUT:
Reshaping arrays
● 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 or change number
of elements in each dimension.
● Syntax:
Reshaping : 1-D to 2D
# importing numpy OUTPUT
import numpy as np Array : [ 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16]
# creating a numpy array First Reshaped Array :
array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16])
[[ 1 2 3 4]
[ 5 6 7 8]
# printing array [ 9 10 11 12]
print("Array : ",array) [13 14 15 16]]
reshaped1 = array.reshape((4, 4)) Second Reshaped Array :
[[ 1 2 3 4 5 6 7 8] [ 9 10
# printing reshaped array
print("First Reshaped Array : ")
11 12 13 14 15 16]]
print(reshaped1)
# creating another reshaped array
reshaped2 = np.reshape(array, (2, 8))
# printing reshaped array
print("Second Reshaped Array : ")
print(reshaped2)
Reshaping : 1-D to 3D
import numpy as np
# creating a numpy array
array = np.array([1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15, 16])
# printing array
print("Array : " + str(array))
# reshaping numpy array
# converting it to 3-D from 1-D array
reshaped = array.reshape((2, 2, 4))
# printing reshaped array
print("Reshaped 3-D Array : ")
print(reshaped)
Can We Reshape Into any Shape?
● Yes, as long as the elements required for reshaping are equal in
both shapes.
● We can reshape an 8 elements 1D array into 4 elements in 2
rows 2D array but we cannot reshape it into a 3 elements 3
rows 2D array as that would require 3x3 = 9 elements.
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
newarr = arr.reshape(3, 3)
print(newarr)
Unknown Dimension
● We are allowed to have one "unknown" dimension.
● Meaning that we do not have to specify an exact number for one of the
dimensions in the reshape method.
● Pass -1 as the value, and NumPy will calculate this number for you.
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
newarr = arr.reshape(2, 2, -1)
print(newarr)
Flattening the array
● Flattening array means converting a multidimensional array into a 1D
array.
● Use reshape(-1) to do this.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
newarr = arr.reshape(-1)
print(newarr)
OUTPUT:
Element-wise Operations
● NumPy supports element-wise operations, which apply operations to each
element in the array.
arr = np.array([1, 2, 3, 4, 5])
# Basic operations
arr_add = arr + 10
arr_mult = arr * 2
arr_squared = arr ** 2 OUTPUT
Original Array: [1 2 3 4 5]
print("Original Array:", arr)
Array + 10: [11 12 13 14 15]
print("Array + 10:", arr_add) Array * 2: [ 2 4 6 8 10]
print("Array * 2:", arr_mult) Array Squared: [ 1 4 9 16 25]
print("Array Squared:", arr_squared)
Aggregate Operations
Aggregation functions in NumPy allow you to perform computations across the
entire array or along a specified axis
Note:
The axis argument in NumPy array specifies which axis to perform the operation along .
● 1D Array (axis=0):
The only axis available. Operations will apply across all elements of the array.
● 2D Array:
axis=0: Operates along rows (vertically).
For example :np.sum(arr_2d,axis=0) calculates the sum of each column
axis=1: Operates along columns (horizontally)
For example :np.sum(arr_2d,axis=1) calculates the sum of each row
numpy.sum: Computes the sum of array elements
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)
total_sum = np.sum(arr)
print(“Total Sum:”, total_sum)
OUTPUT
column_sum = np.sum(arr, axis=0)
row_sum = np.sum(arr, axis=1)
print(“Column Sum:”, column_sum)
print(“Row Sum:”, row_sum)
numpy.mean :Computes the mean(average) of array
elements
arr = np.array([[1, 2, 3], [4, 5, 6]])
# Mean of all elements in the array
print(arr)
average = np.mean(arr)
print("Mean:", average)
# Mean along a specific axis
column_mean = np.mean(arr, axis=0)
row_mean = np.mean(arr, axis=1)
print("Column Mean:", column_mean)
print("Row Mean:", row_mean)
Numpy.min and numpy.max :Compute the min and max
values in array
arr = np.array([[1, 2, 3], [4, 5, 6]])
# Minimum and maximum values in the array
print(arr)
min_value = np.min(arr)
max_value = np.max(arr)
print("Minimum Value:", min_value)
print("Maximum Value:", max_value)
# Minimum and maximum along a specific axis
min_col = np.min(arr, axis=0)
max_row = np.max(arr, axis=1)
print("Minimum Value Along Columns:", min_col)
print("Maximum Value Along Rows:", max_row)
Numpy.median :Computes the median of array elements
arr = np.array([[1, 2, 3], [4, 5, 6]])
# Median of all elements in the array
print(arr)
median = np.median(arr)
print("Median:", median)
# Median along a specific axis
column_median = np.median(arr, axis=0)
row_median = np.median(arr, axis=1)
print("Column Median:", column_median)
print("Row Median:", row_median)
Some of the main aggregate functions in NumPy
NumPy Array Indexing
● Indexing allows accessing individual elements. NumPy uses zero-based indexing, just
like Python lists.
arr = np.array([10, 20, 30, 40, 50])
# Access elements
first_element = arr[0]
last_element = arr[-1]
print("First element:", first_element)
print("Last element:", last_element)
OUTPUT:
First element: 10 Last element: 50
Array Slicing
● Slicing lets you extract subarrays. Use the colon `:` to specify start, stop, and step.
arr = np.array([10, 20, 30, 40, 50])
# Slicing
slice1 = arr[1:4] # Elements from index 1 to 3
slice2
slice3 == arr[:3]
arr[::2] ## First
Every3 other
elements
element
print("Slice
print("Slice [1:4]:", slice1)
print("Slice [:3]:",
[::2]:",slice2)
slice3)
OUTPUT:
Slice
Slice [1:4]:[10
[20 3030]40]
Slice [:3]:
[::2]: [102030 50]
Ways to add row/columns in numpy array
● Add columns in the Numpy array
Method 1: Using np.append()
Method 2:Using np.concatenate
Concatenate function concatenate a sequence of arrays along an existing axis.
Method 3: Using np.insert()
● The insert method adds the values along the given axis at specified indices.
The insert() method takes four arguments:
•array - original array
•obj - indices at which values are inserted
•values - the array to be inserted at the given position
•axis(optional) - the axis along which the values are inserted
Add row in Numpy array
● Method 1: Using np.insert()
Method 2: Using np.append()
● Using np.insert()