Python Unit 5
Python Unit 5
Unit – 𝐕
Python Libraries –
A Python library is a collection of pre-
written, reusable code that developers can
use to perform specific tasks or add
functionality to their programs. Libraries
help save time and effort by providing
ready-to-use modules and functions, so
developers don’t have to write code from
scratch for common or complex tasks.
• NumPy (Numerical Python): is a powerful
Python library used for numerical computing.
It provides support for arrays, matrices, and a
wide range of mathematical operations that
are optimized for performance. NumPy is a
1|Page
fundamental library for scientific computing
and is often used alongside other libraries like
pandas, matplotlib, and scikit-learn.
Features of NumPy –
1. Multi-dimensional Arrays: NumPy provides the
“ndarray” object for creating and manipulating
arrays of any dimension.
2. Mathematical Functions: A wide range of
mathematical operations, including
trigonometric, statistical, and algebraic
functions.
3. Broadcasting: Enables element-wise operations
on arrays of different shapes.
4. Linear Algebra: Tools for matrix operations,
eigenvalues, and linear equation solving.
5. Performance: Operations are implemented in C,
making them faster than pure Python loops.
6. Interoperability: Works seamlessly with other
libraries and data formats.
2|Page
Applications of NumPy –
1. Data Analysis: Efficiently handle large datasets.
2. Machine Learning: Used as a base for ML
libraries like TensorFlow and scikit-learn.
3. Scientific Computing: Solve equations, perform
simulations, and analyze data.
4. Image Processing: Manipulate image data stored
as arrays.
5. Signal Processing: Handle time-series and
waveform data.
# Create a 1D array
arr1 = np.array([1, 2, 3, 4])
# Create a 2D array
arr2 = np.array([[1, 2], [3, 4]])
3|Page
# Create an array of ones
ones = np.ones((2, 4))
2. Array Properties
arr = np.array([[1, 2, 3], [4, 5, 6]])
4|Page
arr2d = np.array([[1, 2, 3], [4, 5, 6]])
print(arr2d[0, 1]) # Element at row 0, column 1
print(arr2d[:, 1]) # All rows, column 1
4. Element-wise Operations
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Broadcasting example
arr3 = np.array([[1, 2, 3], [4, 5, 6]])
print(arr3 + 10) # Add 10 to every element
5. Mathematical Functions
arr = np.array([1, 2, 3, 4])
5|Page
6. Reshaping and Transposing
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(reshaped)
print(transposed)
7. Linear Algebra
from numpy.linalg import inv, det
# Matrix multiplication
result = np.dot(matrix, matrix)
# Matrix inverse
inverse = inv(matrix)
# Determinant
determinant = det(matrix)
6|Page
• Matplotlib: is a widely-used Python library for
creating static, animated, and interactive
visualizations. It is particularly useful for
generating plots, charts, and figures in data
analysis and scientific research. Matplotlib
works well with NumPy, pandas, and other
scientific libraries.
Features of Matplotlib –
1. Comprehensive Plotting: Line plots, bar charts,
histograms, scatter plots, pie charts, and more.
2. Customizability: High level of control over plot
appearance.
3. Integration: Works seamlessly with other
libraries like NumPy and pandas.
4. Interactive Capabilities: Interactive features in
Jupyter Notebooks or GUI backends.
5. Output Formats: Export plots to PNG, PDF, SVG,
etc.
Applications of Matplotlib –
1. Data Analysis: Explore and visualize data trends.
7|Page
2. Scientific Research: Create publication-quality
figures.
3. Machine Learning: Visualize model performance
and predictions.
4. Business Analytics: Generate charts and
dashboards.
plt.plot(x, y)
plt.title("Line Plot Example")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
8|Page
Types of Plots in Matplotlib –
1. Line Plot
import matplotlib.pyplot as plt
import numpy as np
plt.plot(x, y, label="sin(x)")
plt.plot(x, np.cos(x), label="cos(x)")
plt.title("Line Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.legend()
plt.grid()
plt.show()
2. Bar Chart
import matplotlib.pyplot as plt
9|Page
3. Histogram
import matplotlib.pyplot as plt
import numpy as np
data = np.random.randn(1000)
4. Scatter Plot
import matplotlib.pyplot as plt
import numpy as np
x = np.random.rand(50)
y = np.random.rand(50)
colors = np.random.rand(50)
sizes = np.random.rand(50) * 1000
5. Pie Chart
import matplotlib.pyplot as plt
10 | P a g e
labels = ['A', 'B', 'C', 'D']
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0) # Highlight second slice
6. Multiple Subplots
import matplotlib.pyplot as plt
import numpy as np
plt.subplot(2, 1, 2)
plt.plot(x, y2, label='cos(x)', color='red')
plt.legend()
plt.tight_layout()
plt.show()
11 | P a g e
• Pandas: Pandas is a powerful and widely-
used Python library for data analysis and
manipulation. It provides data structures
like DataFrame and Series, making it easier
to handle structured data efficiently.
Pandas is a key tool in data science,
enabling tasks like data cleaning,
exploration, transformation, and analysis.
Features of Pandas:
1. Data Structures:
• Series: 1D labeled data, similar to a column
in a spreadsheet.
• DataFrame: 2D labeled data, similar to a
table or spreadsheet.
2. Data Cleaning: Handle missing values, duplicate
data, and inconsistent formatting.
3. Data Manipulation: Filtering, sorting, merging,
grouping, and reshaping data.
4. Data Analysis: Aggregations, statistics, and pivot
tables.
12 | P a g e
5. File Handling: Read/write data in formats like
CSV, Excel, JSON, SQL, and more.
6. High Performance: Optimized for handling large
datasets.
Applications of Pandas:
1. Data Cleaning: Prepare raw data for analysis.
2. Exploratory Data Analysis (EDA): Summarize and
visualize datasets.
3. Data Transformation: Pivot tables, melting, and
reshaping data.
4. Integration: Use with libraries like NumPy,
Matplotlib, and Scikit-learn for advanced tasks.
2. Creating a DataFrame
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
13 | P a g e
'Age': [25, 30, 35],
'City': ['New York', 'Los Angeles', 'Chicago']
}
df = pd.DataFrame(data)
print(df)
Output:
Name Age City
0 Alice 25 New York
1 Bob 30 Los Angeles
2 Charlie 35 Chicago
3. Creating a Series
series = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
print(series)
Output:
a 10
b 20
c 30
dtype: int64
2. Writing Data
# Write to a CSV file
df.to_csv('output.csv', index=False)
DataFrame Operations
1. Inspecting Data
print(df.head()) # First 5 rows
print(df.tail()) # Last 5 rows
print(df.info()) # Summary of the DataFrame
print(df.describe()) # Statistical summary
15 | P a g e
print(df.iloc[0]) # First row
print(df.loc[1]) # Row with index 1print(df)
3. Filtering Data
# Filter rows where Age > 25
filtered_df = df[df['Age'] > 25]
print(filtered_df)
# Remove a column
df = df.drop('Salary', axis=1)
2. Removing Duplicates
df.drop_duplicates(inplace=True)
16 | P a g e
• OS Library: The OS library in Python provides
a way to interact with the operating system. It
allows you to perform tasks like creating,
deleting, and navigating directories, handling
file paths, and retrieving system information.
17 | P a g e
Commonly Used Functions –
1. Working with Directories
# Get the current working directory
cwd = os.getcwd()
print("Current Directory:", cwd)
# Remove a file
os.remove('file_to_delete.txt')
18 | P a g e
3. Path Manipulation with os.path
The os.path module provides utilities for working with
file paths.
import os
4. Environment Variables
# Get the value of an environment variable
path = os.getenv('PATH')
19 | P a g e
print("PATH Environment Variable:", path)
5. System Information
# Get the operating system name
os_name = os.name # 'posix' for Linux/Mac, 'nt' for Windows
print("OS Name:", os_name)
20 | P a g e
• Data Visualization Using Matplotlib
Library: Matplotlib is a powerful Python
library for creating a wide range of data
visualizations. It allows users to create high-
quality, publication-ready plots and offers
extensive customization options
21 | P a g e
• Use plt.show() to render the plot.
Customization Options –
1. Adding Labels and Titles
plt.title("Custom Plot Title")
plt.xlabel("X-axis Label")
plt.ylabel("Y-axis Label")
3. Adding Legend
plt.legend(['Line Label'], loc='upper right')
22 | P a g e
4. Adding a Grid
plt.grid(color='gray', linestyle=':', linewidth=0.5)
23 | P a g e
Features of Seaborn –
1. Built-in Themes: Offers attractive and
informative default styles.
2. Support for DataFrames: Works seamlessly with
pandas for data handling.
3. Advanced Statistical Plots: Provides
visualizations like heatmaps, pair plots, violin
plots, and more.
4. Automatic Handling: Automatically calculates
and visualizes statistical relationships.
Installing Seaborn –
pip install seaborn
# Example Data
24 | P a g e
data = pd.DataFrame({'x': np.linspace(0, 10, 100), 'y':
np.sin(np.linspace(0, 10, 100))})
sns.lineplot(data=data, x='x', y='y')
plt.show()
2. Scatter Plot
# Example Scatter Plot
sns.scatterplot(x='sepal_length', y='sepal_width', data=sns.load_dataset('iris'),
hue='species')
plt.show()
3. Bar Plot
# Example Bar Plot
sns.barplot(x='day', y='total_bill', data=sns.load_dataset('tips'),
ci=None)
plt.show()
4. Heatmap Plot
# Correlation Heatmap
tips = sns.load_dataset('tips')
sns.heatmap(tips.corr(), annot=True, cmap='coolwarm')
plt.show()
5. Line Plot
# Pairwise Relationships
sns.pairplot(sns.load_dataset('iris'), hue='species')
plt.show()
25 | P a g e
Seaborn Themes and Styles –
# Set Style
sns.set_style('whitegrid')
# Available Themes
sns.set_theme(style='darkgrid') # Styles: darkgrid, whitegrid,
dark, white, ticks
Features of Seaborn –
1. Interactive Plots: Provides zooming, panning,
tooltips, and other interactive elements.
2. Web Integration: Generates HTML/JavaScript
outputs for use in web applications.
3. High Performance: Handles large datasets
efficiently.
4. Rich Visualization Types: Supports advanced
plots like geospatial visualizations, linked plots,
and custom layouts.
26 | P a g e
Installing Bokeh –
pip install bokeh
# Create a figure
p = figure(title="Line Plot Example", x_axis_label='X-axis',
y_axis_label='Y-axis')
# Add a line
p.line(x, y, line_width=2)
2. Scatter Plot
p = figure(title="Scatter Plot Example")
p.scatter(x, y, size=15, color="navy", alpha=0.5)
show(p)
27 | P a g e
3. Bar Plot
from bokeh.models import ColumnDataSource
from bokeh.transform import dodge
source = ColumnDataSource(data=dict(categories=categories,
values=values))
show(p)
4. Interactive Widgets
from bokeh.layouts import column
from bokeh.models import Slider
from bokeh.plotting import curdoc
# Create a plot
p = figure(title="Interactive Example")
line = p.line(x, y, line_width=2)
# Add a slider
slider = Slider(start=1, end=10, step=0.1, value=1,
title="Multiplier")
curdoc().add_root(column(slider, p))
28 | P a g e
Seaborn vs Bokeh –
Feature Seaborn Bokeh
Use case Statistical Data Interactive/Web-
Visualization based Plots
Output Static Interactive HTML/JS
Ease of Use High Medium
Integration Matplotlib, Web Frameworks
Pandas (Flask, Django)
Interactivity Limited Extensive
Performance Good for small Excellent for large
to medium data datasets
class Item(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
30 | P a g e
price = models.DecimalField(max_digits=10, decimal_places=2)
def __str__(self):
return self.name
4. Create a View
Views define what is displayed on the web page.
# myapp/views.py
from django.shortcuts import render
from .models import Item
def home(request):
items = Item.objects.all()
return render(request, 'myapp/home.html', {'items':
items})
31 | P a g e
5. Define a URL Pattern
Map a URL to the view.
# myproject/urls.py
from django.contrib import admin
from django.urls import path
from myapp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
]
6. Create a Template
Templates define the frontend (HTML) of your web
GUI.
<!-- myapp/templates/myapp/home.html -->
<!DOCTYPE html>
<html>
<head>
<title>Item List</title>
</head>
<body>
<h1>Item List</h1>
<ul>
{% for item in items %}
<li>{{ item.name }} - ${{ item.price }}<br>{{
item.description }}</li>
{% endfor %}
</ul>
32 | P a g e
</body>
</html>
33 | P a g e