```html
``` Skip to contentPython has become the world’s most popular programming language for Machine Learning, Artificial Intelligence, Data Science, and Data Analytics. One of the biggest reasons for its popularity is its extensive collection of libraries. Instead of writing every program from scratch, developers can use pre-built libraries that provide thousands of ready-to-use functions, classes, and tools. These libraries save time, improve productivity, and make complex tasks much easier.
Whether you are analyzing data, building Machine Learning models, creating visualizations, or developing Artificial Intelligence applications, Python libraries simplify the development process. Modern Machine Learning projects rely heavily on libraries such as NumPy, Pandas, Matplotlib, Seaborn, Scikit-learn, TensorFlow, and PyTorch. Learning how to install, import, and use these libraries is one of the most important skills for every Python programmer.
In this lesson, you will learn what Python libraries are, why they are important, how to install and import them, and why NumPy and Pandas are considered the foundation of Machine Learning and Data Science.
A Python library is a collection of pre-written code that provides reusable functions, classes, and modules to perform specific tasks. Instead of writing hundreds of lines of code yourself, you can simply import a library and use its built-in functionality.
For example, if you need to calculate mathematical operations, read a CSV file, create charts, or train a Machine Learning model, Python libraries provide ready-made functions that perform these tasks efficiently.
Imagine writing a program to calculate complex mathematical operations, process thousands of rows of data, or create statistical graphs manually. It would require hundreds or even thousands of lines of code.
Using a Python library, the same task can often be completed with just a few lines of code. This increases productivity, reduces programming errors, and allows developers to focus on solving business problems instead of writing repetitive code.
Python libraries have transformed software development by making advanced programming accessible to beginners and professionals alike. Libraries allow developers to build powerful applications without reinventing existing solutions.
A library contains modules, and each module contains functions, classes, and variables. Before using any library, it must be imported into your Python program using the import statement.
import math
print(math.sqrt(25))
Output
5.0
In this example, the math library provides the sqrt() function for calculating the square root of a number.
Python libraries are divided into two main categories.
| Standard Library | Third-Party Library |
|---|---|
| Installed with Python. | Installed separately. |
| No installation required. | Requires pip installation. |
| Examples: math, os, datetime. | Examples: NumPy, Pandas, TensorFlow. |
| General programming tasks. | Specialized tasks such as Machine Learning. |
| Library | Purpose |
|---|---|
| math | Mathematical operations. |
| random | Random number generation. |
| datetime | Date and time operations. |
| os | Operating system functions. |
| sys | Python interpreter interaction. |
| statistics | Statistical calculations. |
| Library | Purpose |
|---|---|
| NumPy | Numerical computing. |
| Pandas | Data manipulation. |
| Matplotlib | Data visualization. |
| Seaborn | Statistical visualization. |
| Scikit-learn | Machine Learning algorithms. |
| TensorFlow | Deep Learning. |
| PyTorch | Deep Learning. |
| OpenCV | Computer Vision. |
Most third-party libraries are installed using the Python package manager called pip.
pip install numpy
pip install pandas
pip install matplotlib
pip install scikit-learn
If you are using Jupyter Notebook or Google Colab, packages can also be installed directly inside notebook cells.
!pip install seaborn
After installation, a library must be imported before it can be used.
import numpy
import pandas
Most Python developers use aliases to shorten library names.
import numpy as np
import pandas as pd
Using aliases makes code shorter and follows industry standards.
Sometimes projects require a specific version of a library. You can check the installed version easily.
import numpy as np
print(np.__version__)
Almost every Machine Learning project begins with data. Before training models, developers must load datasets, clean missing values, manipulate data, perform mathematical operations, and prepare features. NumPy and Pandas provide all the tools required for these tasks.
| Library | Primary Use |
|---|---|
| NumPy | Arrays and mathematical operations. |
| Pandas | Data cleaning and analysis. |
| Matplotlib | Charts and graphs. |
| Seaborn | Statistical visualization. |
| Scikit-learn | Machine Learning models. |
| TensorFlow | Deep Learning. |
| PyTorch | Neural networks and AI. |
np and pd.In the next part, you will learn NumPy Fundamentals, including NumPy arrays, dimensions, indexing, slicing, mathematical operations, broadcasting, reshaping, and random number generation with practical Machine Learning examples.
NumPy (Numerical Python) is one of the most important Python libraries used in Data Science, Artificial Intelligence, Machine Learning, Scientific Computing, and Data Analytics. It provides a powerful multidimensional array object along with thousands of mathematical functions for performing fast numerical computations.
Before NumPy was introduced, Python developers relied on lists to store numerical data. Although Python lists are flexible, they become slow and memory-intensive when working with millions of records. NumPy solves this problem by introducing the ndarray (N-dimensional Array), which stores data more efficiently and performs calculations much faster.
Today, almost every Machine Learning library, including Scikit-learn, TensorFlow, PyTorch, OpenCV, SciPy, and Pandas, relies on NumPy internally. Learning NumPy is therefore one of the first essential steps toward becoming a Machine Learning Engineer or Data Scientist.
Machine Learning algorithms perform millions of mathematical calculations while training predictive models. NumPy provides optimized operations that make these calculations significantly faster than standard Python lists.
If NumPy is not already installed, install it using pip.
pip install numpy
Inside Jupyter Notebook or Google Colab:
!pip install numpy
The standard convention is to import NumPy using the alias np.
import numpy as np
This alias is used by almost every Python programmer and appears throughout Machine Learning documentation.
import numpy as np
print(np.__version__)
The core data structure in NumPy is the ndarray (N-dimensional Array). Unlike Python lists, NumPy arrays store elements of the same data type in contiguous memory, allowing much faster computations.
An ndarray can represent:
import numpy as np
numbers = np.array([10,20,30,40,50])
print(numbers)
Output
[10 20 30 40 50]
import numpy as np
matrix = np.array([[1,2,3],
[4,5,6]])
print(matrix)
Output
[[1 2 3]
[4 5 6]]
import numpy as np
array3d = np.array([
[[1,2],[3,4]],
[[5,6],[7,8]]
])
print(array3d)
Every NumPy array has dimensions that describe its structure.
| Dimension | Description |
|---|---|
| 1D | Vector |
| 2D | Matrix |
| 3D | Tensor |
import numpy as np
arr = np.array([[10,20,30],
[40,50,60]])
print(arr.ndim)
Output
2
The shape attribute returns the number of rows and columns.
import numpy as np
arr = np.array([[1,2,3],
[4,5,6]])
print(arr.shape)
Output
(2,3)
This means the array has:
import numpy as np
arr = np.array([[1,2,3],
[4,5,6]])
print(arr.size)
Output
6
import numpy as np
arr = np.array([1,2,3])
print(arr.dtype)
Output
int64
np.zeros((3,4))
Creates a 3 × 4 matrix filled with zeros.
np.ones((2,5))
Creates a matrix filled with ones.
np.eye(4)
Creates a 4 × 4 identity matrix.
np.arange(1,11)
Output
[1 2 3 4 5 6 7 8 9 10]
np.arange(2,21,2)
np.linspace(0,10,5)
Output
[0. 2.5 5. 7.5 10.]
np.random.rand(5)
np.random.randint(1,100,10)
np.random.rand(3,3)
Suppose you have student marks that need to be processed before training a Machine Learning model.
import numpy as np
marks = np.array([78,82,91,66,88])
print("Average:", marks.mean())
print("Highest:", marks.max())
print("Lowest:", marks.min())
Output
Average: 81.0
Highest: 91
Lowest: 66
In the next part, you will learn NumPy Indexing, Slicing, Reshaping, Broadcasting, Vectorized Operations, Universal Functions (ufuncs), and Mathematical Operations with practical Machine Learning examples.
One of the biggest advantages of NumPy is its ability to quickly access, modify, and manipulate data stored inside arrays. This is achieved using Indexing and Slicing. These techniques are widely used in Machine Learning because datasets often contain thousands or even millions of rows. Instead of processing the entire dataset, data scientists frequently select only the rows and columns required for analysis.
Understanding NumPy indexing and slicing is essential before working with Pandas, Scikit-learn, TensorFlow, or any Machine Learning framework.
Indexing allows you to access individual elements from a NumPy array. Python uses zero-based indexing, which means the first element always has index 0.
import numpy as np
arr = np.array([10,20,30,40,50])
print(arr[0])
print(arr[2])
print(arr[4])
Output
10
30
50
Negative indexing starts from the end of the array.
arr = np.array([10,20,30,40,50])
print(arr[-1])
print(arr[-2])
Output
50
40
Two-dimensional arrays require both row and column indexes.
matrix = np.array([[10,20,30],
[40,50,60],
[70,80,90]])
print(matrix[0,1])
print(matrix[2,2])
Output
20
90
Slicing extracts a portion of an array using the syntax:
array[start:stop:step]
arr = np.array([10,20,30,40,50,60])
print(arr[1:5])
Output
[20 30 40 50]
print(arr[:4])
Output
[10 20 30 40]
print(arr[2:])
Output
[30 40 50 60]
print(arr[::2])
Output
[10 30 50]
matrix = np.array([[10,20,30],
[40,50,60],
[70,80,90]])
print(matrix[1])
Output
[40 50 60]
print(matrix[:,1])
Output
[20 50 80]
print(matrix[0:2])
print(matrix[:,0:2])
arr = np.array([10,20,30])
arr[1] = 100
print(arr)
Output
[10 100 30]
Reshaping changes the dimensions of an array without changing its data.
arr = np.arange(12)
matrix = arr.reshape(3,4)
print(matrix)
Output
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Flatten converts a multidimensional array into a one-dimensional array.
matrix = np.array([[1,2],
[3,4]])
print(matrix.flatten())
Output
[1 2 3 4]
Broadcasting is one of NumPy’s most powerful features. It allows arithmetic operations between arrays of different shapes without writing loops.
arr = np.array([1,2,3,4])
print(arr + 5)
Output
[6 7 8 9]
Instead of adding 5 manually to every element, NumPy performs the operation automatically.
Vectorization allows operations to be performed on an entire array simultaneously, making NumPy significantly faster than traditional Python loops.
arr1 = np.array([1,2,3])
arr2 = np.array([4,5,6])
print(arr1 + arr2)
print(arr1 * arr2)
Output
[5 7 9]
[4 10 18]
Universal Functions, commonly called ufuncs, perform element-wise operations efficiently.
| Function | Description |
|---|---|
| np.sqrt() | Square Root |
| np.square() | Square |
| np.exp() | Exponential |
| np.log() | Natural Logarithm |
| np.sin() | Sine |
| np.cos() | Cosine |
| np.abs() | Absolute Value |
arr = np.array([1,4,9,16])
print(np.sqrt(arr))
Output
[1. 2. 3. 4.]
NumPy provides many built-in statistical functions used in Data Science and Machine Learning.
marks = np.array([72,84,91,67,88])
print(np.mean(marks))
print(np.median(marks))
print(np.std(marks))
print(np.var(marks))
print(np.min(marks))
print(np.max(marks))
arr = np.array([10,20,30])
print(arr + 5)
print(arr - 5)
print(arr * 2)
print(arr / 2)
Suppose student marks need to be increased by 5 bonus points before training a Machine Learning model.
marks = np.array([65,72,81,90])
updated_marks = marks + 5
print(updated_marks)
Output
[70 77 86 95]
| Python List | NumPy Array |
|---|---|
| Slower | Much Faster |
| Higher Memory Usage | Efficient Memory Usage |
| Loop Required | Vectorized Operations |
| Limited Mathematical Functions | Thousands of Built-in Functions |
In the next lesson, you will learn Pandas for Machine Learning, including Series, DataFrames, reading CSV files, selecting rows and columns, filtering data, handling missing values, grouping data, and performing exploratory data analysis (EDA).