I am working on the file "FriendsOfFiends/FriendsOfFiends.ipynb".
The first task is:
Right, now we’ve got all the pieces, let’s put them to use. I’m not going to split this up much but hopefully the comments make sense.
Can you write Python code for it?

# don't overwrite the input data...
tmp_sdss = sdss_data

# initialise a list for the output
# and start a counter for the number of groups found:
groups=[]
grp = 0

# initialise the maximum number of galaxies found in a group:
n_max = 0

# set constraints in terms of linking length
# 1 for the transverse direction; 10 for the radial direction:
b_trans = 1.
b_los = 10.

# start the loop:
while True:

    # check we haven't got zero data:
    if (tmp_sdss.shape[1]<1): break

    # pick an initial data point:
    sdss1 = tmp_sdss[:,0]

    # calculate the separation of the initial point
    # from every other data point in both dimensions:
    sep_sky = calc_trans(sdss1,tmp_sdss)
    sep_los = calc_radial(sdss1,tmp_sdss)

    # find the linking length at the redshift of
    # the initial data point:
    d_ll_sky = calc_tempel(sdss1)
    d_ll_los = d_ll_sky

    # normalise the separation:
    tmp_sep1 = sep_sky/d_ll_sky
    tmp_sep2 = sep_los/d_ll_los

    # identify data points within the linking length:
    grp_indices = np.where( np.logical_and( tmp_sep1<=b_trans, tmp_sep2<=b_los))
    # identify data points outside the linking length:
    other_indices = np.where( np.logical_or( tmp_sep1>b_trans, tmp_sep2>b_los) )

    # put the group members in their own array:
    tmp_grp = np.squeeze(tmp_sdss[:,grp_indices],axis=(1,))
    # remove the group members from the rest of the data:
    tmp_sdss = np.squeeze(tmp_sdss[:,other_indices],axis=(1,))

    # initialise this new group:
    gpt = 0
    group = tmp_grp

    # loop through the linking length catchment of all
    # group members:
    while True:

        # how many group members initially:
        n_mem = group.shape[1]

        # step through them all updating the group:
        for gal in range(gpt,n_mem):

            sep_sky = calc_trans(group[:,gal],tmp_sdss)
            sep_los = calc_radial(group[:,gal],tmp_sdss)

            d_ll_sky = calc_tempel(group[:,gal])
            d_ll_los = d_ll_sky

            tmp_sep1 =...


I am working on the file "FriendsOfFiends/FriendsOfFiends.ipynb".
The first task is:
There are a bunch of different ways that you could view these data in Python. Here I’ve chosen to play with the mayavi library.
Note: this plot will appear in a separate window, even though the matplotlib plots appear in-line.
Can you write Python code for it?

xyz = sdss_data

x = sdss_data[1,:]
y = sdss_data[2,:]
z = sdss_data[3,:]

kde = stats.gaussian_kde(xyz)
density = kde(xyz)

# Plot scatter with mayavi
figure = mlab.figure('DensityPlot')
pts = mlab.points3d(x, y, z, density, scale_mode='none', scale_factor=0.01)
mlab.axes()
mlab.show()


I am working on the file "FriendsOfFiends/FriendsOfFiends.ipynb".
The first task is:
To run this on our output data file:
Can you write Python code for it?

sdss_data = read_sdss("A795_sdss.dat")
print "Number of objects: ",sdss_data.shape[1]


I am working on the file "FriendsOfFiends/FriendsOfFiends.ipynb".
The first task is:
…and let’s calculate the mean position of the cluster in all dimensions:
Can you write Python code for it?

ra_cen = np.mean(groups[np.where(groups[:,4]==a_grp),1])
dec_cen = np.mean(groups[np.where(groups[:,4]==a_grp),2])
z_cen = np.mean(groups[np.where(groups[:,4]==a_grp),3])

print "Mean redshift: ",z_cen


I am working on the file "FrontEnd/Python/Visualization/npvcube/ore_jupyter_dashboard.ipynb".
The first task is:
Exposure Statistics (matplotlib)
This plot produces the classical exposure statistics:
* EE = Expected Exposure
* EPE = Expected Positive Exposure
* ENE = Expected Negative Exposure
* PFE = Potential Future Exposure (at a certain quantile)
Can you write Python code for it?

df_stats = df[(df.NettingSet == netting_set_selector.value) & df.Id.isin(trades_selector.value)][['Id', 'Value', 'Sample', 'DateIndex']].groupby(['DateIndex','Sample']).sum().reset_index()
df_epe = df_stats.copy()
df_ene = df_stats.copy()

df_epe.loc[df_epe['Value']<0,'Value']=0
df_ene.loc[df_ene['Value']>0,'Value']=0

ee = df_stats[['DateIndex', 'Value']].groupby('DateIndex').mean().values[:,0]
epe = df_epe[['DateIndex', 'Value']].groupby('DateIndex').mean().values[:,0]
ene = df_ene[['DateIndex', 'Value']].groupby('DateIndex').mean().values[:,0]

fig_statistics = plt.figure()
ax_statistics = fig_statistics.add_subplot(111)

dates = pd.to_datetime(df['Date']).unique()
dates = dates - dates.min()
years = dates.astype('timedelta64[D]') / np.timedelta64(1, 'D') / 365
date_step=3

def plot_exposure_statistics(pfe_percentile):
    ax_statistics.cla()
    pfe_percentile_val = pfe_percentile['new']
    ax_statistics.plot(years, ee, label=ExposureTypes.EE.value)
    ax_statistics.plot(years, epe, label=ExposureTypes.EPE.value)
    ax_statistics.plot(years, ene, label=ExposureTypes.ENE.value)
    pfe = df_stats[['DateIndex', 'Value']].groupby('DateIndex').quantile(pfe_percentile_val/100).values[:,0]
    ax_statistics.plot(years, pfe, label='PFE ' + str(pfe_percentile_val))
    ax_statistics.set_xticks(years[::date_step])
    ax_statistics.set_xticklabels(years[::date_step])
    ax_statistics.get_xaxis().set_major_formatter(matplotlib.ticker.FuncFormatter(lambda x, p: "{:,.1f}".format(x)))
    ax_statistics.set_xlabel('years')

    #ax_statistics.get_yaxis().set_major_formatter(matplotlib.ticker.FuncFormatter(lambda x, p: "{:,.1f}".format(x)))
    ax_statistics.set_ylabel("Exposure [mn]")
    ax_statistics.get_yaxis().set_major_formatter(matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x), ',')))
    ax_statistics.set_ylim([np.amin(df_stats[['DateIndex', 'Value']].groupby('DateIndex').min().values),np.amax(df_stats[['DateIndex', 'Value']].groupby('DateIndex').max().values)*1.01])

    fontP =...


I am working on the file "FrontEnd/Python/Visualization/npvcube/ore_jupyter_dashboard.ipynb".
The first task is:
Select Netting Sets and Trades
This section allows you to select the netting sets and trades you want to analyze. Select the cube file by either changing the default path in the cell below or by pasting its location in the menu after you executed the cells of this section. Click on Load to load in the data. You can select only one netting set at a time, but arbitrary few or many trades in it.
Can you write Python code for it?

npv_cube_filename = os.path.join(os.getcwd(),'rawcube.csv')

import pandas as pd
from ipywidgets import Select, SelectMultiple, HBox

current_netting_sets = [0]
def set_current_netting_sets(netting_sets):
    current_netting_sets = netting_sets['new']

current_trades = [0]
def set_current_trades(trades):
    current_trades = trades['new']

netting_set_selector = Select(description='Netting Sets:')
trades_selector = SelectMultiple(description='Trades:')
netting_set_selector.observe(set_current_netting_sets, names='value')
trades_selector.observe(set_current_trades, names='value')
def load_cube_file(b):
    csv_file = npv_file_selector.value
    global df
    df = pd.read_csv(csv_file)
    df.columns = ['Id', 'NettingSet', 'DateIndex' , 'Date', 'Sample', 'Value']
    netting_set_selector.options = list(df.NettingSet.unique())
    trades_selector.options = list(df.Id.unique())
    trades_selector.value = trades_selector.options

npv_file_selector = Text(description='Cube File:', value=npv_cube_filename, width=200)
read_npv_button = Button(description='Load')
read_npv_button.on_click(load_cube_file)

load_cube_file(read_npv_button)

display(HBox([npv_file_selector, read_npv_button]))
display(HBox([netting_set_selector, trades_selector]))


I am working on the file "FrontEnd/Python/Visualization/npvcube/ore_jupyter_dashboard.ipynb".
The first task is:
TimeSlider
This plot allows you to slide through the exposure density surface above looking at time slice.
Can you write Python code for it?

from ipywidgets import IntSlider, FloatText

fig_surface = plt.figure()
ax_time_slider = fig_surface.add_subplot(111)
fig_surface.canvas.set_window_title('Time Slider')

def plot_time_slider(change):
    date = change['new']
    ax_time_slider.cla()
    ax_time_slider.plot(dist_space, density_values[date], color='k', label=exposure_selector.value)
    ax_time_slider.get_xaxis().set_major_formatter(matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x), ',')))
    ax_time_slider.set_xlim(npv_min, npv_max)
    ax_time_slider.set_ylim(0, density_max)
    ax_time_slider.set_xlabel("Exposure")
    year_text.value = "{:,.3f}".format(years[date])

time_slider = IntSlider(min=0, max=num_dates-1, value=0, description='DateIndex:')
time_slider.observe(plot_time_slider, names='value')
year_text = FloatText(description="Years:", value=years[time_slider.value])
display(HBox([time_slider, year_text]))


I am working on the file "Fuel_prediction/Fuel prediction and co2 emission.ipynb".
The first task is:
Generating final db
Can you write Python code for it?

cols=['year','make','model','vclass','fuel','cityElec','hwyElec','combElec','cityGas','hwyGas','combGas','co2','co2Rate','smogRate','dist','recharge','co2saved']
db = pd.DataFrame(columns=cols)
db

for index, row in elec.iterrows():
    vclass=row["vclass"]
    if( vclass == 'SUBCOMPACT'):
        vclass=int(1)
    elif(vclass =='MID-SIZE'):
        vclass=int(2)
    elif(vclass =='COMPACT'):
        vclass=int(3)
    elif(vclass =='TWO-SEATER'):
        vclass=int(4)
    elif(vclass =='FULL-SIZE'):
        vclass=int(5)
    elif(vclass =='STATION WAGON - SMALL'):
        vclass=int(6)
    elif(vclass =='SUV - STANDARD'):
        vclass=int(7)
    year=int(row["year"])
    toPredict = pd.DataFrame({'vclass':[vclass], 'year':[year]})
    #print(toPredict.head())
    prediction=model.predict(toPredict)
    #print(prediction)
    dfrow=pd.DataFrame({'year':[year],'make':row["make"],'model':row["model"],'vclass':[vclass],'fuel':row["fuel"],'cityElec':row["cityElec"],'hwyElec':row["hwyElec"],'combElec':row["combElec"],'cityGas':row["cityGas"],'hwyGas':row["hwyGas"],'combGas':row["combGas"],'co2':row["co2"],'co2Rate':row["co2Rate"],'smogRate':row["smogRate"],'dist':row["dist"],'recharge':row["recharge"],'co2saved':prediction})
    db = db.append(dfrow)
db

db=db.reset_index(drop=True)
db

db.to_csv('Database.csv')
db.to_json('Databasejs.json')

db["vehicle"]=db["make"].astype(str) + "  "+db["model"].astype(str)
db

db.drop(["make","model"],inplace=True,axis=1)
db

db.to_csv('Database.csv')
db.to_json('Databasejs.json')


I am working on the file "Fuel_prediction/Fuel prediction and co2 emission.ipynb".
The first task is:
So now what we have is basically a cleaned dataset for electric vehicles and the approximate difference in the CO2 that is emitted by a normal vehicle and the CO2 emitted by an electric car (which is nothing)
so the basic flow structure is
1. Get the source and destination from the user - source GPS destination and destination user entered
2. Get details of the vehicle - make, model and distance until recharge necessary/ current battery level
3. Get the rest of the details from the database/dataframe (year, vehicle class, range, consumption, recharge time)
4. Use the vehicle class and year to predict the amount of CO2 a car using fuel would consume
5. Use the algorithm to check the time and fuel requirements
Algorithm:
inputs:
    1. Given by user: make model currentDistance
    2. Extracted from database: year vehicle class range consumption recharge time
outputs: The difference in CO2 emitted in kilograms, recharge time) and consumption of fuel
Procedure:
1. get inputs from user
2. catculate distance between source and destination
3. check if the distance is greater than current distance that can be viewed on the dashboard of electric vehicles
4. if it is less than, there is no issue and the journey can be completed safely
4. if it is greater than find total distance it can travel with a leeway of 10miles (16 km) by taking total distance-(currentdistance-16)=total charge required
    therefore, total time required to charge=recharge time/range * (total charge required)
