1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
.. Objectives
.. ----------
.. At the end of this tutorial, you will be able to
.. + Read data from files, containing a single column of data using the
.. ``loadtxt`` command.
.. + Read multiple columns of data, separated by spaces or other
.. delimiters.
.. Prerequisites
.. -------------
.. 1. getting started with ``ipython``
.. Author : Puneeth Changanti
Internal Reviewer : Nishanth Amuluru
External Reviewer :
Language Reviewer : Bhanukiran
Checklist OK? : <06-11-2010 Anand, OK> [2010-10-05]
Script
------
.. L1
{{{ Show the first slide containing title, name of the production
team along with the logo of MHRD }}}
.. R1
Hello Friends and Welcome to this tutorial on "loading data from files".
.. L2
{{{ Show slide with objectives }}}
.. R2
At the end of this tutorial, you will be able to,
1. Read data from files, containing a single column of data
#. Read multiple columns of data, separated by spaces or other
delimiters.
.. L3
{{{ switch to the terminal }}}
::
ipython -pylab
.. R3
Let us switch to the terminal and start IPython, using ipython -pylab
.. R4
Now, Let us begin with reading the file primes.txt, which contains
a list of prime numbers listed in a column, using the loadtxt command.
Please make sure that you provide the correct path of the file,
'primes.txt'.
The file, in our case, is present in ``/home/fossee/primes.txt``.
.. L4
{{{ Navigate to the path in the OS, open the file and show it }}}
.. L5
::
cat /home/fossee/primes.txt
.. R5
Otherwise we can use the ``cat`` command to locate the file and read the
contents of it.
.. R6
Now let us read this list into the variable ``primes``.
.. L6
::
primes = loadtxt('/home/fossee/primes.txt')
.. R7
``primes`` is now a sequence of prime numbers, that was listed in the
file,``primes.txt``.
We now type, ``print primes`` to see the sequence printed.
.. L7
::
print primes
.. R8
We observe that all the numbers end with a period. This is so,
because these numbers are actually read as ``floats``.
.. L8
{{{Highlight the output on the terminal}}}
.. R9
Now, let us use the ``loadtxt`` command to read a file ``pendulum.txt``
that contains two columns of data. This file contains the length
of the pendulum in the first column and the corresponding time period
in the second. Note that here ``loadtxt`` needs both the columns to
have equal number of rows.
We use the ``cat`` command to view the contents of this file.
.. L9
::
cat /home/fossee/pendulum.txt
.. R10
Let us, now, read the data into the variable ``pend``. Again, it is
assumed that the file is in ``/home/fossee/``
.. L10
::
pend = loadtxt('/home/fossee/pendulum.txt')
.. R11
Let us now print the variable ``pend`` and see what it contains.
.. L11
::
print pend
.. R12
Notice that ``pend`` is not a simple sequence like ``primes``. It has
two sequences, containing both the columns of the data file. Let us
use an additional argument of the ``loadtxt`` command, to read it into
two separate, simple sequences.
.. L12
::
L, T = loadtxt('/home/fossee/pendulum.txt', unpack=True)
.. R13
Let us now, print the variables L and T, to see what they contain.
.. L13
::
print L
print T
.. R14
Notice, that L and T now contain the first and second columns of data
from the data file, ``pendulum.txt``, and they are both simple
sequences. ``unpack=True`` has given us the two columns into two
separate sequences instead of one complex sequence.
.. L14
.. L15
{{{ Show slide with exercise 1 }}}
.. R15
Till now, we have learnt the basic use of the ``loadtxt``
command.Let us try an example.
Pause the video here, try out the following exercise and resume the video.
Read the file ``pendulum_semicolon.txt`` which contains the same
data as ``pendulum.txt``, but the columns are separated by semi-colons
instead of spaces. Use the IPython help to see how to do this.
.. L16
{{{ switch back to the terminal }}}
::
L, T = loadtxt('/home/fossee/pendulum_semicolon.txt', unpack=True,
delimiter=';')
print L
print T
.. R16
.. L17
{{{ show the summary slide }}}
.. R17
This brings us to the end of this tutorial.In this tutorial,
we have learnt to,
1. To Read data from files, containing a single column of data
using the ``loadtxt`` command.
#. To Read multiple columns of data, separated by spaces or other
delimiters.
.. L18
{{Show self assessment questions slide}}
.. R18
Here are some self assessment questions for you to solve
1. ``loadtxt`` can read data from a file with one column
only. True or False?
2. Given a file ``data.txt`` with three columns of data separated by
spaces, read it into 3 separate simple sequences.
3. Given a file ``data.txt`` with three columns of data separated by
":", read it into 3 separate simple sequences.
.. L19
{{{solution of self assessment questions on slide}}}
.. R19
And the answers,
1. False. ``loadtxt`` command can read data from files having both single
columns as well as multiple columns.
2. A file with three columns of data separated by spaces to be read into
3 separate sequences,
we use the loadtxt command as,
::
x = loadtxt("data.txt", unpack=True)
3. If a file with three columns of data separated by delimiters,we read
it into three separate sequences by using an additional argument of
delimiter in the loadtxt command
::
x = loadtxt("data.txt", unpack=True, delimiter=":")
.. L20
{{{ Show the Thank you slide }}}
.. R20
Hope you have enjoyed this tutorial and found it useful.
Thank you!
|