NumPy • Pandas • Matplotlib • TensorFlow/Keras • PyTorch
Deep Learning depends on powerful Python libraries that make data handling, visualization, and model building much easier. In this chapter, you will learn about the most important libraries used by Deep Learning engineers and researchers: NumPy, Pandas, Matplotlib, TensorFlow/Keras, and PyTorch.
These tools help you load data, visualize trends, build neural networks, and train models.
1. NumPy
NumPy (Numerical Python) is the foundation of scientific computing in Python.
It helps us work with numbers, arrays, and mathematical operations quickly and efficiently.
Why NumPy is Important
- Stores data efficiently as arrays
- Performs fast mathematical operations (much faster than pure Python)
- Used by almost every deep learning library internally
Real-World Example
A neural network receives input in the form of arrays, such as:
- Image pixels
- Temperature records
- Stock prices
NumPy makes it easy to manipulate such data.
Basic NumPy Example
import numpy as np
# Creating an array
arr = np.array([1, 2, 3, 4])
# Performing an operation
doubled = arr * 2
print("Original:", arr)
print("Doubled:", doubled)2. Pandas
Pandas is used for working with tables, also known as dataframes.
It is perfect for handling datasets that come from spreadsheets, CSV files, or databases.
Why Pandas is Important
- Makes it easy to clean data
- Helps filter rows and columns
- Supports reading/writing data files
- Helps create training data for models
Real-World Example
If you are training a model to predict house prices, Pandas can help you:
- Load the dataset
- Remove missing values
- Analyze columns like size, rooms, and location
Basic Pandas Example
import pandas as pd
# Load data from a CSV file
data = pd.read_csv("students.csv")
# View first few rows
print(data.head())
# Select a column
print(data["Marks"])3. Matplotlib
Matplotlib is a library used for visualizing data.
It helps create charts and graphs.
Why Matplotlib is Important
- Helps understand data patterns
- Shows training progress using loss/accuracy curves
- Helps detect overfitting
Real-World Example
When training a model, you can plot:
- Loss decreasing over time
- Accuracy increasing over time
Seeing these graphs helps you understand whether your model is learning correctly.
Basic Matplotlib Example
import matplotlib.pyplot as plt
marks = [50, 60, 65, 80, 90]
students = ["A", "B", "C", "D", "E"]
plt.bar(students, marks)
plt.title("Students Score Chart")
plt.xlabel("Students")
plt.ylabel("Marks")
plt.show()4. TensorFlow / Keras Basics
TensorFlow, developed by Google, is one of the most popular deep learning libraries.
Keras is a user-friendly API inside TensorFlow that makes building neural networks easy and intuitive.
Why TensorFlow/Keras is Important
- Supports building advanced neural networks
- Works on CPU, GPU, and TPU
- Easy to use for beginners
- Used in real companies like Google, DeepMind, and Airbnb
Simple Keras Model Example
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
# Build a simple model
model = Sequential([
Dense(8, activation='relu', input_shape=(4,)),
Dense(1, activation='sigmoid')
])
model.compile(
optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy']
)
print(model.summary())What This Model Does
- First layer: learns 8 features
- Second layer: predicts a yes/no output
- Works well for binary classification tasks
5. PyTorch Basics
PyTorch is another leading deep learning framework created by Facebook (Meta).
It is loved by researchers because it is very flexible and easy to debug.
Why PyTorch is Important
- Very simple and "Pythonic" syntax
- Great for research and rapid prototyping
- Used by universities and advanced AI labs
Basic PyTorch Example
import torch
import torch.nn as nn
import torch.optim as optim
# Simple linear model
model = nn.Sequential(
nn.Linear(4, 8),
nn.ReLU(),
nn.Linear(8, 1),
nn.Sigmoid()
)
# Loss function and optimizer
loss_fn = nn.BCELoss()
optimizer = optim.Adam(model.parameters(), lr=0.01)
# Example input
x = torch.rand(1, 4)
output = model(x)
print(output)What This Model Does
- Takes 4 input values
- Passes them through ReLU
- Outputs a probability using Sigmoid
6. Summary Table of Python Libraries
| Library | Main Use | Why It's Important |
|---|---|---|
| NumPy | Arrays & mathematical operations | Fast and essential for all ML libraries |
| Pandas | Tables, data cleaning | Helps prepare datasets |
| Matplotlib | Charts and graphs | Visualizes data & training progress |
| TensorFlow/Keras | Deep learning models | Easy, powerful, industry standard |
| PyTorch | Research and neural networks | Very flexible and intuitive |
7. Real-World Example: Building an AI for Student Score Prediction
Here is how these libraries work together:
- NumPy: Converts numbers into arrays
- Pandas: Loads and cleans the dataset
- Matplotlib: Plots graphs of performance
- TensorFlow/PyTorch: Builds the prediction model
- Training: Model learns patterns in student study habits
All these libraries support each other like parts of a machine.