5. give time taken for recharge and the fuel consumption in KW/hr and le
6. get values of vehicle class and year from the db and use it to query how much of CO2 would have been consumed by a car that runs on fuel
7. display the results to the user
Example:
source: Chelan, Saskatchewan, Canada

destination: Maymont, Saskatchewan, Canada

distance: 353 km

make: TESLA

model: MODEL S P100D

curent distance: 150 km
Can you write Python code for it?

make="TESLA"
model="MODEL S P100D"
distance=353
curdist=150
res=elec.loc[(elec['make'] == make) & (elec['model'] == model)]
res

maxdist=res['dist'].astype('int')
maxdist

recharge=res['recharge'].astype('int')
recharge

year=res['year'].astype('int')
year

res['vclass'] = res['vclass'].replace('SUBCOMPACT', '1')
res['vclass'] = res['vclass'].replace('MID-SIZE', '2')
res['vclass'] = res['vclass'].replace('COMPACT', '3')
res['vclass'] = res['vclass'].replace('TWO-SEATER', '4')
res['vclass'] = res['vclass'].replace('FULL-SIZE', '5')
res['vclass'] = res['vclass'].replace('STATION WAGON - SMALL', '6')
res['vclass'] = res['vclass'].replace('SUV - STANDARD', '7')
vclass = res['vclass'].astype(int)
vclass

combGas=res['combGas'].astype('float')
combGas

combElec=res['combElec'].astype('float')
combElec

if (curdist>distance):
    rechargedist=0
    rechargetime=0;
    flag=0
else:
    rechargedist=distance-(curdist-16)
    rechargetime=recharge/maxdist*(rechargedist)
    flag=1

rechargetime

toPredict = pd.DataFrame({'vclass': vclass, 'year':year})
toPredict.head()

prediction=model.predict(toPredict)
prediction #shows co2 emitted in grams per km

co2=prediction*distance/1000 #in kg
co2

fuelconsgas=combGas/100*distance
fuelconsgas

fuelconselec=combElec/100*distance
fuelconselec

print("The time you need to reacharge your car to make your journey is %f "%rechargetime)
print("The fuel reqired for the journey is %f KWH and in Le is %f"%(fuelconselec,fuelconsgas))
print("Woo hoo you have saved %f kg of CO2 from destroying our earth."%co2)

import matplotlib.pyplot as plt
import numpy as np
dist=0
curdist=150
x=list()
y=list()
maxdist=507
recharge=12
while dist <1000:

    x.append(dist)
    if (curdist>dist):
        rechargetime=0
    else:
        #if(dist<maxdist):
        rechargedist=dist-(curdist-16)
        mul=rechargedist%maxdist
        add=int(rechargedist/maxdist)
        rechargetime=mul*recharge/maxdist + add*recharge
    y.append(rechargetime)
    dist+=1;
plt.plot(x,y)
plt.show()


I am working on the file "Fuel_prediction/Fuel prediction and co2 emission.ipynb".
The first task is:
Understanding the Table for normal cars
Model   4WD/4X4 = Four-wheel drive
    AWD = All-wheel drive
    CNG = Compressed natural gas
    FFV = Flexible-fuel vehicle
    NGV = Natural gas vehicle
    # = High output engine that provides more power than the standard engine of the same size
Transmission    A = Automatic
    AM = Automated manual
    AS = Automatic with select shift
    AV = Continuously variable
    M = Manual
    3 – 10 = Number of gears
Fuel Type   X = Regular gasoline
    Z = Premium gasoline
    D = Diesel
    E = Ethanol (E85)
    N = Natural Gas
Fuel Consumption    City and highway fuel consumption ratings are shown in litres per 100 kilometres (L/100 km) - combined rating (55% city, 45% hwy) is shown in L/100 km and in miles per imperial gallon (mpg)
CO2 Emissions (g/km)    Estimated tailpipe carbon dioxide emissions (in grams per kilometre) are based on fuel type and the combined fuel consumption rating.
Can you write Python code for it?

df = pd.read_csv("fuel consumption dataset\Original MY2000-2014 Fuel Consumption Ratings (2-cycle).csv")
# take a look at the dataset
df.head()

df.columns

df.describe()

cols = ['MODEL','VEHICLE CLASS', 'CO2 EMISSIONS']
df=df[cols]
newcols = {
    'MODEL': 'year',
    'VEHICLE CLASS':'vclass',
    'CO2 EMISSIONS':'co2'}
df.rename(columns=newcols, inplace=True)
df=df.drop(df.index[0])
df = df.reset_index(drop=True)
df

df['co2'] =  df['co2'].astype(float)
df['vclass']=df['vclass'].astype(str)
df['year'] =  df['year'].astype(int)
df.describe()

df = df.drop( df[ (df.vclass !='SUBCOMPACT' ) & (df.vclass !='MID-SIZE' ) & (df.vclass !='COMPACT' ) & (df.vclass !='TWO-SEATER' ) & (df.vclass !='FULL-SIZE' ) & (df.vclass !='STATION WAGON - SMALL' ) & (df.vclass !='SUV - STANDARD' )].index )
df.index = range(len(df))
df['vclass'] = df['vclass'].replace('SUBCOMPACT', '1')
df['vclass'] = df['vclass'].replace('MID-SIZE', '2')
df['vclass'] = df['vclass'].replace('COMPACT', '3')
df['vclass'] = df['vclass'].replace('TWO-SEATER', '4')
df['vclass'] = df['vclass'].replace('FULL-SIZE', '5')
df['vclass'] = df['vclass'].replace('STATION WAGON - SMALL', '6')
df['vclass'] = df['vclass'].replace('SUV - STANDARD', '7')
df['vclass'] = df['vclass'].astype(int)
df

model = smf.ols(formula='co2 ~ vclass + year', data=df).fit()
model.paramslm = smf.ols(formula='co2 ~ vclass + year', data=df).fit()
model.params #parameters

model.summary()

toPredict = pd.DataFrame({'vclass': [6], 'year':[2000]})
toPredict.head()

prediction=model.predict(toPredict)
prediction


I am working on the file "Full done.ipynb".
The first task is:
Model selection
Difficult to do with classic tools (here)
Problem:
1 - Calculate the BMA coefficient values

2 - Try running with different priors
Can you write Python code for it?

from pyBMA import CoxPHFitter
bmaCox = CoxPHFitter.CoxPHFitter()
bmaCox.fit(df, "lifetime", event_col= "event", priors= [0.5]*4)
bmaCox.summary

#Low probability for everything favours parsimonious models
bmaCox = CoxPHFitter.CoxPHFitter()
bmaCox.fit(df, "lifetime", event_col= "event", priors= [0.1]*4)
bmaCox.summary

#Boost probability of brand
bmaCox = CoxPHFitter.CoxPHFitter()
bmaCox.fit(df, "lifetime", event_col= "event", priors= [0.3, 0.9, 0.001, 0.3])
print(bmaCox.summary)


I am working on the file "Full done.ipynb".
The first task is:
Once we've fit the data, we need to do something useful with it.  Try to do the following things:
1 - Plot the baseline survival function

2 - Predict the functions for a particular set of features

3 - Plot the survival function for two different set of features

4 - For your results in part 3 caculate how much more likely a death event is for one than the other for a given period of time
Can you write Python code for it?

#Solution to 1
fig, axis = plt.subplots(nrows=1, ncols=1)
cf.baseline_survival_.plot(ax = axis, title = "Baseline Survival")

regressors = np.array([[1,45,0,0]])
survival = cf.predict_survival_function(regressors)
survival.head()

#Solution to plotting multiple regressors
fig, axis = plt.subplots(nrows=1, ncols=1, sharex=True)
regressor1 = np.array([[1,45,0,1]])
regressor2 = np.array([[1,23,1,1]])
survival_1 = cf.predict_survival_function(regressor1)
survival_2 = cf.predict_survival_function(regressor2)
plt.plot(survival_1,label = "45 year old male - search")
plt.plot(survival_2,label = "45 year old male - display")
plt.legend(loc = "upper")

odds = survival_1 / survival_2
plt.plot(odds, c = "red")


I am working on the file "Full done.ipynb".
The first task is:
Problems:
7 - Try testing whether the median is greater than a different values
Can you write Python code for it?

medians = [weibull_median(x[0], x[1]) for x in zip(mcmc.trace("alpha"), mcmc.trace("beta"))]
testing_value = 14.9
number_of_greater_samples = sum([x >= testing_value for x in medians])
100 * (number_of_greater_samples / len(medians))


I am working on the file "Full done.ipynb".
The first task is:
Problems:
1 - Try to fit your data from section 1
2 - Use the results to plot the distribution of the median

Note that the media of a Weibull distribution is:
$$β(log 2)^{1/α}$$
Can you write Python code for it?

censor = np.array(df["event"].apply(lambda x: 0 if x else 1).tolist())
alpha = pm.Uniform("alpha", 0,50)
beta = pm.Uniform("beta", 0,50)

@pm.observed
def survival(value=df["lifetime"], alpha = alpha, beta = beta ):
    return sum( (1-censor)*(np.log( alpha/beta) + (alpha-1)*np.log(value/beta)) - (value/beta)**(alpha))

mcmc = pm.MCMC([alpha, beta, survival ] )
mcmc.sample(10000)

def weibull_median(alpha, beta):
    return beta * ((log(2)) ** ( 1 / alpha))
plt.hist([weibull_median(x[0], x[1]) for x in zip(mcmc.trace("alpha"), mcmc.trace("beta"))])


I am working on the file "Full done.ipynb".
The first task is:
The first step in any data analysis is acquiring and munging the data
Our starting data set can be found here:
   http://jakecoltman.com in the pyData post
It is designed to be roughly similar to the output from DCM's path to conversion
Download the file and transform it into something with the columns:
id,lifetime,age,male,event,search,brand
where lifetime is the total time that we observed someone not convert for and event should be 1 if we see a conversion and 0 if we don't.  Note that all values should be converted into ints
It is useful to note that end_date = datetime.datetime(2016, 5, 3, 20, 36, 8, 92165)
Can you write Python code for it?

running_id = 0
output = [[0]]
with open("E:/output.txt") as file_open:
    for row in file_open.read().split("\n"):
        cols = row.split(",")
        if cols[0] == output[-1][0]:
            output[-1].append(cols[1])
            output[-1].append(True)
        else:
            output.append(cols)
    output = output[1:]

for row in output:
    if len(row) == 6:
        row += [datetime(2016, 5, 3, 20, 36, 8, 92165), False]
output = output[1:-1]

def convert_to_days(dt):
    day_diff = dt / np.timedelta64(1, 'D')
    if day_diff == 0:
        return 23.0
    else:
        return day_diff

df = pd.DataFrame(output, columns=["id", "advert_time", "male","age","search","brand","conversion_time","event"])
df["lifetime"] = pd.to_datetime(df["conversion_time"]) - pd.to_datetime(df["advert_time"])
df["lifetime"] = df["lifetime"].apply(convert_to_days)
df["male"] = df["male"].astype(int)
df["search"] = df["search"].astype(int)
df["brand"] = df["brand"].astype(int)
df["age"] = df["age"].astype(int)
df["event"] = df["event"].astype(int)
df = df.drop('advert_time', 1)
df = df.drop('conversion_time', 1)
df = df.set_index("id")
df = df.dropna(thresh=2)
df.median()

###Parametric Bayes
#Shout out to Cam Davidson-Pilon

## Example fully worked model using toy data
## Adapted from http://blog.yhat.com/posts/estimating-user-lifetimes-with-pymc.html
## Note that we've made some corrections

N = 2500

##Generate some random data
lifetime = pm.rweibull( 2, 5, size = N )
birth = pm.runiform(0, 10, N)
censor = ((birth + lifetime) >= 10)
lifetime_ = lifetime.copy()
lifetime_[censor] = 10 - birth[censor]

alpha = pm.Uniform('alpha', 0, 20)
beta = pm.Uniform('beta', 0, 20)

@pm.observed
def survival(value=lifetime_, alpha = alpha, beta = beta ):
    return sum( (1-censor)*(log( alpha/beta) + (alpha-1)*log(value/beta)) - (value/beta)**(alpha))

mcmc = pm.MCMC([alpha, beta, survival ] )
mcmc.sample(50000, 30000)

pm.Matplot.plot(mcmc)
mcmc.trace("alpha")[:]


I am working on the file "Full presentation.ipynb".
The first task is:
If we want to look at covariates, we need a new approach.  We'll use Cox proprtional hazards.  More information here.
Can you write Python code for it?

#Fitting solution
cf = lifelines.CoxPHFitter()
cf.fit(df, 'lifetime', event_col = 'event')
cf.summary


I am working on the file "Full presentation.ipynb".
The first task is:
Model selection
Difficult to do with classic tools (here)
Problem:
1 - Calculate the BMA coefficient values

2 - Compare these results to past the lifelines results

3 - Try running with different priors
Can you write Python code for it?

##Solution to 1
from pyBMA import CoxPHFitter
bmaCox = pyBMA.CoxPHFitter.CoxPHFitter()
bmaCox.fit(df, "lifetime", event_col= "event", priors= [0.5]*4)

