statistics.txt
author Santosh G. Vattam <vattam.santosh@gmail.com>
Tue, 30 Mar 2010 19:17:12 +0530
changeset 7 9794cc414498
parent 6 e1fcec83e1ab
child 46 34df59770550
permissions -rw-r--r--
Minor edits to statistics.txt
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     1
Hello welcome to the tutorial on statistics and dictionaries in Python.
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     2
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     3
In the previous tutorial we saw the `for' loop and lists. Here we shall look into
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     4
calculating mean for the same pendulum experiment and then move on to calculate
7
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
     5
the mean, median and standard deviation for a very large data set.
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
     6
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
     7
Let's start with calculating the mean acceleration due to gravity based on the data from pendulum.txt.
6
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     8
7
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
     9
We first create an empty list `g_list' to which we shall append the values of `g'.
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    10
In []: g_list = []
6
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    11
7
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    12
For each pair of `L' and `t' values in the file `pendulum.txt' we calculate the 
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    13
value of `g' and append it to the list `g_list'
6
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    14
In []: for line in open('pendulum.txt'):
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    15
  ....     point = line.split()
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    16
  ....     L = float(point[0])
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    17
  ....     t = float(point[1])
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    18
  ....     g = 4 * pi * pi * L / (t * t)
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    19
  ....     g_list.append(g)
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    20
7
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    21
We proceed to calculate the mean of the value of `g' from the list `g_list'. 
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    22
Here we shall show three ways of calculating the mean. 
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    23
Firstly, we calculate the sum `total' of the values in `g_list'.
6
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    24
In []: total = 0
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    25
In []: for g in g_list:
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    26
 ....:     total += g
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    27
 ....:
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    28
7
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    29
Once we have the total we calculate by dividing the `total' by the length of `g_list'
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    30
6
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    31
In []: g_mean = total / len(g_list)
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    32
In []: print 'Mean: ', g_mean
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    33
7
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    34
The second method is slightly simpler. Python provides a built-in function called "sum()" that computes the sum of all the elements in a list. 
6
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    35
In []: g_mean = sum(g_list) / len(g_list)
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    36
In []: print 'Mean: ', g_mean
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    37
7
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    38
The third method is the simplest. Python provides a built-in function `mean' that
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    39
calculates the mean of all the elements in a list.
6
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    40
In []: g_mean = mean(g_list)
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    41
In []: print 'Mean: ', g_mean
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    42
7
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    43
Python provides support for dictionaries. Dictionaries are key value pairs. Lists are indexed by integers while dictionaries are indexed by strings. For example:
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    44
In []: d = {'png' : 'image',
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    45
      'txt' : 'text', 
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    46
      'py' : 'python'} 
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    47
is a dictionary. The first element in the pair is called the `key' and the second 
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    48
is called the `value'. The key always has to be a string while the value can be 
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    49
of any type.
6
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    50
7
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    51
Dictionaries are indexed using their keys as shown
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    52
In []: d['txt']
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    53
Out[]: 'text'
6
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    54
7
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    55
In []: d['png']
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    56
Out[]: 'image'
6
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    57
7
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    58
The dictionaries can be searched for the presence of a certain key by typing
6
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    59
In []: 'py' in d
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    60
Out[]: True
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    61
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    62
In []: 'jpg' in d
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    63
Out[]: False
7
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    64
Please note the values cannot be searched in a dictionaries.
6
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    65
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    66
In []: d.keys()
7
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    67
Out[]: ['py', 'txt', 'png']
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    68
is used to obtain the list of all keys in a dictionary
6
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    69
e1fcec83e1ab Added statistics.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    70
In []: d.values()
7
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    71
Out[]: ['python', 'text', 'image']
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    72
is used to obtain the list of all values in a dictionary
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    73
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    74
In []: d
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    75
Out[]: {'png': 'image', 'py': 'python', 'txt': 'text'}
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    76
Please observe that dictionaries do not preserve the order in which the items
9794cc414498 Minor edits to statistics.txt
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 6
diff changeset
    77
were entered. The order of the elements in a dictionary should not be relied upon.