CURVE FITTING (using PYTHON)
AIM:
To demonstrate curve-fitting of a data given between `C_v` and temperature using scipy module of Python.
OBJECTIVES:
- To understand the meaning of curve-fitting and regression
- To understand the working, necessary input parameters, output values of the 'curve_fit' function of scipy module of Python
INTRODUCTION:
In statistical modeling, regression analysis is a set of statistical processes for estimating the relationships between a dependent variable (often called the 'outcome variable') and one or more independent variables (often called 'predictors', 'covariates', or 'features'). The most common form of regression analysis is linear regression, in which a researcher finds the line (or a more complex linear combination) that most closely fits the data according to a specific mathematical criterion.
Points to be noted:
- Now, 'curve_fit' function does the same thing mentioned above.
The parameter it generally takes as input are as follows:
- f : callable
The model function, f(x, …). The model function means the function that we assume the given data represents. It must take the independent variable as the first argument and the parameters to fit as separate remaining arguments
- xdata : array_like or object
The independent variable where the data is measured. Should usually be an M-length sequence or an (k,M)-shaped array for functions with k predictors, but can actually be any object.
- ydata : array_like
The dependent data, a length M array - nominally f(xdata, ...).
The output of the function is as follows:
- popt : array
Generally, this gives out the necessary coefficients of the model function that we have assumed.
- pcov : 2d array
The estimated covariance of popt. The diagonals provide the variance of the parameter estimate.
- We have used np.array(temperature) in line no. 32 and 47. This is because Python stores every list as a string variable as default. But we need numeric value of those data. So, to convert them we have used np.array.
- Now, in same line nos. 32 and 47, a star marker can be seen before popt. This is used as a reference for those variables (here, the coefficients) for the called function. This is a basic example of a pointer.
- To get curve fits perfectly, we need to consider the errors that occur in the process of direct curve fitting as well. The errors can be obtained by the following methods.
- SSE(Sum of squares of errors) – If y = f(x) is the probable fit, then at a general point 'i',
SSE = `sum_(i=0)^N[y(i)-f(x(i))]^2`
- R – square – This method is used for mean centering the data.
SSR = `sum_(i=0)^N[f(x(i))-Mean]^2`
SST = SSR + SSE
`R^2` = `(SSR)/(SST)`
- R – square(adjusted) – This method is used when more different independent variables are used and to know if/ whether these may affect the actual relationship or not.
`(R^2)_(adj)`= `1-[(1-R^2)(n-1)/(n-k-1)]`
where n = no. of data points, k = no. of independent variables.
- RMS -
RMS = `sqrt((SSE)/n)`
PROCEDURE OF COMPLETION:
- The necessary modules i.e numpy, matplotlib and scipy were imported for usages.
- Then, the model functions were defined i.e. the linear fit and the cubic fit.
- Then, the 'curve_fit' function from scipy.optimize module is used for curve-fiiting procedure.
The following code can be considered for getting the curve fit:
# PROGRAM FOR DOING REGRESSION OR CURVE-FITTING
# importing modules
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# cubic polynomial deemed to fit
def func3(t, a, b, c):
return a*pow(t, 3) + b*t + c
# linear polynomial deemed to fit
def func1(t, a, b):
return a*t + b
# reading the file
def read_file():
temperature = []
cp = []
for line in open('data', 'r'):
values = line.split(',')
temperature.append(float(values[0]))
cp.append(float(values[1]))
return [temperature, cp]
# cubic curve fitting
temperature, cp = read_file()
popt1, pcov1 = curve_fit(func1, temperature, cp)
fit_cp1 = func1(np.array(temperature), *popt1)
# plotting
plt.figure(1)
plt.plot(temperature, cp)
plt.plot(temperature, fit_cp1)
plt.grid()
plt.xlabel('Temperature')
plt.ylabel('C_v')
plt.legend(['Original curve', 'Cubic fitted curve'])
plt.show()
# linear curve fitting
temperature, cp = read_file()
popt3, pcov3 = curve_fit(func3, temperature, cp)
fit_cp3 = func3(np.array(temperature), *popt3)
# plotting
plt.figure(2)
plt.plot(temperature, cp)
plt.plot(temperature, fit_cp3)
plt.grid()
plt.xlabel('Temperature')
plt.ylabel('C_v')
plt.legend(['Original curve', 'Linear fitted curve'])
plt.show()
OBSERVATIONS:
The following results arrive:
The fits don't match exactly how it was intended to be. The cubic fit closely relates to the data given. The truncation errors can be optimized by the above mentioned methods or a new polynomial can also be used.
CONCLUSION:
Regression is a powerful tool to gather knowledge about the relationship between any two(or more) given data set. This is an integral part of Machine Learning. To get the perfect fit, the error parameters must always be considered as they will give us a perfect measure for our need.
REFERENCES: