0% found this document useful (0 votes)
4 views33 pages

Python Unit 5

Uploaded by

Ronit Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views33 pages

Python Unit 5

Uploaded by

Ronit Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

The Python Language Notes :

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.

 To install NumPy, use pip as:


Pip install numpy

Key Concept in NumPy –


1. Creating Arrays
import numpy as np

# Create a 1D array
arr1 = np.array([1, 2, 3, 4])

# Create a 2D array
arr2 = np.array([[1, 2], [3, 4]])

# Create an array of zeros


zeros = np.zeros((3, 3))

3|Page
# Create an array of ones
ones = np.ones((2, 4))

# Create an array with a range of numbers


arange = np.arange(0, 10, 2)

# Create an array with random numbers


random = np.random.random((2, 3))

print(arr1, arr2, zeros, ones, arange, random)

2. Array Properties
arr = np.array([[1, 2, 3], [4, 5, 6]])

print(arr.shape) # Dimensions of the array


print(arr.ndim) # Number of dimensions
print(arr.size) # Total number of elements
print(arr.dtype) # Data type of elements

3. Indexing and Slicing


arr = np.array([10, 20, 30, 40, 50])

print(arr[0]) # Access first element


print(arr[1:4]) # Slice from index 1 to 3
print(arr[-1]) # Access last element

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])

# Add, subtract, multiply, divide


print(arr1 + arr2) # [5 7 9]
print(arr1 - arr2) # [-3 -3 -3]
print(arr1 * arr2) # [4 10 18]
print(arr1 / arr2) # [0.25 0.4 0.5]

# 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])

print(np.sqrt(arr)) # Square root


print(np.sin(arr)) # Sine
print(np.mean(arr)) # Mean
print(np.sum(arr)) # Sum of elements
print(np.prod(arr)) # Product of elements
print(np.max(arr)) # Maximum element
print(np.min(arr)) # Minimum element

5|Page
6. Reshaping and Transposing
arr = np.array([[1, 2, 3], [4, 5, 6]])

# Reshape the array


reshaped = arr.reshape(3, 2)

# Transpose the array


transposed = arr.T

print(reshaped)
print(transposed)

7. Linear Algebra
from numpy.linalg import inv, det

matrix = np.array([[1, 2], [3, 4]])

# Matrix multiplication
result = np.dot(matrix, matrix)

# Matrix inverse
inverse = inv(matrix)

# Determinant
determinant = det(matrix)

print(result, inverse, determinant)

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.

 To install NumPy, use pip as:


Pip install numpy

 The main module in Matplotlib is pyplot, often


imported as “plt”:
import matplotlib.pyplot as plt
import numpy as np

# Example: Plot a line


x = np.linspace(0, 10, 100)
y = np.sin(x)

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

x = np.arange(0, 10, 0.1)


y = np.sin(x)

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

categories = ['A', 'B', 'C', 'D']


values = [4, 7, 1, 8]

plt.bar(categories, values, color='blue')


plt.title("Bar Chart")
plt.ylabel("Values")
plt.show()

9|Page
3. Histogram
import matplotlib.pyplot as plt
import numpy as np

data = np.random.randn(1000)

plt.hist(data, bins=20, color='green', edgecolor='black')


plt.title("Histogram")
plt.xlabel("Bins")
plt.ylabel("Frequency")
plt.show()

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

plt.scatter(x, y, c=colors, s=sizes, alpha=0.5, cmap='viridis')


plt.title("Scatter Plot")
plt.colorbar() # Show color scale
plt.show()

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

plt.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',


startangle=140)
plt.title("Pie Chart")
plt.show()

6. Multiple Subplots
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)


y1 = np.sin(x)
y2 = np.cos(x)

plt.subplot(2, 1, 1) # Rows, columns, index


plt.plot(x, y1, label='sin(x)')
plt.legend()

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.

 To install pandas, use pip as:


Pip install pandas

Getting Started with Pandas


1. Importing Pandas
import pandas as pd

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

File Handling in Pandas


1. Reading Data
# Read from a CSV file
14 | P a g e
df = pd.read_csv('data.csv')

# Read from an Excel file


df = pd.read_excel('data.xlsx')

2. Writing Data
# Write to a CSV file
df.to_csv('output.csv', index=False)

# Write to an Excel file


df.to_excel('output.xlsx', 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

2. Selecting Columns and Rows


# Select a single column
print(df['Name'])

# Select multiple columns


print(df[['Name', 'City']])

# Select rows by index

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)

4. Adding and Removing Columns


# Add a new column
df['Salary'] = [50000, 60000, 70000]

# Remove a column
df = df.drop('Salary', axis=1)

Data Cleaning in Pandas


1. Handling Missing Values
# Fill missing values with a specific value
df.fillna(0, inplace=True)

# Drop rows with missing values


df.dropna(inplace=True)

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.

Features of the OS Library –


1. File and Directory Management: Create, delete,
rename, and manage directories and files.
2. Path Manipulation: Work with file and directory
paths.
3. Environment Variables: Access and modify
environment variables.
4. Process Management: Manage system
processes.
5. Cross-Platform Compatibility: Provides a uniform
interface for interacting with different operating
systems.

Importing the OS Library –


import os

17 | P a g e
Commonly Used Functions –
1. Working with Directories
# Get the current working directory
cwd = os.getcwd()
print("Current Directory:", cwd)

# Change the current working directory


os.chdir('/path/to/new/directory')

# List files and directories


