#import important libraries am going to use
%matplotlib inline
from collections import defaultdict
import json
from __future__ import division

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

from matplotlib import rcParams
import matplotlib.cm as cm
import matplotlib as mpl

import seaborn as sns
sns.set_context("talk")
sns.set_style("white")

"""
Explanation: Author: Barnabas Makonda

Data Scource Tanzania OpenData
This dataset contains ranking information of primary schools according to performance in primary school leaving certificate examinations.
End of explanation
"""


df = pd.read_csv('PrimarySchoolsPerfomanceAndLocation-2014.csv')
df.shape

df.columns

col =['NAME','REGION','DISTRICT','OWNERSHIP','PASS_RATE','AVG_MARK','CHANGE_PREVIOUS_YEAR','RANK']
for c in df.columns:
    if c not in col:
        df=df.drop(c,axis=1)

df.shape

df.OWNERSHIP.unique()

df.head(10)

"""
Explanation: Load Data
End of explanation
"""


df.shape[0]

"""
Explanation: We need to know number of primary school which did standard seven exam year 2014
End of explanation
"""


print df.OWNERSHIP.unique()

"""
Explanation: There about 15866 schools
Ownership of the School
can be seen below schools are grouped either Government or Non Government schools and there are about 2833 schools which have no type
End of explanation
"""


df[df.OWNERSHIP.isnull()].shape[0]

"""
Explanation: how many do not have type or they are empty?
End of explanation
"""


df[df.OWNERSHIP.isnull()].head(10)

"""
Explanation: Sample of school which ownership is empty
End of explanation
"""


df[df.OWNERSHIP.notnull()].head(10)

"""
Explanation: Those which ownership is not null, either Government or Non Government
End of explanation
"""


df[df.OWNERSHIP.notnull()].shape[0]

"""
Explanation: and they are 13033
End of explanation
"""


government_schools =sum(df.OWNERSHIP=='GOVERNMENT') #Government schools
nongovernment_schools =sum(df.OWNERSHIP=='NON GOVERNMENT') #nongovernment schools
unknown = sum(df.OWNERSHIP.isnull()) #number of shools with unknown ownership
schl=df.shape[0] #number of schools


Labels =['Govenment', 'Non Government','Unknown']
fractions =[float(government_schools)/schl, float(nongovernment_schools)/schl, float(unknown)/schl] #percentage
colors = ['yellowgreen', 'gold', 'lightskyblue'] #colors for pie chart
explode = (0, 0, 0) #only explode the first slice

plt.pie(fractions, explode=explode, labels=Labels, colors=colors,
        autopct='%1.1f%%', shadow=True, startangle=90)
# Set aspect ratio to be equal so that pie is drawn as a circle.
plt.axis('equal')
plt.show()

"""
Explanation: Lets plot a pie chat to visualize the data
End of explanation
"""


df[['PASS_RATE','AVG_MARK','CHANGE_PREVIOUS_YEAR']].describe()

"""
Explanation: Lets play with the numbers now
Lets look and see summary of the Pass Rate, Avarage Mark and  Change compared to previous year
End of explanation
"""


df[df.PASS_RATE == 100 ]

print "There were %s schools which had 100 pass rate"%sum(df.PASS_RATE == 100 )

df[(df.PASS_RATE == 100) & (df.OWNERSHIP=="GOVERNMENT")].describe()

print "Sample of Government school which have 100% passing rate"
df[(df.PASS_RATE == 100) & (df.OWNERSHIP=="GOVERNMENT")].head(10)

"""
Explanation: The avarage Passing rate was 55.93% and Maximum passing rate was 100%(All students passed examination) while lowest was 0(Nobody passed)
Avarage mark was 108.7 maximum avarage mark per school was 234.7  and minimum was 46.62 
NB:These marks are for 5 subjects hence total of 250 marks
Lets play  a little more

How many schools  did have 100% pass rate?
Is there a government school which had 100% pass rate?
Which school did best of all schools?
Which did bad?
End of explanation
"""


grouped = df[df.PASS_RATE == 100 ].groupby(df['REGION'])
passed_per_region = grouped.count()
passed_per_region.NAME

"""
Explanation: Lets group 100% pass rate by region
End of explanation
"""


df.groupby(df.REGION).count().NAME

"""
Explanation: Dar es salaam leads with 121 schools and Katavi is the last with 7
End of explanation
"""


sns.set_context("notebook")
#lets get mean Pass Rate
mean_pass = df.PASS_RATE.mean()
print mean_pass, df.PASS_RATE.median()

with sns.axes_style("whitegrid"):
    df.PASS_RATE.hist(bins=30, alpha=0.4);
    plt.axvline(mean_pass, 0, 0.75, color='r', label='Mean')
    plt.xlabel("Pass Rate")
    plt.ylabel("Counts")
    plt.title("Passing Rate Hisyogram")
    plt.legend()
    sns.despine()

with sns.axes_style("whitegrid"):
    df.CHANGE_PREVIOUS_YEAR.hist(bins=15, alpha=0.6, color='r');
    plt.xlabel("change of passing rate comapred to 2013")
    plt.ylabel("school number")
    plt.title("Change of passing rate Hisyogram")


with sns.axes_style("whitegrid"):
    df.AVG_MARK.hist(bins=40,alpha=0.6, color='g')
    plt.xlabel("Avarage mark per school")
    plt.ylabel("school number")
    plt.title("Avarage Marks Hisyogram")
    plt.legend()

"""
Explanation: MBEYA is leading by having 1046
We can visulize our data more by using seaborn
End of explanation
"""


