# Commits

committed 7cf4b0f

deleted random files

• Participants
• Parent commits 7327077
• Branches master

# File misc/Customer Count_2013_01_07.pdf

Binary file removed.

# File misc/Double Expo smoothing(holt)_FINAL - Copy.py

`-from pandas import *`
`-`
`-`
`-def DoubleExponentialSmoothing(Input,N,Alpha,Beta,Gamma,Seasons):`
`-`
`-    Num = N`
`-    a = Alpha`
`-    b = Beta`
`-    c = Gamma`
`-    s = Seasons`
`-`
`-    # Create data frame`
`-    df = DataFrame(Input, columns=['Revenue'])`
`-`
`-    # Add Cummulative sums`
`-    df['CummSum'] = df['Revenue'].cumsum()`
`-`
`-    # Initial vallues for L and b`
`-    df['L'] = df['Revenue'].mean()`
`-    df['b'] = (df['Revenue'][1*s] - df['Revenue'][0]) / s`
`-`
`-    # Add seasonality column`
`-    df['season'] = 1.0*df['Revenue']/df['Revenue'].mean()`
`-`
`-    # Initial vallues for s`
`-    df['s'] = df['season'][(1*s)]`
`-`
`-    # Initial forecast = actual`
`-    # we multiply by 1.0 to prevent numbers getting rounded to int`
`-    df['DoubleExpo'] = df['Revenue'][0]*1.0`
`-`
`-`
`-    # Single Exponential Smoothing`
`-    for i in range(Num):`
`-`
`-        # We skip the first one since its a guess`
`-        if (i > s):`
`-`
`-            # Get the previous L and b`
`-            LPrev = df['L'][i-1]`
`-            bPrev = df['b'][i-1]`
`-            #print LPrev, bPrev`
`-`
`-            df['L'][i] = a * (df['Revenue'][i] / df['season'][i-1]) + (1 - a) * (LPrev + bPrev)`
`-            df['b'][i] = b * (df['L'][i] - LPrev) + (1 - b) * bPrev`
`-            df['s'] = c * (df['Revenue'][i] / df['L'][i]) + (1 - c) * df['s'][i-1]`
`-`
`-            # We skip the first two`
`-            if (i > 1):`
`-                # forecast for period i`
`-                df['DoubleExpo'][i] = (df['L'][i-1] + df['b'][i-1]) * df['s'][i-1]`
`-                #print df['DoubleExpo'][i]`
`-`
`-`
`-    # Track Errors`
`-    df['Error'] = df['Revenue'] - df['DoubleExpo']`
`-    df['MFE'] = (df['Error']).mean()`
`-    df['MAD'] = np.fabs(df['Error']).mean()`
`-    df['MSE'] = np.sqrt(np.square(df['Error']).mean())`
`-    df['TS'] = np.sum(df['Error'])/df['MAD']`
`-`
`-    return df`
`-`
`-`
`-# Data set`
`-y = [112,`
`-118,`
`-132,`
`-129,`
`-121,`
`-135,`
`-148,`
`-148,`
`-136,`
`-119,`
`-104,`
`-118,`
`-115,`
`-126,`
`-141,`
`-135,`
`-125,`
`-149,`
`-170,`
`-170,`
`-158,`
`-133,`
`-114,`
`-140,`
`-145,`
`-150,`
`-178,`
`-163,`
`-172,`
`-178,`
`-199,`
`-199,`
`-184,`
`-162,`
`-146,`
`-166`
`-] `
`-#[randint(0,100) for r in xrange(10)]`
`-print 'data: ',y`
`-`
`-`
`-result = DoubleExponentialSmoothing(y,len(y),0.5, 0.5, 0.5, 12)`
`-#print result`
`-print result.to_string() #if data is supressed`

# File misc/Double Expo smoothing(holt)_FINAL.py

`-from pandas import *`
`-`
`-`
`-def DoubleExponentialSmoothing(Input,N,Alpha,Beta):`
`-`
`-    # Create data frame`
`-    df = DataFrame(Input, columns=['Revenue'])`
`-`
`-    # Add Cummulative sums`
`-    df['CummSum'] = df['Revenue'].cumsum()`
`-`
`-    # Initial vallues for L and b`
`-    df['L'] = df['Revenue'][0]*1.0`
`-    df['b'] = 0.0`
`-`
`-    # Initial forecast = actual`
`-    # we multiply by 1.0 to prevent numbers getting rounded to int`
`-    df['DoubleExpo'] = df['Revenue'][0]*1.0`
`-`
`-    Num = N`
`-    a = Alpha`
`-    b = Beta`
`-`
`-    # Single Exponential Smoothing`
`-    for i in range(Num):`
`-`
`-        # We skip the first one since its a guess`
`-        if (i > 0):`
`-`
`-            # Get the previous L and b`
`-            LPrev = df['L'][i-1]`
`-            bPrev = df['b'][i-1]`
`-            #print LPrev, bPrev`
`-`
`-            df['L'][i] = a * df['Revenue'][i] + (1.0 - a) * (LPrev + bPrev)`
`-            df['b'][i] = b * (df['L'][i] - LPrev) + (1 - b) * bPrev`
`-`
`-            # We skip the first two`
`-            if (i > 1):`
`-                # forecast for period i`
`-                df['DoubleExpo'][i] = df['L'][i] + df['b'][i]`
`-                #print df['DoubleExpo'][i]`
`-`
`-`
`-    # Track Errors`
`-    df['Error'] = df['Revenue'] - df['DoubleExpo']`
`-    df['MFE'] = (df['Error']).mean()`
`-    df['MAD'] = np.fabs(df['Error']).mean()`
`-    df['MSE'] = np.sqrt(np.square(df['Error']).mean())`
`-    df['TS'] = np.sum(df['Error'])/df['MAD']`
`-`
`-    return df`
`-`
`-`
`-# Data set`
`-y = [112,`
`-118,`
`-132,`
`-129,`
`-121,`
`-135,`
`-148,`
`-148,`
`-136,`
`-119,`
`-104,`
`-118,`
`-115,`
`-126,`
`-141,`
`-135,`
`-125,`
`-149,`
`-170,`
`-170,`
`-158,`
`-133,`
`-114,`
`-140,`
`-145,`
`-150,`
`-178,`
`-163,`
`-172,`
`-178,`
`-199,`
`-199,`
`-184,`
`-162,`
`-146,`
`-166,`
`-171,`
`-180,`
`-193,`
`-181,`
`-183,`
`-218,`
`-230,`
`-242,`
`-209,`
`-191,`
`-172,`
`-194,`
`-196,`
`-196,`
`-236,`
`-235,`
`-229,`
`-243,`
`-264,`
`-272,`
`-237,`
`-211,`
`-180,`
`-201,`
`-204,`
`-188,`
`-235,`
`-227,`
`-234,`
`-264,`
`-302,`
`-293,`
`-259,`
`-229,`
`-203,`
`-229,`
`-242,`
`-233,`
`-267,`
`-269,`
`-270,`
`-315,`
`-364,`
`-347,`
`-312,`
`-274,`
`-237,`
`-278,`
`-284,`
`-277,`
`-317,`
`-313,`
`-318,`
`-374,`
`-413,`
`-405,`
`-355,`
`-306,`
`-271,`
`-306`
`-]`
`-#[randint(0,100) for r in xrange(10)]`
`-print 'data: ',y`
`-`
`-`
`-result = DoubleExponentialSmoothing(y,len(y),0.5, 0.5)`
`-#print result`
`-print result.to_string() #if data is supressed`