print(bmaCox.summary)

#Low probability for everything favours parsimonious models
bmaCox = pyBMA.CoxPHFitter.CoxPHFitter()
bmaCox.fit(df, "lifetime", event_col= "event", priors= [0.1]*4)
print(bmaCox.summary)

#Low probability for everything favours parsimonious models
bmaCox = pyBMA.CoxPHFitter.CoxPHFitter()
bmaCox.fit(df, "lifetime", event_col= "event", priors= [0.9]*4)
print(bmaCox.summary)

#Low probability for everything favours parsimonious models
bmaCox = pyBMA.CoxPHFitter.CoxPHFitter()
bmaCox.fit(df, "lifetime", event_col= "event", priors= [0.3, 0.9, 0.001, 0.3])
print(bmaCox.summary)


I am working on the file "Full presentation.ipynb".
The first task is:
Once we've fit the data, we need to do something useful with it.  Try to do the following things:
1 - Plot the baseline survival function

2 - Predict the functions for a particular set of features

3 - Plot the survival function for two different set of features

4 - For your results in part 3 caculate how much more likely a death event is for one than the other for a given period of time
Can you write Python code for it?

#Solution to 1
fig, axis = plt.subplots(nrows=1, ncols=1)
cf.baseline_survival_.plot(ax = axis, title = "Baseline Survival")

# Solution to prediction
regressors = np.array([[1,45,0,0]])
survival = cf.predict_survival_function(regressors)
survival

#Solution to plotting multiple regressors
fig, axis = plt.subplots(nrows=1, ncols=1, sharex=True)
regressor1 = np.array([[1,45,0,1]])
regressor2 = np.array([[1,23,1,1]])
survival_1 = cf.predict_survival_function(regressor1)
survival_2 = cf.predict_survival_function(regressor2)
plt.plot(survival_1,label = "32 year old male")
plt.plot(survival_2,label = "46 year old female")
plt.legend(loc = "lower left")

#Difference in survival
odds = survival_1 / survival_2
plt.plot(odds, c = "red")


I am working on the file "Full presentation.ipynb".
The first task is:
Problems:
2 - Try to fit your data from section 1
3 - Use the results to plot the distribution of the median
--------
4 - Try adjusting the number of samples, the burn parameter and the amount of thinning to correct get good answers
5 - Try adjusting the prior and see how it affects the estimate
--------
6 - Try to fit a different distribution to the data
7 - Compare answers
Bonus - test the hypothesis that the true median is greater than a certain amount

For question 2, note that the median of a Weibull is:
$$β(log 2)^{1/α}$$
Can you write Python code for it?

#Solution to question 4:
def weibull_median(alpha, beta):
    return beta * ((log(2)) ** ( 1 / alpha))
plt.hist([weibull_median(x[0], x[1]) for x in zip(mcmc.trace("alpha"), mcmc.trace("beta"))])

#Solution to question 4:
### Increasing the burn parameter allows us to discard results before convergence
### Thinning the results removes autocorrelation

mcmc = pm.MCMC([alpha, beta, survival ] )
mcmc.sample(10000, burn = 3000, thin = 20)

pm.Matplot.plot(mcmc)

#Solution to Q5
## Adjusting the priors impacts the overall result
## If we give a looser, less informative prior then we end up with a broader, shorter distribution
## If we give much more informative priors, then we get a tighter, taller distribution

censor = np.array(df["event"].apply(lambda x: 0 if x else 1).tolist())

## Note the narrowing of the prior
alpha = pm.Normal("alpha", 1.7, 10000)
beta = pm.Normal("beta", 18.5, 10000)

####Uncomment this to see the result of looser priors
## Note this ends up pretty much the same as we're already very loose
#alpha = pm.Uniform("alpha", 0, 30)
#beta = pm.Uniform("beta", 0, 30)

@pm.observed
def survival(value=df["lifetime"], alpha = alpha, beta = beta ):
    return sum( (1-censor)*(np.log( alpha/beta) + (alpha-1)*np.log(value/beta)) - (value/beta)**(alpha))

mcmc = pm.MCMC([alpha, beta, survival ] )
mcmc.sample(10000, burn = 5000, thin = 20)
pm.Matplot.plot(mcmc)
#plt.hist([weibull_median(x[0], x[1]) for x in zip(mcmc.trace("alpha"), mcmc.trace("beta"))])

## Solution to bonus
## Super easy to do in the Bayesian framework, all we need to do is look at what % of samples
## meet our criteria
medians = [weibull_median(x[0], x[1]) for x in zip(mcmc.trace("alpha"), mcmc.trace("beta"))]
testing_value = 15.6
number_of_greater_samples = sum([x >= testing_value for x in medians])
100 * (number_of_greater_samples / len(medians))

#Cox model


I am working on the file "Full.ipynb".
The first task is:
If we want to look at covariates, we need a new approach.  We'll use Cox proprtional hazards.  More information available at https://jakecoltman.gitlab.io/website/post/cox_proportional_hazard/
To fit in python we use the module lifelines:
http://lifelines.readthedocs.io/en/latest/
Can you write Python code for it?


I am working on the file "Full.ipynb".
The first task is:
Model selection
Difficult to do with classic tools (here)
Problem:
1 - Calculate the BMA coefficient values

2 - Try running with different priors
Can you write Python code for it?

#### BMA Coefficient values

#### Different priors


I am working on the file "Full.ipynb".
The first task is:
Once we've fit the data, we need to do something useful with it.  Try to do the following things:
1 - Plot the baseline survival function

2 - Predict the functions for a particular set of features

3 - Plot the survival function for two different set of features

4 - For your results in part 3 caculate how much more likely a death event is for one than the other for a given period of time
Can you write Python code for it?

#### Plot baseline hazard function

#### Predict

#### Plot survival functions for different covariates

#### Plot some odds


I am working on the file "Full.ipynb".
The first task is:
Problems:
4 - Try adjusting the number of samples for burning and thinnning
5 - Try adjusting the prior and see how it affects the estimate
Can you write Python code for it?

#### Adjust burn and thin, both paramters of the mcmc sample function

#### Narrow and broaden prior


I am working on the file "Full.ipynb".
The first task is:
Problems:
1 - Try to fit your data from section 1
2 - Use the results to plot the distribution of the median

Note that the media of a Weibull distribution is:
$$β(log 2)^{1/α}$$
Can you write Python code for it?

#### Fit to your data here

#### Plot the distribution of the median


I am working on the file "Full.ipynb".
The first task is:
The first step in any data analysis is acquiring and munging the data
An example data set can be found at:
   https://jakecoltman.gitlab.io/website/post/pydata/
Download the file output.txt and transform it into a format like below where the event column should be 0 if there's only one entry for an id, and 1 if there are two entries:
End date = datetime.datetime(2016, 5, 3, 20, 36, 8, 92165)
id,time_to_convert,age,male,event,search,brand
Can you write Python code for it?

####Data munging here

###Parametric Bayes
#Shout out to Cam Davidson-Pilon

## Example fully worked model using toy data
## Adapted from http://blog.yhat.com/posts/estimating-user-lifetimes-with-pymc.html

alpha = pm.Uniform("alpha", 0,20)
beta = pm.Uniform("beta", 0,20)
obs = pm.Weibull('obs', alpha, beta, value = df["time_to_convert"], observed = True )
obs.random
@pm.potential
def censorfactor(obs=obs):
    if np.any(obs>23 ):
        return -100000
    else:
        return 0

mcmc = pm.MCMC([alpha, beta, obs, censorfactor ] )
mcmc.sample(5000, burn = 0)

pm.Matplot.plot(mcmc)
mcmc.trace("alpha")[:]


I am working on the file "FullTomographyTrials-Copy1.ipynb".
The first task is:
Original basis Q0
Recovered basis Qc (Controlled-bsais)
Effected basis Qeff = Qt.T * Q0
Use effected basis for error sampling
Learn Qt progressively better
When data comes in from the Qeff alignment, you must transform it back to the standard basis before average with the existing channel estimate

Error x Time x Cycle_ratio
Can you write Python code for it?

D = 0.01
N_ERRORS = 1e6
N_TRIALS = 100
N_CYCLES = np.logspace(1, 3, 10).astype(np.int)
RECORDS = []
for trial in tqdm(range(N_TRIALS)):
    for n_cycles in N_CYCLES:
        n = int(N_ERRORS / n_cycles)
        channel = Channel(kx=0.7, ky=0.2, kz=0.1,
                          Q=np.linalg.qr(np.random.randn(3,3))[0],
                          n=n, d=D)

        RECORDS.append({
                "trial": trial,
                "cycle_length": n,
                "n_cycles": n_cycles,
                "time": 0,
                "Mdist": np.linalg.norm(channel.Mhat-channel.C),
                "Qdist": np.linalg.norm(np.dot(channel.Qc.T, channel.Q) - np.eye(3))
            })
        for cycle in range(n_cycles):
            channel.update()
            RECORDS.append({
                    "trial": trial,
                    "cycle_length": n,
                    "n_cycles": n_cycles,
                    "time": (cycle+1)*n,
                    "Mdist": np.linalg.norm(channel.Mhat-channel.C),
                    "Qdist": np.linalg.norm(np.dot(channel.Qc.T, channel.Q) - np.eye(3))
                })

df = pd.DataFrame(RECORDS)
df.to_csv("{}errorsd{}.csv".format(N_ERRORS,D))

df["cycle_length"] = (N_ERRORS / df["n_cycles"]).astype(np.int)

df.tail(10)

PAL = sns.color_palette("hls", len(N_CYCLES))
fig, ax = plt.subplots(1, 1, figsize=(8,6))

for idx, n_cycles in enumerate(N_CYCLES):
    sel = (df["n_cycles"] == n_cycles)
    subdf = df.loc[sel, :]
    v = subdf.groupby("time").mean()
    s = subdf.groupby("time").std()
    t = v.index.values
    y = v["Mdist"].values
    e = s["Mdist"].values
    ax.loglog(t, y, label=str(subdf.iloc[0, 2]), c=PAL[idx])
    ax.fill_between(t, y-e, y+e, alpha=0.1, color=PAL[idx])
plt.title("Recover error over time for varied ratios of cycles to realignments")
plt.xlabel("Time [cycles]")
plt.ylabel("Basis recovery error")
plt.legend()


I am working on the file "FullTomographyTrials-Copy1.ipynb".
The first task is:
Regime 1 basis alignment
Can you write Python code for it?

D = 0.01
N_TRIALS = 100
MAX_N = int(1e8)
N_STEP = int(1e5)
RECORDS = []
for trial in tqdm(range(N_TRIALS)):
    channel = Channel(kx=0.7, ky=0.2, kz=0.1,
                      Q=np.linalg.qr(np.random.randn(3,3))[0],
                      n=N_STEP, d=D)
    pxhat, pyhat, pzhat = list(np.linalg.svd(channel.Mhat)[1])
    RECORDS.append({
            "trial": trial,
            "time": 0,
            "Mdist": np.linalg.norm(channel.Mhat-channel.C),
            "Qdist": np.linalg.norm(np.dot(channel.Qc.T, channel.Q) - np.eye(3)),
            "pxval": channel.kx, "pyval": channel.ky, "pzval": channel.kz,
            "pxhat": pxhat, "pyhat": pyhat, "pzhat": pzhat
        })
    for time in range(0, MAX_N, N_STEP):
        channel.update()
        pxhat, pyhat, pzhat = list(np.linalg.svd(channel.Mhat)[1])
        RECORDS.append({
                "trial": trial,
                "time": time,
                "Mdist": np.linalg.norm(channel.Mhat-channel.C),
                "Qdist": np.linalg.norm(np.dot(channel.Qc.T, channel.Q) - np.eye(3)),
                "pxval": channel.kx, "pyval": channel.ky, "pzval": channel.kz,
                "pxhat": pxhat, "pyhat": pyhat, "pzhat": pzhat
            })

df = pd.DataFrame(RECORDS)
df.to_csv("regime1.csv")

df = pd.read_csv("regime1.csv")
v = df.groupby("time").mean()["Qdist"]
s = df.groupby("time").std()["Qdist"]

fig, ax = plt.subplots(1, 1, figsize=(8, 6))
t = v.index.values
y = v.values
e = s.values
ax.plot(t, y,)
ax.fill_between(t, y-e, y+e, alpha=0.25)
plt.ylabel("Measure of orthonormality between $Q_{hat}$ and $Q_{val}$")
plt.xlabel("Time [n_errors]")

df = pd.read_csv("regime1.csv")
v = df.groupby("time").mean()["Mdist"]
s = df.groupby("time").std()["Mdist"]

fig, ax = plt.subplots(1, 1, figsize=(8, 6))
t = v.index.values
y = v.values
e = s.values
ax.loglog(t, y,)
ax.fill_between(t, y-e, y+e, alpha=0.25)
plt.ylabel("Norm distance between $M_{hat}$ and $M_{val}$")
plt.xlabel("Time [n_errors]")


I am working on the file "FullTomographyTrials.ipynb".
The first task is:
Constant p_uncorr advantages in regimes 1 and 2
Can you write Python code for it?


