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

From Script | Spoken-Tutorial
Jump to: navigation, search

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