0% found this document useful (0 votes)
38 views25 pages

Lecture 2.3

Uploaded by

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

Lecture 2.3

Uploaded by

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

Apex Institute of Technology

Department of Computer Science & Engineering


Bachelor of Engineering (Computer Science &
Engineering)
Python for Machine Learning – (20CST255)
Prepared By: Dr. Dinesh Vij

Lecture - 8
DISCOVER . LEARN . EMPOWER
Data Visualization in Python
Python for Machine Learning
Course Objective:
The Course aims to: :

CO Will be covered in
Title
Number this lecture
Make students understand the structure, semantics
CO1 and syntax of Python programming Languages.

Make students understand and apply various data


CO2
handling and visualization techniques.

Enable students to develop and implement the first


CO3 principles of data science.

DISCOVER . LEARN . EMPOWER


Python for Machine Learning
Course Outcome:
Upon successful completion of this course, students will be able to:

CO
Title Will be covered in
Number
this lecture
Understand Python programming language by
CO1
navigating software documentation
Development of Python programs using Numpy and
CO2
Pandas.
Visualization of Data Models using Matplotlib and
CO3 Seaborn.
CO4 Implement simple learning strategies using data
science principles.

CO5 Optimize the evaluation results obtained after


applying machine learning model.

DISCOVER . LEARN . EMPOWER


Plotting fundamentals
using Matplotlib

Apex Institute of Technology- CSE


Introduction
matplotlib:

 python 2D plotting library which produces publication quality


figures in a variety of hardcopy formats

 a set of functionalities similar to those of MATLAB

 Matplotlib is a multi-platform data visualization


library built on NumPy arrays and designed to
work with the broader SciPy stack.

 It was introduced by John Hunter in the year 2002.

Apex Institute of Technology- CSE


Introduction (contd.)
matplotlib:

 relatively low-level; some effort needed to create advanced


visualization

 One of the greatest benefits of visualization is


that it allows us visual access to huge amounts
of data in easily digestible visuals.

 Matplotlib consists of several plots like line plots,


scatter plots, barcharts, histograms, pie charts etc.

Apex Institute of Technology- CSE


Introduction (contd.)
Importing matplotlib :
• from matplotlib import pyplot as plt (or)
• import matplotlib.pyplot as plt

Basic plots in Matplotlib :


• Matplotlib comes with a wide variety of plots.
Plots helps to understand trends, patterns, and
to make correlations.
• They’re typically instruments for reasoning
about quantitative information. Some of the
basic plots are covered here.

Apex Institute of Technology- CSE


Line plot
# importing matplotlib module
from matplotlib import pyplot as plt

# x-axis values
x = [5, 2, 9, 4, 7]

# Y-axis values
y = [10, 5, 8, 4, 2]

# Function to plot
plt.plot(x,y)

# function to show the plot


plt.show()

Apex Institute of Technology- CSE


Line plot (contd.)

Apex Institute of Technology- CSE


Bar plot
# importing matplotlib module
from matplotlib import pyplot as plt

# x-axis values
x = [5, 2, 9, 4, 7]

# Y-axis values
y = [10, 5, 8, 4, 2]

# Function to plot the bar


plt.bar(x,y)

# function to show the plot


plt.show()

Apex Institute of Technology- CSE


Bar plot (contd.)

Apex Institute of Technology- CSE


Scatter Plot
# importing matplotlib module
from matplotlib import pyplot as plt

# x-axis values
x = [5, 2, 9, 4, 7]

# Y-axis values
y = [10, 5, 8, 4, 2]

# Function to plot scatter


plt.scatter(x, y)

# function to show the plot


plt.show()

Apex Institute of Technology- CSE


Scatter Plot

Apex Institute of Technology- CSE


Plotting Data
Distributions using
Seaborn
Apex Institute of Technology- CSE
Introduction
Seaborn:

 It is built on the top of matplotlib library and also closely integrated to


the data structures from pandas.

 provides high level interface for drawing attractive statistical graphics

 Similar (in style) to the popular ggplot2 library in R

 It provides beautiful default styles and color palettes to make statistical


plots more attractive.

 Seaborn aims to make visualization the central part of exploring and


understanding data.

 It provides dataset-oriented APIs, so that we can switch between


different visual representations for same variables for better
understanding of dataset.

Apex Institute of Technology- CSE


Different categories of plot in
Seaborn
Plots are basically used for visualizing the relationship between
variables. Those variables can be either be completely numerical or
a category like a group or a class. Seaborn divides plot into the
below categories –

•Relational plots: This plot is used to understand the relation


between two variables.
•Categorical plots: This plot deals with categorical variables and
how they can be visualized.
•Distribution plots: This plot is used for examining univariate and
bivariate distributions
•Regression plots: The regression plots in seaborn are primarily
intended to add a visual guide that helps to emphasize patterns in a
dataset during exploratory data analyses.
•Matrix plots: A matrix plot is an array of scatterplots.
•Multi-plot grids: It is an useful approach to
draw multiple instances of the same plot on different subsets of the
Apex Institute of Technology- CSE
Some basic plots using
descriptionseaborn

distplot histogram
barplot estimate of central tendency for a numeric variable
violinplot similar to boxplot, also shows the probability density of the data

jointplot Scatterplot

regplot Regression plot

pairplot Pairplot

boxplot boxplot
swarmplot categorical scatterplot
factorplot General categorical plot

Apex Institute of Technology- CSE


Dist plot
Seaborn dist plot is used to plot a histogram, with some other variations like
kdeplot and rugplot.

# Importing libraries
import numpy as np
import seaborn as sns

# Selecting style as white,


# dark, whitegrid, darkgrid
# or ticks
sns.set(style="white")

# Generate a random univariate


# dataset
rs = np.random.RandomState(10)
d = rs.normal(size=100)

# Plot a simple histogram and kde


# with binsize determined automatically
sns.distplot(d, kde=True, color="m")

Apex Institute of Technology- CSE


Dist plot (contd.)

Apex Institute of Technology- CSE


Line plot
The line plot is one of the most basic plot in seaborn library. This plot is
mainly used to visualize the data in form of some time series, i.e. in
continuous manner.

import seaborn as sns

sns.set(style="dark")
fmri = sns.load_dataset("fmri")

# Plot the responses for different\


# events and regions
sns.lineplot(x="timepoint",
y="signal",
hue="region",
style="event",
data=fmri)

Apex Institute of Technology- CSE


Line plot (contd.)

Apex Institute of Technology- CSE


Lmplot
The lmplot is another most basic plot. It shows a line representing a linear
regression model along with data points on the 2D-space and x and y can
be set as the horizontal and vertical labels respectively.

import seaborn as sns

sns.set(style="ticks")

# Loading the dataset


df = sns.load_dataset("anscombe")

# Show the results of a linear regression


sns.lmplot(x="x", y="y", data=df)

Apex Institute of Technology- CSE


Lmplot

Apex Institute of Technology- CSE


Suggestive Readings
• https://matplotlib.org/

• https://www.geeksforgeeks.org/python-introduction-matplo
tlib/

• https://seaborn.pydata.org/

• https://www.geeksforgeeks.org/introduction-to-seaborn-pyt
hon/

Apex Institute of Technology- CSE


THANK YOU

For queries
Email: [email protected]

Apex Institute of Technology- CSE

You might also like