# File misc/Double Expo smoothing(holt)_FINAL2.py

`-from scipy.optimize import minimize`
`-import numpy as np`
`-from pandas import *`
`-`
`-#----------------------------------------------------`
`-#-------- Create Function ------------`
`-#----------------------------------------------------`
`-def DoubleExponentialSmoothing(Input,N,Alpha,Beta,test=0):`
`-`
`-    # Create data frame`
`-    df = DataFrame(Input, columns=['Revenue'])`
`-`
`-    # Add Cummulative sums`
`-    df['CummSum'] = df['Revenue'].cumsum()`
`-`
`-    # Initial vallues for L and b`
`-    df['L'] = df['Revenue'][0]*1.0`
`-    df['b'] = 0.0`
`-`
`-    # Initial forecast = actual`
`-    # we multiply by 1.0 to prevent numbers getting rounded to int`
`-    df['DoubleExpo'] = df['Revenue'][0]*1.0`
`-`
`-    Num = N`
`-    a = Alpha`
`-    b = Beta`
`-`
`-    # Single Exponential Smoothing`
`-    for i in range(Num):`
`-`
`-        # We skip the first one since its a guess`
`-        if (i > 0):`
`-`
`-            # Get the previous L and b`
`-            LPrev = df['L'][i-1]`
`-            bPrev = df['b'][i-1]`
`-            #print LPrev, bPrev`
`-`
`-            df['L'][i] = a * df['Revenue'][i] + (1.0 - a) * (LPrev + bPrev)`
`-            df['b'][i] = b * (df['L'][i] - LPrev) + (1 - b) * bPrev`
`-`
`-            # We skip the first two`
`-            if (i > 1):`
`-                # forecast for period i`
`-                df['DoubleExpo'][i] = df['L'][i] + df['b'][i]`
`-                #print df['DoubleExpo'][i]`
`-`
`-`
`-    # Track Errors`
`-    df['Error'] = df['Revenue'] - df['DoubleExpo']`
`-    df['MFE'] = (df['Error']).mean()`
`-    df['MAD'] = np.fabs(df['Error']).mean()`
`-    df['MSE'] = np.sqrt(np.square(df['Error']).mean())`
`-    df['TS'] = np.sum(df['Error'])/df['MAD']`
`-`
`-    #print a,b, df.MAD[0]`
`-`
`-    if test == 0:`
`-        return df.MAD[0]`
`-    else: return df`
`-`
`-#----------------------------------------------------`
`-#-------- Input ------------`
`-#----------------------------------------------------`
`-data = [112,`
`-118,`
`-132,`
`-129,`
`-121,`
`-135,`
`-148,`
`-148,`
`-136,`
`-119,`
`-104,`
`-118,`
`-115,`
`-126,`
`-141,`
`-135,`
`-125,`
`-149,`
`-170,`
`-170,`
`-158,`
`-133,`
`-114,`
`-140,`
`-145,`
`-150,`
`-178,`
`-163,`
`-172,`
`-178,`
`-199,`
`-199,`
`-184,`
`-162,`
`-146,`
`-166,`
`-171,`
`-180,`
`-193,`
`-181,`
`-183,`
`-218,`
`-230,`
`-242,`
`-209,`
`-191,`
`-172,`
`-194,`
`-196,`
`-196,`
`-236,`
`-235,`
`-229,`
`-243,`
`-264,`
`-272,`
`-237,`
`-211,`
`-180,`
`-201,`
`-204,`
`-188,`
`-235,`
`-227,`
`-234,`
`-264,`
`-302,`
`-293,`
`-259,`
`-229,`
`-203,`
`-229,`
`-242,`
`-233,`
`-267,`
`-269,`
`-270,`
`-315,`
`-364,`
`-347,`
`-312,`
`-274,`
`-237,`
`-278,`
`-284,`
`-277,`
`-317,`
`-313,`
`-318,`
`-374,`
`-413,`
`-405,`
`-355,`
`-306,`
`-271,`
`-306`
`-]`
`-`
`-`
`-#----------------------------------------------------`
`-#-------- SOLVER ------------`
`-#----------------------------------------------------`
`-`
`-## Objective Function`
`-fun = lambda x: DoubleExponentialSmoothing(data,len(data),x[0],x[1])`
`-`
`-## Contraints`
`-cons = ({'type': 'ineq', 'fun': lambda x:  x[0] - 0.01}, # x - 0.1 >= 0`
`-        {'type': 'ineq', 'fun': lambda x:  0.99 - x[0]}, # 0.9-x>=0`
`-        {'type': 'ineq', 'fun': lambda x:  x[1] - 0.01}, # x - 0.1 >= 0`
`-        {'type': 'ineq', 'fun': lambda x:  0.99 - x[1]}) # 0.9-x>=0`
`-`
`-## Bounds (note sure what this is yet)`
`-bnds = (None,None)`
`-`
`-## Solver`
`-res = minimize(fun, (0.5,0.5), method='SLSQP', bounds=bnds, constraints=cons)`
`-`
`-##print res`
`-##print res.status`
`-##print res.success`
`-##print res.njev`
`-##print res.nfev`
`-##print res.fun`
`-##for i in res.x:`
`-##    print i`
`-##print res.message`
`-##for i in res.jac:`
`-##    print i`
`-##print res.nit`
`-`
`-# print final results`
`-result = DoubleExponentialSmoothing(data,len(data),res.x[0],res.x[1],1)`
`-#print result`
`-print result.to_string() #if data is supressed`
`-`

# File misc/Expo smoothing.py

`-'''`
`-simple exponential smoothing`
`-go back to last N values`
`-y_t = a * y_t + a * (1-a)^1 * y_t-1 + a * (1-a)^2 * y_t-2 + ... + a*(1-a)^n * y_t-n`
`-'''`
`-from random import random,randint`
`-from pandas import *`
`-`
`-def gen_weights(a,N):`
`-    ws = list()`
`-    for i in range(N):`
`-        w = a * ((1-a)**i)`
`-        ws.append(w)`
`-    return ws`
`-`
`-def weighted(data,ws):`
`-    wt = list()`
`-    for i,x in enumerate(data):`
`-        wt.append(x*ws[i])`
`-    return wt`
`-`
`-def SingleExponentialSmoothing(Input,N,Alpha):`
`-`
`-    Num = N`
`-    a = Alpha`
`-    ws = gen_weights(a,Num)`
`-    data = Input`
`-    weighted_data = weighted(data,ws)`
`-`
`-    print 'weights: ',ws`
`-    print 'weighted data: ',weighted_data`
`-    print 'weighted avg: ',sum(weighted_data)`
`-    return sum(weighted_data)`
`-`
`-y = [randint(0,100) for r in xrange(10)]`
`-print 'data: ',y`
`-`
`-`
`-SingleExponentialSmoothing(y,len(y),0.5)`
`-`
`-# Create data frame`
`-df = DataFrame(y, columns=['Revenue'])`
`-`
`-# Add columns`
`-df['CummSum'] = df['Revenue'].cumsum()`
`-df['SingleExpo'] = ewma(df['Revenue'], span=2)`
`-df['Error'] = df['Revenue'] - df['SingleExpo']`
`-df['MFE'] = (df['Error']).mean()`
`-df['MAD'] = np.fabs(df['Error']).mean()`
`-df['MSE'] = np.sqrt(np.square(df['Error']).mean())`
`-df['TS'] = np.sum(df['Error'])/df['MAD']`
`-`
`-#print df`
`-print df.to_string() #if data is supressed`
`-`
`-`

