Python-3.4.3/C3/Least-square-fit/English
Title of script: Least Square Fit
Author: Aditya Palaparthy, Arun KP
Keywords: Python, IPython, Linear Regression, lstsq(), least square fit, video tutorial
|
|
Show Slide title | Welcome to the spoken tutorial on Least Square Fit. |
Show Slide
Objectives |
In this tutorial, you will learn to,
|
Show Slide
System Specifications |
To record this tutorial, I am using
|
Show Slide
Pre-requisites |
To practise this tutorial, you should know about
If not, see the relevant Python tutorials on this website. |
Slide:
Pendulum.txt |
Before beginning this tutorial,
|
Show Slide
|
Let us start the tutorial with the help of an example.
|
Open the file pendulum.txt | Let us open the file pendulum.txt. |
Open the file 'pendulum.txt' and show | This is an input file generated from a simple pendulum experiment.
|
Open terminal | Let us start ipython.
|
Type ipython3
|
Type ipython3 and press Enter.
|
Type,
from numpy import loadtxt
|
To read the input file and parse the data, we are going to use the loadtxt function.
from numpy import loadtxt
Capital L comma t is equal to loadtxt inside brackets inside double quotes pendulum.txt comma unpack is equal to True
|
Type,
L t
|
Type,
Capital L
t
|
Type, loadtxt? | To know more about loadtxt, type loadtxt question mark
|
Type,
import matplotlib.pyplot as plt tsq = t * t plt.plot(L, tsq, 'bo')
plt.show() |
Let us first plot L versus t square.
plt.show open and close brackets |
Show plot window and highlight the graph | We can see that there is a visible linear trend, but we do not get a straight line connecting them.
|
Close the image | Let me close this image. |
Slide:
least square fit line
|
We need to fit a line through points for the the equation
Capital T square is equal to m asterisk Capital L plus c
|
slide:
steps for least square fit line |
Let us see the steps to generate a least square fit line.
|
Slide Matrix Formulation | In matrix form, the equation can be represented as
tsq is equal to A asterisk p
Each element of this array will contain the square of the time period.
The first column will contain the length of the pendulum. The second column will contain the number 1.
The first row contains the slope of the line. The second row contains the intercept of the line.
|
Type,
from numpy import array,ones_like inter_mat = array((L, ones_like(L))) inter_mat
|
Let us now generate the A matrix with L values.
|
Type,
A = inter_mat.T A |
Now we need the transpose.
Capital A is equal to inter underscore mat dot Capital T
Capital A |
Type,
from numpy.linalg import lstsq result = lstsq(A, tsq, rcond=None)
result |
Now we have both the matrices A and tsq.
result
|
Type,
p = result[0] m, c = p m c |
Now we will store result of index 0 in m and c respectively.
p is equal to result inside square brackets 0
m comma c is equal to p
m c
|
Type,
tsq_fit = m * L + c plt.plot(L, tsq, 'bo') plt.plot(L, tsq_fit, 'r')
plt.show()
|
Now we have m and c.
plt.show open and close brackets
|
Close the image. | Let me close this window. |
Show Slide
Summary slide
|
This brings us to the end of this tutorial. Let us summarize.
|
Show Slide Evaluation
|
Here is a self assessment question for you to solve
|
Show Slide
Solutions
|
And the answer,
|
Show Slide Forum | Please post your timed queries in this forum. |
Show Slide Fossee Forum | Please post your general queries on Python in this forum. |
Show Slide TBC | FOSSEE team coordinates the TBC project. |
Show Slide
Acknowledgement |
Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.
For more details, visit this website. |
Show Slide Thank You | This is Priya from IIT Bombay signing off. Thanks for watching. |