Lecture_6_Python
Lecture_6_Python
Lecture No. 06
1 / 38
Table of Contents
1 Introduction
2 Interpolation
Linear Interpolation
Spline Interpolation
Other Types of Interpolation
3 Regression
Types of Regression
Goodness of Fit
Overfitting and Underfitting
4 Curve Fitting
Introduction
Curve fitting Examples
2 / 38
Introduction
3 / 38
Interpolation
4 / 38
Interpolation
5 / 38
Linear Interpolation
6 / 38
Example 1: Linear Interpolation
import numpy as np
x = np.array ([0, 2, 4, 6, 8, 10])
y = np.array ([0, 4, 8, 16, 32, 64])
x_new = 5
y_new = np.interp(x_new , x, y)
7 / 38
Example 1 (Continued): Interpolating at Multiple Values
x_new2 = [1, 3, 5, 7]
y_new2 = np.interp(x_new2 , x, y)
for i in range(len(x_new2)):
print(f"The estimated value at x = {x_new2[i]} is {y_new2[i]}.")
8 / 38
Spline Interpolation
9 / 38
Example 2: Spline Interpolation
Temperature (°C) : [100, 125, 175, 200, 225, 275, 300, 400, 500]
Heat Capacity (J/mol·K) : [25.15, 25.9, 26.7, 26.9, 27.3, 28.5, 29.1, 32, 37.0]
Estimate heat capacity at intermediate temperatures
T = [150, 250, 350, 450].
import numpy as np
from scipy.interpolate import CubicSpline
temperature = np.array ([100 ,125 ,175 ,200 ,225 ,275 ,300 ,400 ,500])
heat_capacity = np.array ([25.15 , 25.9, 26.7, 26.9, 27.3, 28.5, 29.1, 32, 37.0])
cs = CubicSpline(temperature , heat_capacity)
temp_new = np.array ([150 , 250, 350, 450])
cp_interpolated = cs(temp_new)
for i in range(len(temp_new)):
print(f"The estimated Cp at T = {temp_new[i]} °C is {cp_interpolated [i]:.2f} J/mol·K.")
10 / 38
Example 2: Plot
12 / 38
Introduction to Regression
13 / 38
Regression vs Interpolation
14 / 38
Types of Regression
15 / 38
Linear Regression
y = mx + c
where:
m is the slope (rate of change of y with respect to x).
c is the y-intercept (value of y when x = 0).
The goal is to find m and c that minimize the difference between
predicted and actual values.
Methods: Least squares, numpy.polyfit(), scikit-learn, statsmodels.
16 / 38
Polynomial Regression
y = an x n + an−1 x n−1 + · · · + a1 x + a0
17 / 38
Multiple Linear Regression
y = m1 x1 + m2 x2 + · · · + mn xn + b
18 / 38
Types of Regression Models
19 / 38
Example 3: Linear Regression
Given data for the relationship between temperature and reaction rate:
20 / 38
Example 3: Plotting Linear Fit
# Generate fitted values using the linear model
reaction_rate_fitted = np.polyval(coefficients , temperature)
21 / 38
Example 4: Polynomial Regression
We extend the linear regression example by fitting a 2nd-degree
polynomial.
# Perform quadratic regression ( degree 2)
coefficients = np.polyfit(temperature , reaction_rate , 2)
a, b, c = coefficients
print(f"Quadratic Coefficients: a={a:.2f}, b={b:.2f}, c={c:.2f}")
22 / 38
Evaluating Goodness of Fit
To determine how well the model fits the data, we use metrics such as:
Residuals Analysis: Measures differences between observed and
predicted values.
R-squared (R²): Proportion of variance in the dependent variable
explained by the model.
Mean Squared Error (MSE): Average squared difference between
observed and predicted values.
23 / 38
Python Code to Calculate R² and MSE
24 / 38
Goodness of Fit Results
**Linear Regression**:
R²: 0.97, MSE: 0.49
**Quadratic Regression**:
R²: 0.99, MSE: 0.09
Quadratic regression has a better fit, with a higher R² and lower MSE.
25 / 38
Overfitting and Underfitting
26 / 38
Introduction to Curve Fitting
27 / 38
Steps for Curve Fitting in Python
28 / 38
Example 5: Curve Fitting (Arrhenius Equation)
E
r (T ) = A · exp −
R ·T
where:
r (T ): reaction rate at temperature T ,
A: pre-exponential factor,
E : activation energy,
R = 8.314 J/(mol·K) is the universal gas constant.
The goal is to fit the Arrhenius equation to the data and determine A and
E.
29 / 38
Example 5: Plotting Arrhenius Fit
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
30 / 38
Figure: Comparison of experimental data and Arrhenius fit.
31 / 38
Improving Curve Fitting
32 / 38
Example 6: Gaussian Curve Fitting
(λ − λ0 )2
A(λ) = a · exp −
2 · σ2
Where:
A(λ) is the absorption at wavelength λ,
a is the peak amplitude (maximum absorption),
λ0 is the center of the peak,
σ is the standard deviation (related to the width of the peak).
The goal is to fit the Gaussian model to the experimental data and
determine a, λ0 , and σ.
33 / 38
Experimental Data and Initial Guess
Given data:
Wavelengths (nm) : [400, 420, 440, 460, 480, 500, 520, 540, 560, 580, 600]
Absorption (AU) : [0.12, 0.31, 0.82, 1.50, 1.85, 1.92, 1.67, 1.10, 0.65, 0.30, 0.12]
We will use an initial guess for the Gaussian parameters:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
34 / 38
Fitting the Gaussian Model
Output:
35 / 38
Plotting the Gaussian Fit
# Generate fitted curve using the optimized parameters
wavelengths_fit = np.linspace(min(wavelengths), max(wavelengths), 100)
absorption_fit = gaussian(wavelengths_fit , *popt)
37 / 38
Thank You!
Any questions?