I am working on the file "FullTomographyTrials.ipynb".
The first task is:
Original basis Q0
Recovered basis Qc (Controlled-bsais)
Effected basis Qeff = Qt.T * Q0
Use effected basis for error sampling
Learn Qt progressively better
When data comes in from the Qeff alignment, you must transform it back to the standard basis before average with the existing channel estimate

Error x Time x Cycle_ratio
Can you write Python code for it?


I am working on the file "FullTomographyTrials.ipynb".
The first task is:
Regime 1 basis alignment
Can you write Python code for it?

D = 0.01
N_TRIALS = 100
MAX_N = int(1e6)
N_STEP = int(1e3)
RECORDS = []
for trial in tqdm(range(N_TRIALS)):
    channel = Channel(kx=0.7, ky=0.2, kz=0.1,
                      Q=np.linalg.qr(np.random.randn(3,3))[0],
                      n=N_STEP, d=D)
    pxhat, pyhat, pzhat = list(np.linalg.svd(channel.Mhat)[1])
    RECORDS.append({
            "trial": trial,
            "time": 0,
            "Mdist": np.linalg.norm(channel.Mhat-channel.C),
            "Qdist": np.linalg.norm(np.dot(channel.Qc.T, channel.Q) - np.eye(3)),
            "pxval": channel.kx, "pyval": channel.ky, "pzval": channel.kz,
            "pxhat": pxhat, "pyhat": pyhat, "pzhat": pzhat
        })
    for time in range(0, MAX_N, N_STEP):
        channel.update()
        pxhat, pyhat, pzhat = list(np.linalg.svd(channel.Mhat)[1])
        RECORDS.append({
                "trial": trial,
                "time": time,
                "Mdist": np.linalg.norm(channel.Mhat-channel.C),
                "Qdist": np.linalg.norm(np.dot(channel.Qc.T, channel.Q) - np.eye(3)),
                "pxval": channel.kx, "pyval": channel.ky, "pzval": channel.kz,
                "pxhat": pxhat, "pyhat": pyhat, "pzhat": pzhat
            })

df = pd.DataFrame(RECORDS)
df.to_csv("regime1.csv")

df = pd.read_csv("regime1.csv")
v = df.groupby("time").mean()["Qdist"]
s = df.groupby("time").std()["Qdist"]

fig, ax = plt.subplots(1, 1, figsize=(8, 6))
t = v.index.values
y = v.values
e = s.values
ax.plot(t, y,)
ax.fill_between(t, y-e, y+e, alpha=0.25)
plt.ylabel("Measure of orthonormality between $Q_{hat}$ and $Q_{val}$")
plt.xlabel("Time [n_errors]")

df = pd.read_csv("regime1.csv")
v = df.groupby("time").mean()["Mdist"]
s = df.groupby("time").std()["Mdist"]

fig, ax = plt.subplots(1, 1, figsize=(8, 6))
t = v.index.values
y = v.values
e = s.values
ax.loglog(t, y,)
ax.fill_between(t, y-e, y+e, alpha=0.25)
plt.ylabel("Norm distance between $M_{hat}$ and $M_{val}$")
plt.xlabel("Time [n_errors]")


I am working on the file "FullTomographyTrials.ipynb".
The first task is:
Regime 2 basis alignment
Can you write Python code for it?

D = 0.01
N_TRIALS = 100
MAX_N = int(1e6)
N_STEP = int(1e3)
RECORDS = []
for trial in tqdm(range(N_TRIALS)):
    channel = Channel(kx=0.985, ky=0.01, kz=0.005,
                      Q=np.linalg.qr(np.random.randn(3,3))[0],
                      n=N_STEP, d=D)
    pxhat, pyhat, pzhat = list(np.linalg.svd(channel.Mhat)[1])
    RECORDS.append({
            "trial": trial,
            "time": 0,
            "Mdist": np.linalg.norm(channel.Mhat-channel.C),
            "Qdist": np.linalg.norm(np.dot(channel.Qc.T, channel.Q) - np.eye(3)),
            "pxval": channel.kx, "pyval": channel.ky, "pzval": channel.kz,
            "pxhat": pxhat, "pyhat": pyhat, "pzhat": pzhat
        })
    for time in range(0, MAX_N, N_STEP):
        channel.update()
        pxhat, pyhat, pzhat = list(np.linalg.svd(channel.Mhat)[1])
        RECORDS.append({
                "trial": trial,
                "time": time,
                "Mdist": np.linalg.norm(channel.Mhat-channel.C),
                "Qdist": np.linalg.norm(np.dot(channel.Qc.T, channel.Q) - np.eye(3)),
                "pxval": channel.kx, "pyval": channel.ky, "pzval": channel.kz,
                "pxhat": pxhat, "pyhat": pyhat, "pzhat": pzhat
            })

df = pd.DataFrame(RECORDS)
df.to_csv("regime2.csv")

df = pd.read_csv("regime2.csv")
v = df.groupby("time").mean()["Qdist"]
s = df.groupby("time").std()["Qdist"]

fig, ax = plt.subplots(1, 1, figsize=(8, 6))
t = v.index.values
y = v.values
e = s.values
ax.plot(t, y)
ax.plot(t, y-e, ls="--")
ax.plot(t, y+e, ls="--")
plt.ylabel("Measure of orthonormality between $Q_{hat}$ and $Q_{val}$")
plt.xlabel("Time [n_errors]")

df = pd.read_csv("regime2.csv")
v = df.groupby("time").mean()["Mdist"]
s = df.groupby("time").std()["Mdist"]

fig, ax = plt.subplots(1, 1, figsize=(8, 6))
t = v.index.values
y = v.values
e = s.values
ax.loglog(t, y)
ax.fill_between(t, y-e, y+e, alpha=0.25)
plt.ylabel("Norm distance between $M_{hat}$ and $M_{val}$")
plt.xlabel("Time [n_errors]")


I am working on the file "Funciones Booleanas/Boole.ipynb".
The first task is:
Ahora sí que podemos utilizar el simplificador espresso implementado en pyeda
Can you write Python code for it?


I am working on the file "Funciones Booleanas/Boole.ipynb".
The first task is:
Al utilizar sympy podemos escribir una forma más amigable de una expresión booleana
Can you write Python code for it?


I am working on the file "Funciones Booleanas/Boole.ipynb".
The first task is:
La formas normales conjuntiva y disyuntivas las podemos calcular como sigue
Can you write Python code for it?


I am working on the file "Funciones Booleanas/Boole.ipynb".
The first task is:
La tabla de verdad de una expresión se obtiene como sigue
Can you write Python code for it?


I am working on the file "Funciones Booleanas/Boole.ipynb".
The first task is:
O dar valores de verdad a las variables
Can you write Python code for it?


I am working on the file "Funciones Booleanas/Boole.ipynb".
The first task is:
Podemos hacer una función que pase de minterm a expresions en pyeda
Can you write Python code for it?

def minterm2expr(l,v):
    n = len(l)
    vv=v.copy()
    for i in range(n):
        if not(l[i]):
            vv[i]=Not(vv[i])
    return And(*vv)

x,y,z,t = map(exprvar,"xyzt")

minterm2expr([0,1,0,1],[x,y,z,t])

def minterms2expr(l,v):
    return Or(*[minterm2expr(a,v) for a in l])

hh2=minterms2expr([[0,0,0,0],[0,0,1,0],[0,1,0,0],[0,1,1,0],[0,1,1,1],[1,0,0,0],[1,0,1,0],[1,1,0,0]],[x,y,z,t])

hh2

pprint(sympify(hh2))


I am working on the file "Funciones Booleanas/Boole.ipynb".
The first task is:
Podríamos haber definido directamente p utilizando tablas de verdad
Can you write Python code for it?

p=truthtable([x,y,z], "01111110")

pm, = espresso_tts(p)

pprint(sympify(pm))


I am working on the file "Funciones Booleanas/Boole.ipynb".
The first task is:
Un ejemplo de simplificación
Veamos que el o exclusivo con la definición $x\oplus y=(x\wedge \neg y)\vee (\neg x\wedge y)$ es asociativo
Can you write Python code for it?

x, y, z = map(exprvar,"xyz")

f = lambda x,y : Or(And(x,~ y),And(~x,y))

f(x,y)

expr2truthtable(f(x,y))

f(x,y).equivalent(Xor(x,y))


I am working on the file "Funciones Booleanas/Boole.ipynb".
The first task is:
Una forma de comprobar que dos expresiones son equivalentes es la siguiente
Can you write Python code for it?


I am working on the file "Funciones Booleanas/Boole.ipynb".
The first task is:
Veamos que efectivamente $x\oplus(y\oplus z)=(x\oplus y)\oplus z$
Can you write Python code for it?

pprint(simplify_logic(f(x,f(y,z))))

pprint(simplify_logic(f(f(x,y),z)))

a= f(f(x,y),z)
b= f(x,f(y,z))

a.equivalent(b)


I am working on the file "Funciones Booleanas/Boole.ipynb".
The first task is:
Veamos un ejemplo análogo pero con más variables, y de paso mostramos como definir vectores de variables
Can you write Python code for it?

X = ttvars('x', 4)
f = truthtable(X, "0111111111111110")

fm, = espresso_tts(f)

fm

expr2truthtable(fm)


I am working on the file "Funciones Booleanas/Boole.ipynb".
The first task is:
Y ahora la podemos simplificar
Can you write Python code for it?


I am working on the file "Function Space description.ipynb".
The first task is:
Function Spaces in Nengo
Here is proposed new utilities to add to Nengo to support function space representations.
The basic idea of a function space is that instead of representing vectors, we represent full functions (or even vector fields).
What is a function?
Really, it's just a set of points.  For example, here's a guassian evaluated at a set of points:
Can you write Python code for it?

domain = np.linspace(-1, 1, 2000)

def gaussian(mag, mean, sd):
    return mag * np.exp(-(domain-mean)**2/(2*sd**2))

pylab.plot(domain, gaussian(mag=1, mean=0, sd=0.1))
pylab.show()


I am working on the file "Function Space description.ipynb".
The first task is:
Multidimensional representations
There is nothing restricting this code to 1 dimensional functions.  For example, here is a 2D bump:
Can you write Python code for it?

domain = np.random.uniform(-1, 1, size=(1000, 2))

def gaussian2d(meanx, meany, sd):
    return np.exp(-((domain[:,0]-meanx)**2+(domain[:,1]-meany)**2)/(2*sd**2))

fs = nengo.FunctionSpace(
        nengo.dists.Function(gaussian2d,
                             meanx=nengo.dists.Uniform(-1, 1),
                             meany=nengo.dists.Uniform(-1, 1),
                             sd=nengo.dists.Uniform(0.1, 0.7)),

        n_basis=50)

model = nengo.Network()
with model:
    ens = nengo.Ensemble(n_neurons=500, dimensions=fs.n_basis)
    ens.encoders = fs.project(fs.space)
    ens.eval_points = fs.project(fs.space)

    stimulus = nengo.Node(fs.project(gaussian2d(0,0,0.2)))
    nengo.Connection(stimulus, ens)

    probe = nengo.Probe(ens, synapse=0.01)

sim = nengo.Simulator(model)
sim.run(0.2)

from mpl_toolkits.mplot3d import Axes3D
fig = pylab.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(domain[:,0], domain[:,1], fs.reconstruct(sim.data[probe][-1]))
pylab.show()


I am working on the file "Function Space description.ipynb".
The first task is:
Multiple Distributions
It should be noted that there are three different distributions in play here.  The distribution used to define the space, the distribution for the encoders, and the distribution for the evaluation points.  These can be all different.  I believe we will often want to have the distribution used by the space to be the same as the eval_points, but the encoders are generally pretty different.  For example, I might have encoders that are all gaussians of the same height and width.
Can you write Python code for it?

fs = nengo.FunctionSpace(
        nengo.dists.Function(gaussian,
                             mag=nengo.dists.Uniform(0, 1),
                             mean=nengo.dists.Uniform(-1, 1),
                             sd=nengo.dists.Uniform(0.1, 0.3)),
        n_basis=20)

model = nengo.Network()
with model:
    stim = nengo.Node(fs.project(gaussian(mag=1, mean=0.5, sd=0.1)))
    ens = nengo.Ensemble(n_neurons=500, dimensions=fs.n_basis)
    ens.encoders = fs.project(
                        nengo.dists.Function(gaussian,
                        mean=nengo.dists.Uniform(-1, 1),
                        sd=0.1,
                        mag=1))
    ens.eval_points = fs.project(fs.space)
    nengo.Connection(stim, ens)
    probe_func = nengo.Probe(ens, synapse=0.03)
sim = nengo.Simulator(model)
sim.run(0.2)
pylab.plot(domain, fs.reconstruct(sim.data[probe_func][-1]))
pylab.figure()
pylab.imshow(fs.reconstruct(sim.data[probe_func]), extent=(-1,1,0.2,0), aspect=10.0)
pylab.ylabel('time')
pylab.show()


