I am working on the file "Nature-Cooperation.ipynb".
The first task is:
Fitness Landscape
\begin{align}
\dot{x_0} =& x_0(f_0q-\phi)\
\dot{x_1} =& x_0f_0(1-q)+x_1-\phi x_1
\end{align}

$q = (1-u)^L$: probability of exact copy of master genome.
$u$: probability of a mutation on one gene.
$L$: length of genome.
Can you write Python code for it?

fig = plt.figure()
plt.close(fig)

def genomeSequenceQ(f_0,u,L):
  np.random.seed(0)
  clear_output(wait=True)
  t_f = 10
  dt = 0.1

  x_ = np.random.uniform(size=2)
  x_ = x_/x_.sum()

  f = np.array([f_0,1])
  q = (1-u)**L

  def int_(t,x):
    mean = f[0]*x[0]+f[1]*x[1]
    dev = np.zeros(x.shape[0])
    dev[0] = x[0]*(f[0]*q - mean)
    dev[1] = x[0]*f[0]*(1-q)+x[1] - mean*x[1]
    return dev

  integ = integrate.ode(int_)
  integ.set_integrator("dopri5").set_initial_value(np.asarray(x_))
  y = np.zeros((int(t_f/dt)+1,x_.shape[0]))
  x = np.zeros((int(t_f/dt)+1,x_.shape[0]))
  xdot = np.zeros((int(t_f/dt)+1,x_.shape[0]))

  i = 0
  while integ.successful() and integ.t<t_f:
    y[i,:] = integ.y
    x[i,:] = integ.t
    xdot[i,:] = int_(integ.t,y[i,:])
    integ.integrate(integ.t+dt)
    i=i+1

  fig.clf()
  ax = fig.gca()
  with sns.color_palette("PuBuGn_d",n_colors=2):
    for ind_ in range(x.shape[1]):
      ax.plot(x[:,ind_], y[:,ind_], label=("$f_%d$: %.2f" % (ind_ ,f[ind_])))

  ax.set_ylim(-0.1,1.1)
  ax.set_xlabel("time")
  ax.set_ylabel("Quasi specie")

  ax2 = ax.twinx()
  with sns.color_palette("PuBuGn_d",n_colors=2):
    ax2.plot(np.arange(0,t_f+dt,dt),y.dot(f), label="fitness ",linestyle='-.')
  ax2.set_ylabel('$f$', rotation=0)
  ax2.set_ylim(0,10)
  ax2.grid('off')
  ax.legend(ncol=min(4,x.shape[1]))
  ax2.legend(loc=4)
  display(fig)
  return q

items_geneQ = [
  widgets.IntSlider(
    value=5,
    min=2,
    max=12,
    description="Genome Length",layout=widgets.Layout(width='50%', height='80px')),
  widgets.FloatSlider(
    value=0.05,
    min=0.01,
    max=0.8,
    step = 0.05,
    description="mutatation rate",layout=widgets.Layout(width='50%', height='80px')),
 widgets.FloatSlider(
    value=1,
    min=0.0,
    max=40,
    step=0.05,
    description="max_f",layout=widgets.Layout(width='50%', height='80px'))]

def _GeneCallQ(v):
  q_ = genomeSequenceQ(items_geneQ[2].value,items_geneQ[1].value,items_geneQ[0].value)
  label.value= "f_0 q = %.2f" % (q_*items_geneQ[2].value)
 ...


I am working on the file "Nature-Cooperation.ipynb".
The first task is:
Genomes are Sequences
Quasispecies equation
\begin{align}
\dot{x_i} =\sum_{j=0}^{n} x_j ~ f_j ~ q_{ji} - \phi x_i.
\end{align}

$x$: population ratio of type $i$.
$f_i$: fitness for type $i$.
$q_{ji}$: probability of mutation from type $j$ to $i$
$q_{ji} = u^{h_ij}(1-u)^{L-h_{ij}}$  $~L:$ Length of genome. $~u:$ mutation prob. at one gene.
Can you write Python code for it?

fig = plt.figure()
plt.close(fig)

def genomeSequence(N,drich_alpha,point_mut):
  np.random.seed(0)
  clear_output(wait=True)
  if point_mut is not None:
    L,u = point_mut
  t_f = 10
  dt = 0.1

  x_ = np.random.uniform(size=(N))
  x_ = x_/x_.sum()

  f = np.random.lognormal(size=(N))

  if drich_alpha is not None:
    Q = np.zeros((N,N))
    for j in range(N):
      Q[j,:] = np.random.dirichlet(np.roll(np.logspace(1,drich_alpha+1,N)[::-1], j), 1)
  elif point_mut is not None:
    Q = np.zeros((N,N))
    for j in range(N):
      for i in range(N):
        Q[j,i] = (u**(np.abs(j-i)))*((1-u)**(L-np.abs(j-i)))
  else:
    print("One of the two arguments should not be None")
    return

  def int_(t,x):
    x = np.asarray(x).reshape((x.shape[0],1))
    dev = np.zeros(x.shape[0])
    mean = f.dot(x)
    for i in range(x.shape[0]):
      for j in range(x.shape[0]):
        dev[i] += f[j]*Q[j,i]*x[j]
      dev[i] -= mean*x[i]
    return dev

  integ = integrate.ode(int_)
  integ.set_integrator("dopri5").set_initial_value(np.asarray(x_))
  y = np.zeros((int(t_f/dt)+1,x_.shape[0]))
  x = np.zeros((int(t_f/dt)+1,x_.shape[0]))
  xdot = np.zeros((int(t_f/dt)+1,x_.shape[0]))

  i = 0
  while integ.successful() and integ.t<t_f:
    y[i,:] = integ.y
    x[i,:] = integ.t
    xdot[i,:] = int_(integ.t,y[i,:])
    integ.integrate(integ.t+dt)
    i=i+1

  fig.clf()

  ax = fig.gca()
  with sns.color_palette("PuBuGn_d",n_colors=2):
    for ind_ in range(x.shape[1]):
      ax.plot(x[:,ind_], y[:,ind_], label=("$f_%d$: %.2f" % (ind_ +1,f[ind_])))

  ax.set_ylim(-0.1,1.1)
  ax.set_xlabel("time")
  ax.set_ylabel("Quasi specie")

  ax2 = ax.twinx()
  with sns.color_palette("PuBuGn_d",n_colors=2):
    ax2.plot(np.arange(0,t_f+dt,dt),y.dot(f), label="fitness ",linestyle='-.')
  ax2.set_ylabel('$f$', rotation=0)
  ax2.set_ylim(0,3)
  ax2.grid('off')
  ax.legend(ncol=min(4,x.shape[1]))
  ax2.legend(loc=4)
  display(fig)
  return