# File misc/Expo smoothing_FINAL.py

`-from pandas import *`
`-`
`-`
`-`
`-def SingleExponentialSmoothing(Input,N,Alpha):`
`-`
`-    # Create data frame`
`-    df = DataFrame(Input, columns=['Revenue'])`
`-`
`-    # Add Cummulative sums`
`-    df['CummSum'] = df['Revenue'].cumsum()`
`-`
`-    # Initial forecast = actual`
`-    # we multiply by 1.0 to prevent numbers getting rounded to int`
`-    df['SingleExpo'] = df['Revenue'][0]*1.0`
`-`
`-    Num = N`
`-    a = Alpha`
`-`
`-    # Single Exponential Smoothing`
`-    for i in range(Num):`
`-`
`-        # We skip the first one since its a guess`
`-        if (i > 0):`
`-`
`-            # Get the previous value and previous forecast`
`-            yPrev = df['Revenue'][i-1]`
`-            fPrev = df['SingleExpo'][i-1]`
`-            #print yPrev, fPrev`
`-`
`-            # forecast for period i`
`-            df['SingleExpo'][i] = a * yPrev + (1.0 - a) * fPrev`
`-            #print df['SingleExpo'][i]`
`-`
`-`
`-    # Track Errors`
`-    df['Error'] = df['Revenue'] - df['SingleExpo']`
`-    df['MFE'] = (df['Error']).mean()`
`-    df['MAD'] = np.fabs(df['Error']).mean()`
`-    df['MSE'] = np.sqrt(np.square(df['Error']).mean())`
`-    df['TS'] = np.sum(df['Error'])/df['MAD']`
`-`
`-    return df`
`-`
`-`
`-# Data set`
`-y = [200,215,210,220,230,220,235,215,220,210] `
`-#[randint(0,100) for r in xrange(10)]`
`-print 'data: ',y`
`-`
`-`
`-result = SingleExponentialSmoothing(y,len(y),0.70)`
`-#print result`
`-print result.to_string() #if data is supressed`

# File misc/Expo smoothing_FINAL2.py

`-from scipy.optimize import minimize`
`-import numpy as np`
`-from pandas import *`
`-`
`-#----------------------------------------------------`
`-#-------- Create Function ------------`
`-#----------------------------------------------------`
`-def SingleExponentialSmoothing(Input,N,Alpha,test=0):`
`-`
`-    # Create data frame`
`-    df = DataFrame(Input, columns=['Revenue'])`
`-`
`-    # Add Cummulative sums`
`-    df['CummSum'] = df['Revenue'].cumsum()`
`-`
`-    # Initial forecast = actual`
`-    # we multiply by 1.0 to prevent numbers getting rounded to int`
`-    df['SingleExpo'] = df['Revenue'][0]*1.0`
`-`
`-    Num = N`
`-    a = Alpha`
`-`
`-    # Single Exponential Smoothing`
`-    for i in range(Num):`
`-`
`-        # We skip the first one since its a guess`
`-        if (i > 0):`
`-`
`-            # Get the previous value and previous forecast`
`-            yPrev = df['Revenue'][i-1]`
`-            fPrev = df['SingleExpo'][i-1]`
`-            #print yPrev, fPrev`
`-`
`-            # forecast for period i`
`-            df['SingleExpo'][i] = a * yPrev + (1.0 - a) * fPrev`
`-            #print df['SingleExpo'][i]`
`-`
`-`
`-    # Track Errors`
`-    df['Error'] = df['Revenue'] - df['SingleExpo']`
`-    df['MFE'] = (df['Error']).mean()`
`-    df['MAD'] = np.fabs(df['Error']).mean()`
`-    df['MSE'] = np.sqrt(np.square(df['Error']).mean())`
`-    df['TS'] = np.sum(df['Error'])/df['MAD']`
`-`
`-    #print Alpha, df.MAD[0]`
`-`
`-    if test == 0:`
`-        return df.MAD[0]`
`-    else: return df`
`-`
`-#----------------------------------------------------`
`-#-------- Input ------------`
`-#----------------------------------------------------`
`-data = [1,2,3,4,5,5,5,5,5,5,5,5,5,5,5]`
`-`
`-`
`-#----------------------------------------------------`
`-#-------- SOLVER ------------`
`-#----------------------------------------------------`
`-`
`-## Objective Function`
`-fun = lambda x: SingleExponentialSmoothing(data,len(data),x[0])`
`-`
`-## Contraints`
`-cons = ({'type': 'ineq', 'fun': lambda x:  x[0] - 0.100}, # x - 0.1 >= 0`
`-        {'type': 'ineq', 'fun': lambda x:  0.900 - x[0]}) # 0.9-x>=0`
`-`
`-`
`-## Bounds (note sure what this is yet)`
`-bnds = (None,None)`
`-`
`-## Solver`
`-res = minimize(fun, 0.500, method='SLSQP', bounds=bnds, constraints=cons)`
`-`
`-##print res`
`-##print res.status`
`-##print res.success`
`-##print res.njev`
`-##print res.nfev`
`-##print res.fun`
`-##for i in res.x:`
`-##    print i`
`-##print res.message`
`-##for i in res.jac:`
`-##    print i`
`-##print res.nit`
`-`
`-# print final results`
`-result = SingleExponentialSmoothing(data,len(data),res.x,1)`
`-print result`
`-`

# File misc/Flow Chart.xlsx

Binary file removed.

# File misc/Linear Regression.py

`-from pandas import *`
`-import numpy as np`
`-from statsmodels import *`
`-randn = np.random.randn`
`-`
`-`
`-`
`-ts = Series(randn(20), index=date_range('1/1/2000', periods=20))`
`-print ts`
`-df = DataFrame(randn(20, 2), index=ts.index,`
`-                columns=['A', 'B'])`
`-print df`
`-`
`-model = ols(y=df.index, x=df['A'])`
`-`
`-print model.predict()`
`-`
`-`

# File misc/Moving Average.py

`-import numpy as np`
`-from pandas import *`
`-`
`-`
`-`
`-## Moving Average`
`-def MovingAverage(Input,N):`
`-    WINDOW = N`
`-    weightings = np.repeat(1.0, WINDOW) / WINDOW`
`-    return np.convolve(Input, weightings)[WINDOW-1:-(WINDOW-1)]`
`-    `
`-`
`-data = [1,2,3,4,5,5,5,5,5,5,5,5,5,5,5]`
`-#print MovingAverage(data,2)`
`-`
`-`
`-# Create data frame`
`-df = DataFrame(data, columns=['Revenue'])`
`-`
`-# Add columns`
`-df['CummSum'] = df['Revenue'].cumsum()`
`-df['Mavg'] = rolling_mean(df['Revenue'], 2)`
`-df['Error'] = df['Revenue'] - df['Mavg']`
`-df['MFE'] = (df['Error']).mean()`
`-df['MAD'] = np.fabs(df['Error']).mean()`
`-df['MSE'] = np.sqrt(np.square(df['Error']).mean())`
`-df['TS'] = np.sum(df['Error'])/df['MAD']`
`-`
`-print df`
`-#print df.to_string() #if data is supressed`
`-`
`-`

# File misc/Moving Average_FINAL.py

