Difference between revisions of "Python-3.4.3/C3/Least-square-fit/English"
(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...") |
Nancyvarkey (Talk | contribs) |
||
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 |
− | * ''' | + | * 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. |
− | + | ||
− | + | ||
− | + | ||
− | 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 | + | | 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 | + | The second is the corresponding '''time period''' of the '''pendulum'''. |
− | The '''square''' of '''time | + | 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 | + | | 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''' | + | '''loadtxt''' is a '''method''' available in the '''numpy library'''. |
− | Since '''True''' is passed to '''unpack''' | + | 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''' |
− | |||
− | + | 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 | + | | 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 | + | '''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 | + | | 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 | + | * First generate the two '''matrices tsq''' and '''A'''. |
− | * Use the '''lstsq | + | * 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 | + | | 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''' |
− | + | ||
− | + | ||
|- | |- | ||
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 | ||
|- | |- | ||
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 | + | * 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
|
|
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, please download the file pendulum.txt from the Code files link of 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
|
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
|
Type,
from numpy.linalg import lstsq result = lstsq(A, tsq, rcond=None)
result |
Now we have both the matrices A and tsq.
|
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. |