Difference between revisions of "Python-3.4.3/C3/Least-square-fit/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "'''Title of script''': '''Least Square Fit''' '''Author: Aditya Palaparthy, Arun KP''' '''Keywords: Python, IPython, Linear Regression, lstsq(), least square fit, video tuto...")
 
 
Line 21: Line 21:
  
 
* Generate the '''least square fit '''line for a given set of '''points.'''
 
* Generate the '''least square fit '''line for a given set of '''points.'''
 
 
  
 
|-
 
|-
Line 33: Line 31:
 
* '''Python 3.4.3 '''and
 
* '''Python 3.4.3 '''and
 
* '''IPython 5.1.0'''
 
* '''IPython 5.1.0'''
 
 
  
 
|-
 
|-
Line 43: Line 39:
  
 
* using '''plot''' interactively
 
* using '''plot''' interactively
* '''loading''' '''data''' from '''files'''
+
* loading data from files  
* '''using arrays''' and '''matrices '''and
+
* using '''arrays''' and '''matrices '''and
 
* theoretical knowledge of''' least square method'''
 
* theoretical knowledge of''' least square method'''
  
Line 53: Line 49:
  
 
Pendulum.txt
 
Pendulum.txt
| style="background-color:#ffffff;border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Before beginning this tutorial,
+
| style="background-color:#ffffff;border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Before beginning this tutorial, please download the file''' pendulum.txt '''from the '''Code files''' link of this tutorial.
 
+
 
+
Please download the file''' pendulum.txt '''from the '''Code files''' link of this tutorial.
+
  
  
Save it in the current working directory.
+
Save it in the current '''working directory'''.
  
 
|-
 
|-
Line 69: Line 62:
  
  
Generate a least square fit line for''' L '''versus '''t square''' using the data in the file ''''pendulum.txt''''.  
+
Generate a '''least square fit''' line for''' L '''versus '''t square''' using the data in the file ''''pendulum.txt''''.  
  
 
|-
 
|-
Line 77: Line 70:
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Open the file 'pendulum.txt' and show  
 
| style="background-color:#ffffff;border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Open the file 'pendulum.txt' and show  
| style="background-color:#ffffff;border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| This is an input '''file''' generated from a simple '''pendulum''' '''experiment'''.  
+
| style="background-color:#ffffff;border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| This is an '''input file''' generated from a simple '''pendulum experiment'''.  
  
  
Line 83: Line 76:
  
  
The second is the corresponding '''time''' '''period''' of the '''pendulum'''.  
+
The second is the corresponding '''time period''' of the '''pendulum'''.  
  
  
The '''square''' of '''time''' '''period''' is directly '''proportional''' to its '''length.'''
+
The '''square''' of '''time period''' is directly proportional to its '''length.'''
  
  
Line 106: Line 99:
  
  
From here onwards remember to press the Enter key after typing every command on the terminal.
+
From here onwards remember to press the '''Enter''' key after typing every command on the '''terminal'''.
  
 
|-
 
|-
Line 118: Line 111:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To read the input file and '''parse''' the '''data''', we are going to use the '''loadtxt''' '''function'''.  
+
| style="background-color:#ffffff;border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To read the input file and '''parse''' the data, we are going to use the '''loadtxt function'''.  
  
  
Line 131: Line 124:
  
  
'''loadtxt''' is a method available in the '''numpy''' library.  
+
'''loadtxt''' is a '''method''' available in the '''numpy library'''.  
  
  
Since '''True''' is passed to '''unpack''' argument, the returned array is transposed.
+
Since '''True''' is passed to '''unpack argument''', the returned '''array''' is transposed.
  
  
This means that we will get one array per column in the file.
+
This means that we will get one '''array''' per column in the file.
  
 
|-
 
|-
Line 148: Line 141:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type,
+
| style="background-color:#ffffff;border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type capital '''L'''
  
Capital '''L'''
 
  
 
+
Then type '''t'''
Then type,
+
 
+
'''t'''
+
  
  
Line 183: Line 172:
  
 
'''plt.show()'''
 
'''plt.show()'''
| style="background-color:#ffffff;border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Let us first '''plot''' '''L '''versus '''t square. '''
+
| style="background-color:#ffffff;border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Let us first '''plot L '''versus '''t square. '''
  
  
Line 201: Line 190:
  
  
Looking at the trend, we can now propose a model for the data.
+
Looking at the '''trend''', we can now propose a '''model''' for the data.
  
 
|-
 
|-
Line 210: Line 199:
 
| style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Slide:
 
| style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Slide:
  
'''least''' '''square fit line'''
+
'''least square fit line'''
  
  
  
| style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We need to '''fit''' a '''line''' through '''points''' for the the '''equation'''
+
| style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We need to fit a line through points for the the equation.
  
 
Capital '''T '''''square is''''' '''''equal to''''' m '''''asterisk''''' '''Capital '''L '''''plus''''' c'''
 
Capital '''T '''''square is''''' '''''equal to''''' m '''''asterisk''''' '''Capital '''L '''''plus''''' c'''
  
  
where '''m''' represents the slope of the line and '''c''' represents the intercept of the line.  
+
where '''m''' represents the '''slope''' of the line and '''c''' represents the intercept of the line.  
  
  
We will obtain '''m''' and '''c''' using linear regression.
+
We will obtain '''m''' and '''c''' using '''linear regression'''.
  
 
|-
 
|-
Line 230: Line 219:
 
| style="background-color:#ffffff;border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Let us see the steps to generate a '''least''' '''square fit line.'''
 
| style="background-color:#ffffff;border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Let us see the steps to generate a '''least''' '''square fit line.'''
  
* First generate the two '''matrices''' '''tsq''' and '''A'''.  
+
* First generate the two '''matrices tsq''' and '''A'''.  
* Use the '''lstsq''' '''function''' to find the values of the slope '''m''' and the intercept '''c'''.  
+
* Use the '''lstsq function''' to find the values of the slope '''m''' and the intercept '''c'''.  
 
+
 
+
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Slide Matrix Formulation
 
| style="background-color:#ffffff;border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Slide Matrix Formulation
| style="background-color:#ffffff;border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| In matrix form, the equation can be represented as  
+
| style="background-color:#ffffff;border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| In '''matrix''' form, the equation can be represented as  
  
 
'''tsq''' ''is equal to'' '''A '''''asterisk '''''p'''
 
'''tsq''' ''is equal to'' '''A '''''asterisk '''''p'''
Line 244: Line 231:
 
'''tsq''' is a one-dimensional array of size '''n'''.  
 
'''tsq''' is a one-dimensional array of size '''n'''.  
  
Each element of this array will contain the square of the time period.
+
Each '''element''' of this '''array''' will contain the '''square''' of the '''time period'''.
  
  
'''A''' is a matrix of size '''n '''by '''2'''.  
+
'''A''' is a '''matrix''' of size '''n '''by '''2'''.  
  
The first column will contain the length of the pendulum.  
+
The first column will contain the '''length''' of the '''pendulum'''.  
  
 
The second column will contain the number 1.
 
The second column will contain the number 1.
  
  
'''p''' is a one-dimensional array of size 2.  
+
'''p''' is a one-dimensional '''array''' of size 2.  
  
The first row contains the slope of the line.  
+
The first row contains the '''slope''' of the line.  
  
The second row contains the intercept of the line.
+
The second row contains the '''intercept''' of the line.
  
  
We need to find '''p''' to plot the line.
+
We need to find '''p''' to '''plot''' the line.
  
 
|-
 
|-
Line 274: Line 261:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Let us now generate the '''A''' '''matrix''' with '''L''' values.  
+
| style="background-color:#ffffff;border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Let us now generate the '''A matrix''' with '''L''' values.  
  
  
Line 286: Line 273:
  
  
We see that we have intermediate matrix.
+
We see that we have intermediate '''matrix'''.
  
 
|-
 
|-
Line 302: Line 289:
  
  
Now type,
+
Now type capital '''A'''
 
+
Capital '''A'''
+
  
 
|-
 
|-
Line 326: Line 311:
  
  
Now to see the result, type  
+
Now to see the result, type '''result'''
 
+
'''result'''
+
  
  
Line 343: Line 326:
  
 
'''c'''
 
'''c'''
| style="background-color:#ffffff;border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now we will store result of index 0 in '''m''' and '''c''' respectively.
+
| style="background-color:#ffffff;border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now we will store result of '''index 0''' in '''m''' and '''c''' respectively.
  
  
Line 411: Line 394:
  
  
In this tutorial, we have learnt to,''' '''
+
In this tutorial, we have learnt to,  
 
+
* Generate a least square fit using matrices
+
* Use the function '''lstsq()''' to generate a least square fit line
+
 
+
  
 +
* Generate a '''least square fit''' using '''matrices'''
 +
* Use the '''function lstsq()''' to generate a '''least square fit''' line
  
 
|-
 
|-
Line 425: Line 406:
 
| style="background-color:#ffffff;border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Here is a self assessment question for you to solve  
 
| style="background-color:#ffffff;border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Here is a self assessment question for you to solve  
  
* What does the following function produce?  
+
* What does the following '''function''' produce?  
 
+
 
+
  
 
|-
 
|-
Line 438: Line 417:
 
| style="background-color:#ffffff;border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| And the answer,''' '''
 
| style="background-color:#ffffff;border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| And the answer,''' '''
  
* The function '''ones''' underscore '''like '''''inside brackets inside square brackets ''1 ''comma'' 2 ''comma'' 3 will generate '''array''' ''inside brackets inside square brackets '''''1''' ''comma'' '''1''' ''comma'' '''1'''.  
+
* The '''function ones''''' underscore '''''like '''''inside brackets inside square brackets ''1 ''comma'' 2 ''comma'' 3 will generate '''array''' ''inside brackets inside square brackets '''''1''' ''comma'' '''1''' ''comma'' '''1'''.  
 
+
 
+
  
 
|-
 
|-

Latest revision as of 09:44, 14 November 2018

Title of script: Least Square Fit

Author: Aditya Palaparthy, Arun KP

Keywords: Python, IPython, Linear Regression, lstsq(), least square fit, video tutorial


Visual Cue
Narration
Show Slide title Welcome to the spoken tutorial on Least Square Fit.
Show Slide

Objectives

In this tutorial, you will learn to,
  • Generate the least square fit line for a given set of points.
Show Slide

System Specifications

To record this tutorial, I am using
  • Ubuntu Linux 16.04 operating system
  • Python 3.4.3 and
  • IPython 5.1.0
Show Slide

Pre-requisites

To practise this tutorial, you should know about
  • using plot interactively
  • loading data from files
  • using arrays and matrices and
  • theoretical knowledge of least square method

If not, see the relevant Python tutorials on this website.

Slide:

Pendulum.txt

Before beginning this tutorial, please download the file pendulum.txt from the Code files link of this tutorial.


Save it in the current working directory.

Show Slide


Example

Let us start the tutorial with the help of an example.


Generate a least square fit line for L versus t square using the data in the file 'pendulum.txt'.

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.


The first column is the length of the pendulum.


The second is the corresponding time period of the pendulum.


The square of time period is directly proportional to its length.


We shall plot L versus t square and verify this.

Open terminal Let us start ipython.


Open the terminal.

Type ipython3


Type ipython3 and press Enter.


From here onwards remember to press the Enter key after typing every command on the terminal.

Type,

from numpy import loadtxt


L, t = loadtxt("pendulum.txt", unpack=True)


To read the input file and parse the data, we are going to use the loadtxt function.


Type,

from numpy import loadtxt


Now type,

Capital L comma t is equal to loadtxt inside brackets inside double quotes pendulum.txt comma unpack is equal to True


loadtxt is a method available in the numpy library.


Since True is passed to unpack argument, the returned array is transposed.


This means that we will get one array per column in the file.

Type,

L

t


Type capital L


Then type t


We can see that L and t are length and time values.

Type, loadtxt? To know more about loadtxt, type loadtxt question mark


Press q to exit.

Type,

import matplotlib.pyplot as plt

tsq = t * t

plt.plot(L, tsq, 'bo')


Highlight bo in the code


Type

plt.show()

Let us first plot L versus t square.


Type as shown.


Here bo represents the blue circle marker.


Then type,

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.


Looking at the trend, we can now propose a model for the data.

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


where m represents the slope of the line and c represents the intercept of the line.


We will obtain m and c using linear regression.

slide:

steps for least square fit line

Let us see the steps to generate a least square fit line.
  • First generate the two matrices tsq and A.
  • Use the lstsq function to find the values of the slope m and the intercept c.
Slide Matrix Formulation In matrix form, the equation can be represented as

tsq is equal to A asterisk p


tsq is a one-dimensional array of size n.

Each element of this array will contain the square of the time period.


A is a matrix of size n by 2.

The first column will contain the length of the pendulum.

The second column will contain the number 1.


p is a one-dimensional array of size 2.

The first row contains the slope of the line.

The second row contains the intercept of the line.


We need to find p to plot 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.


We shall generate a matrix with the first row as L values and the second row as ones.


Then take the transpose of it.


Type as shown.


We see that we have intermediate matrix.

Type,

A = inter_mat.T

A

Now we need the transpose.


Type,

Capital A is equal to inter underscore mat dot Capital T


Now type capital A

Type,

from numpy.linalg import lstsq

result = lstsq(A, tsq, rcond=None)


Type,

result

Now we have both the matrices A and tsq.


We only need to use lstsq.


Type as shown.


Now to see the result, type result


The result is a sequence of values.

Type,

p = result[0]

m, c = p

m

c

Now we will store result of index 0 in m and c respectively.


Type,

p is equal to result inside square brackets 0


Then type,

m comma c is equal to p


Now type,

m

c


We can see the values for m and c.

Type,

tsq_fit = m * L + c

plt.plot(L, tsq, 'bo')

plt.plot(L, tsq_fit, 'r')


Type,

plt.show()


Highlight the output

Now we have m and c.


We need to generate the fitted values of t square.


Type as shown.


Then type,

plt.show open and close brackets


We get the least square fit of L versus t square.

Close the image. Let me close this window.
Show Slide

Summary slide


This brings us to the end of this tutorial. Let us summarize.


In this tutorial, we have learnt to,

  • Generate a least square fit using matrices
  • Use the function lstsq() to generate a least square fit line
Show Slide Evaluation


Here is a self assessment question for you to solve
  • What does the following function produce?
Show Slide

Solutions


And the answer,
  • The function ones underscore like inside brackets inside square brackets 1 comma 2 comma 3 will generate array inside brackets inside square brackets 1 comma 1 comma 1.
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.

Contributors and Content Editors

Nancyvarkey, Priyacst