<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="https://script.spoken-tutorial.org/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=Python-3.4.3%2FC3%2FLeast-square-fit%2FEnglish-timed</id>
		<title>Python-3.4.3/C3/Least-square-fit/English-timed - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=Python-3.4.3%2FC3%2FLeast-square-fit%2FEnglish-timed"/>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python-3.4.3/C3/Least-square-fit/English-timed&amp;action=history"/>
		<updated>2026-05-04T16:59:57Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.23.17</generator>

	<entry>
		<id>https://script.spoken-tutorial.org/index.php?title=Python-3.4.3/C3/Least-square-fit/English-timed&amp;diff=47665&amp;oldid=prev</id>
		<title>PoojaMoolya: Created page with &quot;{| border=1 | &lt;center&gt;'''Time'''&lt;/center&gt; | &lt;center&gt;'''Narration'''&lt;/center&gt;  |- | 00:01 | Welcome to the spoken tutorial on '''Least Square Fit'''.   |- |00:06 | In this tuto...&quot;</title>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python-3.4.3/C3/Least-square-fit/English-timed&amp;diff=47665&amp;oldid=prev"/>
				<updated>2019-06-03T07:21:23Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;{| border=1 | &amp;lt;center&amp;gt;&amp;#039;&amp;#039;&amp;#039;Time&amp;#039;&amp;#039;&amp;#039;&amp;lt;/center&amp;gt; | &amp;lt;center&amp;gt;&amp;#039;&amp;#039;&amp;#039;Narration&amp;#039;&amp;#039;&amp;#039;&amp;lt;/center&amp;gt;  |- | 00:01 | Welcome to the spoken tutorial on &amp;#039;&amp;#039;&amp;#039;Least Square Fit&amp;#039;&amp;#039;&amp;#039;.   |- |00:06 | In this tuto...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{| border=1&lt;br /&gt;
| &amp;lt;center&amp;gt;'''Time'''&amp;lt;/center&amp;gt;&lt;br /&gt;
| &amp;lt;center&amp;gt;'''Narration'''&amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 00:01&lt;br /&gt;
| Welcome to the spoken tutorial on '''Least Square Fit'''. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|00:06&lt;br /&gt;
| In this tutorial, you will learn to, &lt;br /&gt;
&lt;br /&gt;
Generate the '''least square fit '''line for a given set of '''points.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 00:16&lt;br /&gt;
| To record this tutorial, I am using '''Ubuntu Linux 16.04''' operating system&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 00:24&lt;br /&gt;
|  '''Python 3.4.3 '''and '''IPython 5.1.0'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 00:31&lt;br /&gt;
| To practise this tutorial, you should know about &lt;br /&gt;
&lt;br /&gt;
using '''plot''' interactively&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 00:38&lt;br /&gt;
|Loading data from files &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 00:41&lt;br /&gt;
| Using '''arrays''' and '''matrices '''and theoretical knowledge of''' least square method'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 00:48&lt;br /&gt;
| If not, see the relevant '''Python '''tutorials on this website.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 00:53&lt;br /&gt;
| Before beginning this tutorial, please download the file''' pendulum.txt '''from the '''Code files''' link of this tutorial.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 01:03&lt;br /&gt;
| Save it in the current '''working directory'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 01:07&lt;br /&gt;
| Let us start the tutorial with the help of an example. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 01:12&lt;br /&gt;
| Generate a '''least square fit''' line for''' L '''versus '''t square''' using the data in the file ''''pendulum.txt''''. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 01:22&lt;br /&gt;
| Let us open the file '''pendulum.txt.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 01:26&lt;br /&gt;
| This is an '''input file''' generated from a simple '''pendulum experiment'''. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 01:32&lt;br /&gt;
| The first column is the '''length''' of the '''pendulum.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 01:36&lt;br /&gt;
| The second is the corresponding '''time period''' of the '''pendulum'''. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 01:41&lt;br /&gt;
| The '''square''' of '''time period''' is directly proportional to its '''length.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 01:47&lt;br /&gt;
| We shall plot '''L''' versus '''t square''' and verify this. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|01:52&lt;br /&gt;
| Let us start '''ipython.''' Open the '''terminal'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|01:57&lt;br /&gt;
| Type '''ipython3 '''and press '''Enter'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 02:03&lt;br /&gt;
| From here onwards remember to press the '''Enter''' key after typing every command on the '''terminal'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 02:10&lt;br /&gt;
| To read the input file and '''parse''' the data, we are going to use the '''loadtxt function'''. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 02:17&lt;br /&gt;
| Type, '''from numpy import loadtxt'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 02:23&lt;br /&gt;
| Now type, Capital '''L '''''comma''''' t '''''is equal to''''' loadtxt''''' inside brackets inside double quotes '''''pendulum.txt '''''comma''''' unpack '''''is equal to''''' True'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 02:37&lt;br /&gt;
| '''loadtxt''' is a '''method''' available in the '''numpy library'''. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 02:42&lt;br /&gt;
| Since '''True''' is passed to '''unpack argument''', the returned '''array''' is transposed.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 02:49&lt;br /&gt;
| This means that we will get one '''array''' per column in the file.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 02:54&lt;br /&gt;
| Type capital '''L'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 02:59&lt;br /&gt;
| Then type '''t'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 03:03&lt;br /&gt;
| We can see that''' L '''and '''t''' are '''length''' and '''time''' values. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 03:08&lt;br /&gt;
| To know more about '''loadtxt, '''type '''loadtxt '''''question mark''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 03:16&lt;br /&gt;
| Press '''q '''to exit.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|03:19&lt;br /&gt;
| Let us first '''plot L '''versus '''t square. '''&lt;br /&gt;
&lt;br /&gt;
Type as shown.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 03:27&lt;br /&gt;
| Here '''bo''' represents the blue circle marker.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 03:32&lt;br /&gt;
| Then type, '''plt.show '''''open and close brackets''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 03:38&lt;br /&gt;
| We can see that there is a '''visible linear trend''', but we do not get a straight line connecting them. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 03:45&lt;br /&gt;
| Looking at the '''trend''', we can now propose a '''model''' for the data.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 03:50&lt;br /&gt;
| Let me close this image.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 03:53&lt;br /&gt;
| We need to fit a line through points for the the equation.&lt;br /&gt;
&lt;br /&gt;
Capital '''T '''''square is''''' '''''equal to''''' m '''''asterisk''''' '''Capital '''L '''''plus''''' c'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 04:03&lt;br /&gt;
| Where '''m''' represents the '''slope''' of the line and '''c''' represents the intercept of the line. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 04:10&lt;br /&gt;
| We will obtain '''m''' and '''c''' using '''linear regression'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 04:15&lt;br /&gt;
| Let us see the steps to generate a '''least''' '''square fit line.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 04:20&lt;br /&gt;
| First generate the two '''matrices tsq''' and '''A'''.  Use the '''lstsq function''' to find the values of the slope '''m''' and the intercept '''c'''. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 04:33&lt;br /&gt;
| In '''matrix''' form, the equation can be represented as &lt;br /&gt;
&lt;br /&gt;
'''tsq''' ''is equal to'' '''A '''''asterisk '''''p'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 04:41&lt;br /&gt;
| '''tsq''' is a one-dimensional array of size '''n'''. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 04:46&lt;br /&gt;
| Each '''element''' of this '''array''' will contain the '''square''' of the '''time period'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 04:52&lt;br /&gt;
| '''A''' is a '''matrix''' of size '''n '''by '''2'''. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 04:56&lt;br /&gt;
| The first column will contain the '''length''' of the '''pendulum'''. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 05:00&lt;br /&gt;
| The second column will contain the number 1.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 05:04&lt;br /&gt;
| '''p''' is a one-dimensional '''array''' of size 2. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 05:08&lt;br /&gt;
| The first row contains the '''slope''' of the line. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 05:12&lt;br /&gt;
| The second row contains the '''intercept''' of the line.&lt;br /&gt;
&lt;br /&gt;
We need to find '''p''' to '''plot''' the line.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 05:20&lt;br /&gt;
| Let us now generate the '''A matrix''' with '''L''' values. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 05:25&lt;br /&gt;
| We shall generate a '''matrix''' with the first '''row''' as '''L''' values and the second '''row''' as ones. &lt;br /&gt;
&lt;br /&gt;
Then take the transpose of it.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 05:34&lt;br /&gt;
| Type as shown.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 05:38&lt;br /&gt;
| We see that we have intermediate '''matrix'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 05:42&lt;br /&gt;
| Now we need the transpose. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 05:45&lt;br /&gt;
| Type, Capital '''A '''is equal to''' inter''''' underscore '''''mat '''dot''' '''Capital '''T '''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 05:53&lt;br /&gt;
| Now type capital '''A'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 05:56&lt;br /&gt;
| Now we have both the '''matrices''' '''A''' and '''tsq'''. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 06:01&lt;br /&gt;
| We only need to use '''lstsq.''' &lt;br /&gt;
&lt;br /&gt;
Type as shown.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 06:08&lt;br /&gt;
| Now to see the result, type '''result'''&lt;br /&gt;
&lt;br /&gt;
The '''result''' is a sequence of '''values'''. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|06:16&lt;br /&gt;
| Now we will store result of '''index 0''' in '''m''' and '''c''' respectively.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 06:23&lt;br /&gt;
| Type, '''p '''''is equal to''''' result '''''inside square brackets''''' 0'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Then type, '''m '''''comma '''''c '''''is equal to''''' p'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 06:34&lt;br /&gt;
| Now type, '''m''' '''c'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 06:39&lt;br /&gt;
| We can see the values for '''m''' and '''c'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|06:43&lt;br /&gt;
| Now we have '''m''' and '''c.''' &lt;br /&gt;
&lt;br /&gt;
We need to generate the fitted values of '''t square'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 06:51&lt;br /&gt;
| Type as shown.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 06:55&lt;br /&gt;
| Then type, '''plt.show''''' open and close brackets''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 07:01&lt;br /&gt;
| We get the least square fit of '''L''' versus '''t square'''. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 07:06&lt;br /&gt;
| Let me close this window.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 07:09&lt;br /&gt;
| This brings us to the end of this tutorial. Let us summarize.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 07:15 &lt;br /&gt;
| In this tutorial, we have learnt to, Generate a '''least square fit''' using '''matrices'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 07:23&lt;br /&gt;
|  Use the '''function lstsq()''' to generate a '''least square fit''' line&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 07:29&lt;br /&gt;
| Here is a self assessment question for you to solve &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 07:33&lt;br /&gt;
| What does the following '''function''' produce? &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 07:35&lt;br /&gt;
| And the answer,&lt;br /&gt;
&lt;br /&gt;
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'''. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 07:51&lt;br /&gt;
| Please post your timed queries in this forum.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 07:55&lt;br /&gt;
| Please post your general queries on Python in this forum.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 08:00&lt;br /&gt;
| FOSSEE team coordinates the TBC project.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 08:04&lt;br /&gt;
| Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.&lt;br /&gt;
&lt;br /&gt;
For more details, visit this website.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 08:14&lt;br /&gt;
| This is Priya from IIT Bombay signing off. Thanks for watching.&lt;br /&gt;
&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>PoojaMoolya</name></author>	</entry>

	</feed>