I am working on the file "Function Space description.ipynb".
The first task is:
Nengo GUI integration
There are two useful tools that help for doing stuff with nengo_gui.  For plotting the function, there is an fs.make_plot_node function.  For input, there is an fs.make_stimulus_node that gives you a node that'll let you control the variables passed in to the underlying function.  For example:
Can you write Python code for it?

model = nengo.Network()
with model:
    ens = nengo.Ensemble(n_neurons=500, dimensions=fs.n_basis)
    ens.encoders = fs.project(fs.space)
    ens.eval_points = fs.project(fs.space)

    # input
    stim = fs.make_stimulus_node(gaussian, 3)
    nengo.Connection(stim, ens)
    stim_control = nengo.Node([1, 0, 0.2])
    nengo.Connection(stim_control, stim)

    #output
    plot = fs.make_plot_node(domain=domain)
    nengo.Connection(ens, plot, synapse=0.1)

from nengo_gui.ipython import IPythonViz
IPythonViz(model, cfg='funcspace.cfg')


I am working on the file "Function Space description.ipynb".
The first task is:
The output display support multiple plots overlaid on top of each other, and has configurable number of points drawn
Can you write Python code for it?

model = nengo.Network()
with model:
    ens = nengo.Ensemble(n_neurons=500, dimensions=fs.n_basis)
    ens.encoders = fs.project(space)
    ens.eval_points = fs.project(fs.space)

    # input
    stim = fs.make_stimulus_node(gaussian, 3)
    nengo.Connection(stim, ens)
    stim_control = nengo.Node([1, 0, 0.2])
    nengo.Connection(stim_control, stim)

    #output
    plot = fs.make_plot_node(domain=domain, n_pts=50, lines=2)
    nengo.Connection(ens, plot[:fs.n_basis], synapse=0.1)
    nengo.Connection(stim, plot[fs.n_basis:], synapse=0.1)

from nengo_gui.ipython import IPythonViz
IPythonViz(model, cfg='funcspace2.cfg')


I am working on the file "Function Space description.ipynb".
The first task is:
The problem with the naive approach
It's fine and correct, but it's slow.  Why?  Well, think about what a Connection is doing now.  In a normal Nengo connection, we have N neurons and D dimensions, and we get this huge speedup because instead of an NxN connection weight matrix, we have an NxD decoder matrix and a DxN encoder matrix.
But in this situation, D starts being bigger than N!  In theory, we can let D go to infinity, and all of a sudden all of our nice encoder and decoder matrices explode up to ridiculous sizes.
Exploiting redundancy
So, what do we do instead?  Well, let's take advantage of the fact that those functions we are representing have tons of redundancy.  What we're going to do is to find a basis space for those functions such that we only have to represent a smaller number of dimensions.  This is done via SVD and taken care of by the new nengo.FunctionSpace object.
Can you write Python code for it?

fs = nengo.FunctionSpace(
        nengo.dists.Function(gaussian,
                             mag=nengo.dists.Uniform(0, 1),
                             mean=nengo.dists.Uniform(-1, 1),
                             sd=nengo.dists.Uniform(0.1, 0.3)),
        n_basis=20)


I am working on the file "Function Space description.ipynb".
The first task is:
To define a function space, we need a bunch of different functions: for example, gaussians at different means and standard deviations and magnitudes.  We could generate this by randomly generating it ourselves:
Can you write Python code for it?

space = []
for i in range(100):
    space.append(gaussian(mag=np.random.uniform(0,1),
                          mean=np.random.uniform(-1,1),
                          sd=np.random.uniform(0.1, 0.3)))
pylab.plot(domain, np.array(space).T)
pylab.show()


I am working on the file "Function.ipynb".
The first task is:
Lambda Function
단일문으로 표현되는 익명함수
익명함수란 이름이 없는 구현체만 존재하는 간단한 함수를 의미
코드 상에서 한번만 사용되는 기능이 있을 때, 굳이 함수로 만들지 않고 1회성으로 만들어서 쓸 때 사용
Can you write Python code for it?

def square(x):
    return x ** 2

#lambda x : x ** 2

square2 = lambda x : x ** 2

print square(4), square2(4)

add3 = lambda x, y : x + y
print add3(20, 30)


I am working on the file "Function.ipynb".
The first task is:
Recursive Function

재귀함수
내부적으로 자기 자신을 호출함
점화식으로 표현되는 모든 수식을 간결히 표현할 수 있음
종료조건이 꼭! 필요함 (명시하지 않으면 무한 호출반복)
코드는 간결하나, 호출에 따른 오버헤드가 큼)
Can you write Python code for it?

def factorial(n):
    mul = 1
    for i in range(2, n+1):
        mul *= i
    return mul

assert(factorial(5) == 120)

#fibonacci sequence

def fibonacci(n):
    fibo = [1, 1]
    for i in range(2, n):
        fibo.append(fibo[i-1] + fibo[i-2])

    return fibo

print fibonacci(5)

#fibonacci recursive

def recursive_fibonacci(n):
    if n == 1 or n ==2:
        return 1
    return recursive_fibonacci(n-1) + recursive_fibonacci(n-2)
print recursive_fibonacci(5)


I am working on the file "Function.ipynb".
The first task is:
e.g)문자열 포맷 함수
Can you write Python code for it?

str1 = 'Hi my name is {} and the weather is {}'.format('younghyo', 'clear')
print str1


I am working on the file "Function.ipynb".
The first task is:
return
함수의 종료 명시
값이 함께 오는 경우는 값을 호출한 곳으로 반환하면서 종료
리턴을 명시하지 않는 경우, 함수의 마지막라인이 실행된 후 리턴
Can you write Python code for it?

def simple():
    return 1

a = simple()
print a

def just_return():
    a = 1
    b = 1
    return

c = just_return()
print c

def add_sub(a, b):
    return a+b, a-b

add_sub(9, 3)

def c_to_f(c):
    f = 1.8 * c + 32
    return f

print c_to_f(36.5)

def odd_sum(nums):
    odd_sum = 0
    for i in nums:
        if i % 2 == 1:
            odd_sum += i
    return odd_sum

def odd_sum2(nums):
    return sum([i for i in nums if i % 2 == 1])

a = [1, 2, 3, 7, 8, 10, 11, 13, 15, 29, 100, 201, 300]

print odd_sum(a)
print odd_sum2(a)

def max_val(nums):
    mval = nums[0]
    for i in nums:
        if mval < i:
            mval = i
    return mval

print max_val(a)

def factorial(n):
    mul = 1
    for i in range(1, n + 1):
        mul *= i
    return mul

factorial(6)


I am working on the file "Functional Programming in Python.ipynb".
The first task is:
Reduce
reduce performs computations on a sequence and returns a single result.
reduce(function, sequence)
For example:
python
from functools import reduce
product = reduce((lambda x, y: x * y), [1, 2, 3, 4])
print(product)
At each step the function passed to reduce is passed two arguments, the current reduced value and the next value from the sequence. By default, the first item in the sequence is used as the initial value of the reduction value.
Can you write Python code for it?


I am working on the file "Functions At Work.ipynb".
The first task is:
ASCII is the precursor to Unicode, a vast lookup table containing a huge number of important symbols, such as world alphabets and ideograms, special symbols (chess pieces, playing cards), emoji.
In some romantic science fiction we might imagine sharing Unicode with distant aliens, as if this were a helpful puzzle piece.  When talking with humans though, it really helps to share a database.
We take all 26 letters from lowercase ascii, a canned (pre-stored) string within the string module, part of the Standard Library, plus the space (space bar, ASCII character 32), and turn that into a list of 27 elements.
Feed two such lists, one a clone of the other, but reshuffled, to the zip type, making pairs, in turn feeding all those pairs in one object to dict, a type which knows how to deal with precisely that format.
Lets look at that same creation process with some integers instead:
Can you write Python code for it?

# feel free to keep re-running this, a different permutation every time!
seq1 = list(range(21))
seq2 = seq1.copy()
shuffle(seq2)
the_zip = tuple(zip(seq1, seq2))  # actually a tuple already
the_zip


I am working on the file "Functions At Work.ipynb".
The first task is:
Although the above data structure is a tuple of tuples, its not in cyclic notation.  Rather, its a step on the way to becoming a dict type object.
Lets feed the above to the native the dict type, an essential job of which is to spawn instances of its own type, given compatible input.  A tuple of pairs is a form the dict type knows how to eat, because the meaning is obvious.  We want each pair to become an element in our dict.
To that end, in its capacity as a dict-maker, the dict type is a callable, as we have seen, taking in the raw material needed for the constitution of new such objects, new dict objects in this case:
Can you write Python code for it?

perm_ints = dict(the_zip) # the_zip is by now a tuple of tuples, which dict will digest
perm_ints


I am working on the file "Functions At Work.ipynb".
The first task is:
Calling types
Calling any type with no arguments logically suggests an empty instance of whatever we're creating, whatever Something, e.g. d = dict() would be "empty dictionary" and the_list = list() should start an empty list.
How about int(); float(); Decimal()?  These would produce their respective versions of zero, one would think, and they do.
Lets do some testing, starting with collection types (objects that hold or contain other objects):
Can you write Python code for it?

the_list = list()
nada_dict = dict(the_list)  # converting
empty_set = set()  # can't use {} as that means empty dict
empty_tuple = tuple()
print(the_list, nada_dict, empty_set, empty_tuple)


I am working on the file "Functions At Work.ipynb".
The first task is:
Don't let the above code intimidate you.
This is about the right size for a function, and does just the right amount of work.
Note that it delegates to the methods our high level data structures bring to the table, such as pop and append.  We get a lot done in just a few lines.
Lets look at these data structures in action, in isolation, before coming back to this function to see exactly what it's about.
Looking at Data Structures
All list type objects know how to append, as a part of their heritage.  Lists also respond to square brackets, or "get item" syntax.
Can you write Python code for it?

zoo = ["bear", "tiger", "lion"]
zoo.append("scorpion")
print(zoo[0], zoo[3]) # print first and last


I am working on the file "Functions At Work.ipynb".
The first task is:
Excuse me?  That doesn't look scrambled at all?  The reason is subtle:  feeding permuation to cyclic above rendered it empty.  We operated upon "the_dict" and in turning out a corresponding tuple of tuples, consumed the object fed it.  The function worked on our only copy, on the original itself.
Can you write Python code for it?


I am working on the file "Functions At Work.ipynb".
The first task is:
Final thing:  I bet you might be wondering about a function that ate a tuple of tuples, some cyclic notation, and gave back the corresponding dictionary object.
This would be the inverse function of cyclic so we could call it inv_cyclic.  That's a great cliff-hanger for a next chapter wouldn't you think?  Feel free to try writing inv_cyclic.
Here's something to get you started:
Can you write Python code for it?

def inv_cyclic(cycles : tuple) -> dict:
    output = {} # empty dig
    pass # more code goes here
    return output


I am working on the file "Functions At Work.ipynb".
The first task is:
If we check for equality (==), we find the answer is False.
You may also compare values using >, <, =>, <=, and != (not equal).  Not all objects are "ordered" such that these operators would make sense.  When we define our own types, we'll have the choice whether to implement these comparators for them.
Can you write Python code for it?

str() == " "  # empty string equals space? (no way)


I am working on the file "Functions At Work.ipynb".
The first task is:
Let's do two things to improve the situation:
* generate permutations from a function, that takes a set of what to permute
* make sure the cyclic function doesn't mess with the passed-in original
Let's do first things first:
Can you write Python code for it?

def make_perm(incoming : set) -> dict:
    seq1 = list(incoming)
    seq2 = seq1.copy()
    shuffle(seq2)
    return dict(zip(seq1, seq2))

make_perm(set(range(10)))

make_perm(the_letters) # defined above as lowercase ascii letters plus space


I am working on the file "Functions At Work.ipynb".
The first task is:
Life is good.  As a final touch, we'd like to deciper or decrypt, what was encrypted, using the very same secret_key.  All encrypt( ) needs to do is reverse the secret key, giving what's called the inverse permutation, and call encrypt with that.
The original message magically reappears.
Lets make a new permutation for this test, so we don't confuse ourselves.
Can you write Python code for it?

cipher = make_perm(the_letters)
c = encrypt("hello alice this is bob lets hope eve does not decrypt this", cipher)

def decrypt(cyphertext : str, secret_key : dict) -> str:
    """
    Turn cyphertext into plaintext using ~self
    """
    reverse_P = {v: k for k, v in secret_key.items()}  # invert me!
    output = ""
    for c in cyphertext:
        output = output + reverse_P.get(c, c)
    return output

c

p = decrypt(c, cipher)

p


I am working on the file "Functions At Work.ipynb".
The first task is:
Notice the keys and values are the same elements.  Think of the values as simply the keys but in a different order.  We could say "monkey maps to bear" and "tiger maps to scorpion".
Now lets feed the above dict to the function and see what we get:
Can you write Python code for it?


I am working on the file "Functions At Work.ipynb".
The first task is:
Now lets get back to our function, wherein these data structures, each with a bag of tricks (methods) play a starring role.
In this function cyclic, we're "diasy chaining" through a dict, meaning each key points to a value, which value then becomes the key to a next value, and so on, until we loop back around to the first in the sequence, if we do (we must eventually because the dict is finite).
Sounds like a square dance of some kind.
Our input dict, what gets the ball rolling so to speak, is not just any dict.  Animals paired with ages would not work.  The docstring mentions expecting a "permutation" which you may think of as a set of objects paired with themselves in a different order.  We might call it a "scramble".  In the case of animals, a permuation might look like this...
Can you write Python code for it?

pairings = {"monkey":"bear", "tiger":"scorpion", "scorpion":"monkey", "bear":"tiger"}
print("Keys:", pairings.keys())
print("Values:", pairings.values())


I am working on the file "Functions At Work.ipynb".
The first task is:
Or just use the keyword del, for delete, if you don't have a need for the object in future:
Can you write Python code for it?

del ages['otter']  # delete this element
ages


I am working on the file "Functions At Work.ipynb".
The first task is:
Our function will work equally well on any permutation dict, as we're not depending on the type of our keys and/or values for this recipe to work.
In the one dict, permutation, we map letters to letters, in this other perm_ints, we maps ints to ints.
The cyclic function doesn't care about these details and works the same either way. Python is good at letting us generalize.
That doesn't mean cyclic is immune from crashing.  It's expecting a dict wherein every value is also a valid key.  Not every dict is like that.
Can you write Python code for it?


I am working on the file "Functions At Work.ipynb".
The first task is:
That's all working great, and now the second change, a new cyclic:
Can you write Python code for it?

def cyclic(the_perm : dict) -> tuple:
    """
    cyclic notation, a compact view of a permutation
    """
    output = []      # we'll make this a tuple before returning

    the_dict = the_perm.copy() # protect the original from exhaustion

    while the_dict:
        start = tuple(the_dict.keys())[0]
        the_cycle = [start]
        the_next = the_dict.pop(start) # take it out of the_dict
        while the_next != start:       # keep going until we cycle
            the_cycle.append(the_next) # following the bread crumb trail...
            the_next = the_dict.pop(the_next)  # look up what we're paired with
        output.append(tuple(the_cycle)) # we've got a cycle, append to output
    return tuple(output) # giving back a tuple object, job complete

original = make_perm(the_letters)  # the_letters is a string

cyclic_view = cyclic(original)

cyclic_view

original


I am working on the file "Functions At Work.ipynb".
The first task is:
The empty string doesn't show up, because it's empty.  Spaces wouldn't show up either, when printed.  However an empty string and single space are different.
The former has a length of 0, the latter a length of 1.
Can you write Python code for it?


I am working on the file "Functions At Work.ipynb".
The first task is:
The resulting tuple is read left to right and says "monkey maps to bear" and "bear to tiger" and "tiger to scorpion". The last element wraps around to the first i.e. "scorpion maps to monkey".  That all corresponds to what we said in the original dict.
Instead of animals, now, lets think in terms of individual letters instead.
If letter 'd' maps to letter 'd' then ('d',) is the cyclic subgroup (an inner tuple); whereas if 'd' maps to 'a', 'a' back to 'd', then ('d' 'a') or ('a' , 'd') says it all.  Any permutation may be expressed as a tuple of such tuples.
Pick a letter, any letter (in our set).  It maps to something, which maps to something else, and so on, until you come back around.  We'll always be able to express a permutation in this form.
Lets run the above function on a permutation mapping all lowercase ASCII letters (US keyboard), as well as space, to themselves.
First, lets create the input dictionary:
Can you write Python code for it?

from string import ascii_lowercase # a string
from random import shuffle
the_letters = list(ascii_lowercase) + [" "] # lowercase plus space, as a list
shuffled = the_letters.copy() # copy me
shuffle(shuffled)             # works "in place" i.e. None returned
permutation = dict(zip(the_letters, shuffled)) # make pairs, each letter w/ random other
print("ASCII", ascii_lowercase)
print("Coding Key:", permutation)


I am working on the file "Functions At Work.ipynb".
The first task is:
You may be wondering, considering the above pipeline, which went from zip type, to tuple, then to dict, whether a dict might eat a zip type directly.  Lets try that...
Can you write Python code for it?

cipher = dict(zip(seq1, seq2))  # will a dict eat a zip?
cipher


I am working on the file "Functions and Methods Homework.ipynb".
The first task is:
Write a Python function that accepts a string and calculate the number of upper case letters and lower case letters.
Sample String : 'Hello Mr. Rogers, how are you this fine Tuesday?'
Expected Output :
No. of Upper case characters : 4
No. of Lower case Characters : 33

If you feel ambitious, explore the Collections module to solve this problem!
Can you write Python code for it?

import collections
import string

def up_low(s):
    c = collections.Counter(s)
    lower = 0
    upper = 0
    for key, count in c.items():
        if key in list(string.ascii_lowercase):
            lower += count
        elif key in list(string.ascii_uppercase):
            upper += count

    print("No. of Uppercase: {up}\nNo. of Lowercase: {low}".format(up=upper, low=lower))

up_low('Hello Mr. Rogers, how are you this fine Tuesday?')

import string
type(string.ascii_lowercase.split())


I am working on the file "Functions and Methods Homework.ipynb".
The first task is:
Write a Python function that checks whether a passed string is palindrome or not.
Note: A palindrome is word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run.
Can you write Python code for it?

def palindrome(s):
    return s == s[::-1]

palindrome('helleh')


I am working on the file "Functions and Methods Homework.ipynb".
The first task is:
Write a Python function that takes a list and returns a new list with unique elements of the first list.
Sample List : [1,1,1,1,2,2,3,3,3,3,4,5]
Unique List : [1, 2, 3, 4, 5]
Can you write Python code for it?

def unique_list(l):
    return list(set(l))

unique_list([1,1,1,1,2,2,3,3,3,3,4,5])


I am working on the file "Functions and Methods/Lambda Expressions.ipynb".
The first task is:
As we can see, the lambda expressions returns the number we expected. How do we return a True or False value? Note: we can't use if statements or returns. This limits the power of the lambda. However, we can have lambda expressions return True or False, still. Observe the following syntax.
Can you write Python code for it?

even_odd = lambda x: True if x % 2 == 0 else False

even_odd(9)


I am working on the file "Functions and Methods/Lambda Expressions.ipynb".
The first task is:
Somewhat scrappy, but let's just say it's pretty close to looking like an actual list. Strings are not mutable so we can't just add a bracket at the beginning or right at the end. There are certain ways to make this possible, however. Operations like this are better suited for functions, anyways.
Lambdas as Parameters
We have a function known as sorted(). This function accepts an iterable, and a key (and reverse, talked below). The iterable alone acts as expected.
Can you write Python code for it?


I am working on the file "Functions and Methods/Methods.ipynb".
The first task is:
Dictionary Methods (Recap)
Can you write Python code for it?

# These are Dictionary Methods
my_dict = {"Sally":19, "Robert":[1, 48, 29], "Wallace":0}

keys = list(my_dict.keys())
values = list(my_dict.values())
popped_value = my_dict.pop("Robert")
print(popped_key)


I am working on the file "Functions and Methods/Methods.ipynb".
The first task is:
List Methods (Recap)
Can you write Python code for it?

# These are examples of list methods.
my_list = ["Bonet", "Triggly", "Ross"]

my_list.append("Triggly")
print("After appending {}".format(my_list))
another_list = my_list.copy()
print("After copying {}".format(another_list))
my_list.clear()
print("After clearing {}".format(my_list))
another_list.insert(2, "Triggly")
print("After insertion: {}".format(another_list))
the_count = another_list.count("Triggly")
print("Count of specific word: {}".format(the_count))


I am working on the file "Functions.ipynb".
The first task is:
Arbitrary Arguments
Python allows unknown number of arguments to be passed to a function through arbitrary arguments. Arbitrary arguments are declared by placing an asterix (*) in front of the parameter name
Can you write Python code for it?

def greet(*names):
    '''This function greets all with a Hello'''
    for name in names:
        print('Hello ',name)

greet('John','Keith','Brian','Jose')


I am working on the file "Functions.ipynb".
The first task is:
Default Arguments
Default arguments will be used if the argument value is not passed to the function. If a value is passed then it will overwrite the default value.
Can you write Python code for it?

def greet(name,msg='Good Evening!'):
    '''Simple greet function with name and message arguments'''
    print("Hello " + name + ', ' + msg)

greet('John')


I am working on the file "Functions.ipynb".
The first task is:
Example 2
Can you write Python code for it?

# Function with arguments
def greet(name):
    '''Simple Greet Function with arguments'''
    print('Hello ', name)

greet('John')

# printing the doc string
print(greet.__doc__)


I am working on the file "Functions.ipynb".
The first task is:
Function Arguments
Example 1
Can you write Python code for it?

def greet(name,msg):
    '''Simple greet function with name and message arguments'''
    print("Hello " + name + ', ' + msg)

greet('John','Good Morning!')

# this will generate error since we missed one argument
greet('John')


I am working on the file "Functions.ipynb".
The first task is:
Global Variable
Variables declared inside the function are not available outside. The following example will generate an error.
Can you write Python code for it?

def myfunc():
    y = 5
    print('Value inside the function ',y)

myfunc()
print('Value outside the function',y)


I am working on the file "Functions.ipynb".
The first task is:
However please note that having a positional argument after keyword argument will result into errors. For example the following example will generate an error.
Can you write Python code for it?


I am working on the file "Functions.ipynb".
The first task is:
Keyword Arguments
Python allows functions to be called using keyword arguments. When we call functions in this way, the order of the arguments can be changed.
Can you write Python code for it?

def greet(name,msg='Good Evening!',salute='Mr'):
    '''Simple greet function with name and message arguments'''
    print("Hello " + salute + '.' + name + ', ' + msg)

# keyword arguments
greet(name="Jack",msg="How are you?")

# keyword arguments - out of order
greet(msg='How do you do?',name="Brian")

# mix of keyword and positional arguments
greet("Jill",salute='Ms',msg="Good to see you.")


I am working on the file "Functions.ipynb".
The first task is:
Later if we decide to extend the multiply function to accept n number of arguments, we need to use the *args feature
Can you write Python code for it?

def multiply(*args):
    x = 1
    for num in args:
        x *= num
    return(x)

print(multiply(2,4))
print(multiply(2,4,5))
print(multiply(3,5,8,9,10))


I am working on the file "Functions.ipynb".
The first task is:
One rule for default arguments is that once an argument has a default value then all the arguments to the right of it must also have default values. The following example will produce an error.
Can you write Python code for it?

def greet(name,msg='Good Evening!',salute):
    '''Simple greet function with name and message arguments'''
    print("Hello " + salute + '.' + name + ', ' + msg)

greet('John','Good Evening','Mr')

def greet(name,msg='Good Evening!',salute='Mr'):
    '''Simple greet function with name and message arguments'''
    print("Hello " + salute + '.' + name + ', ' + msg)

greet('John','Good Evening','Mrs')


I am working on the file "Functions.ipynb".
The first task is:
Ordering Arguments
When ordering arguments within a function or function call, arguments need to occur in a particular order:

Formal positional arguments
*args
Keyword arguments
**kwargs
Can you write Python code for it?

def some_args(arg_1, arg_2, arg_3):
    print("arg_1:", arg_1)
    print("arg_2:", arg_2)
    print("arg_3:", arg_3)

args = ("Sammy", "Casey", "Alex")
some_args(*args)

def some_args(arg_1, arg_2, arg_3):
    print("arg_1:", arg_1)
    print("arg_2:", arg_2)
    print("arg_3:", arg_3)

my_list = [2, 3]
some_args(1, *my_list)

def some_kwargs(kwarg_1, kwarg_2, kwarg_3):
    print("kwarg_1:", kwarg_1)
    print("kwarg_2:", kwarg_2)
    print("kwarg_3:", kwarg_3)

kwargs = {"kwarg_1": "Val", "kwarg_2": "Harper", "kwarg_3": "Remy"}
some_kwargs(**kwargs)


I am working on the file "Functions.ipynb".
The first task is:
Recursive Functions
Function that calls itself is called recursive function.
While the recursive functions are clean and elegant care must be taken as it might take up lot of memory and time and it is also hard to debug.
Please note the recursive function must also have a base condition that stops the recursion otherwise the function calls itself indefinitely.
Can you write Python code for it?

def factorial(num):
    if(num <= 0):
        return 0
    elif(num == 1):
        return 1
    else:
        return(num * factorial(num-1))

num = 4
print("Factorial of number ",num," is ",factorial(num))


I am working on the file "Functions.ipynb".
The first task is:
Scope and Lifetime of Variables
Variables in python has local scope which means parameters and variables defined inside the function is not visible from outside.
Lifetime of a variable is how long the variable exists in the memory. Lifetime of variables defined inside the function exists as long as the function executes. They are destroyed once the function is returned.
Can you write Python code for it?

def myfunc():
    x = 5
    print('Value inside the function ',x)

x = 10
myfunc()
print('Value outside the function',x)


I am working on the file "Functions.ipynb".
The first task is:
Using *args and ***kwargs in functions
When programming, you may not be aware of all the possible use cases of your code, and may want to offer more options for future programmers working with the module, or for users interacting with the code. We can pass a variable number of arguments to a function by using *args and **kwargs in our code.
Using *args
Can you write Python code for it?

def multiply(x,y):
    return x * y

multiply(2,4)


I am working on the file "Functions.ipynb".
The first task is:
You have to use the global keyword for variables if you want to use those variables declared outside the function inside the function
Can you write Python code for it?

def myfunc():
    global z
    z = 5
    print('Value inside the function ',z)

#z = 10
myfunc()
print('Value outside the function',z)


I am working on the file "Functions_Fasta_Input_to_Structure_and_Graph_modifing...-submit.ipynb".
The first task is:
Experiment
Can you write Python code for it?

#generating the graph
#seq,seqs are Not correct they do Not take the zipped output

zip_head_seqs= _sequeceWrapper(file_path)
print ('zip_head_seqs here', zip_head_seqs)
for i, seq in enumerate(zip_head_seqs):
    heads = seq[0]
    seq1 = seq[1]
    mode = 'RNAfold'
    head, seq, struc =_fold(seq)
    G = _make_graph(head, seq, struc)
    display.draw_graph(G, node_size=180, font_size=9, node_border=True, prog='neato')


I am working on the file "Functions_Fasta_Input_to_Structure_and_Graph_modifing...-submit.ipynb".
The first task is:
Get the sequence structure
Can you write Python code for it?

#call RNAfold to get the sequence structure
def _get_sequence_structure(seqs):
    if mode == 'RNAfold':
        return _rnafold_wrapper(seqs)
    else:
        raise Exception('Not known: %s'% self.mode)

def _rnafold_wrapper(sequence):
    head = sequence[0]
    seq = sequence[1].split()[0]
    flags='--noPS'
    cmd = 'echo "%s" | RNAfold %s' % (seq, flags)
    out = sp.check_output(cmd, shell=True)
    #print out
    text = out.strip().split('\n')
    print ('text:', text)
    seq = text[0]
    struc = text[1].split()[0]
    return head, seq, struc


I am working on the file "Fundamentals.ipynb".
The first task is:
Choice of two projections
It turns out our implementation has a nasty deficiency.
Can you write Python code for it?

qr_test(qr_householder1, numpy.eye(1))

qr_test(qr_householder1, numpy.eye(3,2))


I am working on the file "Fundamentals.ipynb".
The first task is:
Classical Gram-Schmidt is highly parallel, but unstable, as evidenced by the lack of orthogonality in $Q$.
Right-looking algorithms
The implementations above have been "left-looking"; when working on column $i$, we compare it only to columns to the left (i.e., $j < i$).  We can reorder the algorithm to look to the right by projecting $q_i$ out of all columns $j > i$.  This algorithm is stable while being just as parallel as gram_schmidt_classical.
Can you write Python code for it?

def gram_schmidt_modified(X):
    Q = X.copy()
    R = numpy.zeros((len(X.T), len(X.T)))
    for i in range(len(Q.T)):
        R[i,i] = numpy.linalg.norm(Q[:,i])
        Q[:,i] /= R[i,i]
        R[i,i+1:] = Q[:,i+1:].T.dot(Q[:,i])
        Q[:,i+1:] -= numpy.outer(Q[:,i], R[i,i+1:])
    return Q, R

qr_test(gram_schmidt_modified, V)


I am working on the file "Fundamentals.ipynb".
The first task is:
Cost of Householder factorization
The dominant cost comes from the line
Python
    R[i:,i:] -= 2 * numpy.outer(v, v.dot(R[i:,i:]))
were R[i:,i:] is an $(m-i)\times(n-i)$ matrix.
This line performs $2(m-i)(n-i)$ operations in v.dot(R[i:,i:]), another $(m-i)(n-i)$ in the "outer" product and again in subtraction.  As written, multiplication by 2 would be another $(m-i)(n-i)$ operations, but is only $m-i$ operations if we rewrite as
Python
    w = 2*v
    R[i:,i:] -= numpy.outer(w, v.dot(R[i:,i:]))
in which case the leading order cost is $4(m-i)(n-i)$.  To compute the total cost, we need to sum over all columns $i$,
$$\begin{split} \sum_{i=1}^n 4(m-i)(n-i) &= 4 \Big[ \sum_{i=1}^n (m-n)(n-i) + \sum_{i=1}^n (n-i)^2 \Big] \
&= 4 (m-n) \sum_{i=1}^n i + 4 \sum_{i=1}^n i^2 \
&\approx 2 (m-n) n^2 + 4 n^3/3 \
&= 2 m n^2 - \frac 2 3 n^3 .
\end{split}$$
Recall that Gram-Schmidt QR cost $2 m n^2$, so Householder costs about the same when $m \gg n$ and is markedly less expensive when $m \approx n$.
Least squares and the normal equations
A least squares problem takes the form: given an $m\times n$ matrix $A$ ($m \ge n$), find $x$ such that
$$ \lVert Ax - b \rVert $$
is minimized.  If $A$ is square and full rank, then this minimizer will satisfy $A x - b = 0$, but that is not the case in general because $b$ is not in the range of $A$.
The residual $A x - b$ must be orthogonal to the range of $A$.

Is this the same as saying $A^T (A x - b) = 0$?
If $QR = A$, is it the same as $Q^T (A x - b) = 0$?

In HW2, we showed that $QQ^T$ is an orthogonal projector onto the range of $Q$.  If $QR = A$,
$$ QQ^T (A x - b) = QQ^T(Q R x - b) = Q (Q^T Q) R x - QQ^T b = QR x - QQ^T b = A x - QQ^T b . $$
So if $b$ is in the range of $A$, we can solve $A x = b$.  If not, we need only orthogonally project $b$ into the range of $A$.
Solution by QR (Householder)
Solve $R x = Q^T b$.

QR factorization costs $2 m n^2 - \frac 2 3 n^3$ operations and is done once per matrix $A$.
Computing $Q^T b$ costs $4 (m-n)n + 2 n^2 = 4 mn - 2n^2$ (using the...
Can you write Python code for it?

# Test accuracy of solver for an ill-conditioned square matrix

x = numpy.linspace(-1,1,19)
A = numpy.vander(x)
print('cond(A) = ',numpy.linalg.cond(A))
Q, R = numpy.linalg.qr(A)
print('cond(R^{-1} Q^T A) =', numpy.linalg.cond(numpy.linalg.solve(R, Q.T.dot(A))))
L = numpy.linalg.cholesky(A.T.dot(A))
print('cond(L^{-T} L^{-1} A^T A) =', numpy.linalg.cond(numpy.linalg.solve(L.T, numpy.linalg.solve(L, A.T.dot(A)))))


I am working on the file "Fundamentals.ipynb".
The first task is:
Gram-Schmidt Orthogonalization
Given a collection of vectors (columns of a matrix), we can find an orthogonal basis by applying the above procedure one column at a time.
Can you write Python code for it?

def gram_schmidt_naive(X):
    Q = numpy.zeros_like(X)
    R = numpy.zeros((len(X.T),len(X.T)))
    for i in range(len(Q.T)):
        v = X[:,i].copy()
        for j in range(i):
            r = v.dot(Q[:,j])
            R[j,i] = r
            v -= r * Q[:,j]    # "modified Gram-Schmidt" - remove each component before next dot product
        R[i,i] = numpy.linalg.norm(v)
        Q[:,i] = v / R[i,i]
    return Q, R

x = numpy.linspace(-1,1,50)
k = 6
A = numpy.vander(x, k, increasing=True)
Q, R = gram_schmidt_naive(A)
print(numpy.linalg.norm(Q.T.dot(Q) - numpy.eye(k)))
print(numpy.linalg.norm(Q.dot(R)-A))
pyplot.figure()
pyplot.plot(x, Q)
A.shape

Q, R = gram_schmidt_naive(numpy.vander(x, 4, increasing=True))
pyplot.figure()
pyplot.plot(x, Q)


I am working on the file "Fundamentals.ipynb".
The first task is:
Householder triangularization
Gram-Schmidt methods perform triangular transformations to build an orthogonal matrix.  As we have seen, $X = QR$ is satisfied accurately, but $Q$ may not be orthogonal when $X$ is ill-conditioned.  Householder triangularization instead applies a sequence of orthogonal transformations to build a triangular matrix.
$$ \underbrace{Q_{n-1} \dotsb Q_0}_{Q^T} A = R $$
The structure of the algorithm is
$$ \underbrace{\begin{bmatrix} * & * & * \ * & * & * \ * & * & * \ * & * & * \ * & * & * \ \end{bmatrix}}{A} \to
\underbrace{\begin{bmatrix} * & * & * \ 0 & * & * \ 0 & * & * \ 0 & * & * \ 0 & * & * \ \end{bmatrix}}{Q_0 A} \to
\underbrace{\begin{bmatrix} * & * & * \ 0 & * & * \ 0 & 0 & * \ 0 & 0 & * \ 0 & 0 & * \ \end{bmatrix}}{Q_1 Q_0 A} \to
\underbrace{\begin{bmatrix} * & * & * \ 0 & * & * \ 0 & 0 & * \ 0 & 0 & 0 \ 0 & 0 & 0 \ \end{bmatrix}}{Q_2 Q_1 Q_0 A}
$$
where the elementary orthogonal matrices $Q_i$ chosen to introduce zeros below the diagonal in the $i$th column of $R$.
Each of these transformations will have the form
$$Q_i = \begin{bmatrix} I_i & 0 \ 0 & F \end{bmatrix}$$
where $F$ is a "reflection" that achieves
$$ F x = \begin{bmatrix} \lVert x \rVert \ 0 \ 0 \ \vdots \end{bmatrix} $$
where $x$ is the column of $R$ from the diagonal down.
This transformation is a reflection across a plane with normal $v = Fx - x = \lVert x \rVert e_1 - x$.

The reflection, as depected above by Trefethen and Bau (1999) can be written $F = I - 2 \frac{v v^T}{v^T v}$.
Can you write Python code for it?

def householder_Q_times(V, x):
    """Apply orthogonal matrix represented as list of Householder reflectors"""
    y = x.copy()
    for i in reversed(range(len(V))):
        y[i:] -= 2 * V[i] * V[i].dot(y[i:])
    return y

def qr_householder1(A):
    "Compute QR factorization using naive Householder reflection"
    m, n = A.shape
    R = A.copy()
    V = []
    for i in range(n):
        x = R[i:,i]
        v = -x
        v[0] += numpy.linalg.norm(x)
        v = v/numpy.linalg.norm(v)     # Normalized reflector plane
        R[i:,i:] -= 2 * numpy.outer(v, v.dot(R[i:,i:]))
        V.append(v)                    # Storing reflectors is equivalent to storing orthogonal matrix
    Q = numpy.eye(m, n)
    for i in range(n):
        Q[:,i] = householder_Q_times(V, Q[:,i])
    return Q, numpy.triu(R[:n,:])

qr_test(qr_householder1, numpy.array([[1.,2],[3,4],[5,6]]))

qr_test(qr_householder1, V)
qr_test(numpy.linalg.qr, V)


I am working on the file "Fundamentals.ipynb".
The first task is:
How accurate is this approximation?
The $u_{10}(x)$ line here is visually on top of the $u_{80}(x)$ line so we might reasonably say that both should be about the same accuracy.  Let's see what happens when we compare the norm of the vector $\exp(x)$ with the approximations $u_n(x)$.
Can you write Python code for it?

pyplot.figure()
for n in (10, 20, 40, 80):
    x, y = u_n(n)
    pyplot.plot(n, numpy.linalg.norm(y - numpy.exp(x)), 'o')


I am working on the file "Fundamentals.ipynb".
The first task is:
Inner products and orthogonality
The inner product
$$ x^T y = \sum_i x_i y_i $$
of vectors (or columns of a matrix) tell us about their magnitude and about the angle.
The norm is induced by the inner product,
$$ \lVert x \rVert = \sqrt{x^T x} $$
and the angle $\theta$ is defined by
$$ \cos \theta = \frac{x^T y}{\lVert x \rVert \, \lVert y \rVert} . $$
Inner products are bilinear, which means that they satisfy some convenient algebraic properties
$$ \begin{split}
(x + y)^T z &= x^T z + y^T z \
x^T (y + z) &= x^T y + x^T z \
(\alpha x)^T (\beta y) &= \alpha \beta x^T y \
\end{split} . $$
The pairwise inner products between two sets of vectors can be expressed by collecting the sets as columns in matrices and writing $A = X^T Y$ where $A_{i,j} = x_i^T y_j$.
It follows from this definition that
$$ (X^T Y)^T = Y^T X .$$
Orthogonal matrices
If $x^T y = 0$ then we say $x$ and $y$ are orthogonal (or "$x$ is orthogonal to $y$").
A vector is said to be normalized if $\lVert x \rVert = 1$.
If $x$ is orthogonal to $y$ and $\lVert x \rVert = \lVert y \rVert = 1$ then we say $x$ and $y$ are orthonormal.
A matrix with orthonormal columns is said to be an orthogonal matrix.
We typically use $Q$ or $U$ and $V$ for matrices that are known/constructed to be orthogonal.
Orthogonal matrices are always full rank -- the columns are linearly independent.
The inverse of a square orthogonal matrix is its transpose:
$$ Q^T Q = Q Q^T = I . $$
Orthogonal matrices are a powerful building block for robust numerical algorithms.
Can you write Python code for it?

# Make some polynomials
x = numpy.linspace(-1,1)
A = numpy.vander(x, 4)
q0 = A.dot(numpy.array([0,0,0,.5]))  # .5
q1 = A.dot(numpy.array([0,0,1,0]))  # x
q2 = A.dot(numpy.array([0,1,0,0]))  # x^2

pyplot.figure()
pyplot.plot(x, numpy.array([q0, q1, q2]).T)

x

# Inner products of even and odd functions

q0 = q0 / numpy.linalg.norm(q0)
q1.dot(q0), q2.dot(q0), q2.dot(q1)

q0

# What is the constant component of q2?

pyplot.figure()
pyplot.plot(x, q2.dot(q0)*q0)

# Let's project that away so that q2 is orthogonal to q0

q2 = q2 - q2.dot(q0)*q0

Q = numpy.array([q0, q1, q2]).T
print(Q.T.dot(Q))
pyplot.figure()
pyplot.plot(x, Q)


I am working on the file "Fundamentals.ipynb".
The first task is:
Relative condition number
Given the relative nature of floating point arithmetic, it is more useful to discuss relative condition number,
$$ \kappa = \max_{\delta x} \frac{|\delta f|/|f|}{|\delta x|/|x|}
= \max_{\delta x} \Big[ \frac{|\delta f|/|\delta x|}{|f| / |x|} \Big] $$
or, if $f$ is differentiable,
$$ \kappa = \max_{\delta x} |f'(x)| \frac{|x|}{|f|} . $$
How does a condition number get big?
Take-home message
The relative accuracy of the best-case algorithm will not be reliably better than $\epsilon_{\text{machine}}$ times the condition number.
$$ \max_{\delta x} \frac{|\delta f|}{|f|} \ge \kappa \cdot \epsilon_{\text{machine}} $$
Can you write Python code for it?

numpy.log(1 + 1e-10)  - numpy.log1p(1e-10)


I am working on the file "Fundamentals.ipynb".
The first task is:
Stability
We use the notation $\tilde f(x)$ to mean a numerical algorithm for approximating $f(x)$.  Additionally, $\tilde x = x (1 + \epsilon)$ is some "good" approximation of the exact input $x$.
(Forward) Stability
"nearly the right answer to nearly the right question"
$$ \frac{\lvert \tilde f(x) - f(\tilde x) \rvert}{| f(\tilde x) |} \in O(\epsilon_{\text{machine}}) $$
for some $\tilde x$ that is close to $x$
Backward Stability
"exactly the right answer to nearly the right question"
$$ \tilde f(x) = f(\tilde x) $$
for some $\tilde x$ that is close to $x$

Every backward stable algorithm is stable.
Not every stable algorithm is backward stable.

Example: $\tilde f(x) = \text{float}(x) + 1$
The algorithm computes
$$\tilde f(x) = \text{float}(x) \oplus 1 = x(1+\epsilon_1) + 1 = (x + 1 + x\epsilon_1)(1 + \epsilon_2) $$
and we can express any $\tilde x = x(1 + \epsilon_3)$.
To see if if the algorithm is stable, we compute
$$ \frac{\tilde f(x) - f(\tilde x)}{|f(\tilde x)|} = \frac{(x + 1 + x\epsilon_1)(1 + \epsilon_2) - [x(1+ \epsilon_3) + 1]}{\tilde x + 1} = \frac{(x + 1)\epsilon_2 + x(\epsilon_1 - \epsilon_3) + O(\epsilon^2)}{x + 1 + x\epsilon_3} . $$
If we can choose $\epsilon_3$ to make this small, then the method will be (forward) stable, and if we can make this expression exactly zero, then we'll have backward stability.
Trying for the latter, we solve for $\epsilon_3$ by setting the numerator equal to zero,
$$ \epsilon_3 = \frac{x + 1}{x}\epsilon_2 + \epsilon_1 + O(\epsilon^2)/x $$
which is small so long as $|x| \gg 0$, but the first term blows up as $x \to 0$.
In other words, the fact that $\epsilon_2$ can produce a large error relative to the input causes this algorithm to not be backward stable.
In contrast, this $x\to 0$ case is not a problem for forward stability because $\epsilon_3 = \epsilon_1$ yields error on the order of $\epsilon_2$.
Example: $\tilde f(x,y) = \text{float}(x) \oplus \text{float}(y)$
Now we are interested in
$$ \frac{\tilde f(x,y) - f(\tilde x,\tilde y)}{f(\tilde x,\tilde y)}...
Can you write Python code for it?

x1 = numpy.array([-0.9, 0.1, 0.5, 0.8])  # points where we know values
y = numpy.array([1, 2.4, -0.2, 1.3])     # values at those points
pyplot.figure()
pyplot.plot(x1, y, '*')
B = numpy.vander(x1, 4)                     # Vandermonde matrix at the known points
Q, R = gram_schmidt_naive(B)
p = numpy.linalg.solve(R, Q.T.dot(y))             # Compute the polynomial coefficients
print(p)
pyplot.plot(x, numpy.vander(x,4).dot(p))                 # Plot the polynomial evaluated at all points
print('B =', B, '\np =', p)

m = 20
V = numpy.vander(numpy.linspace(-1,1,m), increasing=False)
Q, R = gram_schmidt_naive(V)

def qr_test(qr, V):
    Q, R = qr(V)
    m = len(Q.T)
    print(qr.__name__,
          numpy.linalg.norm(Q.dot(R) - V),
          numpy.linalg.norm(Q.T.dot(Q) - numpy.eye(m)))

qr_test(gram_schmidt_naive, V)
qr_test(numpy.linalg.qr, V)

def gram_schmidt_classical(X):
    Q = numpy.zeros_like(X)
    R = numpy.zeros((len(X.T),len(X.T)))
    for i in range(len(Q.T)):
        v = X[:,i].copy()
        R[:i,i] = Q[:,:i].T.dot(v)
        v -= Q[:,:i].dot(R[:i,i])
        R[i,i] = numpy.linalg.norm(v)
        Q[:,i] = v / R[i,i]
    return Q, R

qr_test(gram_schmidt_classical, V[:,:15])
# Q, R = numpy.linalg.qr(V)
#print(Q[:,0])


I am working on the file "Fundamentals.ipynb".
The first task is:
Theorem: all full-rank $m\times n$ matrices ($m \ge n$) have a unique $Q R$ factorization with $R_{j,j} > 0$.
Absolute condition number
Consider a function $f: X \to Y$ and define the absolute condition number
$$ \hat\kappa = \lim_{\delta \to 0} \max_{|\delta x| < \delta} \frac{|f(x + \delta x) - f(x)|}{|\delta x|} = \max_{\delta x} \frac{|\delta f|}{|\delta x|}. $$
If $f$ is differentiable, then $\hat\kappa = |f'(x)|$.
Floating point arithmetic
Floating point arithmetic $x \circledast y := \text{float}(x * y)$ is exact within a relative accuracy $\epsilon_{\text{machine}}$.  Formally,
$$ x \circledast y = (x * y) (1 + \epsilon) $$
for some $|\epsilon| \le \epsilon_{\text{machine}}$.
Can you write Python code for it?

eps = numpy.float32(1)
while numpy.float32(1) + eps > 1:
    eps /= numpy.float64(2)
eps_machine = 2*eps  # We call this "machine epsilon"
print(eps_machine)

format((.2 - 1/3) + 2/15, '.20f')

format(.1, '.20f')


I am working on the file "Fundamentals_of_deep-learning/notebooks/01_Introduction_Building_a_neural_network.ipynb".
The first task is:
Adjusting the learning rate such that the neural network re-starts learning:
Can you write Python code for it?

epsilon = 1e-6 # learning rate for gradient descent
reg_lambda = 0.01 # regularization strength

model = build_model(50, print_loss=True)


I am working on the file "Fundamentals_of_deep-learning/notebooks/01_Introduction_Building_a_neural_network.ipynb".
The first task is:
Compile keras model
Can you write Python code for it?

model.compile(optimizer=optimizer,
              loss="binary_crossentropy",
              metrics=['accuracy'])


I am working on the file "Fundamentals_of_deep-learning/notebooks/01_Introduction_Building_a_neural_network.ipynb".
The first task is:
Create a neural network using Keras Sequential API
Can you write Python code for it?

model = Sequential()
model.add(Dense(5, activation="relu",
                input_shape=(2,)))
model.add(Dense(5, activation="relu"))
model.add(Dense(1, activation="relu"))


I am working on the file "Fundamentals_of_deep-learning/notebooks/01_Introduction_Building_a_neural_network.ipynb".
The first task is:
Create a neural network:
Can you write Python code for it?

num_examples = len(X) # training set size
nn_input_dim = 2 # input layer dimensionality
nn_output_dim = 2 # output layer dimensionality


I am working on the file "Fundamentals_of_deep-learning/notebooks/01_Introduction_Building_a_neural_network.ipynb".
The first task is:
Create a random number seed:

Random number seeding is useful for producing reproducible results.
Can you write Python code for it?


I am working on the file "Fundamentals_of_deep-learning/notebooks/01_Introduction_Building_a_neural_network.ipynb".
The first task is:
Create a synapse matrix:

A function applied to the syanpses.
For the first synapse, weights matrix of shape: input_shape_1 x input_shape_2 is created.
For the second synapse, weights matrix of shape: input_shape_2 x output_dim is created.
This function also introduces the first hyper-parameter in neural network tuning called 'bias_val', which is the bias value for the synaptic function.
Can you write Python code for it?

bias_val = 1

output_dim = 1

input_shape_1 = x.shape[1]
input_shape_2 = x.shape[0]

hidden_layer_size = 5

synapse_0 = 2*np.random.random((input_shape_1, hidden_layer_size)) - bias_val
synapse_1 = 2*np.random.random((hidden_layer_size, output_dim)) - bias_val

loss_col = []

print (synapse_0.shape)

synapse_0

print (synapse_1.shape)


I am working on the file "Fundamentals_of_deep-learning/notebooks/01_Introduction_Building_a_neural_network.ipynb".
The first task is:
Create a synthetic dataset for training and generating predictions:
Can you write Python code for it?

x_data = np.float32(np.random.rand(2,500))
y_data = np.dot([0.5, 0.7], x_data) + 0.6


I am working on the file "Fundamentals_of_deep-learning/notebooks/01_Introduction_Building_a_neural_network.ipynb".
The first task is:
Create an activation function for layer 2
Can you write Python code for it?

layer_2 = sigmoid(np.dot(layer_1, synapse_1) - bias_val)

layer_2.shape

layer_2


I am working on the file "Fundamentals_of_deep-learning/notebooks/01_Introduction_Building_a_neural_network.ipynb".
The first task is:
Create input and output data
Can you write Python code for it?


I am working on the file "Fundamentals_of_deep-learning/notebooks/01_Introduction_Building_a_neural_network.ipynb".
The first task is:
Create sigmoid function:

The sigmoid function takes two input arguments: x and a boolean argument called 'derivative'.
When the boolean argument is set as true, the sigmoid function calculates the derivative of x.
The derivative of x is required when calculating error or performing back-propagation.
The sigmoid function runs in every single neuron.
The sigmoid funtion feeds forward the data by converting the numeric matrices to probablities.

To implement the logistic sigmoid function using numpy, we use the mathematical formula:

Backpropagation:

Method to make the network better.
Mathematically we need to compute the derivative of the activation function.

If sigmoid function can be expressed as follows:

Then, the first derivative of this function can be expressed as:

Forwardpropagation and backpropagation functions using sigmoid activation:
Implementing sigmoid function using math library in python:
Can you write Python code for it?

import math
x  = -1.2
y = 1/(1+math.exp(-x))
print (y)

import numpy as np
y = 1/(1+np.exp(-x))
print (y)

def sigmoid(x, derivative=False):
    """
    Parameters:
      x: input
      derivative: boolean to specify if the derivative of the function should be computed
    """
    if derivative:
        return (x*(1-x))
    return (1/(1+np.exp(-x)))

sigmoid(-1.2, derivative=False)

x = -1.2
y_d = (1/(1+np.exp(x)))*(1-(1/(1+np.exp(x))))

y_d

sigmoid(0.23147521650098238, derivative=True)


I am working on the file "Fundamentals_of_deep-learning/notebooks/01_Introduction_Building_a_neural_network.ipynb".
The first task is:
Define the output data matrix as numpy array:
Can you write Python code for it?