`-from pandas import *`
`-`
`-`
`-## Moving Average`
`-def MovingAverage(Input,N):`
`-`
`-    # Create data frame`
`-    df = DataFrame(Input, columns=['Revenue'])`
`-`
`-    # Add columns`
`-    df['CummSum'] = df['Revenue'].cumsum()`
`-    df['Mavg'] = rolling_mean(df['Revenue'], N)`
`-    df['Error'] = df['Revenue'] - df['Mavg']`
`-    df['MFE'] = (df['Error']).mean()`
`-    df['MAD'] = np.fabs(df['Error']).mean()`
`-    df['MSE'] = np.sqrt(np.square(df['Error']).mean())`
`-    df['TS'] = np.sum(df['Error'])/df['MAD']`
`-`
`-    return df`
`-    `
`-    `
`-# data set`
`-data = [1,2,3,4,5,5,5,5,5,5,5,5,5,5,5]`
`-result = MovingAverage(data,2)`
`-`
`-print result`
`-#print result.to_string() #if data is supressed`
`-`
`-`

# File misc/Moving Average_FINAL2.py

`-from pandas import *`
`-`
`-#----------------------------------------------------`
`-#-------- Create Function ------------`
`-#----------------------------------------------------`
`-`
`-## Moving Average`
`-def MovingAverage(Input,N,test=0):`
`-`
`-    # Create data frame`
`-    df = DataFrame(Input, columns=['Revenue'])`
`-`
`-    # Add columns`
`-    df['CummSum'] = df['Revenue'].cumsum()`
`-    df['Mavg'] = rolling_mean(df['Revenue'], N)`
`-    df['Error'] = df['Revenue'] - df['Mavg']`
`-    df['MFE'] = (df['Error']).mean()`
`-    df['MAD'] = np.fabs(df['Error']).mean()`
`-    df['MSE'] = np.sqrt(np.square(df['Error']).mean())`
`-    df['TS'] = np.sum(df['Error'])/df['MAD']`
`-`
`-    if test == 0:`
`-        return df.MAD[0]`
`-    else: return df`
`-    `
`-#----------------------------------------------------`
`-#-------- Input ------------`
`-#----------------------------------------------------`
`-    `
`-# data set`
`-data = [1,2,3,4,5,5,5,5,5,5,5,5,5,5,5]`
`-`
`-`
`-#----------------------------------------------------`
`-#-------- Ghetto Solver ------------`
`-#----------------------------------------------------`
`-`
`-# Calculate the maximum number of iterations allowed (max = 10)`
`-MaxIter = [10, len(data)/2]`
`-#print MaxIter, min(MaxIter)`
`-`
`-# Create array to hold N, f(N)`
`-sol = []`
`-`
`-# populate array`
`-for i in range(min(MaxIter)):`
`-    if i>1:`
`-        sol.append([i,MovingAverage(data,i)])`
`-`
`-#print sol`
`-# Sort array on the f(N) column, ascending`
`-# this will be the optimal solution`
`-sol = sorted(sol, key=lambda x: x[1])`
`-`
`-# Optimal solution = sol[0][0] = 1st value in 1st array   `
`-result = MovingAverage(data,sol[0][0],1)`
`-`
`-#print result`
`-print result.to_string() #if data is supressed`

# File misc/Python Example.py

