Python-3.4.3/C3/Least-square-fit/English-timed

From Script | Spoken-Tutorial
Revision as of 12:51, 3 June 2019 by PoojaMoolya (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Time
Narration
00:01 Welcome to the spoken tutorial on Least Square Fit.
00:06 In this tutorial, you will learn to,

Generate the least square fit line for a given set of points.

00:16 To record this tutorial, I am using Ubuntu Linux 16.04 operating system
00:24 Python 3.4.3 and IPython 5.1.0
00:31 To practise this tutorial, you should know about

using plot interactively

00:38 Loading data from files
00:41 Using arrays and matrices and theoretical knowledge of least square method
00:48 If not, see the relevant Python tutorials on this website.
00:53 Before beginning this tutorial, please download the file pendulum.txt from the Code files link of this tutorial.
01:03 Save it in the current working directory.
01:07 Let us start the tutorial with the help of an example.
01:12 Generate a least square fit line for L versus t square using the data in the file 'pendulum.txt'.
01:22 Let us open the file pendulum.txt.
01:26 This is an input file generated from a simple pendulum experiment.
01:32 The first column is the length of the pendulum.
01:36 The second is the corresponding time period of the pendulum.
01:41 The square of time period is directly proportional to its length.
01:47 We shall plot L versus t square and verify this.
01:52 Let us start ipython. Open the terminal.
01:57 Type ipython3 and press Enter.
02:03 From here onwards remember to press the Enter key after typing every command on the terminal.
02:10 To read the input file and parse the data, we are going to use the loadtxt function.
02:17 Type, from numpy import loadtxt
02:23 Now type, Capital L comma t is equal to loadtxt inside brackets inside double quotes pendulum.txt comma unpack is equal to True
02:37 loadtxt is a method available in the numpy library.
02:42 Since True is passed to unpack argument, the returned array is transposed.
02:49 This means that we will get one array per column in the file.
02:54 Type capital L
02:59 Then type t
03:03 We can see that L and t are length and time values.
03:08 To know more about loadtxt, type loadtxt question mark
03:16 Press q to exit.
03:19 Let us first plot L versus t square.

Type as shown.

03:27 Here bo represents the blue circle marker.
03:32 Then type, plt.show open and close brackets
03:38 We can see that there is a visible linear trend, but we do not get a straight line connecting them.
03:45 Looking at the trend, we can now propose a model for the data.
03:50 Let me close this image.
03:53 We need to fit a line through points for the the equation.

Capital T square is equal to m asterisk Capital L plus c

04:03 Where m represents the slope of the line and c represents the intercept of the line.
04:10 We will obtain m and c using linear regression.
04:15 Let us see the steps to generate a least square fit line.
04:20 First generate the two matrices tsq and A. Use the lstsq function to find the values of the slope m and the intercept c.
04:33 In matrix form, the equation can be represented as

tsq is equal to A asterisk p

04:41 tsq is a one-dimensional array of size n.
04:46 Each element of this array will contain the square of the time period.
04:52 A is a matrix of size n by 2.
04:56 The first column will contain the length of the pendulum.
05:00 The second column will contain the number 1.
05:04 p is a one-dimensional array of size 2.
05:08 The first row contains the slope of the line.
05:12 The second row contains the intercept of the line.

We need to find p to plot the line.

05:20 Let us now generate the A matrix with L values.
05:25 We shall generate a matrix with the first row as L values and the second row as ones.

Then take the transpose of it.

05:34 Type as shown.
05:38 We see that we have intermediate matrix.
05:42 Now we need the transpose.
05:45 Type, Capital A is equal to inter underscore mat dot Capital T
05:53 Now type capital A
05:56 Now we have both the matrices A and tsq.
06:01 We only need to use lstsq.

Type as shown.

06:08 Now to see the result, type result

The result is a sequence of values.

06:16 Now we will store result of index 0 in m and c respectively.
06:23 Type, p is equal to result inside square brackets 0


Then type, m comma c is equal to p

06:34 Now type, m c
06:39 We can see the values for m and c.
06:43 Now we have m and c.

We need to generate the fitted values of t square.

06:51 Type as shown.
06:55 Then type, plt.show open and close brackets
07:01 We get the least square fit of L versus t square.
07:06 Let me close this window.
07:09 This brings us to the end of this tutorial. Let us summarize.
07:15 In this tutorial, we have learnt to, Generate a least square fit using matrices
07:23 Use the function lstsq() to generate a least square fit line
07:29 Here is a self assessment question for you to solve
07:33 What does the following function produce?
07:35 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.

07:51 Please post your timed queries in this forum.
07:55 Please post your general queries on Python in this forum.
08:00 FOSSEE team coordinates the TBC project.
08:04 Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.

For more details, visit this website.

08:14 This is Priya from IIT Bombay signing off. Thanks for watching.

Contributors and Content Editors

PoojaMoolya