day1/sslc1.py
author Christopher Burns <chris.d.burns@gmail.com>
Wed, 16 Jun 2010 23:19:09 -0700
branchscipy2010
changeset 403 9b8f03e012ba
parent 248 1ebf842cb035
permissions -rw-r--r--
Add notes on day2 material for sessions 7-12
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
248
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     1
from pylab import *
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     2
from scipy import *
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     3
from scipy import stats
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     4
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     5
scores = [[], [], [], [], []]
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     6
ninety_percents = [{}, {}, {}, {}, {}]
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     7
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     8
for record in open('sslc1.txt'):
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     9
    record = record.strip()
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    10
    fields = record.split(';')
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    11
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    12
    region_code = fields[0].strip()
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    13
   
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    14
    for i, field in enumerate(fields[3:8]):
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    15
        if region_code not in ninety_percents[i]:
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    16
            ninety_percents[i][region_code] = 0
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    17
        score_str = field.strip()
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    18
        score = 0 if score_str == 'AA' else int(score_str)
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    19
        scores[i].append(score)
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    20
        if score > 90:
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    21
            ninety_percents[i][region_code] += 1
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    22
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    23
subj_total = []
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    24
for subject in ninety_percents:
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    25
    subj_total.append(sum(subject.values()))
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    26
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    27
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    28
figure(1)
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    29
pie(ninety_percents[3].values(), labels=ninety_percents[3].keys())
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    30
title('Students scoring 90% and above in science by region')
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    31
savefig('/tmp/science.png')
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    32
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    33
figure(2)
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    34
pie(subj_total, labels=['English', 'Hindi', 'Maths', 'Science', 'Social'])
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    35
title('Students scoring more than 90% by subject(All regions combined).')
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    36
savefig('/tmp/all_regions.png')
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    37
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    38
math_scores = array(scores[2])
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    39
# Mean score in Maths(All regions combined)
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    40
print "Mean: ", mean(math_scores)
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    41
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    42
# Median score in Maths(All regions combined)
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    43
print "Median: ", median(math_scores)
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    44
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    45
# Mode score in Maths(All regions combined)
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    46
print "Mode: ", stats.mode(math_scores)
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    47
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    48
# Standard deviation of scores in Maths(All regions combined)
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    49
print "Standard Deviation: ", std(math_scores)
1ebf842cb035 Added sslc.py Session 3 day 1 solution code.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    50