`-from sqlalchemy import *`
`-from datetime import datetime, date, time`
`-from pandas import *`
`-from scipy.optimize import minimize`
`-import numpy as np`
`-from xlwt import *`
`-import matplotlib.pyplot as plt`
`-from PIL import Image`
`-import os`
`-`
`-#----------------------------------------------------`
`-#-------- SQL Section ------------`
`-#----------------------------------------------------`
`-`
`-# Parameters`
`-ServerName = "devdb4\sql4"`
`-Database = "BizIntel"`
`-StartDate = date(2011, 1, 1)`
`-EndDate = date(2012, 10, 31)`
`-`
`-# Make sure pyobdc is installed`
`-engine = create_engine('mssql+pyodbc://' + ServerName + '/' + Database)`
`-conn = engine.connect()`
`-`
`-# Required for querying tables`
`-metadata = MetaData(conn)`
`-`
`-# Tables to query`
`-TableName = "RPT_GA_AccountingSummary"`
`-`
`-# Table to query`
`-tbl = Table(TableName, metadata, autoload=True, schema="dbo")`
`-`
`-# select Market, BillDate, Revenue WHERE BillDate>= StartDate and BillDate < EndDate`
`-sql = select([literal("GA").label("Market"),tbl.c.BillDate,tbl.c.Revenue], and_(tbl.c.BillDate >= StartDate,tbl.c.BillDate < EndDate))`
`-result = conn.execute(sql)`
`-`
`-dataDF = DataFrame(data=list(result))#result.fetchall())`
`-dataDF.columns = result.keys()`
`-dataDF = dataDF.set_index('BillDate', drop=True)`
`-dataDF['Revenue'] = -1*dataDF['Revenue'].astype(np.float64)`
`-#dataDF = dataDF.ix[:10]`
`-#print dataDF.head()`
`-`
`-Monthly = dataDF.resample('MS', how={'Market': 'first', 'Revenue': 'sum'}, closed='left', label='left')`
`-#print Monthly.head()`
`-`
`-# Close connection`
`-conn.close()`
`-`
`-`
`-#----------------------------------------------------`
`-#-------- Create Functions ------------`
`-#----------------------------------------------------`
`-`
`-def MovingAverage(Input,N,test=0):`
`-`
`-    # Create data frame`
`-    df = DataFrame(Input, columns=['Revenue'])`
`-`
`-    # Add columns`
`-    df['CummSum'] = df['Revenue'].cumsum()`
`-    df['Mavg'] = rolling_mean(df['Revenue'], N)`
`-    df['MaError'] = df['Revenue'] - df['Mavg']`
`-    df['MaMFE'] = (df['MaError']).mean()`
`-    df['MaMAD'] = np.fabs(df['MaError']).mean()`
`-    df['MaMSE'] = np.sqrt(np.square(df['MaError']).mean())`
`-    df['MaTS'] = np.sum(df['MaError'])/df['MaMAD']`
`-`
`-    if test == 0:`
`-        return df.MaMAD[0]`
`-    else: return df`
`-`
`-`
`-def SingleExponentialSmoothing(Input,N,Alpha,test=0):`
`-`
`-    # Create data frame`
`-    df = DataFrame(Input, columns=['Revenue'])`
`-`
`-    # Add Cummulative sums`
`-    df['CummSum'] = df['Revenue'].cumsum()`
`-`
`-    # Initial forecast = actual`
`-    # we multiply by 1.0 to prevent numbers getting rounded to int`
`-    df['SingleExpo'] = df['Revenue'][0]*1.0`
`-`
`-    Num = N`
`-    a = Alpha`
`-`
`-    # Single Exponential Smoothing`
`-    for i in range(Num):`
`-`
`-        # We skip the first one since its a guess`
`-        if (i > 0):`
`-`
`-            # Get the previous value and previous forecast`
`-            yPrev = df['Revenue'][i-1]`
`-            fPrev = df['SingleExpo'][i-1]`
`-            #print yPrev, fPrev`
`-`
`-            # forecast for period i`
`-            df['SingleExpo'][i] = a * yPrev + (1.0 - a) * fPrev`
`-            #print df['SingleExpo'][i]`
`-`
`-`
`-    # Track Errors`
`-    df['UnoError'] = df['Revenue'] - df['SingleExpo']`
`-    df['UnoMFE'] = (df['UnoError']).mean()`
`-    df['UnoMAD'] = np.fabs(df['UnoError']).mean()`
`-    df['UnoMSE'] = np.sqrt(np.square(df['UnoError']).mean())`
`-    df['UnoTS'] = np.sum(df['UnoError'])/df['UnoMAD']`
`-`
`-    #print Alpha, df.UnoMAD[0]`
`-`
`-    if test == 0:`
`-        return df.UnoMAD[0]`
`-    else: return df`
`-`
`-`
`-def DoubleExponentialSmoothing(Input,N,Alpha,Beta,test=0):`
`-`
`-    # Create data frame`
`-    df = DataFrame(Input, columns=['Revenue'])`
`-`
`-    # Add Cummulative sums`
`-    df['CummSum'] = df['Revenue'].cumsum()`
`-`
`-    # Initial vallues for L and b`
`-    df['L'] = df['Revenue'][0]*1.0`
`-    df['b'] = 0.0`
`-`
`-    # Initial forecast = actual`
`-    # we multiply by 1.0 to prevent numbers getting rounded to int`
`-    df['DoubleExpo'] = df['Revenue'][0]*1.0`
`-`
`-    Num = N`
`-    a = Alpha`
`-    b = Beta`
`-`
`-    # Double Exponential Smoothing`
`-    for i in range(Num):`
`-`
`-        # We skip the first one since its a guess`
`-        if (i > 0):`
`-`
`-            # Get the previous L and b`
`-            LPrev = df['L'][i-1]`
`-            bPrev = df['b'][i-1]`
`-            #print LPrev, bPrev`
`-`
`-            df['L'][i] = a * df['Revenue'][i] + (1.0 - a) * (LPrev + bPrev)`
`-            df['b'][i] = b * (df['L'][i] - LPrev) + (1 - b) * bPrev`
`-`
`-            # We skip the first two`
`-            if (i > 1):`
`-                # forecast for period i`
`-                df['DoubleExpo'][i] = df['L'][i] + df['b'][i]`
`-                #print df['DoubleExpo'][i]`
`-`
`-`
`-    # Track Errors`
`-    df['DosError'] = df['Revenue'] - df['DoubleExpo']`
`-    df['DosMFE'] = (df['DosError']).mean()`
`-    df['DosMAD'] = np.fabs(df['DosError']).mean()`
`-    df['DosMSE'] = np.sqrt(np.square(df['DosError']).mean())`
`-    df['DosTS'] = np.sum(df['DosError'])/df['DosMAD']`
`-`
`-    #print a,b, df.DosMAD[0]`
`-`
`-    if test == 0:`
`-        return df.DosMAD[0]`
`-    else: return df`
`-`
`-def TripleExponentialSmoothing(Input,N,Alpha,Beta,Gamma,Seasons,test=0):`
`-`
`-    # NOTE: Any multiplication done by 1.0 is to prevent numbers from getting rounded to int`
`-    Num = N`
`-    a = Alpha`
`-    b = Beta`
`-    c = Gamma`
`-    s = Seasons`
`-`
`-    # Create data frame`
`-    df = DataFrame(Input, columns=['Revenue'])`
`-`
`-    # Add Cummulative sums`
`-    df['CummSum'] = df['Revenue'].cumsum()`
`-`
`-    # Initial values for L`
`-    '''  L = average of the first period (s)`
`-           = (x1+x2+x3+...+xs) / s`
`-`
`-             **start calculating L at time s`
`-    '''`
`-    df['L'] = df['Revenue'][:s].mean()`
`-    df['L'][:s-1] = None #erase all values before period s`
`-`
`-    # Initial values for b`
`-    '''     b = (current value - first value) / ( period(s) - 1 )`
`-`
`-             **start calculating b at time s`
`-    '''`
`-    df['b'] = 1.0*(df['Revenue'][s-1] - df['Revenue'][0]) / (s - 1)`
`-    df['b'][:s-1] = None #erase all values before period s`
`-   `
`-    # Add initial seasonality for period s`
`-    '''     s = (current value) / (average of the first period(s) )`
`-`
`-             **only calculate for the first period s`
`-    '''`
`-    df['s'] = 1.0*df['Revenue'][:s-1]/df['Revenue'][:s].mean()`
`-`
`-    # Initial value at time s-1`
`-    # this is exactly the row after the previous "df['s'] =" statement `
`-    df['s'][s-1] = 1.0*df['Revenue'][s-1]/df['Revenue'][:s].mean()`
`-    `
`-    # Initial forecast = actual`
`-    '''     It does not matter what number you set the initial forecast,`
`-            we only do this to create the column`
`-    '''`
`-    df['TripleExpo'] = df['Revenue'][0]*1.0`
`-    df['TripleExpo'][:s] = None #erase all values before and including period s`
`-`
`-`
`-    # Triple Exponential Smoothing`
`-    for i in range(Num):`
`-`
`-        # We start at the end of period s`
`-        if (i >= s):`
`-`
`-            # Get the previous L and b`
`-            LPrev = df['L'][i-1]`
`-            bPrev = df['b'][i-1]`
`-            #print LPrev, bPrev`
`-`
`-            '''`
`-                Eq1. L1 = alpha * (y1 /	S0) + (1 - alpha) * (L0 + b0)`
`-                Eq2. b1 = beta * (L1 - L0) + (1 - beta) * b0`
`-                Eq3. S1 = Gamma * (y1 / L1) + (1 - Gamma) * S0`
`-                Eq4. F(1+m) = L1 + (b1 * S0)`
`-            '''`
`-            df['L'][i] = a * (df['Revenue'][i] / df['s'][i-s]) + (1 - a) * (LPrev + bPrev)`
`-            df['b'][i] = b * (df['L'][i] - LPrev) + (1 - b) * bPrev`
`-            df['s'][i] = c * (df['Revenue'][i] / df['L'][i]) + (1 - c) * df['s'][i-s]`
`-            #print  df['L'][i], df['b'][i], df['s'][i]`
`-`
`-            # forecast for period i`
`-            df['TripleExpo'][i] = (df['L'][i-1] + df['b'][i-1]) * df['s'][i-s]`
`-            #print df['TripleExpo'][i]`
`-`
`-`
`-    # Track Errors`
`-    df['TresError'] = df['Revenue'] - df['TripleExpo']`
`-    df['TresMFE'] = (df['TresError']).mean()`
`-    df['TresMAD'] = np.fabs(df['TresError']).mean()`
`-    df['TresMSE'] = np.sqrt(np.square(df['TresError']).mean())`
`-    df['TresTS'] = np.sum(df['TresError'])/df['TresMAD']`
`-`
`-    #print a,b, df.TresMAD[0]`
`-`
`-    if test == 0:`
`-        return df.TresMAD[0]`
`-    else: return df`
`-`
`-    `
`-#----------------------------------------------------`
`-#-------- Input ------------`
`-#----------------------------------------------------`
`-    `
`-# data set`
`-data = []`
`-data = list(Monthly['Revenue'])`
`-#print data`
`-`
`-`
`-#----------------------------------------------------`
`-#-------- Ghetto Solver ------------`
`-#----------------------------------------------------`
`-`
`-# Calculate the maximum number of iterations allowed (max = 10)`
`-MaxIter = [10, len(data)/2]`
`-#print MaxIter, min(MaxIter)`
`-`
`-# Create array to hold N, f(N)`
`-sol = []`
`-`
`-# populate array`
`-for i in range(min(MaxIter)):`
`-    if i>1:`
`-        sol.append([i,MovingAverage(data,i)])`
`-`
`-#print sol`
`-# Sort array on the f(N) column, ascending`
`-# this will be the optimal solution`
`-sol = sorted(sol, key=lambda x: x[1])`
`-`
`-# Optimal solution = sol[0][0] = 1st value in 1st array   `
`-MAresult = MovingAverage(data,sol[0][0],1)`
`-`
`-#----------------------------------------------------`
`-#-------- Single Exponential Solver ------------`
`-#----------------------------------------------------`
`-`
`-## Objective Function`
`-Singlefun = lambda x: SingleExponentialSmoothing(data,len(data),x[0])`
`-`
`-## Contraints`
`-Singlecons = ({'type': 'ineq', 'fun': lambda x:  x[0] - 0.100}, # x - 0.1 >= 0`
`-        {'type': 'ineq', 'fun': lambda x:  0.900 - x[0]}) # 0.9-x>=0`
`-`
`-`
`-## Bounds (note sure what this is yet)`
`-Singlebnds = (None,None)`
`-`
`-## Solver`
`-Singleres = minimize(Singlefun, 0.500, method='SLSQP', bounds=Singlebnds, constraints=Singlecons)`
`-`
`-##print res`
`-##print res.status`
`-##print res.success`
`-##print res.njev`
`-##print res.nfev`
`-##print res.fun`
`-##for i in res.x:`
`-##    print i`
`-##print res.message`
`-##for i in res.jac:`
`-##    print i`
`-##print res.nit`
`-`
`-# print final results`
`-Singleresult = SingleExponentialSmoothing(data,len(data),Singleres.x,1)`
`-`
`-#----------------------------------------------------`
`-#-------- Double Exponential Solver ------------`
`-#----------------------------------------------------`
`-`
`-## Objective Function`
`-Doublefun = lambda x: DoubleExponentialSmoothing(data,len(data),x[0],x[1])`
`-`
`-## Contraints`
`-Doublecons = ({'type': 'ineq', 'fun': lambda x:  x[0] - 0.01}, # x - 0.1 >= 0`
`-        {'type': 'ineq', 'fun': lambda x:  0.99 - x[0]}, # 0.9-x>=0`
`-        {'type': 'ineq', 'fun': lambda x:  x[1] - 0.01}, # x - 0.1 >= 0`
`-        {'type': 'ineq', 'fun': lambda x:  0.99 - x[1]}) # 0.9-x>=0`
`-`
`-## Bounds (note sure what this is yet)`
`-Doublebnds = (None,None)`
`-`
`-## Solver`
`-Doubleres = minimize(Doublefun, (0.5,0.5), method='SLSQP', bounds=Doublebnds, constraints=Doublecons)`
`-`
`-# print final results`
`-Doubleresult = DoubleExponentialSmoothing(data,len(data),Doubleres.x[0],Doubleres.x[1],1)`
`-`
`-`
`-`
`-#----------------------------------------------------`
`-#-------- Triple Exponential Solver ------------`
`-#----------------------------------------------------`
`-`
`-## Objective Function`
`-Triplefun = lambda x: TripleExponentialSmoothing(data,len(data),x[0],x[1],x[2],12)`
`-`
`-## Contraints`
`-Triplecons = ({'type': 'ineq', 'fun': lambda x:  x[0] - 0.01}, # x - 0.1 >= 0`
`-        {'type': 'ineq', 'fun': lambda x:  0.99 - x[0]}, # 0.9-x>=0`
`-        {'type': 'ineq', 'fun': lambda x:  x[1] - 0.01}, # x - 0.1 >= 0`
`-        {'type': 'ineq', 'fun': lambda x:  0.99 - x[1]}, # 0.9-x>=0`
`-        {'type': 'ineq', 'fun': lambda x:  x[2] - 0.01}, # x - 0.1 >= 0`
`-        {'type': 'ineq', 'fun': lambda x:  0.99 - x[2]}) # 0.9-x>=0`
`-`
`-## Bounds (note sure what this is yet)`
`-Triplebnds = (None,None)`
`-`
`-## Solver`
`-Tripleres = minimize(Triplefun, (0.5,0.5,0.5), method='SLSQP', bounds=Triplebnds, constraints=Triplecons)`
`-`
`-# print final results`
`-Tripleresult = TripleExponentialSmoothing(data,len(data),Tripleres.x[0],Tripleres.x[1],Tripleres.x[2],12,1)`
`-`
`-`
`-#----------------------------------------------------`
`-#-------- Merge Results ------------`
`-#----------------------------------------------------`
`-`
`-pieces = [MAresult,`
`-          Singleresult[['SingleExpo', 'UnoError', 'UnoMFE', 'UnoMAD', 'UnoMSE', 'UnoTS']],`
`-          Doubleresult[['L', 'b', 'DoubleExpo', 'DosError', 'DosMFE', 'DosMAD', 'DosMSE', 'DosTS']],`
`-          Tripleresult[['L', 'b', 's', 'TripleExpo', 'TresError', 'TresMFE', 'TresMAD', 'TresMSE', 'TresTS']]]`
`-bigdata = concat(pieces,axis=1)`
`-`
`-bigdata = bigdata.set_index(Monthly.index)`
`-`
`-#print bigdata`
`-#print bigdata.to_string() #if data is supressed`
`-`
`-#----------------------------------------------------`
`-#-------- Create Plots ------------`
`-#----------------------------------------------------`
`-`
`-# Convert png to bmp`
`-def ConvertImg(file_in,file_out):`
`-    img = Image.open(file_in)`
`-`
`-    #print len(img.split())  # test`
`-    if len(img.split()) == 4:`
`-        # prevent IOError: cannot write mode RGBA as BMP`
`-        r, g, b, a = img.split()`
`-        img = Image.merge("RGB", (r, g, b))`
`-        img.save(file_out)`
`-    else:`
`-        img.save(file_out)`
`-`
`-plot1 = bigdata[['Revenue','Mavg']].plot()`
`-plt.savefig('MApng.png')`
`-ConvertImg('MApng.png', 'MA.bmp')`
`-`
`-plot2 = bigdata[['Revenue','SingleExpo']].plot()`
`-plt.savefig('Singlepng.png')`
`-ConvertImg('Singlepng.png', 'Single.bmp')`
`-`
`-plot3 = bigdata[['Revenue','DoubleExpo']].plot()`
`-plt.savefig('Doublepng.png')`
`-ConvertImg('Doublepng.png', 'Double.bmp')`
`-`
`-plot3 = bigdata[['Revenue','TripleExpo']].plot()`
`-plt.savefig('Triplepng.png')`
`-ConvertImg('Triplepng.png', 'Triple.bmp')`
`-`
`-# Delete temp files`
`-os.remove('MApng.png')`
`-os.remove('Singlepng.png')`
`-os.remove('Doublepng.png')`
`-os.remove('Triplepng.png')`
`-`
`-#----------------------------------------------------`
`-#-------- Excel Output ------------`
`-#----------------------------------------------------`
`-`
`-# Get today's date`
`-CurrentTime = datetime.now()`
`-CurrentTime = CurrentTime.strftime("%Y-%m-%d_%H-%M-%S")`
`-`
`-# Create a workbook`
`-wb = Workbook()`
`-`
`-# Add a sheet/tab`
`-ws = wb.add_sheet('Forecast Summary')`
`-ws0 = wb.add_sheet('MovingAverage')`
`-ws1 = wb.add_sheet('Single')`
`-ws2 = wb.add_sheet('Double')`
`-ws3 = wb.add_sheet('Triple')`
`-`
`-# Formatting`
`-bold_xf = easyxf('font: bold on')`
`-num_xf = easyxf(num_format_str='\$#,##0.00') # sets currency format in Excel`
`-`
`-# Write text to cell`
`-for i, col in enumerate(bigdata[['Mavg','SingleExpo','DoubleExpo','TripleExpo']].columns):`
`-    #print i, col`
`-    ws.write(0, i, col, bold_xf)`
`-`
`-forecasts = bigdata[['Mavg','SingleExpo','DoubleExpo','TripleExpo']].ix[-1].T.iteritems()`
`-for i, (a,b) in enumerate(forecasts):`
`-    #print i, a,b`
`-    ws.write(1, i, b, num_xf)`
`-`
`-`
`-# Add picture at location (2,1)`
`-# Note: Only accepts bmp files`
`-# i.e. ws0.insert_bitmap('C:\Users\username\Desktop/test.bmp', 2, 1)`
`-ws0.insert_bitmap('MA.bmp', 2, 1)`
`-ws1.insert_bitmap('Single.bmp', 2, 1)`
`-ws2.insert_bitmap('Double.bmp', 2, 1)`
`-ws3.insert_bitmap('Triple.bmp', 2, 1)`
`-`
`-# Write excel file`
`-# Note: This will overwrite any other files with the same name`
`-wb.save('hello.xls')`
`-`
`-# Delete temp files`
`-os.remove('MA.bmp')`
`-os.remove('Single.bmp')`
`-os.remove('Double.bmp')`
`-os.remove('Triple.bmp')`

# File misc/Python Example.xls

Binary file removed.

# File misc/Python vs SQL.docx

Binary file removed.

# File misc/Python_101_20121210.ipynb

`-{`
`- "metadata": {`
`-  "name": "Python_101_20121210"`
`- },`
`- "nbformat": 3,`
`- "nbformat_minor": 0,`
`- "worksheets": [`
`-  {`
`-   "cells": [`
`-    {`
`-     "cell_type": "heading",`
`-     "level": 1,`
`-     "metadata": {},`
`-     "source": [`
`-      "Python 101"`
`-     ]`
`-    },`
`-    {`
`-     "cell_type": "heading",`
`-     "level": 3,`
`-     "metadata": {},`
`-     "source": [`
`-      "Importing Packages"`
`-     ]`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "from numpy import *\n",`
`-      "from pandas import *\n",`
`-      "\n",`
`-      "#using import * brings ALL the packages classes and functions into the name space,\n",`
`-      "#for large packages you can bring in only some parts by doing from [name] import [class/object]\n",`
`-      "#to avoid name space conflicts you can also rename what you import\n",`
`-      "\n",`
`-      "#import pandas and rename it\n",`
`-      "import pandas as pd\n",`
`-      "#import the Series and dataframe classes\n",`
`-      "from pandas import Series, DataFrame\n",`
`-      "\n",`
`-      "#common naming conventions. numpy is np, pandas pd, and matplotlib is plt\n",`
`-      "import numpy as np\n",`
`-      "import pandas as pd\n",`
`-      "import matplotlib.pyplot as plt"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "heading",`
`-     "level": 3,`
`-     "metadata": {},`
`-     "source": [`
`-      "Declaring Variables"`
`-     ]`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "#no data types need to be associated with variables\n",`
`-      "#all variables are \"Objects\" in python. \n",`
`-      "\n",`
`-      "num = 5\n",`
`-      "alpha = 'hello world'\n",`
`-      "mixed = str(num) + ' ' + alpha\n",`
`-      "\n",`
`-      "print num\n",`
`-      "print alpha\n",`
`-      "print mixed"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "heading",`
`-     "level": 2,`
`-     "metadata": {},`
`-     "source": [`
`-      "Lists"`
`-     ]`
`-    },`
`-    {`
`-     "cell_type": "heading",`
`-     "level": 3,`
`-     "metadata": {},`
`-     "source": [`
`-      "Creating"`
`-     ]`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "#this is a list\n",`
`-      "a=[0,1,2,3,4,5,6,7,8,9]\n",`
`-      "a"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "heading",`
`-     "level": 3,`
`-     "metadata": {},`
`-     "source": [`
`-      "Slice and Dice"`
`-     ]`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "#use [x] to access the item at location x in the list\n",`
`-      "#all lists start at 0\n",`
`-      "print 'first item', a[0]\n",`
`-      "\n",`
`-      "#you can also index from back by using -1 for last, -2 for \"second from last\" etc\n",`
`-      "print 'last item', a[-1]\n",`
`-      "\n",`
`-      "#you can \"slice\" a list using : and ::\n",`
`-      "print 'first three items', a[:3]\n",`
`-      "print 'last three items', a[-3:]\n",`
`-      "print 'start at the 4th item', a[3:]\n",`
`-      "print 'the odd items', a[::2]"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "#all lists have a length, use len(list_name) to get the number of items in it\n",`
`-      "#mathematical functions can also, in general, be applied to lists if they contain numbers\n",`
`-      "print 'length of list', len(a)\n",`
`-      "print 'largets number in list', max(a)\n",`
`-      "print 'smallest number', min(a)\n",`
`-      "print 'average', mean(a)\n",`
`-      "\n",`
`-      "#we can find the index of the max and min using argmax() and argmin()\n",`
`-      "print 'the largest number in the list is', max(a), 'and is found at index:', argmax(a)"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "heading",`
`-     "level": 3,`
`-     "metadata": {},`
`-     "source": [`
`-      "Add Items"`
`-     ]`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "#items can be added to a list by using list_name.append(item)\n",`
`-      "\n",`
`-      "#add 3 to the list\n",`
`-      "a.append(3)\n",`
`-      "#add 4\n",`
`-      "a.append(4)\n",`
`-      "a"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "#finally, we can de dupe a list by using the unique function\n",`
`-      "#we can also sort a list using sort(list_name)\n",`
`-      "\n",`
`-      "print 'sorted list', sort(a)\n",`
`-      "print 'select distinct values', unique(a)"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "heading",`
`-     "level": 3,`
`-     "metadata": {},`
`-     "source": [`
`-      "Remove Items"`
`-     ]`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "#items can be removed from the list using list_name.remove(item)\n",`
`-      "\n",`
`-      "#remove 3 from the list\n",`
`-      "a.remove(3)\n",`
`-      "#remove 4\n",`
`-      "a.remove(4)\n",`
`-      "a"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "heading",`
`-     "level": 3,`
`-     "metadata": {},`
`-     "source": [`
`-      "Itterators"`
`-     ]`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "#we can itterate over n items using a for loop\n",`
`-      "\n",`
`-      "#a shortcut for making the list [0,...,n-1] is the function range(n)\n",`
`-      "\n",`
`-      "#print the numbers 0 -4\n",`
`-      "for i in range(5):\n",`
`-      "    print i"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "#print the numbers 0 -4\n",`
`-      "for i in range(0,5):\n",`
`-      "    print i"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "#print the a list\n",`
`-      "for i in a:\n",`
`-      "    print i"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "#itterating over something and appending is a common way of building lists\n",`
`-      "\n",`
`-      "#create array\n",`
`-      "output=[]\n",`
`-      "\n",`
`-      "#build the list holding the first 4 squares by using a for loop\n",`
`-      "for i in range(5):\n",`
`-      "    output.append(i**2) #**2 operator means squared\n",`
`-      "output"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "#this works but is slow, a faster way to do this is to use list comprehension\n",`
`-      "\n",`
`-      "output2=[i**2 for i in range(5)]\n",`
`-      "output2"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "#we can also put conditions in the list comprehension\n",`
`-      "\n",`
`-      "#build the first 10 squares for all the even numbers\n",`
`-      "output3=[i**2 for i in range(10) if i%2==0] # % is means modulus (remainder)\n",`
`-      "output3"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "#the zip command lines up two lists together\n",`
`-      "\n",`
`-      "L1=[1,2,3]\n",`
`-      "L2=['x','y','z']\n",`
`-      "\n",`
`-      "#the output is a list of tuples\n",`
`-      "print zip(L1,L2)"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "#if they are of different size, it gets chopped off\n",`
`-      "\n",`
`-      "L1=[1,2,3,4]\n",`
`-      "L2=['x','y','z']\n",`
`-      "\n",`
`-      "#the output is a list of tuples\n",`
`-      "print zip(L1,L2)"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "#it is very common to itterate over lists using zip\n",`
`-      "\n",`
`-      "for list1,list2 in zip(L1,L2):\n",`
`-      "    print list1,list2"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "#this can also be done with list comprehension\n",`
`-      "print [(x,y) for x,y in zip(L1,L2)]\n",`
`-      "\n",`
`-      "#we can also make more complex lists\n",`
`-      "output=[(x,y,str(x)+y) for x,y in zip(L1,L2)]\n",`
`-      "\n",`
`-      "#itterate over our output for a nicer looking print statement\n",`
`-      "for z in output:\n",`
`-      "    print z\n",`
`-      "\n",`
`-      "#we can also do this differently\n",`
`-      "for a1,a2,a3 in output:\n",`
`-      "    print a1,a2,a3"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "heading",`
`-     "level": 3,`
`-     "metadata": {},`
`-     "source": [`
`-      "IF ELSE Statements"`
`-     ]`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "if 1==1:\n",`
`-      "    print 'one equals one'"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "if 1<0:\n",`
`-      "    print 'one is less than zero'\n",`
`-      "else:\n",`
`-      "    print '1 does not equal to 0'"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "if 1<>1: #you can also use the != operator\n",`
`-      "    print 'one does not equal to one '\n",`
`-      "elif 1==0:\n",`
`-      "    print '1 is equal to zero'\n",`
`-      "else:\n",`
`-      "    print '1 does not equal to 0'\n",`
`-      "    \n"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "if (1==1 and 0<1):\n",`
`-      "    print 'and operator'\n",`
`-      "    \n",`
`-      "if (1==1 or 0==1):\n",`
`-      "    print 'or operator'"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "heading",`
`-     "level": 3,`
`-     "metadata": {},`
`-     "source": [`
`-      "WHILE Statements"`
`-     ]`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "#counter\n",`
`-      "i = 0\n",`
`-      "\n",`
`-      "#loop while i < 5\n",`
`-      "while i < 5:\n",`
`-      "    print i\n",`
`-      "    \n",`
`-      "    #increment counter\n",`
`-      "    i = i + 1"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "heading",`
`-     "level": 2,`
`-     "metadata": {},`
`-     "source": [`
`-      "Functions"`
`-     ]`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "#define functions\n",`
`-      "\n",`
`-      "def SimpleAdd(Number):\n",`
`-      "    return Number\n",`
`-      "\n",`
`-      "def SimpleAdd2(N1, N2):\n",`
`-      "    return N1 + N2\n",`
`-      "\n",`
`-      "def SimpleAdd3(N1=2, N2=2):\n",`
`-      "    return N1 + N2\n",`
`-      "\n",`
`-      "#return a 10\n",`
`-      "print SimpleAdd(10)\n",`
`-      "\n",`
`-      "#return 2 + 5\n",`
`-      "print SimpleAdd2(2,5)\n",`
`-      "\n",`
`-      "#return 1 + 3\n",`
`-      "print SimpleAdd3(1,3)\n",`
`-      "\n",`
`-      "#use default parameters\n",`
`-      "print SimpleAdd3()\n"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "heading",`
`-     "level": 2,`
`-     "metadata": {},`
`-     "source": [`
`-      "Excel"`
`-     ]`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "from xlwt import *"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "heading",`
`-     "level": 3,`
`-     "metadata": {},`
`-     "source": [`
`-      "Add an image"`
`-     ]`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "# Create a workbook\n",`
`-      "wb = Workbook()\n",`
`-      "\n",`
`-      "# Add a sheet/tab\n",`
`-      "ws0 = wb.add_sheet('Picture_Test')\n",`
`-      "\n",`
`-      "# Add picture at location (2,1)\n",`
`-      "# Note: Only accepts bmp files\n",`
`-      "# i.e. ws0.insert_bitmap('C:\\Users\\username\\Desktop/test.bmp', 2, 1)\n",`
`-      "ws0.insert_bitmap('DataFiles\\\\testpic.bmp', 2, 1)\n",`
`-      "\n",`
`-      "# Write excel file\n",`
`-      "# Note: This will overwrite any other files with the same name\n",`
`-      "wb.save('AddImage.xls')"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "heading",`
`-     "level": 3,`
`-     "metadata": {},`
`-     "source": [`
`-      "Convert image to BMP"`
`-     ]`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "#convert an image file to a .bmp image file using PIL\n",`
`-      "from PIL import Image\n",`
`-      "\n",`
`-      "# Convert to bmp\n",`
`-      "def ConvertImg(file_in,file_out):\n",`
`-      "    img = Image.open(file_in)\n",`
`-      "\n",`
`-      "    #print len(img.split())  # test\n",`
`-      "    if len(img.split()) == 4:\n",`
`-      "        # prevent IOError: cannot write mode RGBA as BMP\n",`
`-      "        r, g, b, a = img.split()\n",`
`-      "        img = Image.merge(\"RGB\", (r, g, b))\n",`
`-      "        img.save(file_out)\n",`
`-      "    else:\n",`
`-      "        img.save(file_out)\n",`
`-      "\n",`
`-      "ConvertImg('DataFiles/pngInput.png', 'DataFiles/bmpOutput.bmp')"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "heading",`
`-     "level": 2,`
`-     "metadata": {},`
`-     "source": [`
`-      "Delete Files"`
`-     ]`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "import os\n",`
`-      "\n",`
`-      "# Delete file\n",`
`-      "os.remove('DataFiles/bmpOutput.bmp')"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "heading",`
`-     "level": 2,`
`-     "metadata": {},`
`-     "source": [`
`-      "Dates"`
`-     ]`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "from datetime import datetime, date, time\n",`
`-      "\n",`
`-      "# Get today's date\n",`
`-      "CurrentTime = datetime.now()\n",`
`-      "CurrentTime = CurrentTime.strftime(\"%Y-%m-%d_%H-%M-%S\")\n",`
`-      "CurrentTime"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "heading",`
`-     "level": 2,`
`-     "metadata": {},`
`-     "source": [`
`-      "Pandas"`
`-     ]`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "# What version are we on?\n",`
`-      "pandas.__version__"`
`-     ],`
`-     "language": "python",`
`-     "metadata": {},`
`-     "outputs": []`
`-    },`
`-    {`
`-     "cell_type": "code",`
`-     "collapsed": false,`
`-     "input": [`
`-      "# This will be used in many of the examples so we declare it at the top\n",`
`-      "randn = np.random.randn"`