UNIT-5 Detailed Notes
UNIT-5 Detailed Notes
REGULAR EXPRESSION:
Python RegEx
RegEx can be used to check if a string contains the specified search pattern.
RegEx Module
Python has a built-in package called re, which can be used to work with Regular
Expressions.
import re
RegEx in Python
When you have imported the re module, you can start using regular expressions:
Example:
import re
#Check if the string starts with "The" and ends with "Spain": txt = "The rain in
Spain"
x = re.search("^The.*Spain$", txt)
if x:
print("YES! We have a match!") else:
print("No match")
Output:
Special sequences:
.SPAN()
Returns a tuple containing the start-, and end positions of the match.
.STRING()
It returns a string passed into the function.
.GROUP()
The part of the string is returned where the match is found. .SPAN():
Example program:
import re
txt = "The Beautiful Women"
x = re.search(r"\bW\w+", str)
print(x.span())
Output:
(14, 19)
.String():
Example program:
import re
txt = "The Beautiful Women"
x = re.search(r"\bW\w+",txt)
print(x.string())
Output:
Women
.group()
Example program:
import re
txt = "The Beautiful Women"
x =re.search(r"\bB\w“, “\bW\w”,txt)
print(x.group())
Output:
Beautiful Women
LINEAR REGRESSION:
❖ Linear regression is a statistical method used for modelling the relationship
between a dependent variable and one or more independent variables by fitting a
linear equation to the observed data. The simplest form of linear regression with
one independent variable is called simple linear regression, while multiple linear
regression involves two or more independent variables.
Types of Linear Regression:
Simple Linear Regression:
where:
Y is the dependent variable
X is the independent variable
β0 is the intercept
β1 is the slope
Multiple Linear Regression:
This involves more than one independent variable and one dependent variable. The
equation for multiple linear regression is:
where:
Y is the dependent variable
X1, X2, …, Xp are the independent variables
β0 is the intercept
β1, β2, …, βn are the slopes
Some other regression types are-
∙ Polynomial Regression – Polynomial regression goes beyond simple linear
regression by incorporating higher-order polynomial terms of the independent
variable(s) into the model. It is represented by the general equation:
∙ Ridge Regression – Ridge regression is a regularization technique used to prevent
overfitting in linear regression models, especially when dealing with multiple
independent variables. It introduces a penalty term to the least squares objective
function, biasing the model towards solutions with smaller coefficients. The
equation for ridge regression becomes:
∙ Lasso Regression – Lasso regression is another regularization technique that uses
an L1 penalty term to shrink the coefficients of less important independent
variables towards zero, effectively performing feature selection. The equation for
lasso regression becomes:
∙ Elastic Net Regression – Elastic net regression combines the penalties of ridge and
lasso regression, offering a balance between their strengths. It uses a mixed penalty
term of the form
The goal of the algorithm is to find the best Fit Line equation that can predict the
values based on the independent variables.
In regression set of records are present with X and Y values and these values are
used to learn a function so if you want to predict Y from an unknown X this learned
function can be used. In regression we have to find the value of Y, So, a function is
required that
predicts continuous Y in the case of regression given X as independent features.
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
plt.show()
In [1]: In [6]:
# Import
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
x = np.array([0,5])
y = np.array([10,40]) # Multiple line
plt.plot(x,y)
localhost:8888/notebooks/Matplotlib_Practise.ipynb 1/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
x = np.array([1, 2, 6, 8])
y = np.array([3, 8, 1, 10])
plt.plot(x, y)
plt.show()
# Line STYLE
localhost:8888/notebooks/Matplotlib_Practise.ipynb 2/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
x = np.array([1, 2, 6, 8])
y = np.array([3, 8, 1, 10])
plt.plot(x, y, ls='dotted')
plt.show()
# Line color
localhost:8888/notebooks/Matplotlib_Practise.ipynb 3/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
x = np.array([1, 2, 6, 8])
y = np.array([3, 8, 1, 10])
# Line width
localhost:8888/notebooks/Matplotlib_Practise.ipynb 4/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
x = np.array([1, 2, 6, 8])
y = np.array([3, 8, 1, 10])
x = np.array([1, 2, 6, 8])
y = np.array([3, 8, 1, 10])
plt.plot(x, y, 'o')
plt.show()
# MATPLOTLIB MARKERS
localhost:8888/notebooks/Matplotlib_Practise.ipynb 6/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
x = np.array([1, 2, 6, 8])
y = np.array([3, 8, 1, 10])
plt.plot(x, y, marker='D')
plt.show()
# Format strings-(marker|line|color)
localhost:8888/notebooks/Matplotlib_Practise.ipynb 7/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
x = np.array([1, 2, 6, 8])
y = np.array([3, 8, 1, 10])
# MARKER SIZE
localhost:8888/notebooks/Matplotlib_Practise.ipynb 8/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
x = np.array([1, 2, 6, 8])
y = np.array([3, 8, 1, 10])
# MARKER COLOR:
localhost:8888/notebooks/Matplotlib_Practise.ipynb 9/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
x = np.array([1, 2, 6, 8])
y = np.array([3, 8, 1, 10])
# markerfacecolor or mfc
localhost:8888/notebooks/Matplotlib_Practise.ipynb 10/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
x = np.array([1, 2, 6, 8])
y = np.array([3, 8, 1, 10])
# Matplotlib labels
localhost:8888/notebooks/Matplotlib_Practise.ipynb 11/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
plt.plot(x, y)
plt.xlabel("Temperature")
plt.ylabel("Gradient")
plt.show()
# Title for a plot
localhost:8888/notebooks/Matplotlib_Practise.ipynb 12/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
plt.plot(x, y)
plt.xlabel("Temperature")
plt.ylabel("Gradient")
plt.title("Comparison") # Fixed indentation here
plt.show()
# Matplotlib grids
localhost:8888/notebooks/Matplotlib_Practise.ipynb 13/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
plt.plot(x, y)
plt.xlabel("Temperature")
plt.ylabel("Gradient")
plt.grid()
plt.show()
# Grid lines for x axis
localhost:8888/notebooks/Matplotlib_Practise.ipynb 14/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
x = np.array([0, 5])
y = np.array([10, 40])
plt.plot(x, y)
plt.grid(axis='x') # You may also use plt.grid(True, axis='x')
plt.show()
# Grid lines for y axis
localhost:8888/notebooks/Matplotlib_Practise.ipynb 15/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
x = np.array([0, 5])
y = np.array([10, 40])
plt.plot(x, y)
plt.grid(axis='y')
plt.show()
# Subplots
localhost:8888/notebooks/Matplotlib_Practise.ipynb 16/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x, y)
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(1, 2, 2)
plt.plot(x, y)
plt.show()
localhost:8888/notebooks/Matplotlib_Practise.ipynb 17/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(2, 3, 1)
plt.plot(x, y)
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(2, 3, 2)
plt.plot(x, y)
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(2, 3, 3)
plt.plot(x, y)
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(2, 3, 4)
plt.plot(x, y)
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(2, 3, 5)
plt.plot(x, y)
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(2, 3, 6)
plt.plot(x, y)
plt.show()
localhost:8888/notebooks/Matplotlib_Practise.ipynb 18/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
# Title for subplot
localhost:8888/notebooks/Matplotlib_Practise.ipynb 19/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [25]: import matplotlib.pyplot as plt
import numpy as np
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x, y)
plt.title("SALES")
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(1, 2, 2)
plt.plot(x, y)
plt.title("INCOME")
plt.show()
# Super title
localhost:8888/notebooks/Matplotlib_Practise.ipynb 20/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [26]: import matplotlib.pyplot as plt
import numpy as np
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x, y)
plt.title("SALES")
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(1, 2, 2)
plt.plot(x, y)
plt.title("INCOME")
plt.suptitle("MY SHOP")
plt.show()
# Scatter plot
localhost:8888/notebooks/Matplotlib_Practise.ipynb 21/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [27]: import matplotlib.pyplot as plt
import numpy as np
plt.scatter(x, y)
plt.show()
# Multiple plots
localhost:8888/notebooks/Matplotlib_Practise.ipynb 22/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [28]: import matplotlib.pyplot as plt
import numpy as np
plt.scatter(x1, y1)
plt.scatter(x2, y2)
plt.show()
# color
localhost:8888/notebooks/Matplotlib_Practise.ipynb 23/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [29]: import matplotlib.pyplot as plt
import numpy as np
plt.show()
localhost:8888/notebooks/Matplotlib_Practise.ipynb 24/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [30]: import matplotlib.pyplot as plt
import numpy as np
plt.scatter(x, y, c=colors)
plt.show()
# Color map
localhost:8888/notebooks/Matplotlib_Practise.ipynb 25/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [31]: import matplotlib.pyplot as plt
import numpy as np
plt.show()
# DOT SIZE
localhost:8888/notebooks/Matplotlib_Practise.ipynb 26/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [32]: import matplotlib.pyplot as plt
import numpy as np
plt.scatter(x, y, s=sizes)
plt.show()
# ALPHA
localhost:8888/notebooks/Matplotlib_Practise.ipynb 27/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [33]: import matplotlib.pyplot as plt
import numpy as np
localhost:8888/notebooks/Matplotlib_Practise.ipynb 28/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [34]: import matplotlib.pyplot as plt
import numpy as np
x = np.random.randint(100, size=(100))
y = np.random.randint(100, size=(100))
colors = np.random.randint(100, size=(100))
sizes = 10 * np.random.randint(100, size=(100))
# Matplotlib Bars:
localhost:8888/notebooks/Matplotlib_Practise.ipynb 29/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [35]: import matplotlib.pyplot as plt
import numpy as np
plt.bar(x, y)
plt.show()
# Horizontal Bars
localhost:8888/notebooks/Matplotlib_Practise.ipynb 30/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [36]: import matplotlib.pyplot as plt
import numpy as np
plt.barh(x, y)
plt.show()
# Bar Color
localhost:8888/notebooks/Matplotlib_Practise.ipynb 31/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [37]: import matplotlib.pyplot as plt
import numpy as np
plt.bar(x, y, color="red")
plt.show()
# Color Names
localhost:8888/notebooks/Matplotlib_Practise.ipynb 32/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [38]: import matplotlib.pyplot as plt
import numpy as np
plt.bar(x, y, color="hotpink")
plt.show()
# Bar Width
localhost:8888/notebooks/Matplotlib_Practise.ipynb 33/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [39]: import matplotlib.pyplot as plt
import numpy as np
# Bar Height
localhost:8888/notebooks/Matplotlib_Practise.ipynb 34/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [40]: import matplotlib.pyplot as plt
import numpy as np
# Histogram
localhost:8888/notebooks/Matplotlib_Practise.ipynb 35/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [41]: import matplotlib.pyplot as plt
import numpy as np
localhost:8888/notebooks/Matplotlib_Practise.ipynb 36/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [42]: import matplotlib.pyplot as plt
import numpy as np
# Labels:
localhost:8888/notebooks/Matplotlib_Practise.ipynb 37/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [43]: import matplotlib.pyplot as plt
import numpy as np
plt.pie(y, labels=mylabels)
plt.show()
# Start Angle
localhost:8888/notebooks/Matplotlib_Practise.ipynb 38/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [44]: import matplotlib.pyplot as plt
import numpy as np
# Explode
localhost:8888/notebooks/Matplotlib_Practise.ipynb 39/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [45]: import matplotlib.pyplot as plt
import numpy as np
# Colors
localhost:8888/notebooks/Matplotlib_Practise.ipynb 40/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [46]: import matplotlib.pyplot as plt
import numpy as np
# Legend
localhost:8888/notebooks/Matplotlib_Practise.ipynb 41/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [47]: import matplotlib.pyplot as plt
import numpy as np
plt.pie(y, labels=mylabels)
plt.legend()
plt.show()
localhost:8888/notebooks/Matplotlib_Practise.ipynb 42/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
import matplotlib.pyplot as plt
In [48]: In [ ]: import numpy as np
plt.pie(y, labels=mylabels)
plt.legend(title="Four Fruits:")
plt.show()
localhost:8888/notebooks/Matplotlib_Practise.ipynb 43/43