<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Setup
Step1:  Load Data
Step2:  Constructing Histograms
Any pandas series object of list like object can be converted in to ahistogram to map the values of that sequece to their frequencies
Step3:  Getting frequencies for values
Step4:  or equivilantly
Step5:  Visualising
When plotting, the following keyword arguments can be passed to configure the plot once it has been generated
Step6:  By default the plotting method used is pyplot.bar to plot each data points frequency. 
Note that the apparent pattern in the bars above is due to the plot rendering itself (lots of bars very close together), not the underlying data. Zooming in to the plot shows this to be the case. 
To be able to parse in the number of bins, we need to use plt.hist instead of plt.bar. to do this pass in mpl_hist=True. Additional keywords are then parsed to pyplot.hist instead of pyplot.bar
Step7:  multiple plots are possible with the multiplot command that plots multiple objects on a single axes
Step8:  specific plotting parameters can be passed via the plt_kwds argument
Step9:  Methods
Explorative / summary
Step10:  Manipulation
<ASSISTANT_TASK:>
Python Code:
# Imports
import os
import sys
import pandas as pd
import seaborn as sb
# Custom Imports
sys.path.insert(0, '../../')
import stats_toolbox as st
from stats_toolbox.utils.data_loaders import load_fem_preg_2002
# Graphics setup 
%pylab inline --no-import-all
sb.set_context('notebook', font_scale=1.5)
Explanation: Setup
End of explanation
from stats_toolbox.utils.data_loaders import load_fem_preg_2002
# Load and Clean Data
df = load_fem_preg_2002('../data')
full_term = df[df['prglngth'] >= 37]
weights = df.birthwgt_kg.dropna()
Explanation: Load Data
End of explanation
H = st.Hist(df.totalwgt_lb, label='Birth Weight')
Explanation: Constructing Histograms
Any pandas series object of list like object can be converted in to ahistogram to map the values of that sequece to their frequencies
End of explanation
H.freq(8)
Explanation: Getting frequencies for values
End of explanation
H[8]
Explanation: or equivilantly
End of explanation
H.plot(title='My First Histogram', xlabel='Wieght (lb)', legend=True)
Explanation: Visualising
When plotting, the following keyword arguments can be passed to configure the plot once it has been generated:
title, xlabel, ylabel, xscale, yscale, xticks, yticks, axis, xlim, ylim, legend
These all call the appropriate method from pyplot to alter the plot with the values passed in. e.g.
End of explanation
H.plot(mpl_hist=True, bins=30, histtype='stepfilled', alpha=0.7, 
      title='My Second Histogram', xlabel='Wieght (lb)', legend=True)
Explanation: By default the plotting method used is pyplot.bar to plot each data points frequency. 
Note that the apparent pattern in the bars above is due to the plot rendering itself (lots of bars very close together), not the underlying data. Zooming in to the plot shows this to be the case. 
To be able to parse in the number of bins, we need to use plt.hist instead of plt.bar. to do this pass in mpl_hist=True. Additional keywords are then parsed to pyplot.hist instead of pyplot.bar
End of explanation
hist1 = st.Hist(np.repeat(np.arange(0,20,2), np.arange(20,0,-2)), label='Hist 1')
hist2 = st.Hist(np.repeat(np.arange(10,30,2), np.arange(20,0,-2)), label='Hist 2')
f = st.multiplot([hist1, hist2], title='My Title', ylim=(0,22))
Explanation: multiple plots are possible with the multiplot command that plots multiple objects on a single axes
End of explanation
f = st.multiplot([hist1, hist2], plt_kwds={'mpl_hist': True, 'bins': 10}, 
                 title='My Title', ylim=(0,22))
Explanation: specific plotting parameters can be passed via the plt_kwds argument
End of explanation
# Find the n largest/smallest values and thier frequencies 
top20 = H.largest(20)
bottom5 = H.smallest(5)
# Find the most/least frequent values
top5feq = H.most_freq(5)
bottom5freq = H.least_freq(5)
# Calculate stats
print(H.mean(), H.var(), H.std())
Explanation: Methods
Explorative / summary
End of explanation
hist1 = st.Hist(np.repeat(np.arange(0,20,1), np.arange(20,0,-1)))
# Increment or multiply, changes are made inplace
hist1.incr(4, 5)
hist1.mult(10, 2.5)
hist1.plot()
# Histogram Arithmatic
hist1 = st.Hist(np.repeat(np.arange(0,20,1), np.arange(20,0,-1)))
hist2 = st.Hist(np.repeat(np.arange(10,30,1), np.arange(20,0,-1)))
hist3 = hist1 + hist2
hist3.plot()
(hist1 - hist2).plot(ylim=(-12, 22))
H_first = st.Hist(full_term.prglngth[full_term.birthord == 1], label='1st born')
H_other = st.Hist(full_term.prglngth[full_term.birthord != 1], label='other')
(H_first - H_other).plot(title = 'Differences in Pregnancy length between first and other babies')
Explanation: Manipulation
End of explanation
<END_TASK>