items = os.listdir('.')
print("Items in Directory:", items)

# Create a new directory


os.mkdir('new_folder')

# Remove an empty directory


os.rmdir('new_folder')

2. Working with Files


# Rename a file
os.rename('old_name.txt', 'new_name.txt')

# 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

# Check if a path exists


exists = os.path.exists('file_or_folder_path')

# Check if it's a file


is_file = os.path.isfile('path_to_file')

# Check if it's a directory


is_dir = os.path.isdir('path_to_directory')

# Get the absolute path


abs_path = os.path.abspath('relative_path')

# Get the directory name from a path


dir_name = os.path.dirname('/path/to/file.txt')

# Get the base name (file name) from a path


file_name = os.path.basename('/path/to/file.txt')

# Join paths (cross-platform safe)


full_path = os.path.join('/path', 'to', 'file.txt')

4. Environment Variables
# Get the value of an environment variable
path = os.getenv('PATH')

19 | P a g e
print("PATH Environment Variable:", path)

# Set a new environment variable


os.environ['MY_VAR'] = 'my_value'

# Delete an environment variable


del os.environ['MY_VAR']

5. System Information
# Get the operating system name
os_name = os.name # 'posix' for Linux/Mac, 'nt' for Windows
print("OS Name:", os_name)

# Execute a system command


os.system('ls') # Runs 'ls' command in Linux/Mac or 'dir' in
Windows

6. Temporary Files and Directories


The OS library provides tools to handle temporary files
and directories.
import tempfile

# Create a temporary file


temp_file = tempfile.NamedTemporaryFile(delete=False)
print("Temporary File:", temp_file.name)

# Create a temporary directory


temp_dir = tempfile.TemporaryDirectory()
print("Temporary Directory:", temp_dir.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

Steps to Visualize Data Using Matplotlib –


1. Import the Library
import matplotlib.pyplot as plt
2. Prepare Data
• You can use lists, NumPy arrays, or pandas
DataFrames to store your data.
• Ensure your data is clean and ready for
visualization.
3. Choose a Plot Type
• Matplotlib supports various plot types,
including line plots, bar charts, scatter plots,
histograms, and pie charts.
4. Customize the Plot
• Add titles, labels, legends, gridlines, and
other elements for clarity.
5. Display the Plot

21 | P a g e
• Use plt.show() to render the plot.

Applications of Data Visualization –


1. Exploratory Data Analysis (EDA): Understand
data trends and patterns.
2. Business Insights: Create dashboards and visual
reports.
3. Scientific Research: Present experimental data
effectively.
4. Machine Learning: Visualize model performance
and data distributions.

Customization Options –
1. Adding Labels and Titles
plt.title("Custom Plot Title")
plt.xlabel("X-axis Label")
plt.ylabel("Y-axis Label")

2. Modifying Plot Appearance


plt.plot(x, y, color='purple', linestyle='--', linewidth=2, marker='o')

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)

5. Saving the Plot


plt.savefig("plot.png", dpi=300)

• Seaborn and Bokeh Libraries: Seaborn and


Bokeh are two powerful Python libraries used
for data visualization. While both serve the
same purpose, they cater to different needs
and offer unique functionalities:
• Seaborn: Built on Matplotlib, Seaborn
simplifies the creation of informative
statistical plots with enhanced aesthetics.
• Bokeh: Designed for interactive and web-
ready visualizations, Bokeh enables
dynamic and real-time plotting.

Seaborn: is widely used for creating statistical


visualizations with minimal effort. It is built on top
of Matplotlib and tightly integrates with pandas for
handling datasets.

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

Getting Started with Seaborn –


import seaborn as sns
import matplotlib.pyplot as plt

Common Seaborn Plots –


1. Line Plot
import numpy as np
import pandas as pd

# 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

Bokeh: is a library for creating interactive, web-


ready visualizations. It supports real-time and
streaming data, making it ideal for dashboards and
web applications.

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

Getting Started with Bokeh –


from bokeh.plotting import figure, show

Creating Plots in Bokeh –


1. Line Plot
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]

# 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)

# Display the plot


show(p)

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

categories = ['A', 'B', 'C']


values = [10, 20, 15]

source = ColumnDataSource(data=dict(categories=categories,
values=values))

p = figure(x_range=categories, title="Bar Plot Example",


x_axis_label="Categories", y_axis_label="Values")
p.vbar(x='categories', top='values', width=0.8, source=source,
color="blue")

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

• GUI Development in Python Using


Django: Django is a high-level Python web
framework used primarily for web
development, not for traditional GUI
(desktop-based Graphical User Interface)
applications. However, if you're looking to
create a web-based GUI, Django is an
excellent choice because it provides tools to
build interactive, database-driven web
applications efficiently.
29 | P a g e
Getting Started with Django:
1. Install Django
pip install django

2. Create a Django Project


django-admin startproject myproject
cd myproject

3. Run the Development Server


python manage.py runserver

Developing a Web GUI Using Django:


1. Create an Application
Django applications are modular components of a
project.
python manage.py startapp myapp

2. Define a Model (Database Interaction)


Define the data structure in the models.py file of your
app.
# myapp/models.py
from django.db import models

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

Run Migrations to create the database table:


python manage.py makemigrations
python manage.py migrate

3. Add the App to the Project


Register the app in INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
...,
'myapp',
]

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>

7. Start the Development Server


Run the server and view the web-based GUI in your
browser:
python manage.py runserver

33 | P a g e

You might also like