items_gene = [
  widgets.IntSlider(
    value=2,
    min=2,
    max=6,
   ...


I am working on the file "Nature-Cooperation.ipynb".
The first task is:
Hacking into Evolutionary Dynamics!
This Jupyter notebook implements some of the ideas in following two books, specifically chapters 1-5 in Evolutionary Dynamics. For better undrestanding of the equations and code please consult the books and relevant papers.
This notebook contains interactive contents using Javascript, please download and execute it on Jupyter.
Can you write Python code for it?


I am working on the file "Nature-Cooperation.ipynb".
The first task is:
Multiple species.
Can you write Python code for it?

objects_1 = []
status_label_1 = widgets.Label()
_ = call_back_mute(3,objects_1,status_label_1,lambda x:updateplot(x,objects_1,status_label_1))


I am working on the file "Nature-Cooperation.ipynb".
The first task is:
Prisoners Dillema
Payoff matrix:
\begin{align}
\begin{pmatrix}
   & C & D\
  C & 3 &  0 \
  D & 5 &  1 \
  \end{pmatrix}
\end{align}
The Nash equilibria in this game is to always defect (D,D).
Can you write Python code for it?

R = 3
S = 0
T = 5
P = 1

payoff = [[R,S],[T,P]]
evolutionaryGame([0.6,0.4],payoff,["Cooperate","Defect"])


I am working on the file "Nature-Cooperation.ipynb".
The first task is:
Selection-Mutation

Selection operates whenever different types of individuals reproduce at different rates.

\begin{align}
\dot{\vec{x}} =\vec{x}Q-\phi\vec{x}.
\end{align}

$\vec{x}$: population ratio of type $i$.
$Q$: Mutation matrix.
$\phi$: average fitness
Can you write Python code for it?

fig = plt.figure()
plt.close(fig)
def twoCell(init_,rate):
  clear_output(wait=True)
  t_f = 10
  dt = 0.1
  update_rate = np.asarray(rate)
  def int_(t,x):
    dev = x.T.dot(update_rate)-x
    return dev

  integ = integrate.ode(int_)
  y = np.zeros((int(t_f/dt)+1,update_rate.shape[0]))
  x = np.zeros((int(t_f/dt)+1,update_rate.shape[0]))
  xdot = np.zeros((int(t_f/dt)+1,update_rate.shape[0]))
  integ.set_integrator("dopri5").set_initial_value(np.asarray(init_))
  i = 0
  while integ.successful() and integ.t<t_f:
    y[i,:] = integ.y
    x[i,:] = integ.t
    xdot[i,:] = int_(integ.t,y[i,:])
    integ.integrate(integ.t+dt)
    i=i+1
  fig.clf()
  ax = fig.gca()
  with sns.color_palette("PuBuGn_d",n_colors=x.shape[1]):
    for ind_ in range(x.shape[1]):
      ax.plot(x[:,ind_], y[:,ind_], label="type "+str(ind_ +1))

  ax.set_ylim(-0.1,1.1)
  ax.set_xlabel("time")
  ax.set_ylabel("population ratio")

  ax2 = ax.twinx()
  with sns.color_palette("PuBuGn_d",n_colors=x.shape[1]):
    for ind_ in range(x.shape[1]):
      ax2.plot(x[:,ind_], xdot[:,ind_], label="d type "+str(ind_ +1),linestyle='--')
  ax2.set_ylabel('$\dot{x}$', rotation=0)
  ax2.grid('off')
  ax.legend(ncol=x.shape[1])
  ax2.legend(loc=4,ncol=x.shape[1])
  display(fig)
  return

items_mute = [
  widgets.IntText(
    value=2,
    min=2,
    max=5.0,
    description="r",layout=widgets.Layout(width='50%', height='80px'))
  ,widgets.Button(
    description="submit")]

def updateplot(v,objects,status_label):
  init = []
  rates = []
  for ind_,obj in enumerate(objects):
    if ind_ < len(objects)-1:
      init.append(obj[0].value)
    else:
      if sum(init)>1:
        status_label.value = "Initial rates should sum to <1"
        return
      else:
        status_label.value = ""
      init.append(1-sum(init))
    rate_ = []
    for j in range(1,len(objects)):
      rate_.append(obj[j].value)
    if sum(rate_)>1:
      status_label.value = "sum of mutation rates should sum to <1"
      return
    else:
      status_label.value = ""

   ...


I am working on the file "Nature.ipynb".
The first task is:
Counting galaxies in the Hubble deep field
The notebook can utilize powerful Python libraries that span everything from statistics and machine learning to signal and image processing. This example uses the image-processing library scikit-image to identify galaxies in an image of the sky provided by the Hubble Space Telescope using a blob feature detection algorithm (an approach known as the determinant of Hessian ).
After running the cell, you can play with the parameters of the detection algorithm to find galaxies of different sizes and prominences:

The max_sigma parameter determines the maximum size of the objects that will be identified.
The threshold parameter can be reduced to detect less prominent objects.
Can you write Python code for it?

# Import matplotlib (plotting), skimage (image processing) and interact (user interfaces)
# This enables their use in the Notebook.
%matplotlib inline
from matplotlib import pyplot as plt

from skimage import data
from skimage.feature import blob_doh
from skimage.color import rgb2gray

from IPython.html.widgets import interact, fixed

# Extract the first 500px square of the Hubble Deep Field.
image = data.hubble_deep_field()[0:500, 0:500]
image_gray = rgb2gray(image)

def plot_blobs(max_sigma=30, threshold=0.1, gray=False):
    """
    Plot the image and the blobs that have been found.
    """
    blobs = blob_doh(image_gray, max_sigma=max_sigma, threshold=threshold)

    fig, ax = plt.subplots(figsize=(8,8))
    ax.set_title('Galaxies in the Hubble Deep Field')

    if gray:
        ax.imshow(image_gray, interpolation='nearest', cmap='gray_r')
        circle_color = 'red'
    else:
        ax.imshow(image, interpolation='nearest')
        circle_color = 'yellow'
    for blob in blobs:
        y, x, r = blob
        c = plt.Circle((x, y), r, color=circle_color, linewidth=2, fill=False)
        ax.add_patch(c)

# Use interact to explore the galaxy detection algorithm.
interact(plot_blobs, max_sigma=(10, 40, 2), threshold=(0.005, 0.02, 0.001));


I am working on the file "Nazarov/XGBoost.ipynb".
The first task is:
As usual do the train-test split.
Can you write Python code for it?

X_train, X_, y_train, y_ = train_test_split( X, y, test_size = 0.25, random_state = random_state )
X_valid, X_test, y_valid, y_test = train_test_split( X_, y_, test_size = 0.5, random_state = random_state )


I am working on the file "Nazarov/XGBoost.ipynb".
The first task is:
Besides these methods xgboost.booster exports:
load_model( fname ) and save_model( fname ).
Let's check out the confusuion matrix
Can you write Python code for it?

pd.DataFrame( sk.metrics.confusion_matrix( y_test, y_predict ), index = clf_.classes_, columns = clf_.classes_ )


I am working on the file "Nazarov/XGBoost.ipynb".
The first task is:
DMatrix exports several useful methods:
- num_col() : returns the number of columns;
- num_row() : gets the number of items;
- save_binary( fname ) : saves the DMatrix object into a specified file.
For a more detailed list, it is useful to have a look at the official manual
Having dafined the datasets, it is the right time to initialize the booster. To this end one uses xgboost.Learner class. Among other parameters, its instance is initialized with a dictionary of parameters, which allows for a more flexible booster initialization.
Can you write Python code for it?

xgb_params = {
    'bst:max_depth':2,
    'bst:eta':1,
    'silent':1,
    'objective':'multi:softmax',
    'num_class': 9,
    'nthread': 2,
    'eval_metric' : 'auc'
}


I am working on the file "Nazarov/XGBoost.ipynb".
The first task is:
Fit the a gradient boosted tree ensemble.
Can you write Python code for it?

clf_.fit( X_train, y_train, eval_set = [ ( X_valid, y_valid ), ], verbose = True )


I am working on the file "Nazarov/XGBoost.ipynb".
The first task is:
Let's check out the confusuion matrix
Can you write Python code for it?


I am working on the file "Nazarov/XGBoost.ipynb".
The first task is:
Mind the seed!!
Can you write Python code for it?

random_state = np.random.RandomState( seed = 0x0BADC0DE )


I am working on the file "Nazarov/XGBoost.ipynb".
The first task is:
Now import some ML stuff
Can you write Python code for it?

import sklearn as sk, xgboost as xg
# from sklearn.model_selection import train_test_split
from sklearn.cross_validation import train_test_split


I am working on the file "Nazarov/XGBoost.ipynb".
The first task is:
Now let's validate.
Can you write Python code for it?

y_predict = clf_.predict( X_test )
y_score = clf_.predict_proba( X_test )


I am working on the file "Nazarov/XGBoost.ipynb".
The first task is:
alternative interface
Internally XGBoost relies heavily on a custom dataset format DMatrix. It is ...
The interface, which is exposed into python has three capabilities:
- load datasets in libSVM compatible format;
- load SciPy's sparse matrices;
- load Numpy's ndarrays.
Let's load the train dataset using numpy interface :
- data : the matrix of features $X$;
- label : the observation labels $y$ (could be categorical or numeric);
- missing : a vector of values that encode missing observations;
- feature_names : the columns names of $X$;
- feature_types : defines the python types of each column of $X$, in case of heterogeneous data;
- weight : the vector of nonnegative weights of each observation in the dataset.
Can you write Python code for it?

train_dmat = xg.DMatrix( data = X_train,
                         label = y_train,
                         feature_names = None,
                         feature_types = None )
test_dmat = xg.DMatrix( data = X_test, label = y_test )


I am working on the file "NearestNeighbor.ipynb".
The first task is:
Note that, while I said that there is no built in function for this, there are any number of ways to accomplish this!  For example, using map, np.mgrid(), transposes, etc.
The code below accomplishes it in just one line, but I thought that it would make more sense if we broke it down like we did above.
Can you write Python code for it?

# Comparison of KDE and K-Nearest Neighbors "smoothing"
# Based on Ivezic, Figure 6.4
# Author: Jake VanderPlas
# License: BSD
#   The figure produced by this code is published in the textbook
#   "Statistics, Data Mining, and Machine Learning in Astronomy" (2013)
#   For more information, see http://astroML.github.com
#   To report a bug or issue, use the following forum:
#    https://groups.google.com/forum/#!forum/astroml-general

%matplotlib inline
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.colors import LogNorm

from scipy.spatial import cKDTree

from astroML.datasets import fetch_great_wall
from astroML.density_estimation import KDE, KNeighborsDensity

#------------------------------------------------------------
# Fetch the great wall data
X = fetch_great_wall()

#------------------------------------------------------------
# Create  the grid on which to evaluate the results
Nx = 50
Ny = 125
xmin, xmax = (-375, -175)
ymin, ymax = (-300, 200)

#------------------------------------------------------------
# Evaluate for several models
Xgrid = np.vstack(map(np.ravel, np.meshgrid(np.linspace(xmin, xmax, Nx), np.linspace(ymin, ymax, Ny)))).T

#print Xgrid

kde = KDE(metric='gaussian', h=5)
dens_KDE = kde.fit(X).eval(Xgrid).reshape((Ny, Nx))

knn5 = KNeighborsDensity('bayesian', 5)
dens_k5 = knn5.fit(X).eval(Xgrid).reshape((Ny, Nx))

knn40 = KNeighborsDensity('bayesian', 40)
dens_k40 = knn40.fit(X).eval(Xgrid).reshape((Ny, Nx))

#------------------------------------------------------------
# Plot the results
fig = plt.figure(figsize=(10, 5))
fig.subplots_adjust(left=0.12, right=0.95, bottom=0.2, top=0.9,
                    hspace=0.01, wspace=0.01)

# First plot: scatter the points
ax1 = plt.subplot(221, aspect='equal')
ax1.scatter(X[:, 1], X[:, 0], s=1, lw=0, c='k')
ax1.text(0.95, 0.9, "input", ha='right', va='top',
         transform=ax1.transAxes,
         bbox=dict(boxstyle='round', ec='k', fc='w'))

# Second plot: KDE
ax2 = plt.subplot(222,...


I am working on the file "Nematic/Annulus_Simple_Matplotlib.ipynb".
The first task is:
Plotting along a line
It turns out the the solution "u" is a function that can be evaluated at a point.  So in the next cell we loop through a line and make a vector of points for plotting. You just need to give it coordinates $u(x,y)$.
Can you write Python code for it?

y = np.linspace(r1,r2*0.99,100)
uu = []
np.array(uu)
for i in range(len(y)):
    yy = y[i]
    uu.append(u(0.0,yy)) #evaluate u along y axis
plt.figure()
plt.plot(y,uu)
plt.grid(True)
plt.xlabel('y')
plt.ylabel('V')

u


I am working on the file "Nested Deep GPs.ipynb".
The first task is:
Example on Robot Wireless Data
Can you write Python code for it?

import pods
data = pods.datasets.robot_wireless()
data['X'].shape
Y = data['Y']
n = Y.shape[0]
t = np.linspace(0, n-1, n)[:, None]

model = build_supervised(t, Y, (2,15), (40, 40, 40))
model.optimize(messages=True)


I am working on the file "Nested Deep GPs.ipynb".
The first task is:
Modelling with a GP
Now we attempt to model the step function with a Gaussian process. Parameters are chosen by type-II maximum likelihood.
Can you write Python code for it?

model0 = GPy.models.GPRegression(X,Y)
model0.optimize('bfgs', max_iters=1000, messages=1)


I am working on the file "Nested Deep GPs.ipynb".
The first task is:
Next some plotting for the notebook.
Can you write Python code for it?

import matplotlib
from matplotlib import pyplot as plt
matplotlib.rcParams['figure.figsize'] = (16,8)
%matplotlib inline


I am working on the file "Nested Deep GPs.ipynb".
The first task is:
Non Gaussian Derivatives
Gaussian process models have Gaussian distributed derivatives. This means that they tend to struggle with approximating step functions, which have derivates which are either zero or infinite. Duvenaud et al showed that as we increase the number of layers in the model the distribution over derivatives can become more heavy tailed. Let's examine this in practice by fitting the model to a step function.
Can you write Python code for it?

np.random.seed(0)
n = 30 # number of data
d = 1  # number of dimensions
# dependent variable is linearly spaced.
X = np.linspace(0,1,n)[:,None]
# response variable is step function
Y = np.where(X>0.5, 1,0) + np.random.randn(n,1)*0.02
# where to plot the model predictions
Xtest = np.linspace(-1,2,500)[:,None]


I am working on the file "Nested Deep GPs.ipynb".
The first task is:
Three Hidden Layers
Finally we consider three hidden layers.
Can you write Python code for it?

model3 = build_supervised(X, Y, (2,2,2), (15, 15, 15, 15))
model3.optimize('bfgs', messages=1,max_iters=5000)

model3.plot((-1, 2),3)


I am working on the file "Nested Deep GPs.ipynb".
The first task is:
Two Hidden Layers
Next we consider two hidden layers.
Can you write Python code for it?

model2 = build_supervised(X, Y, Qs=(1,1), Ms=(15,15,15))
model2.optimize('bfgs', max_iters=1000, messages=1)

model2.plot((-1, 2),3)


I am working on the file "Nested Deep GPs.ipynb".
The first task is:
We can plot the regression to see if it has managed to fit the data.
Can you write Python code for it?


I am working on the file "Nested Deep GPs.ipynb".
The first task is:
and we note that the model is overly smooth and results in a variance that is too high.
Deep GP
Now we will consider a deep Gaussian process. Firstly, we'll set up some model parameters and a helper
We can't plot the direct output of the deep GP, so we'll use Monte Carlo sampling. To aid comparison, we first plot the Monte Carlo samples from the original GP.
One Hidden Layer
For our first experiment we create a deep GP with one hidden layer. The model is easily constructed by creating different layer objects for the deep GP and then concatenating them.
Can you write Python code for it?

model1 = build_supervised(X, Y, Qs=(1,), Ms=(15,15))


I am working on the file "Nested Statements and Scope.ipynb".
The first task is:
Built-in
These are the built-in function names in Python (don't overwrite these!)
Can you write Python code for it?


I am working on the file "Nested Statements and Scope.ipynb".
The first task is:
Enclosing function locals
This occurs when we have a function inside a function (nested functions)
Can you write Python code for it?

name = 'This is a global name'

def greet():
    # Enclosing function
    name = 'Sammy'

    def hello():
        print 'Hello '+name

    hello()

greet()


I am working on the file "Nested Statements and Scope.ipynb".
The first task is:
Local Variables
When you declare variables inside a function definition, they are not related in any way to other variables with the same names used outside the function - i.e. variable names are local to the function. This is called the scope of the variable. All variables have the scope of the block they are declared in starting from the point of definition of the name.
Example:
Can you write Python code for it?

x = 50

def func(x):
    print ('x is', x)
    x = 2
    print ('Changed local x to', x)

func(x)
print ('x is still', x)


I am working on the file "Nested Statements and Scope.ipynb".
The first task is:
The first time that we print the value of the name x with the first line in the function’s body, Python uses the value of the parameter declared in the main block, above the function definition.
Next, we assign the value 2 to x. The name x is local to our function. So, when we change the value of x in the function, the x defined in the main block remains unaffected.
With the last print statement, we display the value of x as defined in the main block, thereby confirming that it is actually unaffected by the local assignment within the previously called function.
The global statement
If you want to assign a value to a name defined at the top level of the program (i.e. not inside any kind of scope such as functions or classes), then you have to tell Python that the name is not local, but it is global. We do this using the global statement. It is impossible to assign a value to a variable defined outside a function without the global statement.
You can use the values of such variables defined outside the function (assuming there is no variable with the same name within the function). However, this is not encouraged and should be avoided since it becomes unclear to the reader of the program as to where that variable’s definition is. Using the global statement makes it amply clear that the variable is defined in an outermost block.
Example:
Can you write Python code for it?

x = 50

def func():
    global x
    print ('This function is now using the global x!')
    print ('Because of global x is: ', x)
    x = 2
    print ('Ran func(), changed global x to', x)

print ('Before calling func(), x is: ', x)
func()
print ('Value of x (outside of func()) is: ', x)


I am working on the file "Nested Statements and Scope.ipynb".
The first task is:
What do you imagine the output of printer() is? 25 or 50? What is the output of print x? 25 or 50?
50 it is a local variable in printer
25 it is a global variable
Can you write Python code for it?


I am working on the file "Nested_Sampling/Nested_Sampling.ipynb".
The first task is:
2.1 Estimate Z using True X
Can you write Python code for it?

plt.plot(true_logX, L_list, '.')

def integral_L(X_list, L_list):
    """
    Integral of L with respect X given X_list and L_list
    """
    Z = 0
    Z_list = []
    for i in range(len(X_list)-1):
        Z += 0.5 * (X_list[i] - X_list[i+1]) * (L_list[i] + L_list[i+1])
        Z_list.append(Z)
    Z += (1 - X_list[0]) * L_list[0] + (X_list[-1] - 0) * L_list[-1]
    Z_list.append(Z)
    return (Z, Z_list)

Z, Z_list = integral_L(true_X_list, L_list)

np.log(Z)

plt.plot(Z_list)


I am working on the file "Nested_Sampling/Nested_Sampling.ipynb".
The first task is:
2.2 Estimate Z using Sampled X
Can you write Python code for it?

sample_X_lists = []
Z_list = []
for k in range(1000):
    sample_X = 1
    tmp = []
    for i in range(len(L_list)):
        sample_X = np.random.uniform() * sample_X
        tmp.append(sample_X)
    sample_X_lists.append(tmp)
    Z,_ = integral_L(sample_X_lists[-1], L_list)
    Z_list.append(Z)

styles = ['+','.','>']
for k in range(3):
    plt.plot(np.log(sample_X_lists[k]), L_list, styles[k])

(np.mean(np.log(Z_list)), np.std(np.log(Z_list)))


I am working on the file "Nested_Sampling/Nested_Sampling.ipynb".
The first task is:
3. Nested Sampling with J Particle for the Simple Gaussian Problem
Can you write Python code for it?

N = 10
K = 800
r = 1
thetas = [np.array(uniform_sample_from_n_ball(r, C)) for i in range(N)]
rs = [np.sqrt(np.sum(theta*theta)) for theta in thetas]
Ls = [np.exp(-r**2/(2*sigma**2)) for r in rs]

theta_list = []
L_list = []
r_list = []
true_X_list = []
for i in range(K):
    minIdx = np.argmin(Ls)
    theta = thetas.pop(minIdx)
    r = rs.pop(minIdx)
    L = Ls.pop(minIdx)
    X = r**C
    theta_list.append(theta)
    r_list.append(r)
    L_list.append(L)
    true_X_list.append(X)

    theta = uniform_sample_from_n_ball(r, C)
    r = np.sqrt(np.sum(np.array(theta)**2))
    L = np.exp(-r**2/(2*sigma**2))
    thetas.append(theta)
    rs.append(r)
    Ls.append(L)

plt.plot(np.log(true_X_list), L_list, '.')

sample_X_lists = []
Z_list = []
for k in range(100):
    sample_X = 1
    tmp = []
    for i in range(len(L_list)):
        t = np.max([np.random.uniform() for m in range(N)])
        sample_X = t * sample_X
        tmp.append(sample_X)
    sample_X_lists.append(tmp)
    Z,_ = integral_L(sample_X_lists[-1], L_list)
    Z_list.append(Z)

(np.mean(np.log(Z_list)), np.std(np.log(Z_list)))


I am working on the file "Nested_Sampling/Nested_Sampling.ipynb".
The first task is:
It is clear from the Figure 1 that the bulk of mass contributing most to the value of evidence Z is around $t = \text{log}(X) = -34.$ Another observation is that $L(t)$ is a Gaussian shape distribution. To estimate where the peak is and how large is the variance, we can expand the log of integral function $\text{log}(L(t)) = -\frac{\text{exp}(2t/C)}{2\sigma^2}+t$ using Taylor series around its maximum point.
The derivative of $\text{log}(L(t))$ is given by
$$\frac{d\text{log}(L(t))}{dt} = 1 - \frac{1}{C\sigma^2} \text{exp}(2t/C).$$
Taking the derivative to be zero, we have the maximum point at $t_M = C\text{log}\sqrt{\sigma^2C} = -34.54.$
The second derivative at the maximum point is given by
$$\frac{d^2\text{log}(L(t))}{dt^2}|{t=t_M} = -\frac{2}{C^2\sigma^2}\text{exp}(2t/C)|{t=t_M} = -\frac{2}{C}.$$
Therefore the distribution can be approximated by a Gaussin distribution with a mean of -34.54 and a standard variation of $\sqrt{C/2} = 2.24.$
2. Nested Sampling with 1 Particle for the Simple Gaussian Problem
Can you write Python code for it?

def uniform_sample_from_n_ball(r, n):
    """
    Uniformally sample from the n-dimensional ball with radius r
    """
    ## sample from unit n-sphere using Gaussian distribution
    X = np.array([np.random.normal() for i in range(n)])
    X = X / np.sqrt(np.sum(X*X))

    ## scale
    u = np.random.uniform()
    s = u ** (1.0/n) * r
    X = s * X
    return list(X)

K = 80
r = 1
r_list = []
theta_list = []
L_list = []
true_X_list = []
for i in range(K):
    theta = uniform_sample_from_n_ball(r, C)
    r = np.sqrt(np.sum(np.array(theta)**2))
    L = np.exp(-r**2/(2*sigma**2))
    X = r**C

    theta_list.append(theta)
    r_list.append(r)
    L_list.append(L)
    true_X_list.append(X)

true_logX = np.log(true_X_list)


I am working on the file "Nested_Sampling/Nested_Sampling.ipynb".
The first task is:
The plot of the integral function $\text{exp}(-\frac{\text{exp}(2t/C)}{2\sigma^2}+t)$ when $t = \text{log}X$ is used as the integral variable.
Can you write Python code for it?

## define the function L(t)
def L_t(t, C = 10, sigma = 0.01):
    return np.exp(-1/(2*sigma**2)*np.exp(2*t/C) + t)

## plot the function L(t)
T = np.linspace(-70,0,1000)
Y = []
for t in T:
    Y.append(L_t(t))
plt.plot(T,Y)
plt.xlabel("t = LogX")
plt.ylabel("L(t)")
plt.title("Figure 1. Likelihood L(t) as a Function of t = logX")


I am working on the file "NetHack's functions Rne, Rn2 and Rnz in Python 3.ipynb".
The first task is:
Examples
Can you write Python code for it?

np.asarray([rnz(3) for _ in range(100)])

np.asarray([rnz(3, truncation=10) for _ in range(100)])


I am working on the file "NetHack's functions Rne, Rn2 and Rnz in Python 3.ipynb".
The first task is:
For x=350
Can you write Python code for it?

np.asarray([rnz(350) for _ in range(100)])

_ = plt.hist(np.asarray([rnz(350) for _ in range(100000)]), bins=200)

np.asarray([rnz(350, truncation=10) for _ in range(100)])

_ = plt.hist(np.asarray([rnz(350, truncation=10) for _ in range(10000)]), bins=200)


I am working on the file "NetHack's functions Rne, Rn2 and Rnz in Python 3.ipynb".
The first task is:
In the NetHack game, the player's experience is used as default value of the truncation parameter...
Can you write Python code for it?

np.asarray([rne(3) for _ in range(50)])

plt.hist(np.asarray([rne(3) for _ in range(10000)]), bins=5)

np.asarray([rne(4, truncation=10) for _ in range(50)])

plt.hist(np.asarray([rne(4, truncation=10) for _ in range(10000)]), bins=10)


I am working on the file "NetHack's functions Rne, Rn2 and Rnz in Python 3.ipynb".
The first task is:
Let's check what this page says about rne(4):

The rne(4) call returns an integer from 1 to 5, with the following probabilities:
|Number| Probability |
|:-----|------------:|
| 1 | 3/4 |
| 2 | 3/16 |
| 3 | 3/64 |
| 4 | 3/256 |
| 5 | 1/256 |
Can you write Python code for it?

ref_table = {1: 3/4, 2: 3/16, 3: 3/64, 4: 3/256, 5: 1/256}
ref_table

N = 100000
table = Counter([rne(4, truncation=5) for _ in range(N)])
for k in table:
    table[k] /= N
table = dict(table)
table

rel_diff = lambda x, y: abs(x - y) / x
for k in ref_table:
    x, y = ref_table[k], table[k]
    r = rel_diff(x, y)
    print(f"For k={k}: relative difference is {r:.3g} between {x:.3g} (expectation) and {y:.3g} (with N={N} samples).")


I am working on the file "NetHack's functions Rne, Rn2 and Rnz in Python 3.ipynb".
The first task is:
Seems true !

Rnz distribution
It's not too hard to write.
Can you write Python code for it?

def rnz(i, truncation=10):
    x = i
    tmp = 1000
    tmp += rn2(1000)
    tmp *= rne(4, truncation=truncation)
    flip = rn2(2)
    if flip:
        x *= tmp
        x /= 1000
    else:
        x *= 1000
        x /= tmp
    return int(x)


I am working on the file "NetHack's functions Rne, Rn2 and Rnz in Python 3.ipynb".
The first task is:
Testing for rn2(x) == 0 gives a $1/x$ probability :
Can you write Python code for it?

from collections import Counter

Counter([rn2(10) == 0 for _ in range(100)])

Counter([rn2(10) == 0 for _ in range(1000)])

Counter([rn2(10) == 0 for _ in range(10000)])


I am working on the file "Network Science/Preferential Attachment.ipynb".
The first task is:
Experiment 1
- Alpha = 1,
- hypothesis: degree distribution should be exponential,
Can you write Python code for it?

# ramp up...
T1 = G.copy()
simulate(T1,m+1,m,rounds=3000)

hist(T1)

power_law_graph(T1)


I am working on the file "Network Science/Preferential Attachment.ipynb".
The first task is:
Experiment 2
- Alpha < 1,
- Hypothesis: degree distribution should be a stretched exponential,
Can you write Python code for it?

def draw(G,disregard=[],alpha=1):

    nodes = { }
    lookup = dict(G.degree())

    denominator = 0.0
    for k,v in lookup.items():
        if k in disregard:
            continue

        nodes[k] = (v ** alpha)
        denominator = denominator + nodes[k]

    #print(np.array(list(nodes.values()))/denominator)

    drawing = random.uniform(0,1)

    key = None
    bottom = 0.0
    top = 0.0

    for k,v in nodes.items():
        key = k
        top = bottom + (v / denominator)

        if (drawing >= bottom) & (drawing < top):
            key = k
            break

        bottom = top

    # print(drawing, " ", bottom, "-", top, ":", key)
    return key

# randomly gets attachments of the new node
def get_attachments(G,alpha=1,m=1):
    if m < 1:
        return []

    nodes = []

    i = 0
    while i < m:
        node = draw(G,disregard=nodes,alpha=alpha)
        nodes.append(node)
        i = i + 1

    # print()
    return nodes

def simulate2(G,node,alpha=1,m=1,rounds=100):
    while node <= rounds:
        attachments = get_attachments(G,alpha,m=m)
        G.add_node(node)

        for attachment in attachments:
            G.add_edge(node,attachment)

        node = node + 1

T2 = G.copy()
simulate2(T2,m+1,.5,m,3000)

hist(T2)

power_law_graph(T2)


I am working on the file "Network Science/Preferential Attachment.ipynb".
The first task is:
Simulation
Can you write Python code for it?

T = G.copy()
simulate(T,m+1,m,rounds=15)

plot(T,skew=True)


I am working on the file "NetworkX/.ipynb_checkpoints/Networkx-checkpoint.ipynb".
The first task is:
A dibujar!!!
Ahora elijan una gráfica de las que generamos anteriormente y muestrenla a los demás
Can you write Python code for it?

nx.draw(W, node_size=50,node_color="green", alpha=0.5)
gr.title('Wheel graph', fontsize= 18)
gr.show()

nx.average_shortest_path_length(W)

Hyp=nx.hypercube_graph(6)
nx.draw(Hyp, node_size=50,node_color="orange", alpha=0.5)
gr.title('Otra gráfica', fontsize= 18)
gr.show()

lollipop=nx.lollipop_graph(10,20)
nx.draw(lollipop, node_size=50,node_color="orange", alpha=0.5)
gr.title('Otra gráfica', fontsize= 18)
gr.show()

nx.average_shortest_path_length(lollipop)

nx.draw(GPL, node_size=100, node_color="pink", alpha=0.5)
gr.title('Pyladies graph', fontsize= 18)
gr.show()

nx.average_shortest_path_length(GPL)

nx.draw_random(RB, node_size=100, node_color="RED", alpha=0.5)
gr.title('RHESUS BRAIN graph', fontsize= 18)
gr.show()

nx.average_shortest_path_length(RB)


I am working on the file "NetworkX/.ipynb_checkpoints/Networkx-checkpoint.ipynb".
The first task is:
Borremos los nodos
Can you write Python code for it?

"""
Uno a uno
"""
G.remove_node(3)
G.node

"""
Todos
"""

G.clear()
G.node

"""
Podemos añadir nodos a partir de una variable
"""

H=[5, 6, 7, 8, 9, 12]
G.add_nodes_from(H)
G.node

H=nx.path_graph(10)
G.add_nodes_from(H)
G.node
#print (H)


I am working on the file "NetworkX/.ipynb_checkpoints/Networkx-checkpoint.ipynb".
The first task is:
Clásicas gráficas pequeñas
Can you write Python code for it?

petersen=nx.petersen_graph()
tutte=nx.tutte_graph()
maze=nx.sedgewick_maze_graph()
tet=nx.tetrahedral_graph()


I am working on the file "NetworkX/.ipynb_checkpoints/Networkx-checkpoint.ipynb".
The first task is:
Dibujemos la gráfica
Can you write Python code for it?

ba=nx.barabasi_albert_graph(40,5)
nx.draw(ba, node_size=80,node_color="blue", alpha=0.5)
#nx.draw_circular(ba, node_size=80,node_color="blue", alpha=0.5)
gr.title('Barabasi Albert Graph', fontsize= 18) # Se requiere de matplotlib... aqui esta importada como gr
gr.show()

ba=nx.barabasi_albert_graph(40,5)
#nx.draw(ba, node_size=80,node_color="blue", alpha=0.5)
nx.draw_circular(ba, node_size=80,node_color="blue", alpha=0.5)
gr.title('Barabasi Albert Graph', fontsize= 18) # Se requiere de matplotlib... aqui esta importada como gr
gr.show()

nx.average_shortest_path_length(ba)

nx.draw(ws, node_size=80,node_color="blue", alpha=0.5)
gr.title('Watts Strogatz Grap', fontsize= 18) # Se requiere de matplotlib... aqui esta importada como gr
gr.show()

nx.average_shortest_path_length(ws)


I am working on the file "NetworkX/.ipynb_checkpoints/Networkx-checkpoint.ipynb".
The first task is:
Edge Attributes
Podemos añadir y manipular los atributos de los vertices usando add_edge(), add_edges_from(), subscript notation, or G.edge.
Can you write Python code for it?

GPL.add_edge(2,5, weight=2)

GPL.add_edges_from([('Erika','Jane Doe'),('Jane Doe','Ale')], color='red')

GPL.add_edges_from([('Ale','Marco',{'color':'blue'}), ('Erika','Erin',{'weight':8}), (3,5,{'weight':8})])

GPL['Ale']['Erika']['weight'] = 3

GPL.edge['Ale']['Marco']['weight'] = 4

GPL.edge


I am working on the file "NetworkX/.ipynb_checkpoints/Networkx-checkpoint.ipynb".
The first task is:
Generadores de gráficas estocásticas, e.g.
Can you write Python code for it?


I am working on the file "NetworkX/.ipynb_checkpoints/Networkx-checkpoint.ipynb".
The first task is:
Nos faltan más miembros del grupo y sus datos... como lo hacemos?
Ejemplo:
Can you write Python code for it?

GPL.add_node('Jane Doe', edad = 25, entidad = 'Ciencias')

GPL.nodes(data=True)


I am working on the file "NetworkX/.ipynb_checkpoints/Networkx-checkpoint.ipynb".
The first task is:
O de diferentes tipos de archivos como por ejemplo ".graphml" o ".leda"
http://networkx.readthedocs.io/en/networkx-1.11/reference/readwrite.html
El archivo que vamos a usar para el ejemplo contiene información acerca de la conectividad entre las áreas del cerebro de un mono rhesus
Archivos como éste los podemos descargar de  http://openconnecto.me/graph-services/download/
Can you write Python code for it?

RB=nx.read_leda('rhesus_brain_2.leda')
#RB=nx.read_graphml('rhesus_brain_2.graphml')
nx.info(RB)

RB.node

nx.is_directed(RB)

RB.neighbors('VIP')

RB.node['VIP']

RB.edge['VIP']

nx.flow_hierarchy(RB)
#Returns the flow hierarchy of a directed network.
#Flow hierarchy is defined as the fraction of edges not participating in cycles in a directed graph

largest_sc = sorted(nx.strongly_connected_components(RB))
#largest_sc = max(nx.strongly_connected_components(RB))
largest_sc

AttarctComp=sorted(nx.attracting_components(RB))
AttarctComp

degree=(nx.degree_histogram(RB))
degreeX=(sc.arange(len(degree)))

gr.bar(degreeX, degree)
gr.title("Degree Histogram")
gr.xlabel("k")
gr.ylabel("Frequency")

fig = gr.gcf()

degree_sequence=sorted(degree,reverse=True) # degree sequence
#print "Degree sequence", degree_sequence
dmax=max(degree_sequence)

gr.loglog(degree_sequence,'b-',marker='o')
gr.title("Degree rank plot")
gr.ylabel("degree")
gr.xlabel("rank")

#gr.savefig("degree_histogram.png")
gr.show()


I am working on the file "NetworkX/.ipynb_checkpoints/Networkx-checkpoint.ipynb".
The first task is:
Podemos usar las funciones y algoritmos dentro de otras funciones
Can you write Python code for it?

degree=list();
clustering=list();

for n in sc.arange(len(GPL.node)):   # Some properties of the graph
    degree.append(sorted(nx.degree(GPL,[n]).values())) # así nos regresa solo los valores de grado
    #degree.append(nx.degree(GPL,[n]))
        #así nos regresa los nodos con su correspondiente valor de grado de conectividad
    clustering.append(nx.clustering(GPL,[n]))

print(degree)
print(clustering)

#¿que pasó aqui?... no esta accesando a la informacion de los nodos que son strings y no números... cuidado con eso!

G=nx.Graph()
e=[('a','b',0.3),('b','c',0.9),('a','c',0.5),('c','d',1.2)]
G.add_weighted_edges_from(e)

nx.dijkstra_path(G,'a','d') # encuentra el camino mas corto y con mayor peso entre los nodos


I am working on the file "NetworkX/.ipynb_checkpoints/Networkx-checkpoint.ipynb".
The first task is:
ahora vamos con los vertices!
Can you write Python code for it?

GPL.add_edge('Ale','Erika')
GPL.edge

e=('Erika','Jane Doe')
GPL.add_edge(*e)
GPL.edge

GPL.add_edges_from([('Jane Doe','Ale'),('Ale','Marco')])
GPL.number_of_edges()
# ¿pero que pasó aqui? Marco no estaba incluido en los nodos del grupo Pyladies

GPL.node


I am working on the file "NetworkX/NetworkX/Networkx.ipynb".
The first task is:
Ahora le vamos a agregar nodos
Can you write Python code for it?

"""
    Uno a uno
"""
G.add_node(1)

"""
    O en forma de lista
"""

G.add_nodes_from([2,3,4,5])

G.node


I am working on the file "NetworkX/NetworkX/Networkx.ipynb".
The first task is:
Borremos los nodos
Can you write Python code for it?


I am working on the file "NetworkX/NetworkX/Networkx.ipynb".
The first task is:
Clásicas gráficas pequeñas
Can you write Python code for it?


I am working on the file "NetworkX/NetworkX/Networkx.ipynb".
The first task is:
Dibujemos la gráfica
Can you write Python code for it?


I am working on the file "NetworkX/NetworkX/Networkx.ipynb".
The first task is:
Podemos usar las funciones y algoritmos dentro de otras funciones
Can you write Python code for it?


I am working on the file "NetworkX/NetworkX/Networkx.ipynb".
The first task is:
Tambien podemos importar a partir de varias estructuras de datos
Por ejemplo:
from_pandas_dataframe(df, source, target, edge_attr=None, create_using=None)
Can you write Python code for it?

import pandas as pd
import numpy as np
r = np.random.RandomState(seed=5)
ints = r.randint(1, 10, size=(3,2))
a = ['A', 'B', 'C']
b = ['D', 'A', 'E']
df = pd.DataFrame(ints, columns=['weight', 'cost'])
df['a'] = a
df['b'] = b
df

G=nx.from_pandas_dataframe(df, 'a', 'b', ['weight', 'cost'])
nx.info(G)

G['E']['C']['cost']


I am working on the file "NetworkX/NetworkX/Networkx.ipynb".
The first task is:
Ups!! ahora hay que borrar esos nodos!.. como lo hacemos??
Tip: remove_nodes_from
Can you write Python code for it?

GPL.remove_nodes_from(['A','l','e'])
GPL.node

GPL.add_node("Ale", edad = 30 )
GPL.node["Ale"]

GPL.node['Ale']['entidad'] = 'IFC'
GPL.node

GPL.number_of_nodes()


I am working on the file "NetworkX/NetworkX/Networkx.ipynb".
The first task is:
Vamos a darle nombre!
Se pueden asignar atributos a la gráfica como a cada uno de sus elementos. Los nodos pueden ser también strings!
Can you write Python code for it?

GPL=nx.Graph(Name=["Pyladies"], librería= "Scipy")
GPL.graph

"""
Ahora vamos a editar el atributo
"""

GPL.graph['librería']='Networkx'
GPL.graph

"""
Empecemos a añadir los nodos
"""

GPL.add_node('Erika')

GPL.add_nodes_from("Ale", edad = 30 )  # que pasa si hacemos esto???

GPL.nodes(data=True)


I am working on the file "NetworkX/Networkx.ipynb".
The first task is:
Ahora le vamos a agregar nodos
Can you write Python code for it?

"""
    Uno a uno
"""
G.add_node(1)

"""
    O en forma de lista
"""

G.add_nodes_from([2,3,4,5])

G.node

G.graph


I am working on the file "NetworkX/Networkx.ipynb".
The first task is:
Borremos los nodos
Can you write Python code for it?

"""
Uno a uno
"""
G.remove_node(3)
G.node

"""
Todos
"""

G.clear()
G.node

"""
Podemos añadir nodos a partir de una variable
"""

H=[5, 6, 7, 8, 9, 12]
G.add_nodes_from(H)
G.node

H=nx.path_graph(10)
G.add_nodes_from(H)
G.node
#print (H)

H.graph


I am working on the file "NetworkX/Networkx.ipynb".
The first task is:
Dibujemos la gráfica
Can you write Python code for it?


I am working on the file "NetworkX/Networkx.ipynb".
The first task is:
Edge Attributes
Podemos añadir y manipular los atributos de los vertices usando add_edge(), add_edges_from(), subscript notation, or G.edge.
Can you write Python code for it?

GPL.add_edge(2,5, weight=2)

GPL.add_edges_from([['Erika','Jane Doe'],['Jane Doe','Ale']], color='red')

GPL.add_edges_from([('Ale','Marco',{'color':'blue'}), ('Erika','Erin',{'weight':8}), (3,5,{'weight':8})])

GPL['Ale']['Erika']['weight'] = 3

GPL.edge['Ale']['Marco']['weight'] = 4

GPL.edge


I am working on the file "NetworkX/Networkx.ipynb".
The first task is:
Ejemplo:
Can you write Python code for it?

GPL.node

GPL.add_node('Jane Doe', edad = 25, entidad = 'Ciencias')

GPL.nodes(data=True)


I am working on the file "NetworkX/Networkx.ipynb".
The first task is:
Ejercicio
Ahora podemos practicar leyendo y analizando el archivo:   mixed.species_brain_1.graphml
Generadores de gráficas
Para hacer nuestros ejercicios podemos usar generadores de redes que ya tenemos a
disposición en Networkx por ejemplo:
barbell_graph(m1, m2[, create_using]) Return the Barbell Graph: two complete graphs connected by a path.
complete_graph(n[, create_using]) Return the complete graph K_n with n nodes.
complete_multipartite_graph(*block_sizes) Returns the complete multipartite graph with the specified block sizes.
dorogovtsev_goltsev_mendes_graph(n[, ...]) Return the hierarchically constructed Dorogovtsev-Goltsev-Mendes graph.
grid_graph(dim[, periodic]) Return the n-dimensional grid graph.
hypercube_graph(n) Return the n-dimensional hypercube.
lollipop_graph(m, n[, create_using]) Return the Lollipop Graph; Km connected to Pm.
path_graph(n[, create_using]) Return the Path graph P_n of n nodes linearly connected by n-1 edges.
wheel_graph(n[, create_using]) Return the wheel graph: a single hub node connected to each node of the (n-1)-node cycle graph
Can you write Python code for it?

W=nx.wheel_graph(10)
W.edge

G=nx.complete_graph(8)
G.edge


I am working on the file "NetworkX/Networkx.ipynb".
The first task is:
Generadores de gráficas estocásticas, e.g.
Can you write Python code for it?


I am working on the file "NetworkX/Networkx.ipynb".
The first task is:
Nos faltan más miembros del grupo y sus datos... como lo hacemos?
Can you write Python code for it?

names = ['Araceli', 'Erika', 'Desiré', 'Isaac']
ages = ['27', '30', 'x', '28']
GPL.add_nodes_from(names, edad = ages, entidad = 'UNAM')


I am working on the file "NetworkX/Networkx.ipynb".
The first task is:
Podemos usar las funciones y algoritmos dentro de otras funciones
Can you write Python code for it?

degree=list();
clustering=list();

for n in sc.arange(len(GPL.node)):   # Some properties of the graph
    degree.append(sorted(nx.degree(GPL,[n]).values())) # así nos regresa solo los valores de grado
    #degree.append(nx.degree(GPL,[n]))
        #así nos regresa los nodos con su correspondiente valor de grado de conectividad
    clustering.append(nx.clustering(GPL,[n]))

print(degree)
print(clustering)

#¿que pasó aqui?... no esta accesando a la informacion de los nodos que son strings y no números... cuidado con eso!

sorted(GPL.degree().values())

sorted(nx.degree(GPL).values())

G=nx.Graph()
e=[('a','b',0.3),('b','c',0.9),('a','c',0.5),('c','d',1.2)]
G.add_weighted_edges_from(e)

nx.dijkstra_path(G,'a','d') # encuentra el camino mas corto y con mayor peso entre los nodos


I am working on the file "NetworkX/Networkx.ipynb".
The first task is:
Tambien podemos importar a partir de varias estructuras de datos
Por ejemplo:
from_pandas_dataframe(df, source, target, edge_attr=None, create_using=None)
Can you write Python code for it?


I am working on the file "NetworkX/Networkx.ipynb".
The first task is:
También podemos generar la matriz adjacente a otros formatos
Can you write Python code for it?

A = nx.to_scipy_sparse_matrix(GPL)
print(A.todense())

A = nx.to_numpy_matrix(GPL)
print(A)

A = nx.to_pandas_dataframe(GPL)
print(A)


I am working on the file "NetworkX/Networkx.ipynb".
The first task is:
Vamos a darle nombre!
Se pueden asignar atributos a la gráfica como a cada uno de sus elementos. Los nodos pueden ser también strings!
Can you write Python code for it?


I am working on the file "NetworkX/Networkx.ipynb".
The first task is:
¿Que más le podemos preguntar a nuestra gráfica?

La distribución de conexiones (o vecinos) P (k): Es la probabilidad de que un nodo escogido al azar tenga k conexiones (o vecinos). (degree distribution)
Por ejemplo, en una red de contactos sexuales P (k) es la probabilidad de que una persona escogida al azar en una sociedad haya tenido k parejas sexuales distintas a lo largo de su vida.

El coeficiente de agrupamiento (C): Es la probabilidad de que dos nodos estén conectados directamente a un tercer nodo, estén conectados entre sí. (clustering coefficient)
Por ejemplo, en una red de amistades, es la probabilidad  de que dos de mis amigos sean ellos mismos amigos uno del otro.

La longitud mínima  entre dos nodos vi y vj: Es el número mínimo de “brincos”que se tienen que dar para llegar de un nodo vi de la red a otro nodo vj de la red. (path lenght)

La longitud promedio de la red (L): Es el promedio de las longitudes mínimas entre todas las posibles parejas de nodos (vi, vj) de la red.

Algoritmos y funciones
http://networkx.readthedocs.io/en/networkx-1.11/reference/algorithms.html
http://networkx.readthedocs.io/en/networkx-1.11/reference/functions.html
Podemos usar varios algoritmos y funciones que nos dan información acerca de la red, por ejemplo:
average_clustering(G, nodes=None, weight=None, count_zeros=True) : Es el coeficiente de agrupamiento.
Compute the average clustering coefficient for the graph G.
shortest_path_length(G[, source, target, weight]) Encuentra el camino mas corto entre los nodos
Compute shortest path lengths in the graph.
degree_histogram(G) Return a list of the frequency of each degree value.
info(G[, n])    Print short summary of information for the graph G or the node n.
Ejemplos:
Can you write Python code for it?

nx.info(GPL)
#nx.info(GPL, 'Ale')

nx.get_node_attributes(GPL,'Erika')

nx.clustering(GPL)

nx.degree(GPL)

nx.degree_histogram(GPL)


I am working on the file "Network_analysis.ipynb".
The first task is:
Load data.
Can you write Python code for it?

client_info = pd.read_csv('data/client_info.csv')
demographic_info = pd.read_csv('data/demographic_data.csv')
transaction_info = pd.read_csv('data/transction_info.csv')
order_info = pd.read_csv('data/order_info.csv')


I am working on the file "Network_analysis.ipynb".
The first task is:
Look at it in a directed graph.
Can you write Python code for it?

Gdi = nx.DiGraph()
Gdi.add_nodes_from(giant.nodes())
Gdi.add_edges_from(giant.edges())
nx.draw_circular(Gdi)


I am working on the file "Network_analysis.ipynb".
The first task is:
Look at the largest connected components.
A connected component is a cluster of nodes who are connected by an edge. This can uncover certain structures of possible unwanted behavor.
Can you write Python code for it?

giant = max(nx.connected_component_subgraphs(G), key=len)
nx.draw_circular(giant)


I am working on the file "Networks.ipynb".
The first task is:
1.  Constructia grafului pornind de la lista nodurilor si lista arcelor
Se defineste lista nodurilor, V, si lista arcelor, E, si apoi se apeleaza  pentru graful G metoda
add_nodes_from(V), respectiv add_edges_from(E).
Se pot adauga noduri/arce individuale
apeland metoda add_node()/add_edge():
Can you write Python code for it?


I am working on the file "Networks.ipynb".
The first task is:
Conform teoriei din cursul 11, vectorul rating asociat unei retele orientate este vectorul propriu
al valorii proprii dominante a matricii de conectivitate, transpusa:
Can you write Python code for it?

Lamb, V=np.linalg.eig(W.transpose()) # aflam radacinile polinomului caracteristic a matricii W^T
print Lamb.round(3)


I am working on the file "Networks.ipynb".
The first task is:
Deci nodul retelei cu cea  mai mare popularitate este nodul 4, urmat de 0, 3,2,1.
Sa aplicam acum aceasta procedura pentru retele neorientate si apoi retele orientate, folosind
pachetul networkx:
Definirea unui graf in networkx
Importam modulul networkx astfel:
Can you write Python code for it?


I am working on the file "Networks.ipynb".
The first task is:
Deci valoarea proprie dominanta este:
Can you write Python code for it?

lambD=np.amax(np.fabs(Lamb))# valoarea proprie dominanta calculata


I am working on the file "Networks.ipynb".
The first task is:
Linia urmatoare   defineste un graf vid,  G, neorientat (G este un obiect din clasa Graph):
Can you write Python code for it?


I am working on the file "Networks.ipynb".
The first task is:
Pentru  a calcula radacinile polinomului caracteristic al matricii $A$ si vectorii proprii corespunzatori, apelam functia np.linalg.eig(A) care returneaza array-ul 1D, Lamb, ce contine radacinile polinomului caracteristic si array-ul 2D, V,
care are pe o coloana j coordonatele unui vector propriu corespunzator valorii proprii Lamb[j].
Can you write Python code for it?

Lamb, V=np.linalg.eig(A)
print  'Radacinile polinomului caracteristic sunt\n', Lamb
print'\n iar vectorii proprii corespunzatori: \n', V.round(2)

print 'Vectorul propriu corespunzator valorii', Lamb[3], 'este:\n', V[:,3].round(2)


I am working on the file "Networks.ipynb".
The first task is:
Popularitatea nodurilor unei retele orientate
Constructia unei retele (graf) orientat se realizeaza la fel ca in cazul celor neorientate,
doar ca obiectul nu mai este declarat de tip Graph, ci DiGraph.
Can you write Python code for it?

H=nx.DiGraph()
n=5
Noduri=[k for k in range(n)]
Arce=[(0,3), (0,4), (1,2),(1,3), (1,4), (2,3), (4,1), (4,3)]
H.add_nodes_from(Noduri)
H.add_edges_from(Arce)
nx.draw(H, node_color='r', with_labels=True, alpha=0.5)


I am working on the file "Networks.ipynb".
The first task is:
Pozitionarea relativa a nodurilor este realizata conform algoritmului numit spring layout algorithm.
Exista mai multe modalitati de amplasare a nodurilor in spatiu, dar aceasta este cea mai convenabila pentru prezentarea noastra.
Extragem matricea de adiacenta a grafului:
Can you write Python code for it?

A=nx.adjacency_matrix(G)#  A este un obiect al unei clase speciale in networkx

#A.todense() defineste matricea de adiacenta ca un obiect al unei clase din numpy,
#dar NU clasa `numpy.array`

print A.todense()

print type(A.todense())


I am working on the file "Networks.ipynb".
The first task is:
Remarcam ca nodul 4 care are cel mai mare coeficient de popularitate are si cel mai mare
grad (este "cel mai conectat" nod din retea).
2. Constructia grafului neorientat pornind de la matricea sa de adiacenta.
Daca se da matricea de adiacenta, $A$,  a unui graf atunci graful este creat de functia:
G= nx.from_numpy_matrix(A):
Can you write Python code for it?


I am working on the file "Networks.ipynb".
The first task is:
Sa construim o retea orientata din matricea sa adiacenta si sa determinam popularitatea nodurilor:
Can you write Python code for it?


I am working on the file "Networks.ipynb".
The first task is:
Sa determinam coeficientul de popularitate a nodurilor acestei retele. Cum graful asociat este neorientat
matricea de adiacenta este simetrica si deci are sigur toate radacinile polinomului caracteristic, reale (Cursul 12).
Can you write Python code for it?

Lamb,V=np.linalg.eig(A)

lamb=np.amax(Lamb)# radacinile fiind reale, valoarea dominata este maximumul valorilor proprii
j=np.argmax(Lamb)#pozitia in Lamb a valorii maxime
print j
x=V[:,j]
print 'Valoarea proprie dominanta este:', lamb
print 'Vectorul propriu corespunzator:\n', x.round(3)


I am working on the file "Networks.ipynb".
The first task is:
Sa realizam acum ranking-ul nodurilor, sortand elementele vectorului $r$, descrescator si retinand
indicii ce dau pozitia initiala in r a elementelor sortate.
Can you write Python code for it?

ranking=np.argsort(r)[::-1]     #Functia np.argsort, sorteaza crescator array-ul 1D, rating,
                                # si returneaza indicii din r a elementelor sortate
                                # Pentru a gasi ordinea indicilor pentru sortarea descrescatoare
                                # se inverseaza elementele array-ului returnat de
                                # np.argsort(rating) folosind notatia tipica pt reversing, [::-1]
print ranking


I am working on the file "Neural Network/.ipynb_checkpoints/Neural network-checkpoint.ipynb".
The first task is:
Now we plot the two classes as a scatter plot!
Can you write Python code for it?

plt.scatter(xa, ya, color = 'b')
plt.hold(True)
plt.scatter(xb, yb, color = 'r')
plt.title('Scatter plot of the data, N = 30')


I am working on the file "Neural Network/.ipynb_checkpoints/Neural network-checkpoint.ipynb".
The first task is:
Now we want to classify this synthetic data using the perceptron model which will be trained using this data, and then we will test using the same data (this is called, self classification test). To proceed further we first need to train our perceptron model using the theory above.
Here the dimention of weight vector $\textbf{w}$ is 3 (as we just need to estimate a line). So we initilize the parameters as ones.
Can you write Python code for it?

w = np.ones(3, dtype=np.float64) # the weights
iter_max = 100 # maximum number of iterations
error = 100.0 # randomly initilize the classification error
it = 0 # variable to store the iteration number
eta = 0.02 # the step size (try varying this)
classified_labels = np.ones(N)


I am working on the file "Neural Network/.ipynb_checkpoints/Neural network-checkpoint.ipynb".
The first task is:
We can see that this perceptron model classifies the data very well :) lest check how close the weights are to the actual line we took to generate the data
Can you write Python code for it?

x = np.linspace(-1,1,100)
y = -(w[0] * x + w[2]) / w[1]
plt.plot(x,y,color='b')
x = np.linspace(-1,1,100)
y = -(a * x + c) / b
plt.hold(True)
plt.plot(x,y,color='r')
plt.legend(['predicted', 'original'])


I am working on the file "Neural Network/Neural network.ipynb".
The first task is:
Lets do some stylish plotting
Can you write Python code for it?

conf_arr = conf_mat

norm_conf = []
for i in conf_arr:
    a = 0
    tmp_arr = []
    a = sum(i, 0)
    for j in i:
        tmp_arr.append(float(j)/float(a))
    norm_conf.append(tmp_arr)

fig = plt.figure()
plt.clf()
ax = fig.add_subplot(111)
ax.set_aspect(1)
res = ax.imshow(np.array(norm_conf), cmap=plt.cm.Spectral,
                interpolation='none')
ax.grid('off')
width, height = np.array(conf_arr).shape

for x in xrange(width):
    for y in xrange(height):
        ax.annotate(str(conf_arr[x][y]), xy=(y, x),
                    horizontalalignment='center',
                    verticalalignment='center')

cb = fig.colorbar(res)
alphabet = ['car', 'fad','mas', 'glu', 'con', 'adi']
plt.xticks(range(width), alphabet[:width])
plt.yticks(range(height), alphabet[:height])


I am working on the file "Neural Network/Neural network.ipynb".
The first task is:
Now how do we solve for the parameters. Easy, we apply simple gradient descent on the objective function (the function of the parameters to be estimated, which is to be minimized). So we take the derivative of the equation (2) and we get
$$ \textbf{w}^{(l+1)} = \textbf{w}^{(l)} + \eta \sum \limits_{n \in \mathcal{M}} \phi (\textbf{x}_n) t_n $$
So now we start coding the actual parameter estimation part.
Can you write Python code for it?

while (error != 0 and it < iter_max):
    # Update Rules
    temp_vec = np.zeros(3, dtype=np.float64)
    temp = np.zeros(3, dtype=np.float64)
    for i in range(N):
        if label[i] != classified_labels[i]:
            temp += eta * np.array([x[i], y[i], 1]) * label[i]
    w += temp
    # recompute the classification
    for i in range(N):
        temp1 = w[0]*x[i] + w[1]*y[i] + w[2]
        if temp1 > 0:
            classified_labels[i] = 1
        else:
            classified_labels[i] = -1

    # compute the misclassification error
    error = 0
    for i in range(N):
        temp1 = w[0]*x[i] + w[1]*y[i] + w[2]
        if label[i] != classified_labels[i]:
            error += - label[i] * temp1

    w = w / np.linalg.norm(w, ord=2)
    it +=1

print("Done!")

x = np.linspace(-1,1,100)
y = -(w[0] * x + w[2]) / w[1]
plt.scatter(xa, ya, color = 'b')
plt.hold(True)
plt.scatter(xb, yb, color = 'r')
plt.plot(x,y, color='k')
plt.title('Perceptron classified data (the line)')


I am working on the file "Neural Network/Neural network.ipynb".
The first task is:
Now lets start with the class definition
So essentially the weights in the class is the list of arrays of weights between two layers, for example if the layers = [2,3,2], that will result in the figure 1 exactly. we will have weights[0] = 3x4 matrix and weights[1] = 4x2 matrix (refer to the figure one and see how this makes sense).
Now we move to training this neural network, where we will apply an algorithm called stochastic gradient descent its is same as standard gradient descent except for the fact that it makes an update to the weight vector using a single data point at a time. These data points are either selected by cycling through the data or choosing randomly. So we have
$$\textbf{w}^{(\tau + 1)} = \textbf{w}^{(\tau)} - \eta \nabla E_n(\textbf{w}^{(\tau)}) \quad \quad \quad (15)$$
So now using the above information we can substitute the value of the gradient we found by back propogation in the stochastic gradient equation. This is what we will use to write the training function for the class.
Can you write Python code for it?

class neural_network:
    def __init__(self, layers):
        """
        layers: A list containing the number of units in each layer.
                Should be at least two values (this also includes the input layer)
        actfn : String to select the type of activation function
        """

        self.act_fn = tanh # the activation function of the hidden layers
        self.act_fn_derv = tanh_derv # the derivative of the activation function of the hidden layers

        self.weights = [] # list of the weights
        # now we need to initialize these weights
        # We have to keep in mind that every layer except for the output will have one extra bias unit
        # we initialize the weights randomly between [-0.5,0.5]

        for i in range(1,len(layers) - 1):
            self.weights.append(0.5 * (2 * np.random.random([layers[i-1] + 1, layers[i] + 1]) - 1))

        # and for between the last hidden layer and the output layer
        self.weights.append(0.5 * (2 * np.random.random([layers[i] + 1, layers[i + 1]]) - 1))

    # This is the training function
    def train(self, X, t, eta = 0.05, max_iter = 10000):
        """
        X: Training data input set X = Nxdi, di= dimension of the input data > 1, N = number of data points
        t: Training data output set, t = Nxdo , do = dimension of the output data
        eta : double
            it is the step size for the stochastic gradient descent
        max_iter : int
            Number of iterations of the gradient descent
        """

        # first we need to add another unit to the input
        # as the input we have provided does not have a
        # bias input (which we set as one)
        X = np.atleast_2d(X) # google this command! it's just for smooth computation of matrices
        temp = np.ones((X.shape[0], X.shape[1] + 1))
        temp[:, 0:X.shape[1]] = X
        X = temp

        # Now for the stochastic gradient descent iterations

        for k in range(max_iter):
     ...


I am working on the file "Neural Network/Neural network.ipynb".
The first task is:
Now lets try to apply again the three layer neural network to make the classification and see how the self and cross classification error comes into play.
We work with self classification here, if one wants to modify the implementation of cross validation, the choose random 10% of the data to be kept as testing and train the model on remaining 90%, and then compare the classification accuracy.
Can you write Python code for it?

nn = neural_network([2, 2, 1])
inputs = np.zeros([N, 2])
inputs[:,0] = x
inputs[:,1] = y
nn.train(inputs, label, eta = 0.05)
labelout = np.ones(N)
xao = []
yao = []
xbo = []
ybo = []
for i in range(N):
    out = nn.predict_out(inputs[i,:])
    if(out > 0):
        xao.append(inputs[i,0])
        yao.append(inputs[i,1])
    else:
        xbo.append(inputs[i,0])
        ybo.append(inputs[i,1])
        labelout[i] = -1

plt.scatter(xao, yao, color = 'b')
plt.hold(True)
plt.scatter(xbo, ybo, color = 'r')
plt.title('Scatter plot of output')


I am working on the file "Neural Network/Neural network.ipynb".
The first task is:
Now we plot the two classes as a scatter plot!
Can you write Python code for it?


I am working on the file "Neural Network/Neural network.ipynb".
The first task is:
Now we want to classify this synthetic data using the perceptron model which will be trained using this data, and then we will test using the same data (this is called, self classification test). To proceed further we first need to train our perceptron model using the theory above.
Here the dimention of weight vector $\textbf{w}$ is 3 (as we just need to estimate a line). So we initilize the parameters as ones.
Can you write Python code for it?

w = np.ones(3, dtype=np.float64) # the weights
iter_max = 2000 # maximum number of iterations
error = 1000.0 # randomly initilize the classification error
it = 0 # variable to store the iteration number
eta = 0.001 # the step size (try varying this)
classified_labels = np.ones(N)


I am working on the file "Neural Network/Neural network.ipynb".
The first task is:
This classification does not work well, but in neural networks there are many things which can be introduced which will make this system extremely good (one of them is momentum, read it if interested). This same problem is tackled here and they use additional things to reach their required accuracy . Also maybe this system works for some other parameters who knows :D.
We are now just looking at the misshits, but that does not give us a correct indication of how are the errors due to the neural networks distributed. For this we use something called as a confusion matrix (https://en.wikipedia.org/wiki/Confusion_matrix), it is a simple idea do read the wikipedia article before moving further. So lets construct the confusion matrix!
Can you write Python code for it?

import sklearn
from sklearn.metrics import confusion_matrix

# Figure out why our confusion matrix will be of size 6x6
predict_arr = []
actual_arr = []
for i in range(X.shape[0]):
    temp = nn.predict_out(X[i,:])
    temp[temp != np.max(temp)] = 0
    temp[temp > 0] = 1
    if temp[0] == 1.0:
        predict_arr.append('car')
    if temp[1] == 1.0:
        predict_arr.append('fad')
    if temp[2] == 1.0:
        predict_arr.append('mas')
    if temp[3] == 1.0:
        predict_arr.append('glu')
    if temp[4] == 1.0:
        predict_arr.append('con')
    if temp[5] == 1.0:
        predict_arr.append('adi')

    temp = y[i,:]
    if temp[0] == 1:
        actual_arr.append('car')
    if temp[1] == 1:
        actual_arr.append('fad')
    if temp[2] == 1:
        actual_arr.append('mas')
    if temp[3] == 1:
        actual_arr.append('glu')
    if temp[4] == 1:
        actual_arr.append('con')
    if temp[5] == 1:
        actual_arr.append('adi')

conf_mat = confusion_matrix(actual_arr, predict_arr, labels = ['car', 'fad', 'mas', 'glu', 'con', 'adi'])

conf_mat


I am working on the file "Neural Network/Neural network.ipynb".
The first task is:
Try changing the N and see how the prediction changes! Now we will move on to see how this will help us in neural networks. Also if the system does not work, then try changing the eta and iterations (they may need to be tuned as we are generating different random data each time). Also the lines need not be equal as the same partition can be achieved with different lines :)
Multilayer Perceptron
Brief Intro to Neural Networks
In very short and sweet terms neural networks aims at making mathematical constructs for information processing mimicing biological systems. Disregarding several constraints that actual biological construct gives us, we can use the core idea of neural networks for many pattern recognition applications, the following aspects of the theory are taken from Christopher Bishop's book.
The most basic..
The multilayer perceptron that we are going to discuss now is one of the most simplest and widely used models for neural networks, it's also known as Feed Forward Networks.
The goal is to extend the regression model by making the $\phi (x_j)$ depend on some parameters which are to be estimated along with the set of weights ${w}_j$. So by (3) we now use simple linear comination to describe the neural network in terms of series of functional transformations
(A) M linear combinations of the inputs $(x_1,...,x_D)$ to generate activations
$$a_j = \sum \limits_{i=1}^D w_{ji}^{(1)}x_i + w_{j0}^{(1)} \quad \quad \quad (3)$$
The superscript (1) denotes the first layer of the neural network.
(B) Transformation of activations by a differentiable, non-linear activation function h(.)
$$z_i = h(a_i) \quad \quad \quad (4)$$
these $z_i$ are the hidden units and the two equations (3) and (4) combine to what looks like a perceptron model.
(C) Similar to inputs we have output activations which are constructed by linearly combining the hidden units
$$a_k = \sum \limits_{j=1}^{M} w_{kj}^{(2)}z_j + w_{k0}^{(2)} \quad \quad \quad (5)$$
(D) Lastly the output activations passed through a non-linear activation function...
Can you write Python code for it?

def tanh(x):
    return np.tanh(x)

def tanh_derv(x):
    # work the math out
    return 1.0 - np.tanh(x)**2


I am working on the file "Neural Network/Neural network.ipynb".
The first task is:
We see this works pretty well where the perceptron totally fails! Now lets do something more challanging.
The simulated data sectioned with non-linear function
We again generate the data but now not linerealy separated, so lets first do that
Can you write Python code for it?

N = 1000
x = np.zeros(N, dtype=np.float64)
y = np.zeros(N, dtype=np.float64)
for k in range(N):
    x[k], y[k] = [np.random.uniform(-1,1) for i in range(2)]

label = np.ones(N) # stores the labels for two classes, 1 for C1 and -1 for C2
xa = []
ya = []
xb = []
yb = []
N1 = 0
N2 = 0

# lets take the function y = 2x^2 - 1 to divide the points
for k in range(N):
    temp = y[k] - 2*x[k]**2 + 1
    if temp > 0:
        xa.append(x[k])
        ya.append(y[k])
        N1 += 1
    else:
        label[k] = -1
        xb.append(x[k])
        yb.append(y[k])
        N2 += 1

plt.scatter(xa, ya, color = 'b')
plt.hold(True)
plt.scatter(xb, yb, color = 'r')
plt.title('Scatter plot of the data, N = 500')


I am working on the file "Neural Networks And Deep Learning/Deep Neural Network - Application.ipynb".
The first task is:
2 - Outline of the Assignment
To build your neural network, you will be implementing several "helper functions". These helper functions will be used in the next assignment to build a two-layer neural network and an L-layer neural network. Each small helper function you will implement will have detailed instructions that will walk you through the necessary steps. Here is an outline of this assignment, you will:

Initialize the parameters for a two-layer network and for an $L$-layer neural network.
Implement the forward propagation module (shown in purple in the figure below).
Complete the LINEAR part of a layer's forward propagation step (resulting in $Z^{[l]}$).
We give you the ACTIVATION function (relu/sigmoid).
Combine the previous two steps into a new [LINEAR->ACTIVATION] forward function.
Stack the [LINEAR->RELU] forward function L-1 time (for layers 1 through L-1) and add a [LINEAR->SIGMOID] at the end (for the final layer $L$). This gives you a new L_model_forward function.

Compute the loss.
Implement the backward propagation module (denoted in red in the figure below).
Complete the LINEAR part of a layer's backward propagation step.
We give you the gradient of the ACTIVATE function (relu_backward/sigmoid_backward)
Combine the previous two steps into a new [LINEAR->ACTIVATION] backward function.
Stack [LINEAR->RELU] backward L-1 times and add [LINEAR->SIGMOID] backward in a new L_model_backward function

Finally update the parameters.

 Figure 1

Note that for every forward function, there is a corresponding backward function. That is why at every step of your forward module you will be storing some values in a cache. The cached values are useful for computing gradients. In the backpropagation module you will then use the cache to calculate the gradients. This assignment will show you exactly how to carry out each of these steps.
3 - Initialization
You will write two helper functions that...
Can you write Python code for it?

# GRADED FUNCTION: initialize_parameters

def initialize_parameters(n_x, n_h, n_y):
    """
    Argument:
    n_x -- size of the input layer
    n_h -- size of the hidden layer
    n_y -- size of the output layer

    Returns:
    parameters -- python dictionary containing your parameters:
                    W1 -- weight matrix of shape (n_h, n_x)
                    b1 -- bias vector of shape (n_h, 1)
                    W2 -- weight matrix of shape (n_y, n_h)
                    b2 -- bias vector of shape (n_y, 1)
    """

    np.random.seed(1)

    ### START CODE HERE ### (≈ 4 lines of code)
    W1 = np.random.randn(n_h, n_x) * 0.01
    b1 = np.zeros(shape=(n_h, 1))
    W2 = np.random.randn(n_y, n_h) * 0.01
    b2 = np.zeros(shape=(n_y, 1))
    ### END CODE HERE ###

    assert(W1.shape == (n_h, n_x))
    assert(b1.shape == (n_h, 1))
    assert(W2.shape == (n_y, n_h))
    assert(b2.shape == (n_y, 1))

    parameters = {"W1": W1,
                  "b1": b1,
                  "W2": W2,
                  "b2": b2}

    return parameters

parameters = initialize_parameters(2,2,1)
print("W1 = " + str(parameters["W1"]))
print("b1 = " + str(parameters["b1"]))
print("W2 = " + str(parameters["W2"]))
print("b2 = " + str(parameters["b2"]))


I am working on the file "Neural Networks And Deep Learning/Deep Neural Network - Application.ipynb".
The first task is:
Expected Output:

    **cost**
     0.41493159961539694

6 - Backward propagation module
Just like with forward propagation, you will implement helper functions for backpropagation. Remember that back propagation is used to calculate the gradient of the loss function with respect to the parameters.
Reminder:

 Figure 3 : Forward and Backward propagation for LINEAR->RELU->LINEAR->SIGMOID
 The purple blocks represent the forward propagation, and the red blocks represent the backward propagation.

Now, similar to forward propagation, you are going to build the backward propagation in three steps:
- LINEAR backward
- LINEAR -> ACTIVATION backward where ACTIVATION computes the derivative of either the ReLU or sigmoid activation
- [LINEAR -> RELU] $\times$ (L-1) -> LINEAR -> SIGMOID backward (whole...
Can you write Python code for it?

# GRADED FUNCTION: linear_backward

def linear_backward(dZ, cache):
    """
    Implement the linear portion of backward propagation for a single layer (layer l)

    Arguments:
    dZ -- Gradient of the cost with respect to the linear output (of current layer l)
    cache -- tuple of values (A_prev, W, b) coming from the forward propagation in the current layer

    Returns:
    dA_prev -- Gradient of the cost with respect to the activation (of the previous layer l-1), same shape as A_prev
    dW -- Gradient of the cost with respect to W (current layer l), same shape as W
    db -- Gradient of the cost with respect to b (current layer l), same shape as b
    """
    A_prev, W, b = cache
    m = A_prev.shape[1]

    ### START CODE HERE ### (≈ 3 lines of code)
    dW = np.dot(dZ, cache[0].T) / m
    db = np.squeeze(np.sum(dZ, axis=1, keepdims=True)) / m
    dA_prev = np.dot(cache[1].T, dZ)
    ### END CODE HERE ###

    assert (dA_prev.shape == A_prev.shape)
    assert (dW.shape == W.shape)
    assert (isinstance(db, float))

    return dA_prev, dW, db

# Set up some test inputs
dZ, linear_cache = linear_backward_test_case()

dA_prev, dW, db = linear_backward(dZ, linear_cache)
print ("dA_prev = "+ str(dA_prev))
print ("dW = " + str(dW))
print ("db = " + str(db))


I am working on the file "Neural Networks And Deep Learning/Deep Neural Network - Application.ipynb".
The first task is:
Expected Output:

     **dA_prev**
     [[  2.38272385   5.85438014]
 [  6.31969219  15.52755701]
 [ -3.97876302  -9.77586689]]

         **dW**
         [[ 2.77870358 -0.05500058 -5.13144969]]

         **db**
         5.527840195

6.2 - Linear-Activation backward
Next, you will create a function that merges the two helper functions: linear_backward and the backward step for the activation linear_activation_backward.
To help you implement linear_activation_backward, we provided two backward functions:
- sigmoid_backward: Implements the backward propagation for SIGMOID unit. You can call it as follows:
python
dZ = sigmoid_backward(dA, activation_cache)

relu_backward: Implements the backward propagation for RELU unit. You can call it as follows:

python
dZ = relu_backward(dA, activation_cache)
If $g(.)$ is the activation function,
sigmoid_backward and relu_backward compute $$dZ^{[l]} = dA^{[l]} * g'(Z^{[l]}) \tag{11}$$.
Exercise: Implement the backpropagation for the LINEAR->ACTIVATION layer.
Can you write Python code for it?

# GRADED FUNCTION: linear_activation_backward

def linear_activation_backward(dA, cache, activation):
    """
    Implement the backward propagation for the LINEAR->ACTIVATION layer.

    Arguments:
    dA -- post-activation gradient for current layer l
    cache -- tuple of values (linear_cache, activation_cache) we store for computing backward propagation efficiently
    activation -- the activation to be used in this layer, stored as a text string: "sigmoid" or "relu"

    Returns:
    dA_prev -- Gradient of the cost with respect to the activation (of the previous layer l-1), same shape as A_prev
    dW -- Gradient of the cost with respect to W (current layer l), same shape as W
    db -- Gradient of the cost with respect to b (current layer l), same shape as b
    """
    linear_cache, activation_cache = cache

    if activation == "relu":
        ### START CODE HERE ### (≈ 2 lines of code)
        dZ = relu_backward(dA, activation_cache)
        ### END CODE HERE ###

    elif activation == "sigmoid":
        ### START CODE HERE ### (≈ 2 lines of code)
        dZ = sigmoid_backward(dA, activation_cache)
        ### END CODE HERE ###

    # Shorten the code
    dA_prev, dW, db = linear_backward(dZ, linear_cache)

    return dA_prev, dW, db

AL, linear_activation_cache = linear_activation_backward_test_case()

dA_prev, dW, db = linear_activation_backward(AL, linear_activation_cache, activation = "sigmoid")
print ("sigmoid:")
print ("dA_prev = "+ str(dA_prev))
print ("dW = " + str(dW))
print ("db = " + str(db) + "\n")

dA_prev, dW, db = linear_activation_backward(AL, linear_activation_cache, activation = "relu")
print ("relu:")
print ("dA_prev = "+ str(dA_prev))
print ("dW = " + str(dW))
print ("db = " + str(db))


I am working on the file "Neural Networks And Deep Learning/Deep Neural Network - Application.ipynb".
The first task is:
Expected output:

     **Z**
     [[ 3.1980455   7.85763489]]

4.2 - Linear-Activation Forward
In this notebook, you will use two activation functions:

Sigmoid: $\sigma(Z) = \sigma(W A + b) = \frac{1}{ 1 + e^{-(W A + b)}}$. We have provided you with the sigmoid function. This function returns two items: the activation value "a" and a "cache" that contains "Z" (it's what we will feed in to the corresponding backward function). To use it you could just call:
python
A, activation_cache = sigmoid(Z)

ReLU: The mathematical formula for ReLu is $A = RELU(Z) = max(0, Z)$. We have provided you with the relu function. This function returns two items: the activation value "A" and a "cache" that contains "Z" (it's what we will feed in to the corresponding backward function). To use it you could just call:
python
A, activation_cache = relu(Z)

For more convenience, you are going to group two functions (Linear and Activation) into one function (LINEAR->ACTIVATION). Hence, you will implement a function that does the LINEAR forward step followed by an ACTIVATION forward step.
Exercise: Implement the forward propagation of the LINEAR->ACTIVATION layer. Mathematical relation is: $A^{[l]} = g(Z^{[l]}) = g(W^{[l]}A^{[l-1]} +b^{[l]})$ where the activation "g" can be sigmoid() or relu(). Use linear_forward() and the correct activation function.
Can you write Python code for it?

# GRADED FUNCTION: linear_activation_forward

def linear_activation_forward(A_prev, W, b, activation):
    """
    Implement the forward propagation for the LINEAR->ACTIVATION layer

    Arguments:
    A_prev -- activations from previous layer (or input data): (size of previous layer, number of examples)
    W -- weights matrix: numpy array of shape (size of current layer, size of previous layer)
    b -- bias vector, numpy array of shape (size of the current layer, 1)
    activation -- the activation to be used in this layer, stored as a text string: "sigmoid" or "relu"

    Returns:
    A -- the output of the activation function, also called the post-activation value
    cache -- a python dictionary containing "linear_cache" and "activation_cache";
             stored for computing the backward pass efficiently
    """

    if activation == "sigmoid":
        # Inputs: "A_prev, W, b". Outputs: "A, activation_cache".
        ### START CODE HERE ### (≈ 2 lines of code)
        Z, linear_cache = linear_forward(A_prev, W, b)
        A, activation_cache = sigmoid(Z)
        ### END CODE HERE ###

    elif activation == "relu":
        # Inputs: "A_prev, W, b". Outputs: "A, activation_cache".
        ### START CODE HERE ### (≈ 2 lines of code)
        Z, linear_cache = linear_forward(A_prev, W, b)
        A, activation_cache = relu(Z)
        ### END CODE HERE ###

    assert (A.shape == (W.shape[0], A_prev.shape[1]))
    cache = (linear_cache, activation_cache)

    return A, cache

A_prev, W, b = linear_activation_forward_test_case()

A, linear_activation_cache = linear_activation_forward(A_prev, W, b, activation = "sigmoid")
print("With sigmoid: A = " + str(A))

A, linear_activation_cache = linear_activation_forward(A_prev, W, b, activation = "relu")
print("With ReLU: A = " + str(A))


I am working on the file "Neural Networks And Deep Learning/Deep Neural Network - Application.ipynb".
The first task is:
Expected output:

     **W1**
    [[ 0.01788628  0.0043651   0.00096497 -0.01863493 -0.00277388]
 [-0.00354759 -0.00082741 -0.00627001 -0.00043818 -0.00477218]
 [-0.01313865  0.00884622  0.00881318  0.01709573  0.00050034]
 [-0.00404677 -0.0054536  -0.01546477  0.00982367 -0.01101068]]

    **b1**
    [[ 0.]
 [ 0.]
 [ 0.]
 [ 0.]]

    **W2**
    [[-0.01185047 -0.0020565   0.01486148  0.00236716]
 [-0.01023785 -0.00712993  0.00625245 -0.00160513]
 [-0.00768836 -0.00230031  0.00745056  0.01976111]]

    **b2**
    [[ 0.]
 [ 0.]
 [ 0.]]

4 - Forward propagation module
4.1 - Linear Forward
Now that you have initialized your parameters, you will do the forward propagation module. You will start by implementing some basic functions that you will use later when implementing the model. You will complete three functions in this order:

LINEAR
LINEAR -> ACTIVATION where ACTIVATION will be either ReLU or Sigmoid.
[LINEAR -> RELU] $\times$ (L-1) -> LINEAR -> SIGMOID (whole model)

The linear forward module (vectorized over all the examples) computes the following equations:
$$Z^{[l]} = W^{[l]}A^{[l-1]} +b^{[l]}\tag{4}$$
where $A^{[0]} = X$.
Exercise: Build the linear part of forward propagation.
Reminder:
The mathematical representation of this unit is $Z^{[l]} = W^{[l]}A^{[l-1]} +b^{[l]}$. You may also find np.dot() useful. If your dimensions don't match, printing W.shape may help.
Can you write Python code for it?

# GRADED FUNCTION: linear_forward

def linear_forward(A, W, b):
    """
    Implement the linear part of a layer's forward propagation.

    Arguments:
    A -- activations from previous layer (or input data): (size of previous layer, number of examples)
    W -- weights matrix: numpy array of shape (size of current layer, size of previous layer)
    b -- bias vector, numpy array of shape (size of the current layer, 1)

    Returns:
    Z -- the input of the activation function, also called pre-activation parameter
    cache -- a python dictionary containing "A", "W" and "b" ; stored for computing the backward pass efficiently
    """

    ### START CODE HERE ### (≈ 1 line of code)
    Z = np.dot(W, A) + b
    ### END CODE HERE ###

    assert(Z.shape == (W.shape[0], A.shape[1]))
    cache = (A, W, b)

    return Z, cache

A, W, b = linear_forward_test_case()

Z, linear_cache = linear_forward(A, W, b)
print("Z = " + str(Z))


I am working on the file "Neural Networks And Deep Learning/Logistic Regression with a Neural Network mindset.ipynb".
The first task is:
2 - Overview of the Problem set
Problem Statement: You are given a dataset ("data.h5") containing:
    - a training set of m_train images labeled as cat (y=1) or non-cat (y=0)
    - a test set of m_test images labeled as cat or non-cat
    - each image is of shape (num_px, num_px, 3) where 3 is for the 3 channels (RGB). Thus, each image is square (height = num_px) and (width = num_px).
You will build a simple image-recognition algorithm that can correctly classify pictures as cat or non-cat.
Let's get more familiar with the dataset. Load the data by running the following code.
Can you write Python code for it?

# Loading the data (cat/non-cat)
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()


I am working on the file "Neural Networks And Deep Learning/Logistic Regression with a Neural Network mindset.ipynb".
The first task is:

What you need to remember:
Common steps for pre-processing a new dataset are:
- Figure out the dimensions and shapes of the problem (m_train, m_test, num_px, ...)
- Reshape the datasets such that each example is now a vector of size (num_px * num_px * 3, 1)
- "Standardize" the data
3 - General Architecture of the learning algorithm
It's time to design a simple algorithm to distinguish cat images from non-cat images.
You will build a Logistic Regression, using a Neural Network mindset. The following Figure explains why Logistic Regression is actually a very simple Neural Network!

Mathematical expression of the algorithm:
For one example $x^{(i)}$:
$$z^{(i)} = w^T x^{(i)} + b \tag{1}$$
$$\hat{y}^{(i)} = a^{(i)} = sigmoid(z^{(i)})\tag{2}$$
$$ \mathcal{L}(a^{(i)}, y^{(i)}) =  - y^{(i)}  \log(a^{(i)}) - (1-y^{(i)} )  \log(1-a^{(i)})\tag{3}$$
The cost is then computed by summing over all training examples:
$$ J = \frac{1}{m} \sum_{i=1}^m \mathcal{L}(a^{(i)}, y^{(i)})\tag{6}$$
Key steps:
In this exercise, you will carry out the following steps:
    - Initialize the parameters of the model
    - Learn the parameters for the model by minimizing the cost
    - Use the learned parameters to make predictions (on the test set)
    - Analyse the results and conclude
4 - Building the parts of our algorithm ##
The main steps for building a Neural Network are:
1. Define the model structure (such as number of input features)
2. Initialize the model's parameters
3. Loop:
    - Calculate current loss (forward propagation)
    - Calculate current gradient (backward propagation)
    - Update parameters (gradient descent)
You often build 1-3 separately and integrate them into one function we call model().
4.1 - Helper functions
Exercise: Using your code from "Python Basics", implement sigmoid(). As you've seen in the figure above, you need to compute $sigmoid( w^T x + b)$ to make predictions.
Can you write Python code for it?

# GRADED FUNCTION: sigmoid

def sigmoid(z):
    """
    Compute the sigmoid of z

    Arguments:
    x -- A scalar or numpy array of any size.

    Return:
    s -- sigmoid(z)
    """

    ### START CODE HERE ### (≈ 1 line of code)
    s = 1 / (1 + np.exp(-z))
    ### END CODE HERE ###

    return s

print ("sigmoid(0) = " + str(sigmoid(0)))
print ("sigmoid(9.2) = " + str(sigmoid(9.2)))


I am working on the file "Neural Networks And Deep Learning/Logistic Regression with a Neural Network mindset.ipynb".
The first task is:
Expected Output for m_train, m_test and num_px:

    **m_train**
     209

    **m_test**
     50

    **num_px**
     64

For convenience, you should now reshape images of shape (num_px, num_px, 3) in a numpy-array of shape (num_px $$ num_px $$ 3, 1). After this, our training (and test) dataset is a numpy-array where each column represents a flattened image. There should be m_train (respectively m_test) columns.
Exercise: Reshape the training and test data sets so that images of size (num_px, num_px, 3) are flattened into single vectors of shape (num_px $$ num_px $$ 3, 1).
A trick when you want to flatten a matrix X of shape (a,b,c,d) to a matrix X_flatten of shape (b$$c$$d, a) is to use:
python
X_flatten = X.reshape(X.shape[0], -1).T      # X.T is the transpose of X
Can you write Python code for it?

# Reshape the training and test examples

### START CODE HERE ### (≈ 2 lines of code)
train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0], -1).T
test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0], -1).T
### END CODE HERE ###

print ("train_set_x_flatten shape: " + str(train_set_x_flatten.shape))
print ("train_set_y shape: " + str(train_set_y.shape))
print ("test_set_x_flatten shape: " + str(test_set_x_flatten.shape))
print ("test_set_y shape: " + str(test_set_y.shape))
print ("sanity check after reshaping: " + str(train_set_x_flatten[0:5,0]))


I am working on the file "Neural Networks And Deep Learning/Logistic Regression with a Neural Network mindset.ipynb".
The first task is:
Expected Output:

          ** dw **
         [[ 0.99993216]
 [ 1.99980262]]

          ** db **
         0.499935230625

          ** cost **
         6.000064773192205

d) Optimization

You have initialized your parameters.
You are also able to compute a cost function and its gradient.
Now, you want to update the parameters using gradient descent.

Exercise: Write down the optimization function. The goal is to learn $w$ and $b$ by minimizing the cost function $J$. For a parameter $\theta$, the update rule is $ \theta = \theta - \alpha \text{ } d\theta$, where $\alpha$ is the learning rate.
Can you write Python code for it?

# GRADED FUNCTION: optimize

def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost = False):
    """
    This function optimizes w and b by running a gradient descent algorithm

    Arguments:
    w -- weights, a numpy array of size (num_px * num_px * 3, 1)
    b -- bias, a scalar
    X -- data of shape (num_px * num_px * 3, number of examples)
    Y -- true "label" vector (containing 0 if non-cat, 1 if cat), of shape (1, number of examples)
    num_iterations -- number of iterations of the optimization loop
    learning_rate -- learning rate of the gradient descent update rule
    print_cost -- True to print the loss every 100 steps

    Returns:
    params -- dictionary containing the weights w and bias b
    grads -- dictionary containing the gradients of the weights and bias with respect to the cost function
    costs -- list of all the costs computed during the optimization, this will be used to plot the learning curve.

    Tips:
    You basically need to write down two steps and iterate through them:
        1) Calculate the cost and the gradient for the current parameters. Use propagate().
        2) Update the parameters using gradient descent rule for w and b.
    """

    costs = []

    for i in range(num_iterations):

        # Cost and gradient calculation (≈ 1-4 lines of code)
        ### START CODE HERE ###
        grads, cost = propagate(w, b, X, Y)
        ### END CODE HERE ###

        # Retrieve derivatives from grads
        dw = grads["dw"]
        db = grads["db"]

        # update rule (≈ 2 lines of code)
        ### START CODE HERE ###
        w = w - learning_rate * dw  # need to broadcast
        b = b - learning_rate * db
        ### END CODE HERE ###

        # Record the costs
        if i % 100 == 0:
            costs.append(cost)

        # Print the cost every 100 training examples
        if print_cost and i % 100 == 0:
            print ("Cost after iteration %i: %f" % (i, cost))

   ...


I am working on the file "Neural Networks And Deep Learning/Logistic Regression with a Neural Network mindset.ipynb".
The first task is:
Expected Output:

          ** w **
         [[ 0.]
 [ 0.]]

          ** b **
         0

For image inputs, w will be of shape (num_px $\times$ num_px $\times$ 3, 1).
4.3 - Forward and Backward propagation
Now that your parameters are initialized, you can do the "forward" and "backward" propagation steps for learning the parameters.
Exercise: Implement a function propagate() that computes the cost function and its gradient.
Hints:
Forward Propagation:
- You get X
- You compute $A = \sigma(w^T X + b) = (a^{(0)}, a^{(1)}, ..., a^{(m-1)}, a^{(m)})$
- You calculate the cost function: $J = -\frac{1}{m}\sum_{i=1}^{m}y^{(i)}\log(a^{(i)})+(1-y^{(i)})\log(1-a^{(i)})$
Here are the two formulas you will be using:
$$ \frac{\partial J}{\partial w} = \frac{1}{m}X(A-Y)^T\tag{7}$$
$$ \frac{\partial J}{\partial b} = \frac{1}{m} \sum_{i=1}^m (a^{(i)}-y^{(i)})\tag{8}$$
Can you write Python code for it?

# GRADED FUNCTION: propagate

def propagate(w, b, X, Y):
    """
    Implement the cost function and its gradient for the propagation explained above

    Arguments:
    w -- weights, a numpy array of size (num_px * num_px * 3, 1)
    b -- bias, a scalar
    X -- data of size (num_px * num_px * 3, number of examples)
    Y -- true "label" vector (containing 0 if non-cat, 1 if cat) of size (1, number of examples)

    Return:
    cost -- negative log-likelihood cost for logistic regression
    dw -- gradient of the loss with respect to w, thus same shape as w
    db -- gradient of the loss with respect to b, thus same shape as b

    Tips:
    - Write your code step by step for the propagation
    """

    m = X.shape[1]

    # FORWARD PROPAGATION (FROM X TO COST)
    ### START CODE HERE ### (≈ 2 lines of code)
    A = sigmoid(np.dot(w.T, X) + b)  # compute activation
    cost = (- 1 / m) * np.sum(Y * np.log(A) + (1 - Y) * (np.log(1 - A)))  # compute cost
    ### END CODE HERE ###

    # BACKWARD PROPAGATION (TO FIND GRAD)
    ### START CODE HERE ### (≈ 2 lines of code)
    dw = (1 / m) * np.dot(X, (A - Y).T)
    db = (1 / m) * np.sum(A - Y)
    ### END CODE HERE ###

    assert(dw.shape == w.shape)
    assert(db.dtype == float)
    cost = np.squeeze(cost)
    assert(cost.shape == ())

    grads = {"dw": dw,
             "db": db}

    return grads, cost

w, b, X, Y = np.array([[1], [2]]), 2, np.array([[1,2], [3,4]]), np.array([[1, 0]])
grads, cost = propagate(w, b, X, Y)
print ("dw = " + str(grads["dw"]))
print ("db = " + str(grads["db"]))
print ("cost = " + str(cost))


I am working on the file "Neural Networks And Deep Learning/Logistic Regression with a Neural Network mindset.ipynb".
The first task is:
Expected Output:

    **train_set_x_flatten shape**
     (12288, 209)

    **train_set_y shape**
    (1, 209)

    **test_set_x_flatten shape**
    (12288, 50)

    **test_set_y shape**
    (1, 50)

  **sanity check after reshaping**
  [17 31 56 22 33]

To represent color images, the red, green and blue channels (RGB) must be specified for each pixel, and so the pixel value is actually a vector of three numbers ranging from 0 to 255.
One common preprocessing step in machine learning is to center and standardize your dataset, meaning that you substract the mean of the whole numpy array from each example, and then divide each example by the standard deviation of the whole numpy array. But for picture datasets, it is simpler and more convenient and works almost as well to just divide every row of the dataset by 255 (the maximum value of a pixel channel).

Let's standardize our dataset.
Can you write Python code for it?

train_set_x = train_set_x_flatten / 255.
test_set_x = test_set_x_flatten / 255.


I am working on the file "Neural Networks And Deep Learning/Logistic Regression with a Neural Network mindset.ipynb".
The first task is:
Expected Output:

        **w**
       [[ 0.1124579 ]
 [ 0.23106775]]

        **b**
        1.55930492484

        **dw**
        [[ 0.90158428]
 [ 1.76250842]]

        **db**
        0.430462071679

Exercise: The previous function will output the learned w and b. We are able to use w and b to predict the labels for a dataset X. Implement the predict() function. There is two steps to computing predictions:

Calculate $\hat{Y} = A = \sigma(w^T X + b)$

Convert the entries of a into 0 (if activation <= 0.5) or 1 (if activation > 0.5), stores the predictions in a vector Y_prediction. If you wish, you can use an if/else statement in a for loop (though there is also a way to vectorize this).
Can you write Python code for it?

# GRADED FUNCTION: predict

def predict(w, b, X):
    '''
    Predict whether the label is 0 or 1 using learned logistic regression parameters (w, b)

    Arguments:
    w -- weights, a numpy array of size (num_px * num_px * 3, 1)
    b -- bias, a scalar
    X -- data of size (num_px * num_px * 3, number of examples)

    Returns:
    Y_prediction -- a numpy array (vector) containing all predictions (0/1) for the examples in X
    '''

    m = X.shape[1]
    Y_prediction = np.zeros((1, m))
    w = w.reshape(X.shape[0], 1)

    # Compute vector "A" predicting the probabilities of a cat being present in the picture
    ### START CODE HERE ### (≈ 1 line of code)
    A = sigmoid(np.dot(w.T, X) + b)
    ### END CODE HERE ###

    for i in range(A.shape[1]):
        # Convert probabilities a[0,i] to actual predictions p[0,i]
        ### START CODE HERE ### (≈ 4 lines of code)
        Y_prediction[0, i] = 1 if A[0, i] > 0.5 else 0
        ### END CODE HERE ###

    assert(Y_prediction.shape == (1, m))

    return Y_prediction

print("predictions = " + str(predict(w, b, X)))


I am working on the file "Neural Networks And Deep Learning/Logistic Regression with a Neural Network mindset.ipynb".
The first task is:
Expected Output:

         **Train Accuracy**
         99.04306220095694 %

        **Test Accuracy**
         70.0 %

Comment: Training accuracy is close to 100%. This is a good sanity check: your model is working and has high enough capacity to fit the training data. Test error is 68%. It is actually not bad for this simple model, given the small dataset we used and that logistic regression is a linear classifier. But no worries, you'll build an even better classifier next week!
Also, you see that the model is clearly overfitting the training data. Later in this specialization you will learn how to reduce overfitting, for example by using regularization. Using the code below (and changing the index variable) you can look at predictions on pictures of the test set.
Can you write Python code for it?

# Example of a picture that was wrongly classified.
index = 5
plt.imshow(test_set_x[:,index].reshape((num_px, num_px, 3)))
print ("y = " + str(test_set_y[0, index]) + ", you predicted that it is a \"" + classes[d["Y_prediction_test"][0, index]].decode("utf-8") +  "\" picture.")