I am working on the file "Integration.ipynb".
The first task is:
Fundamental Theorem of Calculus
Let $f(x)$ be a continuous function and define $F(x)$ by
$$ F(x) = \int_a^x f(s) ds . $$
Then $F(x)$ is uniformly continuous, differentiable, and
$$ F'(x) = f(x) . $$
We say that $F$ is an antiderivative of $f$.
This implies that
$$ \int_a^b f(x) dx = F(b) - F(a) . $$
We will test the accuracy of our integration schemes using an antiderivative provided in our tests.
Method of Manufactured Solutions
Analytically integrating an arbitrary function is hard, tends to require trickery, and is not always possible to express in closed form (e.g., elliptic integrals).  In contrast, analytic differentiation involves straightforward application of the product rule and chain rule.  So if we just choose an arbitrary function $F$ (the antiderivative), we can compute $f = F'$, then numerically integrate $\int_a^b f$ and compare to $F(b) - F(a)$.  We have used this technique to produce the tests array above.
Newton-Cotes methods
Newton-Cotes methods approximate $f(x)$ using piecewise polynomial functions.  Polynomials are easy to integrate analytically so we need only sum up the integrals over each piecewise interval.
Midpoint rule
The midpoint method uses piecewise constant functions over some number $n$ of intervals.
Can you write Python code for it?

def fint_midpoint(f, a, b, n=20):
    dx = (b - a)/n     # Width of each interval
    x = numpy.linspace(a+dx/2, b-dx/2, n)
    return numpy.sum(f(x))*dx

for t in tests:
    a, b = -2, 2.01
    I = fint_midpoint(t.f, a, b, 51)
    print('{:12s}: I={: 10e} error={: 10e}'.
          format(t.__name__, I, I - (t.F(b) - t.F(a))))

# How fast does this method converge?

def plot_accuracy(fint, tests, ns, ref=[1,2], plot=pyplot.loglog):
    a, b = -2, 2.001
    ns = numpy.array(ns)
    pyplot.figure()
    for t in tests:
        Is = numpy.array([fint(t.f, a, b, n) for n in ns])
        Errors = numpy.abs(Is - (t.F(b) - t.F(a)))
        plot(ns, Errors, '*', label=t.__name__)
    for k in ref:
        plot(ns, 1/ns**k, label='$1/n^{:d}$'.format(k))
    pyplot.ylabel('Error')
    pyplot.xlabel('n')
    pyplot.legend(loc='lower left')

plot_accuracy(fint_midpoint, tests, range(2,30))


I am working on the file "Integration.ipynb".
The first task is:
Measuring cost
A common situation with numerical integration is that the function $f(x)$ has a significant cost relative to the additions and multiplications needed to scale and sum the values.  The cost per integration point is constant for most methods that we will consider, therefore the number of function evaluations $n$ is a good measure of the cost.  Some algorithms may have higher cost per quadrature point in which case we will need to trade off the superlinear cost of quadrature with the linear cost of evaluating the function.
Trapezoid Rule
The trapezoid rule uses piecewise linear functions on each interval.
$$\begin{split} \int_a^b f(a) + \frac{f(b) - f(a)}{b - a} (x - a) &= f(a) (x-a) + \frac{f(b) - f(a)}{2(b - a)} (x - a)^2 \Big|_{x=a}^b \
&= f(a) (b-a) + \frac{f(b) - f(a)}{2(b - a)} (b-a)^2 \
&= \frac{b-a}{2} \big( f(a) + f(b) \big) .
\end{split} $$

Can you get to the same result using a geometric argument?
What happens when we sum over a bunch of adjacent intervals?
Can you write Python code for it?

x = numpy.linspace(-1,2,4)
print(x[0:2])
ix = [0,2]
print(x[ix])
print(x[[0,-1]])

def fint_trapezoid(f, a, b, n=20):
    dx = (b - a) / (n - 1)     # We evaluate endpoints of n-1 intervals
    x = numpy.linspace(a, b, n)
    fx = f(x)
    fx[[0,-1]] *= .5
    # fx[0] *= .5; fx[-1] *= .5
    return numpy.sum(fx)*dx

plot_accuracy(fint_trapezoid, tests, range(2,30))

for t in tests:
    a, b = -2, 2
    npoints = 10*2**numpy.arange(5)
    for n in npoints:
        I = fint_trapezoid(t.f, a, b, n)
        print('{:12s}: n={: 4d} I={: 10f} error={: 10f}'.
              format(t.__name__, n, I, I - (t.F(b) - t.F(a))))


I am working on the file "Integration.ipynb".
The first task is:
Observations
This scheme fits the $n$ data points with an $n-1$ degree polynomial, thus is exact for integrating polynomials of degree $n-1$.  The leading error term is $O(h^n)$ where $h$ is the width of the interval.
To integrate a complicated function, one can either raise the degree $n$ or partition the interval $(a,b)$.
In fint_legendre_cos, we created the Vandermonde matrix $P$ in the Legendre basis.  But since we only need $c_0$, we could have written c0 = inv(P)[0].dot(fx) (just use the first row of $P^{-1}$).  In other words, our integration scheme is
$$ \int_{a}^b \approx (b-a) \sum_i w_i f(x_i) $$
where w = inv(P)[0].  This set ${x_i, w_i}$ is called a quadrature.
Doing better
We used the cosspace points because they make interpolation stable (versus linsace, for example).  We needed interpolation to be well-behaved for the integral of the interpolating function to be a good approximation.  But perhaps there are other stable points that could allow us to integrate higher degree polynomials.  To this end, suppose a polynomial on the interval $[-1,1]$ can be written as
$$ P_n(x) q(x) + r(x) $$
where $P_n(x)$ is the $n$th Legendre polnomials and both $q(x)$ and $r(x)$ are polynomials of maximum degree $n-1$.

Can every polynomials of degree $2n-1$ be written in the above form?
How many roots does $P_n(x)$ have on the interval?
Can we choose points ${x_i}$ such that the first term is 0?

If $P_n(x_i) = 0$ for each $x_i$, then we need only integrate $r(x)$, which is done exactly by integrating its interpolating polynomial.  How do we find these roots $x_i$?
Derivatives of Legendre Polynomials
In the Interpolation unit, we learned that Legendre polynomials satisfy the recurrence
$$\begin{split}
P_0(x) &= 1 \
P_1(x) &= x \
(n+1) P_{n+1}(x) &= (2n+1) x P_n(x) - n P_{n-1}(x) .
\end{split}$$
It can also be shown that the derivatives satisfy a recurrence,
$$ P_{n+1}'(x) = (2n+1) P_n(x) + P_{n-1}'(x) . $$
With the ability to compute $P_n(x)$ and $P_n'(x)$, we can use Newton's method to compute the roots.
It...
Can you write Python code for it?

def fint_legendre(f, a, b, n):
    """Gauss-Legendre integration using Golub-Welsch algorithm"""
    beta = .5/numpy.sqrt(1-(2.*numpy.arange(1,n))**(-2))
    T = numpy.diag(beta,-1) + numpy.diag(beta, 1)
    D, V = numpy.linalg.eig(T)
    w = V[0,:]**2 * (b-a)
    x = (a+b)/2 + (b-a)/2 * D
    return w.dot(f(x))

fint_legendre(tests[0].f, -1, 1, 3)

plot_accuracy(fint_legendre, tests, range(3,25), ref=[2,3,4], plot=pyplot.semilogy)
pyplot.title('Gauss-Legendre Integration')

fint_legendre(lambda x: (x-.5)**9, -1, 1, n=5) - ((1-.5)**10/10 - (-1-.5)**10/10)


I am working on the file "Integration/Integration.ipynb".
The first task is:

Fundamental Theorem of Calculus
$f(x)$ is continuous in $[a,b]$
$F(x) = \int^x_af(t)dt$
- where $x$ is in $[a,b]$
$\frac{dF}{dx} = \frac{d}{dx}\int^x_af(t)dt = f(x)$
Example:
$F(x) = \int^x_a\frac{\cos^2t}{-\sin t^2}dt$
$F\prime(x) = \frac{d}{dx}\int^x_a\frac{\cos^2t}{-\sin t^2}dt = \frac{\cos^2x}{-\sin x^2}$
Example 2:
$F(x) = \int^{x^2}_a\frac{\cos^2t}{-\sin t^2}dt$
$F\prime(x) = \frac{d}{dx}\int^{x^2}_a\frac{\cos^2t}{-\sin t^2}dt$
$= \frac{\cos^2x^2}{-\sin x^4}\times \frac{d}{dx}x^2$
$= 2\frac{\cos^2x^2}{-\sin x^4}$

Basic Integration

Integrating powers of x
$\int Ax^ndx = \frac{A}{n+1}x^{n+1} + C$

to find the derivative we use $\frac{d}{dx}ax^n = anx^{n-1}$
we do the opposite with $\int ax^ndx = a\frac{1}{n+1}x^{n+1}$
we add $C$ as we cant find out the constant of the original function

Example
$\int 2x^5dx = \frac{1}{3}x^{6} + C$

Integrating other basic terms
Integrating $e^{kx}$
$\int Ae^{kx + b} dx = \frac{A}{k}e^{kx + b} + C$

the derivative is $\frac{d}{dx}e^x = e^x$
to differentiate, we would use the chain rule on the function of x and $\therefore$ multiply by k

Example
$\int 3e^{9x + 2} dx = \frac{1}{3}e^{9x + 2} + C$
Integrating $\frac{1}{x}$
$\int A\frac{n}{x} dx = An\ln x + C$
$\int A\frac{f\prime(x)}{f(x)} dx = A\ln|f(x)| + C$

in the second rule, the top is caused by the chain rule

Example
$\int 2\frac{6}{x} dx = 12\ln x + C$
Example 2
$\int 2\frac{10x}{5x^2 + 3} dx = 2\ln |5x^2 + 3| + C$
Integrating $\sin x$
$\int A\sin(kx) dx = -A\frac{1}{k}\cos(kx) + C$
Example
$\int 4\sin(2x) dx = -2\cos(2x) + C$
Integrating $\cos x$
$\int A\cos(kx) dx = A\frac{1}{k}\sin(kx) + C$
Example
$\int 11\cos(3x) dx = \frac{11}{3}\sin(3x) + C$

Definite Integrals
This is where there are defined boundaries on the x or y axis

Area under graph
$F(x) = \int f(x)dx$
$\int_a^b f(x)dx = F(b) - F(a)$

if the...
Can you write Python code for it?

x = np.linspace(-5, 5, 201)

def f(x):
    return 6*x**2 - 20

def F(x):
    return 2*x**3 - 20*x

y = f(x)
start = 60
end = 160
section = x[start:end+1]

fig, ax = plt.subplots(1, figsize=(8,4))
ax.plot(x,y, 'g', label='y = 2x')

ax.fill_between(section,f(section), color='blue', alpha=0.3, label='area under graph')
ax.plot(x[start], 0, 'om', color='purple', label='a')
ax.plot(x[end], 0, 'om', color='r', label='b')

ax.grid(True)
ax.legend()
plt.show()

print 'shaded net area =', F(x[end]) - F(x[start])


I am working on the file "Integration/Integration.ipynb".
The first task is:
Adaptive methods and higher order approximations
In some cases, particularly for integrands that are rapidly varying, a very large number of steps may be needed to achieve the desired accuracy, which means the calculation can become slow.
So how do we choose the number $N$ of steps for our integrals? In our example calculations we just chose round numbers and looked to see if the results seemed reasonable.  A more common situation is that we want to calculate the value of an integral to a given accuracy, such as four decimal places, and we would like to know how many steps will be needed. So long as the desired accuracy does not exceed the fundamental limit set by the machine precision of our computer— the rounding error that limits all calculations—then it should always be possible to meet our goal by using a large enough number of steps. At the same time, we want to avoid using more steps than are necessary, since more steps take more time and our calculation will be slower.
Ideally we would like an $N$ that gives us the accuracy we want and no more.  A simple way to achieve this is to start with a small value of $N$ and repeatedly double it until we achieve the accuracy we want.  This method is an example of an adaptive integration method, one that changes its own parameters to get a desired answer.
The trapezoidal rule is based on approximating an integrand $f(x)$ with straight-line segments, while Simpson’s rule uses quadratics. We can create higher-order (and hence potentially more accurate) rules by using higher-order polynomials, fitting $f(x)$ with cubics, quartics, and so forth. The general form of the trapezoidal and Simpson rules is
$$
\int_a^b f(x)dx \simeq \sum\limits_{k=1}^{N}w_kf(x_k)
$$
where the $x_k$ are the positions of the sample points at which we calculate the integrand and the $w_k$ are some set of weights.  In the trapezoidal rule, the first and last weights are $\tfrac{1}{2}$ and the others are all 1, while in Simpson’s rule the weights are $\tfrac{1}{3}$ for the first and last...
Can you write Python code for it?

import scipy.integrate as integrate

integrate?


I am working on the file "Integration/Integration.ipynb".
The first task is:
An overview of the module is provided by the help command, but it produces a lot of output.  Here's a quick summary:
Methods for Integrating Functions given function object.
quad          -- General purpose integration.
dblquad       -- General purpose double integration.
tplquad       -- General purpose triple integration.
fixed_quad    -- Integrate func(x) using Gaussian quadrature of order n.
quadrature    -- Integrate with given tolerance using Gaussian quadrature.
romberg       -- Integrate func using Romberg integration.

Methods for Integrating Functions given fixed samples.
trapz         -- Use trapezoidal rule to compute integral from samples.
cumtrapz      -- Use trapezoidal rule to cumulatively compute integral.
simps         -- Use Simpson's rule to compute integral from samples.
romb          -- Use Romberg Integration to compute integral from (2**k + 1) evenly-spaced samples.

See the special module's orthogonal polynomials (scipy.special) for Gaussian quadrature roots and weights for other weighting factors and regions.
Interface to numerical integrators of ODE systems.
odeint        -- General integration of ordinary differential equations.
ode           -- Integrate ODE using VODE and ZVODE routines.

General integration (quad)
The scipy function quad is provided to integrate a function of one variable between two points. The points can be  $\pm\infty$ ($\pm$ np.infty) to indicate infinite limits. For example, suppose you wish to integrate the following:
$$
I = \int_0^{2\pi} e^{-x}\sin(x)dx
$$
This could be computed using quad as:
Can you write Python code for it?

fun = lambda x : np.exp(-x)*np.sin(x)

result,error = integrate.quad(fun, 0, 2*np.pi)

print(result,error)


I am working on the file "Integration/Integration.ipynb".
The first task is:
In this case the analytic solution is exactly 1/2, so again pretty good.
We can calculate the error in the result by looking at the difference between the exact result and the numerical value from quad with
Can you write Python code for it?


I am working on the file "Integration/Integration.ipynb".
The first task is:
In this case, the numerically-computed integral is within $10^{-16}$ of the exact result — well below the reported error bound.
Integrating array data
When you want to compute the integral for an array of data (such as our thermistor resistance-temperature data from the Interpolation lesson), you don't have the luxury of varying your choice of $N$, the number of slices (unless you create an interpolated approximation to your data).
There are three functions for computing integrals given only samples: trapz , simps, and romb.  The trapezoidal rule approximates the function as a straight line between adjacent points while Simpson’s rule approximates the function between three adjacent points as a parabola, as we have already seen. The first two functions can also handle non-equally-spaced samples (something we did not code ourselves) which is a useful extension to these integration rules.
If the samples are equally-spaced and the number of samples available is $2^k+1$ for some integer $k$, then Romberg integration can be used to obtain high-precision estimates of the integral using the available samples. Romberg integration is an adaptive method that uses the trapezoid rule at step-sizes related by a power of two and then performs something called Richardson extrapolation on these estimates to approximate the integral with a higher-degree of accuracy. (A different interface to Romberg integration useful when the function can be provided is also available as romberg).
Applying simps to array data
Here is an example of using simps to compute the integral for some discrete data:
Can you write Python code for it?

x = np.arange(0, 20, 2)
y = np.array([0, 3, 5, 2, 8, 9, 0, -3, 4, 9], dtype = float)
plt.plot(x,y)
plt.xlabel('x')
plt.ylabel('y')
#Show the integration area as a filled region
plt.fill_between(x, y, y2=0,color='red',hatch='//',alpha=0.2);

I = integrate.simps(y,x)
print(I)


I am working on the file "Integration/Integration.ipynb".
The first task is:
Introduction
We often calculate integrals in physics (electromagnetism, thermodynamics, quantum mechanics, etc.).  In calculus, you learned how to evaluate integrals analytically.  Some functions are too difficult to integrate analytically and for these we need to use the computer to integrate numerically.  A numerical integral goes back to the basic principles of calculus.  Given a function $f(x)$, we need to find the area under the curve between two limits, $a$ and $b$:
$$
I(a,b) = \int_a^b f(x) dx
$$
There is no known way to calculate such an area exactly in all cases on a computer, but we can do it approximately by dividing up the area into rectangular slices and adding them all together.  Unfortunately, this is a poor approximation, since the rectangles under and overshoot the function:

Trapezoidal Rule
A better approach, which involves very little extra work, is to divide the area into trapezoids rather than rectangles.  The area under the trapezoids is a considerably better approximation to the area under the curve, and this approach, though simple, often gives perfectly adequate results.

We can improve the approximation by making the size of the trapezoids smaller.  Suppose we divide the interval from $a$ to $b$ into $N$ slices or steps, so that each slice has width $h = (b − a)/N$ . Then the right-hand side of the $k$ th slice falls at $a+kh$, and the left-hand side falls at $a+kh−h$ = $a+(k−1)h$ . Thus the area of the trapezoid for this slice is
$$
A_k = \tfrac{1}{2}h[ f(a+(k−1)h)+ f(a+kh) ]
$$
This is the trapezoidal rule. It gives us a trapezoidal approximation to the area under one slice of our function.
Now our approximation for the area under the whole curve is the sum of the areas of the trapezoids for all $N$ slices
$$
I(a,b) \simeq \sum\limits_{k=1}^N A_k = \tfrac{1}{2}h \sum\limits_{k=1}^N [ f(a+(k−1)h)+ f(a+kh) ] = h \left[ \tfrac{1}{2}f(a) + \tfrac{1}{2}f(b) + \sum\limits_{k=1}^{N-1} f(a+kh)\right]
$$
Note the structure...
Can you write Python code for it?


I am working on the file "Integration/Integration.ipynb".
The first task is:
The correct answer is
$$
I(0,2) = \int_0^2 (x^4-2x+1)dx = \left[\tfrac{1}{5}x^5-x^2+x\right]_0^2 = 4.4
$$
So our result is off by about 2%.
Simpson's Rule
The trapezoidal rule estimates the area under a curve by approximating the curve with straight-line segments.  We can often get a better result if we approximate the function instead with curves of some kind.  Simpson's rule uses quadratic curves.  In order to specify a quadratic completely one needs three points, not just two as with a straight line. So in this method we take a pair of adjacent slices and fit a quadratic through the three points that mark the boundaries of those slices.
Given a function $f(x)$ and spacing between adjacent points $h$, if we fit a quadratic curve $ax^2 + bx + c$  through the points $x$ = $-h$, 0, $+h$, we get
$$
f(-h) = ah^2 - bh + c, \hspace{1cm} f(0) = c, \hspace{1cm} f(h) = ah^2 +bh +c
$$
Solving for $a$, $b$, and $c$ gives:
$$
a = \frac{1}{h^2}\left[\tfrac{1}{2}f(-h) - f(0) + \tfrac{1}{2}f(h)\right], \hspace{1cm} b = \frac{1}{2h}\left[f(h)-f(-h)\right], \hspace{1cm} c = f(0)
$$
and the area under the curve of $f(x)$ from $-h$ to $+h$ is given approximately by the area under the quadratic:
$$
I(-h,h) \simeq \int_{-h}^h (ax^2+bx+c)dx = \tfrac{2}{3}ah^3 + 2ch = \tfrac{1}{3}h[f(-h)+4f(0)+f(h)]
$$
This is Simpson’s rule. It gives us an approximation to the area under two adjacent slices of our function. Note that the final formula for the area involves only $h$ and the value of the function at evenly spaced points, just as with the trapezoidal rule. So to use Simpson’s rule we don’t actually have to worry about the details of fitting a quadratic—we just plug numbers into this formula and it gives us an answer. This makes Simpson’s rule almost as simple to use as the trapezoidal rule, and yet Simpson’s rule often gives much more accurate answers.
Applying Simpson’s rule involves dividing the domain of integration into many slices and using the rule to separately estimate the area under successive pairs of slices, then adding...
Can you write Python code for it?


I am working on the file "Integration/Integration.ipynb".
The first task is:
Triple integrals using tplquad
We can also numerically evaluate a triple integral:
$$ \int_{x=0}^{\pi}\int_{y=0}^{1}\int_{z=-1}^{1} y\sin(x)+z\cos(x) dxdydz$$
Can you write Python code for it?

from scipy.integrate import tplquad

#AGAIN: the order of arguments matters - inner to outer
integrand = lambda x,y,z: y * np.sin(x) + z * np.cos(x)

zmin = -1
zmax = 1

ymin = lambda z: 0
ymax = lambda z: 1

#Note the order of these arguments:
xmin = lambda y,z: 0
xmax = lambda y,z: np.pi

#Here the order of limits is outer to inner
I, err = tplquad(integrand, zmin, zmax, ymin, ymax, xmin, xmax)
print(I, err)


I am working on the file "Integration/IntegrationEx01.ipynb".
The first task is:
Now use scipy.integrate.quad to integrate the f and g functions and see how the result compares with your trapz function. Print the results and errors.
Can you write Python code for it?

integrate.quad(f,0,1)

trapz(f, 0, 1, 1000)

integrate.quad(g,0,np.pi)

trapz(g, 0, np.pi, 1000)

assert True # leave this cell to grade the previous one


I am working on the file "Integration/IntegrationEx02.ipynb".
The first task is:
Indefinite integrals
Here is a table of definite integrals. Many of these integrals has a number of parameters $a$, $b$, etc.
Find five of these integrals and perform the following steps:

Typeset the integral using LateX in a Markdown cell.
Define an integrand function that computes the value of the integrand.
Define an integral_approx funciton that uses scipy.integrate.quad to peform the integral.
Define an integral_exact function that computes the exact value of the integral.
Call and print the return value of integral_approx and integral_exact for one set of parameters.

Here is an example to show what your solutions should look like:
Example
Here is the integral I am performing:
$$ I_1 = \int_0^\infty \frac{dx}{x^2 + a^2} = \frac{\pi}{2a} $$
Can you write Python code for it?

def integrand(x, a):
    return 1.0/(x**2 + a**2)

def integral_approx(a):
    # Use the args keyword argument to feed extra arguments to your integrand
    I, e = integrate.quad(integrand, 0, np.inf, args=(a,))
    return I

def integral_exact(a):
    return 0.5*np.pi/a

print("Numerical: ", integral_approx(1.0))
print("Exact    : ", integral_exact(1.0))

assert True # leave this cell to grade the above integral


I am working on the file "Integration/IntegrationEx02.ipynb".
The first task is:
Integral 1
$$ I_1 = \int_0^a {\sqrt{a^2-x^2} dx} = \frac{\pi a^2}{4} $$
Can you write Python code for it?

def integrand(x, a):
    return np.sqrt(a**2 - x**2)

def integral_approx(a):
    # Use the args keyword argument to feed extra arguments to your integrand
    I, e = integrate.quad(integrand, 0, a, args=(a,))
    return I

def integral_exact(a):
    return 0.25*np.pi

print("Numerical: ", integral_approx(1.0))
print("Exact    : ", integral_exact(1.0))

assert True # leave this cell to grade the above integral


I am working on the file "Integration/IntegrationEx02.ipynb".
The first task is:
Integral 3
$$ I_3 = \int_0^{2\pi} \frac{dx}{a+b\sin{x}} = {\frac{2\pi}{\sqrt{a^2-b^2}}} $$
Can you write Python code for it?

def integrand(x,a,b):
        return 1/(a+ b*np.sin(x))

def integral_approx(a,b):
    I, e = integrate.quad(integrand, 0, 2*np.pi,args=(a,b))
    return I

def integral_exact(a,b):
    return 2*np.pi/np.sqrt(a**2-b**2)

print("Numerical: ", integral_approx(10,0))
print("Exact    : ", integral_exact(10,0))

assert True # leave this cell to grade the above integral


I am working on the file "Integration/IntegrationEx02.ipynb".
The first task is:
Integral 5
$$ I_5 = \int_0^{\infty} \frac{x}{e^{x}-1} = {\frac{\pi^2}{6}} $$
Can you write Python code for it?

def integrand(x):
    return x/(np.exp(x)-1)

def integral_approx():
    I, e = integrate.quad(integrand, 0, np.inf)
    return I

def integral_exact():
    return (1/6)*np.pi**2

print("Numerical: ", integral_approx())
print("Exact    : ", integral_exact())

assert True # leave this cell to grade the above integral


I am working on the file "IntegrationNumerique.ipynb".
The first task is:
Calcul de $h$, le pas. Avec $n$ valeurs de $x$, on a $n-1$ intervalles.
Can you write Python code for it?


I am working on the file "IntegrationNumerique.ipynb".
The first task is:
Note sur la documentation :
--no-import-all  Prevent IPython from performing `import *` into the interactive namespace.
Étude d'une fonction
Dans cet exemple, nous étudierons la fonction suivante :
\begin{equation}
    f(x) = (x^2 - 3x - 6) \exp\left(-x\right)
\end{equation}
Cette fonction est intégrable analytiquement. Il est alors nécessaires de faire deux intégrations par partie successives.
Commençons par définir une fonction qui renvoie l'image de la fonction $f$ pour une valeur de $x$. La fonction exponnentielle n'est pas disponnible dans python par défaut, il faut la prendre dans le module numpy :
Can you write Python code for it?

def fonction(x):
    """ Retourne la valeur de la fonction en x """
    return (x**2 - 3*x - 6) * np.exp(-x)


I am working on the file "IntegrationNumerique.ipynb".
The first task is:
On choisit le nombre de valeurs de x :
Can you write Python code for it?


I am working on the file "Integração Numérica.ipynb".
The first task is:
Em seguida, nós plotamos o resultado. Devido a que a função v_xy retorna uma lista, o processo de extrair os dados fica um pouco mais complicado, e nós vamos omitir a explicação para essa sintaxe.
Can you write Python code for it?


I am working on the file "Integração Numérica.ipynb".
The first task is:
Modelo predador-presa de Lotka-Volterra
O que acontece quando temos um sistema com mais do que uma variável de estado? O que acontece, por exemplo, quando temos duas populações evoluindo e interagindo, ao invés de apenas uma? Bom, para estudar isso, nós precisamos de mais do que uma equação diferencial. Entretanto, os métodos que nós utilizamos para uma equação se estendem para sistemas de quaiser número de equações! Nós não precisamos mudar nossa função solveE, e podemos estender nossa análise da derivada de $x(t)$ à sistemas de equações envolvendo várias derivadas.
Para demonstrar a função que escrevemos em Python para integrar numericamente sistemas de equações diferenciais, vamos tentar integrar um sistema com duas equações diferenciais. Para modelar um sistema biológico em que duas populações interagem ecologicamente através da predação, Lotka e Volterra cada um independentemente desenvolveu o seguinte modelo:
\begin{eqnarray}
\frac{dx}{dt} =  ax - bxy \
\frac{dy}{dt} = -cy + dxy
\end{eqnarray}
Onde

$x$: população de presas
$y$: população de predadores
$a$: taxa de natalidade de presas
$b$: taxa de predação de $x$ por $y$
$c$: taxa de mortalidade de predadores
$d$: eficiência de conversão de biomassa de presa para predador

Vamos escrever uma função em Python que represente o modelo de Lotka-Volterra. Para tanto, como primeiro argumento da nossa função nós temos que passar uma lista v com 2 valores, um para cada variável de estado. No corpo da função nós vamos extrair esses valores e lhes dar nomes x e y apenas por clareza.
Can you write Python code for it?

def v_xy(v, a = 1.5, b = 1, c = 3, d = 1):
    'Determina a evolução de um sistema predador-presa\
    do tipo Lotka-Volterra no instante "t". Retorna um\
    vetor "v_n" com "v_n[0]" sendo as presas e "v_n[1]"\
    os predadores.'
    x = v[0]
    y = v[1]
    dx = a*x - b*x*y
    dy = -c*y + d*x*y
    vn = np.array([dx,dy])
    return vn


I am working on the file "Integração Numérica.ipynb".
The first task is:
Nós colocamos as os parâmetros r e k como variáveis padrão da nossa função g_x para que não seja preciso declarar esses valores quando invocarmos a função. Por exemplo:
Can you write Python code for it?


I am working on the file "Integração Numérica.ipynb".
The first task is:
Nós precisamos converter essa lista para um vetor do Numpy se não quisermos que nosso código forneça algum erro estranho:
Can you write Python code for it?


I am working on the file "Intensity normalization test.ipynb".
The first task is:
Save the standard histogram:
Save the histogram to apply it to unseen images afterwards:
Can you write Python code for it?

standard_path = 'histograms/standard_test.npy'
np.save(standard_path, [standard_scale, perc])


I am working on the file "IntentMatching/sentence_similarity_gensim_wmd.ipynb".
The first task is:
Algorithm
Can you write Python code for it?

print ("provide your intent")
input_intent = input()
intent_similiarity_map = dict()

print ("\n")
for each in intent_list:

    #calculate distance between two sentences using WMD algorithm (Word Movers Distance)
    distance = model.wmdistance(each, input_intent)
    # map the values into a dictionary
    intent_similiarity_map[each] = distance

print (intent_similiarity_map)
print ("\n")
print ("Selected Intent for the given user input")
# pick the intent with minimum distance
print (min(intent_similiarity_map, key = intent_similiarity_map.get))


I am working on the file "IntentMatching/sentence_similarity_gensim_wmd.ipynb".
The first task is:
Load the Google's pre-trained model
Can you write Python code for it?

#load word2vec model, here GoogleNews is used , this should be downloaded and to be loaded from the local path
model = gensim.models.KeyedVectors.load_word2vec_format('GoogleNews-vectors-negative300.bin', binary=True)

# intent list for the application
intent_list = ['Read the news', 'Hello', 'Get my news', 'Get feed', 'Read my feed']


I am working on the file "Interact/.ipynb_checkpoints/InteractEx02-checkpoint.ipynb".
The first task is:
In matplotlib, the line style and color can be set with a third argument to plot. Examples of this argument:

dashed red: r--
blue circles: bo
dotted black: k.

Write a plot_sine2(a, b, style) function that has a third style argument that allows you to set the line style of the plot. The style should default to a blue line.
Can you write Python code for it?

def plot_sine2(a,b,style):
    labels = ['0','$\pi$.','$2\pi$.','$3\pi$.','$4\pi$.']
    x = [0,np.pi,2*np.pi,3*np.pi,4*np.pi]
    t = np.arange(0,4*np.pi,.1);
    plt.figure(figsize=(15,5));
    plt.plot(t,np.sin(a*t+b),style);
    plt.xlim(0,4*np.pi);
    plt.xticks(x, labels);
    plt.xlabel('x')
    plt.ylabel('y')
    plt.title('Plot if Sin(x) vs x')
    plt.tick_params(axis='x',top='off')
    plt.tick_params(axis='y',right='off')
    plt.tick_params(axis='x',top='off',direction='out')
    plt.tick_params(axis='y',right='off',direction='out')

plot_sine2(4.0, -1.0, 'r--')


I am working on the file "Interact/.ipynb_checkpoints/InteractEx02-checkpoint.ipynb".
The first task is:
Use interact to create a UI for plot_sine2.

Use a slider for a and b as above.
Use a drop down menu for selecting the line style between a dotted blue line line, black circles and red triangles.
Can you write Python code for it?

interact(plot_sine2,a=(0.0,5.0,.1),b=(-5.0,5.0,.1),style={'Dotted Blue Line': 'b.', 'Black Circles': 'ko', 'Red Triangles': 'r^'});

assert True # leave this for grading the plot_sine2 exercise


I am working on the file "Interact/.ipynb_checkpoints/InteractEx03-checkpoint.ipynb".
The first task is:
To create an animation of a soliton propagating in time, we are going to precompute the soliton data and store it in a 2d array. To set this up, we create the following variables and arrays:
Can you write Python code for it?

tmin = 0.0
tmax = 10.0
tpoints = 100
t = np.linspace(tmin, tmax, tpoints)

xmin = 0.0
xmax = 10.0
xpoints = 200
x = np.linspace(xmin, xmax, xpoints)

c = 1.0
a = 0.0


I am working on the file "Interact/.ipynb_checkpoints/InteractEx03-checkpoint.ipynb".
The first task is:
Using interact for animation with data
A soliton is a constant velocity wave that maintains its shape as it propagates. They arise from non-linear wave equations, such has the Korteweg–de Vries equation, which has the following analytical solution:
$$
\phi(x,t) = \frac{1}{2} c \mathrm{sech}^2 \left[ \frac{\sqrt{c}}{2} \left(x - ct - a \right) \right]
$$
The constant c is the velocity and the constant a is the initial location of the soliton.
Define soliton(x, t, c, a) function that computes the value of the soliton wave for the given arguments. Your function should work when the postion x or t are NumPy arrays, in which case it should return a NumPy array itself.
Can you write Python code for it?

def soliton(x, t, c, a):
    """Return phi(x, t) for a soliton wave with constants c and a."""
    return (1/2)*c*(1/np.cosh((np.sqrt(c)/2)*(x - c*t)-a))

assert np.allclose(soliton(np.array([0]),0.0,1.0,0.0), np.array([0.5]))


I am working on the file "Interact/.ipynb_checkpoints/InteractEx03-checkpoint.ipynb".
The first task is:
Write a plot_soliton_data(i) function that plots the soliton wave $\phi(x, t[i])$. Customize your plot to make it effective and beautiful.
Can you write Python code for it?

def plot_soliton_data(i=0):
    """Plot the soliton data at t[i] versus x."""
    plt.figure(figsize=(9,6))
    plt.plot(soliton(x,t[i],c,a))
    plt.title('Plot of Soliton(x) vs x at a specfic time t')
    plt.xlabel('x')
    plt.ylabel('Soliton(x)')
    plt.tick_params(axis='x',top='off',direction='out')
    plt.tick_params(axis='y',right='off',direction='out')

plot_soliton_data(0)

assert True # leave this for grading the plot_soliton_data function


I am working on the file "Interact/.ipynb_checkpoints/InteractEx04-checkpoint.ipynb".
The first task is:
Use interact to explore the plot_random_line function using:

m: a float valued slider from -10.0 to 10.0 with steps of 0.1.
b: a float valued slider from -5.0 to 5.0 with steps of 0.1.
sigma: a float valued slider from 0.0 to 5.0 with steps of 0.01.
size: an int valued slider from 10 to 100 with steps of 10.
color: a dropdown with options for red, green and blue.
Can you write Python code for it?

interact(plot_random_line,m=(-10.0,10.0,0.1),b=(-5.0,5.0,0.1),sigma=(0.0,5.0,0.01),size=(10,100,10),color=('red','green','blue'));

#### assert True # use this cell to grade the plot_random_line interact


I am working on the file "Interact/.ipynb_checkpoints/InteractEx04-checkpoint.ipynb".
The first task is:
Write a function named plot_random_line that takes the same arguments as random_line and creates a random line using random_line and then plots the x and y points using Matplotlib's scatter function:

Make the marker color settable through a color keyword argument with a default of red.
Display the range $x=[-1.1,1.1]$ and $y=[-10.0,10.0]$.
Customize your plot to make it effective and beautiful.
Can you write Python code for it?

def ticks_out(ax):
    """Move the ticks to the outside of the box."""
    ax.get_xaxis().set_tick_params(direction='out', width=1, which='both')
    ax.get_yaxis().set_tick_params(direction='out', width=1, which='both')

def plot_random_line(m, b, sigma, size=10, color='red'):
    """Plot a random line with slope m, intercept b and size points."""
    x,y = random_line(m, b, sigma, size)
    plt.figure(figsize=(9,6))
    plt.scatter(x,y,color = color)
    plt.xlim(-1.1,1.1)
    plt.ylim(-10.0,10.0)
    plt.xlabel('x')
    plt.ylabel('y')
    plt.title('Plot of a line with Gaussian noise')
    plt.tick_params(axis='x',top='off',direction='out')
    plt.tick_params(axis='y',right='off',direction='out')

plot_random_line(5.0, -1.0, 2.0, 50)

assert True # use this cell to grade the plot_random_line function


I am working on the file "Interact/.ipynb_checkpoints/MatplotlibEx03-checkpoint.ipynb".
The first task is:
Next make a visualization using one of the pcolor functions:
Can you write Python code for it?

plt.figure(figsize=(9,6))
plt.pcolormesh(XX,YY,well2d(XX, YY, nx, ny, L),cmap=('spectral'))
plt.colorbar(shrink=.8)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Contour plot of two dimensional wave function in an infinite well')
plt.xlabel('x')
plt.ylabel('y')
plt.tick_params(axis='x',top='off',direction='out')
plt.tick_params(axis='y',right='off',direction='out')

assert True # use this cell for grading the pcolor plot


I am working on the file "Interact/.ipynb_checkpoints/MatplotlibEx03-checkpoint.ipynb".
The first task is:
The contour, contourf, pcolor and pcolormesh functions of Matplotlib can be used for effective visualizations of 2d scalar fields. Use the Matplotlib documentation to learn how to use these functions along with the numpy.meshgrid function to visualize the above wavefunction:

Use $n_x=3$, $n_y=2$ and $L=0$.
Use the limits $[0,1]$ for the x and y axis.
Customize your plot to make it effective and beautiful.
Use a non-default colormap.
Add a colorbar to you visualization.

First make a plot using one of the contour functions:
Can you write Python code for it?

nx = 3
ny = 2
L = 1
x = np.linspace(0,L,1000)
y = np.linspace(0,L,1000)
XX,YY = np.meshgrid(x,y)
plt.figure(figsize=(9,6))
f = plt.contourf(XX,YY,well2d(XX, YY, nx, ny, L),cmap=('seismic'))
plt.xlabel('x')
plt.ylabel('y')
plt.title('Contour plot of two dimensional wave function in an infinite well')
plt.tick_params(axis='x',top='off',direction='out')
plt.tick_params(axis='y',right='off',direction='out')
plt.colorbar(shrink=.8)

assert True # use this cell for grading the contour plot


I am working on the file "Interact/InteractEx01.ipynb".
The first task is:
Interact basics
Write a print_sum function that prints the sum of its arguments a and b.
Can you write Python code for it?

def print_sum(a=0.0, b=0):
    print(a+b)


I am working on the file "Interact/InteractEx01.ipynb".
The first task is:
Use the interact function to interact with the print_string function.

s should be a textbox with the initial value "Hello World!".
length should be a checkbox with an initial value of True.
Can you write Python code for it?

interact(print_string,s='Hello World!',length=True);

assert True # leave this for grading the print_string exercise


I am working on the file "Interact/InteractEx02.ipynb".
The first task is:
Plotting with parameters
Write a plot_sin1(a, b) function that plots $sin(ax+b)$ over the interval $[0,4\pi]$.

Customize your visualization to make it effective and beautiful.
Customize the box, grid, spines and ticks to match the requirements of this data.
Use enough points along the x-axis to get a smooth plot.
For the x-axis tick locations use integer multiples of $\pi$.
For the x-axis tick labels use multiples of pi using LaTeX: $3\pi$.
Can you write Python code for it?

def plot_sine1(a,b):
    labels = ['0','$\pi$.','$2\pi$.','$3\pi$.','$4\pi$.']
    x = [0,np.pi,2*np.pi,3*np.pi,4*np.pi]
    t = np.arange(0,4*np.pi,.01);
    plt.figure(figsize=(15,5));
    plt.plot(t,np.sin(a*t+b));
    plt.xlim(0,4*np.pi);
    plt.xticks(x, labels);
    plt.xlabel('x')
    plt.ylabel('sin(ax+b)')
    plt.title('Plot if Sin(ax+b) vs x')
    plt.tick_params(axis='x',top='off')
    plt.tick_params(axis='y',right='off')
    plt.tick_params(axis='x',top='off',direction='out')
    plt.tick_params(axis='y',right='off',direction='out')

plot_sine1(5, 3.4)


I am working on the file "Interact/InteractEx02.ipynb".
The first task is:
Use interact to create a UI for plot_sine2.

Use a slider for a and b as above.
Use a drop down menu for selecting the line style between a dotted blue line line, black circles and red triangles.
Can you write Python code for it?


I am working on the file "Interact/InteractEx03.ipynb".
The first task is:
Compute a 2d NumPy array called phi:

It should have a dtype of float.
It should have a shape of (xpoints, tpoints).
phi[i,j] should contain the value $\phi(x[i],t[j])$.
Can you write Python code for it?

phi = np.zeros((xpoints,tpoints))
for i in x:
    for j in t:
        phi[i,j]=soliton(x[i],t[j],c,a)

assert phi.shape==(xpoints, tpoints)
assert phi.ndim==2
assert phi.dtype==np.dtype(float)
assert phi[0,0]==soliton(x[0],t[0],c,a)


I am working on the file "Interact/InteractEx03.ipynb".
The first task is:
To create an animation of a soliton propagating in time, we are going to precompute the soliton data and store it in a 2d array. To set this up, we create the following variables and arrays:
Can you write Python code for it?


I am working on the file "Interact/InteractEx03.ipynb".
The first task is:
Write a plot_soliton_data(i) function that plots the soliton wave $\phi(x, t[i])$. Customize your plot to make it effective and beautiful.
Can you write Python code for it?


I am working on the file "Interact/InteractEx04.ipynb".
The first task is:
Use interact to explore the plot_random_line function using:

m: a float valued slider from -10.0 to 10.0 with steps of 0.1.
b: a float valued slider from -5.0 to 5.0 with steps of 0.1.
sigma: a float valued slider from 0.0 to 5.0 with steps of 0.01.
size: an int valued slider from 10 to 100 with steps of 10.
color: a dropdown with options for red, green and blue.
Can you write Python code for it?


I am working on the file "Interact/InteractEx04.ipynb".
The first task is:
Write a function named plot_random_line that takes the same arguments as random_line and creates a random line using random_line and then plots the x and y points using Matplotlib's scatter function:

Make the marker color settable through a color keyword argument with a default of red.
Display the range $x=[-1.1,1.1]$ and $y=[-10.0,10.0]$.
Customize your plot to make it effective and beautiful.
Can you write Python code for it?


I am working on the file "Interacting with R.ipynb".
The first task is:
%Rpush uses a lot more memory than if you save the file with numpy and load into R using the RcppCNPy library!
Can you write Python code for it?

%R rm(ADmat)

mem_usage = memory_usage(-1, interval=1, timeout=1)
print(mem_usage)

%R gc();

mem_usage = memory_usage(-1, interval=1, timeout=1)
print(mem_usage)


I am working on the file "Interacting with R.ipynb".
The first task is:
ADmat is gone but memory is not released
Can you write Python code for it?

%R gc(); # garbage collection

mem_usage = memory_usage(-1, interval=1, timeout=1)
print(mem_usage)


I am working on the file "Interacting with R.ipynb".
The first task is:
Create some data
typical LFP matrix, 32 channels, 1.5 million samples at 1 kHz
Can you write Python code for it?

ADmat = np.random.randn(32, 1500000) / 100

whos ndarray

mem_usage = memory_usage(-1, interval=1, timeout=1)
print(mem_usage)


I am working on the file "Interacting with R.ipynb".
The first task is:
clean up from previous runs, as writing over takes longer than deleting and writing
Can you write Python code for it?


I am working on the file "Interacting with R.ipynb".
The first task is:
load into R using R magic
RcppCNPy is a fantastic library for R that lets you read and write numpy data file.
Can you write Python code for it?

%%R
library(RcppCNPy)
setwd("~/temp")
ADmat = npyLoad('test.npy', type="numeric", dotranspose=FALSE)

%R str(ADmat)

%R ls()

mem_usage = memory_usage(-1, interval=1, timeout=1)
print(mem_usage)


I am working on the file "Interacting with R.ipynb".
The first task is:
now the memory consumed by ADmat is released but the extra memory is still consumed
Can you write Python code for it?


I am working on the file "Interacting with R.ipynb".
The first task is:
now the memory is released
Can you write Python code for it?


I am working on the file "Interacting with R.ipynb".
The first task is:
remove ADmat to assess memory use with Rpush below
Can you write Python code for it?

%R rm(list=ls())

%R ls()

mem_usage = memory_usage(-1, interval=1, timeout=1)
print(mem_usage)


I am working on the file "Interacting with R.ipynb".
The first task is:
the memory_profiler library
I used the memory_profiler library to assess memory usage in this notebook.
Can you write Python code for it?

# https://pypi.python.org/pypi/memory_profiler
from memory_profiler import memory_usage
mem_usage = memory_usage(-1, interval=1, timeout=1)
print(mem_usage)


I am working on the file "Intercept Distribution .ipynb".
The first task is:
Let's see what happens when we apply this transformation to the intercepts
Can you write Python code for it?

ens = nengo.Ensemble(n_neurons=10000, dimensions=16, add_to_container=False)
intercepts = ens.intercepts.sample(n=ens.n_neurons, d=1)[:,0]
intercepts2 = [find_x_for_p(x_int/2+0.5, ens.dimensions) for x_int in intercepts]
ens.intercepts = intercepts2

pylab.figure(figsize=(14,8))
pylab.subplot(2, 2, 1)
seaborn.distplot(intercepts)
pylab.xlabel('original intercepts')
pylab.subplot(2, 2, 2)
seaborn.distplot([analytic_proportion(x, ens.dimensions) for x in intercepts])
pylab.xlabel('proportion for original intercepts')
pylab.subplot(2, 2, 3)
seaborn.distplot(intercepts2)
pylab.xlabel('new intercepts')
pylab.subplot(2, 2, 4)
seaborn.distplot([analytic_proportion(x, ens.dimensions) for x in intercepts2])
pylab.xlabel('proportion for new intercepts')


I am working on the file "Intercept Distribution .ipynb".
The first task is:
Now let's check to see how it performs
Can you write Python code for it?

seed = 1
print AreaInterceptTrial().run(d=16, verbose=False, seed=seed)
print AreaInterceptTrial().run(d=16, verbose=False, use_area=True, seed=seed)


I am working on the file "Intercept Distribution .ipynb".
The first task is:
Now let's define a pytry.Trial to let us explore the accuracy of the system.  As we vary the number of dimensions, we will see how well we can decode a constant function ($1$), linear value ($x_i$), squared value ($x_i^2$), and other quadratic combinatons ($x_i y_i$).
Can you write Python code for it?

class AreaInterceptTrial(pytry.Trial):
    def params(self):
        self.param('number of neurons per dimension', n_per_d=50)
        self.param('number of dimensions', d=1)
        self.param('use AreaIntercept distribution', use_area=False)

    def evaluate(self, p):
        model = nengo.Network(seed=p.seed)
        intercepts = nengo.dists.Uniform(-1, 1)
        if p.use_area:
            intercepts = AreaIntercepts(p.d, base=intercepts)
        n_neurons = p.n_per_d * p.d
        with model:
            ens = nengo.Ensemble(n_neurons, p.d, intercepts=intercepts)

            def func_constant(x):
                return 1
            constant = nengo.Node(size_in=1)
            c_constant = nengo.Connection(ens, constant, function=func_constant)

            def func_linear(x):
                return x
            linear = nengo.Node(size_in=p.d)
            c_linear = nengo.Connection(ens, linear, function=func_linear)

            def func_square(x):
                return x**2
            square = nengo.Node(size_in=p.d)
            c_square = nengo.Connection(ens, square, function=func_square)

            def func_quad(x):
                r = []
                for i, xx in enumerate(x):
                    for j, yy in enumerate(x[i+1:]):
                        r.append(xx*yy)
                return r
            count = len(func_quad(np.zeros(p.d)))
            if count > 0:
                quad = nengo.Node(size_in=count)
                c_quad = nengo.Connection(ens, quad, function=func_quad)
            else:
                c_quad = None

        sim = nengo.Simulator(model)
        return dict(
            constant = np.mean(sim.data[c_constant].solver_info['rmses']),
            linear = np.mean(sim.data[c_linear].solver_info['rmses']),
            square = np.mean(sim.data[c_square].solver_info['rmses']),
            quad = np.mean(sim.data[c_quad].solver_info['rmses']) if c_quad is not None else 0,
            )


I am working on the file "Intercept Distribution .ipynb".
The first task is:
This indicates an intercept of 0.5 in 2-dimensons only fires for 19.5% of the represented area.
Let's compare this analytic proportion to the empirically estimated proportion, just to check if it works.
Can you write Python code for it?

def plot_intercept_distribution(ens):
    pylab.subplot(1,3,1)
    intercepts = ens.intercepts
    if isinstance(intercepts, nengo.dists.Distribution):
        intercepts = intercepts.sample(ens.n_neurons)
    seaborn.distplot(intercepts, bins=np.linspace(-1.2, 1.2, 25))
    pylab.xlabel('intercept')
    pylab.xlim(-1.5, 1.5)

    pylab.subplot(1,3,2)
    pts = ens.eval_points.sample(n=1000, d=ens.dimensions)
    model = nengo.Network()
    model.ensembles.append(ens)
    sim = nengo.Simulator(model)
    _, activity = nengo.utils.ensemble.tuning_curves(ens, sim, inputs=pts)
    p = np.mean(activity>0, axis=0)
    seaborn.distplot(p, bins=20)
    pylab.xlabel('proportion of pts neuron is active for\n(sampled)')

    p2 = [analytic_proportion(x, ens.dimensions) for x in intercepts]
    pylab.subplot(1,3,3)
    seaborn.distplot(p2, bins=20)
    pylab.xlabel('proportion of pts neuron is active for\n(analytic)')

ens = nengo.Ensemble(n_neurons=10000, dimensions=16, add_to_container=False)
pylab.figure(figsize=(14,3))
plot_intercept_distribution(ens)


I am working on the file "Intercept Distribution .ipynb".
The first task is:
Those look pretty similar to me!  This shows our analytic approach is working well.
Now, we need to reverse this effect.  What we want is a system that we can give an p value, and it will find the x-intercept value that gives that proportion.  Fortunately, scipy has an inverse for the beta function, so a bit of algebra on $p={1 \over 2} I_{1-x^2}({{d+1} \over 2}, {1 \over 2})$ gives us
$x = \sqrt{1-I^{-1}_{2p}({{d+1} \over 2}, {1 \over 2})}$
Can you write Python code for it?

def find_x_for_p(p, d):
    sign = 1
    if p > 0.5:
        p = 1.0 - p
        sign = -1
    return sign * np.sqrt(1-scipy.special.betaincinv((d+1)/2.0, 0.5, 2*p))

print find_x_for_p(0.7, 2)


I am working on the file "Intercept Distribution .ipynb".
The first task is:
Those peaks at 0% and 100% are very worrying!  It looks like a lot of these neurons aren't really helping much.
To understand this a bit better, let's see if we can directly compute this mapping from intercept to proportion active.  To do this, we need to compute the volume of a hyperspherical cap https://en.wikipedia.org/wiki/Spherical_cap#Hyperspherical_cap
<img src=https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Spherical_cap_diagram.tiff/lossless-page1-220px-Spherical_cap_diagram.tiff.png>
The formula for the volume in general is $V = {1 \over 2} C_d r^d I_{2rh-h^2 \over r^2}({d+1 \over 2}, {1 \over 2})$ where $C_d$ is the volume of a unit hyperball of dimension $d$ and $I_x(a,b)$ is the regularized incomplete beta function.
In our case, if $x$ is the intercept, then $r=1$, $h=1-x$.  We want the proportion, so we divide by $C_d$, leaving:
$p={1 \over 2} I_{1-x^2}({{d+1} \over 2}, {1 \over 2})$
Of course, this formula only works for $x>=0$.  For $x<0$ we can flip the sign of $x$ and then subtract 1 at the end.
Can you write Python code for it?

import scipy.special
def analytic_proportion(x, d):
    flip = False
    if x < 0:
        x = -x
        flip = True

    if x >= 1.0:
        value = 0
    else:
        value = 0.5 * scipy.special.betainc((d+1)/2.0, 0.5, 1 - x**2)

    if flip:
        value = 1.0 - value
    return value

print analytic_proportion(0.5, 2)


I am working on the file "Intercept Distribution .ipynb".
The first task is:
We start by empirically computing this distribution, just by generating tuning curves and counting the number of non-zero values for each neuron.
Can you write Python code for it?

def plot_intercept_distribution(ens):
    pylab.subplot(1,2,1)
    intercepts = ens.intercepts.sample(ens.n_neurons)
    seaborn.distplot(intercepts, bins=20)
    pylab.xlabel('intercept')

    pylab.subplot(1,2,2)
    pts = ens.eval_points.sample(n=1000, d=ens.dimensions)
    model = nengo.Network()
    model.ensembles.append(ens)
    sim = nengo.Simulator(model)
    _, activity = nengo.utils.ensemble.tuning_curves(ens, sim, inputs=pts)
    p = np.mean(activity>0, axis=0)
    seaborn.distplot(p, bins=20)
    pylab.xlabel('proportion of pts neuron is active for')

for D in [1, 2, 4, 8, 16, 32]:
    ens = nengo.Ensemble(n_neurons=10000, dimensions=D, add_to_container=False)
    pylab.figure(figsize=(14,4))
    plot_intercept_distribution(ens)
    pylab.title('Dimensions = %d' % D)


I am working on the file "Interferencia Múltiples Ondas/.ipynb_checkpoints/InterferenciaMultiplesOndas-checkpoint.ipynb".
The first task is:
Como vemos en la figura superior, cuanto mayor sea el valor del coeficiente de fineza, más estrechos son los máximos de $I_t$ lo que se traduce en anillos más estrechos en el patrón de interferencia que obtenemos a la salida de la lámina. Además el contraste es a su vez mayor. Hay que recordar que el coeficiente de fineza es mayor cuanto mayor sea la reflectividad en cada cara de la lámina.
Se puede demostrar que la anchura de cada pico es igual a $\Delta \delta_G = 4/\sqrt{F}$
La figura superior muestra la irradiancia en función del desfase geométrico $\delta_G$. Pero éste a su vez depende de varios factores $\delta_G = 2 k n e cos(\theta_t)$.
Si consideramos incidencia normal, $cos(\theta_t) = 1 \;\;\;$ y vemos que $\delta_G$ depende de la longitud de onda de la radiación incidente a través del vector de ondas $k = 2 \pi / \lambda $.
Podemos pues dibujar la irradiancia total en función de $\lambda$ (para incidencia normal)
Can you write Python code for it?

# Programa para dibujar la irradiancia transmitida en función de la longitud de onda. Filtros interferenciales.
fig = figure(figsize=(14,7))
Lambda = np.linspace(400,700,200) # Creamos un vector de longitudes de onda en el visible (en nm)
F = np.array([0.2,1,50,200]) #mismo vector de coeficientes de fineza que en el caso anterior
n = 1.38 # índice de refracción de la lámina (escogemos la del MgF2)
e = 599 # escogemos el espesor de la lámina (en nm)
deltaG = (4.0*pi/Lambda)*n*e
for i in np.arange(len(F)):
    It = 1.0/(1.0 + F[i]*sin(deltaG/2)**2)
    texto = 'F =' + str(F[i])
    plot(Lambda,It,label=texto)
    xlabel(r'$\lambda$ (nm)')
    ylabel(r'$I_t/I_0$')
    legend()


I am working on the file "Interferencia Múltiples Ondas/.ipynb_checkpoints/InterferenciaMultiplesOndas-checkpoint.ipynb".
The first task is:
Cálculo del campo total en reflexión y en transmisión
En la figura anterior se muestran las sucesivas reflexiones internas en la lámina que generan las ondas que queremos hacer interferir. El procedimiento sería análogo a lo ya estudiado con 2 ondas. Colocando una lente y observando en su plano focal obtendremos anillos de interferencia.
Llamaremos al coeficiente de reflexión en la transición aire-lámina r, y al de transmisión t, mientras que llamaremos r' y t' a los coeficientes para la transición lámina-aire.
Con esto en mente, podemos ver que las ondas que interfieren en transmisión son,
$$E_{t1} = E_0 t t' e^{i \omega t}$$
$$E_{t2} = E_0 t t' r'^2 e^{i[ \omega t - \delta_G ]}$$
$$E_{t3} = E_0 t t' r'^4 e^{i[ \omega t - 2 \delta_G ]}$$
.
.
.
$$E_{tN} = E_0 t t' r'^{2(N-1)} e^{i[ \omega t - (N-1) \delta_G ]}$$
donde $\delta_G = \frac{4 \pi}{\lambda} n h cos(\theta_t)$ es el desfase entre ondas sucesivas debido a la diferencia de caminos (el desfase debido a las reflexiones se tiene en cuenta en los coeficientes de reflexión).
Igualmente, las ondas que interfieren en reflexión son,
$$E_{r1} = E_0 r e^{i \omega t}$$
$$E_{r2} = E_0 t t' r' e^{i[ \omega t - \delta_G ]}$$
$$E_{r3} = E_0 t t' r'^3 e^{i[ \omega t - 2 \delta_G ]}$$
.
.
.
$$E_{rN} = E_0 t t' r'^{(2N-3)} e^{i[ \omega t - (N-1) \delta_G ]}$$

Si la lámina es suficientemente larga o el ángulo de incidencia no muy grande, tendremos muchas ondas interfiriendo ($N \rightarrow \infty$). Sumando todas, tendremos el campo total en transmisión o reflexión. Esta suma puede realizarse y se obtiene,
$$E_t = E_0 e^{i \omega t} \left[\frac{t t'}{1 - r^2 e^{-i\delta_G}} \right]$$
$$E_r = E_0 e^{i \omega t} \left[\frac{r(1 - e^{-i\delta_G})}{1 - r^2 e^{-i\delta_G}} \right]$$
donde en la última expresión se ha utilizado que $r = r'$ y que $t t' = 1 - r^2$ (por conservarción de la energía y dado que no consideramos absorción en el medio)
Cálculo de la irradiancia
De las expresiones anteriores llegamos a que la irradiancia en transmisión/reflexión es igual a,
$$I_t...
Can you write Python code for it?

# Programa para dibujar la irradiancia transmitida. Filtros interferenciales.
import numpy as np
from numpy import *
from matplotlib.pyplot import *
style.use('fivethirtyeight')
%matplotlib inline
import matplotlib
matplotlib.rcParams.update({'font.size': 16})
fig = figure(figsize=(14,7))
deltaG = np.arange(0,8*pi,8*pi/200) # Creamos un vector de desfases geométricos
F = np.array([0.2,1,50,200])
for i in np.arange(len(F)):
    It = 1.0/(1.0 + F[i]*sin(deltaG/2)**2)
    texto = 'F =' + str(F[i])
    plot(deltaG,It,label=texto)
    xlabel(r'$\delta_G$')
    ylabel(r'$I_t/I_0$')
    legend()


I am working on the file "Interferencia Múltiples Ondas/.ipynb_checkpoints/InterferenciaMultiplesOndas-checkpoint.ipynb".
The first task is:
Lo que vemos en la anterior figura es que si $F$ es suficientemente grande, podemos utilizar la lámina como un filtro espectral, ya que la transmitancia cae a valores muy cercanos a cero fuera de los picos que dan los máximos. Para aumentar $F$, se suelen usar recubrimientos metálicos (entonces los desfases debido a las reflexiones no son ya 0 ó $\pi$).
Si queremos obtener máxima transmitancia para $\lambda_0$, el espesor habrá de ser tal que se cumpla,
$$\frac{4 \pi}{\lambda_0} n e = 2 m \pi \implies e = \frac{m \lambda_0}{2 n}$$
Otro punto a destactar es que si se cumple la condición de máximo de transmitancia $\delta_G = 2 m \pi \;$  para una longitud de onda $\lambda_0 \;\;\;$ entonces también lo tendremos para las longitudes de onda $\lambda = \frac{\lambda_0}{m},, m = 2,3,... \;\;\;\;$ Normalmente, estas otras longitudes de onda se encuentran bastante alejadas y pueden ser filtradas añadiendo algún medio que las absorba (por ej. colorantes).
Por último vamos a ver una figura de cómo se verían los anillos en el plano focal de una lente situada después de nuestra lámina planoparalela. Para observar cómo cambia la anchura de los anillos con el valor de $F$, cambiar su valor (en la parte superior del código) y volver a ejecutar la celda.
Can you write Python code for it?

# MODIFICAR ESTE VALOR PARA VER COMO CAMBIA LA FIGURA

F = 20 # coeficiente de fineza
####################
fig = figure(figsize=(10,10))
Lambda =590 #(nm)
n = 1.38 # índice de refracción de la lámina (escogemos la del MgF2)
e = 3230 # escogemos el espesor de la lámina (en nm)
focal = 30 #(mm)
x = np.linspace(-30,30,500)
[X,Y] = meshgrid(x,x)
rho =  np.sqrt(X**2 + Y**2) #(mm)
rhof = rho/focal
theta_i = np.arctan2(focal,rho)
theta_t = np.arcsin((1.0/n)*sin(theta_i))

deltaG = (4.0*pi/Lambda)*n*e*cos(theta_t)

I_t = 1.0/(1.0 + F*sin(deltaG/2)**2)
#plot(I_t[:,100])
pcolormesh(X,Y,I_t,cmap=cm.hot);


I am working on the file "InteriorPoint.ipynb".
The first task is:
For those of you viewing this statically, rather than interactively, here are a few snapshots at $\mu = 1.0, 0.5, 0.25, \text{ and } 0.125$
Can you write Python code for it?

plt.figure()
plt.plot(x, -x, 'k--')

muvec = [1.0, 0.5, 0.25, 0.125]

for mu in muvec:

    f = -x - mu*np.log(5 - x)
    plt.plot(x, f)

plt.xlim([0, 5.1])
plt.ylim([-5, 2])
plt.xlabel('x')
plt.ylabel('f')
plt.show()


I am working on the file "InteriorPoint.ipynb".
The first task is:
Let's now plot this objective plus the penalty, or in other words our new function to minimize.  We start with $\mu = 1.0$.  Also shown for reference is the line $f(x) = -x$ which is the objective function.  The constraint $x \le 5$ is like a wall at $x = 5$.  We see that the log function creates a barrier that approximates the wall.  As we decrease the valye of $\mu$, the barrier will increase in steepness and closer approximate the wall.
Can you write Python code for it?

plt.figure()
plt.plot(x, -x, 'k--')
mu = 1.0
f = -x - mu*np.log(5 - x)
plt.plot(x, f)
plt.xlim([0, 5.1])
plt.ylim([-5, 2])
plt.xlabel('x')
plt.ylabel('f')
plt.show()


I am working on the file "InteriorPoint.ipynb".
The first task is:
We can see that it actus like a steep wall near $x=5$ with small effect elsewhere.  The next example is interactive, letting you set a $\mu$.  Notice that as $\mu$ becomes smaller and smaller the penalty more closely represents a perfect barrier.
Can you write Python code for it?

from ipywidgets import interact

@interact(mu=(0.1, 2.0, 0.1))
def logbarrier(mu):
    f = -mu*np.log(5 - x)
    plt.plot(x, f)
    plt.xlim([0, 5.1])
    plt.ylim([-2, 2])
    plt.xlabel('x')
    plt.ylabel('f')
    plt.show()


I am working on the file "Interior_Point_Method_Example.ipynb".
The first task is:
LP in standard form
$\begin{gather}
\min \quad -x_1 - 4x_2\
\begin{aligned}
s.a.
2x_1 - x_2 -x_3  &=  0\
x_1 - 3x_2 +x_4  &=  0 \
x_1 +  x_2+x_5  &= 4 \
\quad x_1, x_2, x_3, x_4, x_5  & \geq  0 \
\end{aligned}
\end{gather}$
We see ($x_1,x_2$)=(1,1) is a interior point so we choose it as initial point x_0
x_0=$\begin{bmatrix}1\1\1\2\2\end{bmatrix}$
A= $\begin{bmatrix}2& -1 & -1 & 0 & 0\
1& -3 & 0 & 1 & 0\
1 & 1 & 0 & 0 & 1\
\end{bmatrix}$
Initial Solution z=-5
Can you write Python code for it?

x      = np.linspace(0, 4, 100)
y1 = 2*x
y2 = x/3
y3 = 4 - x
plt.figure(figsize=(8, 6))
plt.plot(x, y1)
plt.plot(x, y2)
plt.plot(x, y3)
plt.xlim((0, 3.5))
plt.ylim((0, 4))
plt.xlabel('x1')
plt.ylabel('x2')
y5 = np.minimum(y1, y3)
plt.fill_between(x[:-25], y2[:-25], y5[:-25], color='red', alpha=0.5)
plt.scatter([1],[1],color='black')
plt.annotate('x_0',(1.05,1.05))


I am working on the file "Interior_Point_Method_Example.ipynb".
The first task is:
Now lets write a function to run $n$ iterations
Can you write Python code for it?

mu       = 100
gamma    = 0.8
A        = np.array([[2,-1,-1,0,0],[1,-3,0,1,0],[1,1,0,0,1]])
vector_1 = np.ones((5,1))
c        = np.array([[-1],[-4],[0],[0],[0]])

x        = np.array([[1],[1],[1],[2],[2]]) #Punto inicial

x1s      = [] #Lista vacia para guardar x_1's
x2s      = [] #Lista vacia para guardar x_2's
x1s.append(x[0,0])
x2s.append(x[1,0])

for iteracion in range(100):

  X = np.array([[x[0,0],0,0,0,0],[0,x[1,0],0,0,0],
                [0,0,x[2,0],0,0],[0,0,0,x[3,0],0],[0,0,0,0,x[4,0]]])
  #RESOLVER ECUACION 4

  #------Lado izquierdo
  izq_ec_4 = np.matmul( A, np.matmul( np.power(X,2),A.T ) )

  #------Lado derecho
  #                     -mu*A*X*1                       +       AX^2c
  der_ec_4 = -mu*np.matmul( A,np.matmul( X,vector_1 ) ) + np.matmul( A,np.matmul( np.power(X,2),c ) )

  #------Determino dy

  dy = np.linalg.solve(izq_ec_4, der_ec_4)

  #RESOLVER ECUACION 3

  ds = np.matmul(-1*A.T,dy) #ds=-A^T*dy

  #RESOLVER ECUACION 1

  izq_ec_1 = mu*np.power(np.linalg.inv(X),2) #mu*X^-2

  der_ec_1 = mu*np.matmul(np.linalg.inv(X),vector_1)-c-ds #mu*X^-1*1-c-ds

  dx       = np.linalg.solve(izq_ec_1,der_ec_1)

  #ACTUALIZAR vector x

  x  = x + dx
  mu = mu*gamma
  x1s.append( x[0,0] )
  x2s.append( x[1,0] )

x  = np.linspace(0, 4, 100)
y1 = 2*x
y2 = x/3
y3 = 4 - x
plt.figure(figsize=(8, 6))
plt.plot(x, y1)
plt.plot(x, y2)
plt.plot(x, y3)
plt.xlim((0, 3.5))
plt.ylim((0, 4))
plt.xlabel('x1')
plt.ylabel('x2')
y5 = np.minimum(y1, y3)
plt.fill_between(x[:-25], y2[:-25], y5[:-25], color='red', alpha=0.5)

for iteracion in range(100):
  plt.scatter(x1s[iteracion],x2s[iteracion],color='black')
  if iteracion % 10 == 0:
    nombre = 'x_'+str(iteracion)
    plt.annotate(nombre,(x1s[iteracion]+0.05,x2s[iteracion]+0.05))


I am working on the file "Interpolation/.ipynb_checkpoints/InterpolationEx01-checkpoint.ipynb".
The first task is:
2D trajectory interpolation
The file trajectory.npz contains 3 Numpy arrays that describe a 2d trajectory of a particle as a function of time:

t which has discrete values of time t[i].
x which has values of the x position at those times: x[i] = x(t[i]).
x which has values of the y position at those times: y[i] = y(t[i]).

Load those arrays into this notebook and save them as variables x, y and t:
Can you write Python code for it?

dictionary = np.load('trajectory.npz')

y = dictionary.items()[0][1]
t = dictionary.items()[1][1]
x = dictionary.items()[2][1]

assert isinstance(x, np.ndarray) and len(x)==40
assert isinstance(y, np.ndarray) and len(y)==40
assert isinstance(t, np.ndarray) and len(t)==40


I am working on the file "Interpolation/.ipynb_checkpoints/InterpolationEx01-checkpoint.ipynb".
The first task is:
Make a parametric plot of ${x(t),y(t)}$ that shows the interpolated values and the original points:

For the interpolated points, use a solid line.
For the original points, use circles of a different color and no line.
Customize you plot to make it effective and beautiful.
Can you write Python code for it?

plt.figure(figsize=(12,8));
plt.plot(x, y, marker='o', linestyle='', label='Original Data')
plt.plot(newx, newy, label='Interpolated Curve');
plt.legend();
plt.xlabel('X(t)');
plt.ylabel('Y(t)');
plt.title('Position as a Function of Time');

assert True # leave this to grade the trajectory plot


I am working on the file "Interpolation/.ipynb_checkpoints/InterpolationEx01-checkpoint.ipynb".
The first task is:
Use these arrays to create interpolated functions $x(t)$ and $y(t)$. Then use those functions to create the following arrays:

newt which has 200 points between ${t_{min},t_{max}}$.
newx which has the interpolated values of $x(t)$ at those times.
newy which has the interpolated values of $y(t)$ at those times.
Can you write Python code for it?

x_approx = interp1d(t, x, kind='cubic')
y_approx = interp1d(t, y, kind='cubic')

newt = np.linspace(0,4,200)
newx = x_approx(newt)
newy = y_approx(newt)

assert newt[0]==t.min()
assert newt[-1]==t.max()
assert len(newt)==200
assert len(newx)==200
assert len(newy)==200


I am working on the file "Interpolation/.ipynb_checkpoints/InterpolationEx02-checkpoint.ipynb".
The first task is:
Sparse 2d interpolation
In this example the values of a scalar field $f(x,y)$ are known at a very limited set of points in a square domain:

The square domain covers the region $x\in[-5,5]$ and $y\in[-5,5]$.
The values of $f(x,y)$ are zero on the boundary of the square at integer spaced points.
The value of $f$ is known at a single interior point: $f(0,0)=1.0$.
The function $f$ is not known at any other points.

Create arrays x, y, f:

x should be a 1d array of the x coordinates on the boundary and the 1 interior point.
y should be a 1d array of the y coordinates on the boundary and the 1 interior point.
f should be a 1d array of the values of f at the corresponding x and y coordinates.

You might find that np.hstack is helpful.
Can you write Python code for it?

x = np.empty((1,),dtype=int)
x[0] = 0
for i in range(-4,5):
    x = np.hstack((x,np.array((i,i))))

x = np.hstack((x,np.array([-5]*11)))
x = np.hstack((x,np.array([5]*11)))

y = np.empty((1,),dtype=int)
y[0]=0
y = np.hstack((y,np.array((5,-5)*9)))

for i in range(-5,6):
    y = np.hstack((y,np.array((i))))
for i in range(-5,6):
    y = np.hstack((y,np.array((i))))

f=np.zeros_like(y)
f[0]=1


I am working on the file "Interpolation/.ipynb_checkpoints/InterpolationEx02-checkpoint.ipynb".
The first task is:
The following plot should show the points on the boundary and the single point in the interior:
Can you write Python code for it?

plt.scatter(x, y);

assert x.shape==(41,)
assert y.shape==(41,)
assert f.shape==(41,)
assert np.count_nonzero(f)==1


I am working on the file "Interpolation/InterpolationEx01.ipynb".
The first task is:
2D trajectory interpolation
The file trajectory.npz contains 3 Numpy arrays that describe a 2d trajectory of a particle as a function of time:

t which has discrete values of time t[i].
x which has values of the x position at those times: x[i] = x(t[i]).
x which has values of the y position at those times: y[i] = y(t[i]).

Load those arrays into this notebook and save them as variables x, y and t:
Can you write Python code for it?


I am working on the file "Interpolation/InterpolationEx01.ipynb".
The first task is:
Make a parametric plot of ${x(t),y(t)}$ that shows the interpolated values and the original points:

For the interpolated points, use a solid line.
For the original points, use circles of a different color and no line.
Customize you plot to make it effective and beautiful.
Can you write Python code for it?


I am working on the file "Interpolation/InterpolationEx02.ipynb".
The first task is:
Plot the values of the interpolated scalar field using a contour plot. Customize your plot to make it effective and beautiful.
Can you write Python code for it?

plt.contourf(Xnew,Ynew,Fnew,cmap='jet');
plt.colorbar(shrink=.8);
plt.xlabel('x value');
plt.ylabel('y value');
plt.title('Contour Plot of Interpolated Function');

assert True # leave this to grade the plot


I am working on the file "Interpolation/InterpolationEx02.ipynb".
The first task is:
The following plot should show the points on the boundary and the single point in the interior:
Can you write Python code for it?


I am working on the file "Interrupt.ipynb".
The first task is:
Using Python interrupts is a bit complicated since the Interrupt Servie Routine (ISR) module for Python can only work with objects. Basic types won't work, so you'll need to declare a class to use the objects. In this case, we use a class to keep track of the status of one of the LEDs. When the button is pressed, we'll flip the LED.
Can you write Python code for it?

# Calling an interrupt requires that we specify a function to run when the
# interrupt is triggered. This class simply holds our GPIO that flips upon
# interrupt. The LED is held in the led object, and the current state
# of the LED is held in status
class Counter:
  status = False
  led = mraa.Gpio(25)
  led.dir(mraa.DIR_OUT)

# Create an instance of the class
c = Counter()

# This is the function that will be called when the interrupt happens. It will
# flip the status as it is held by the class and apply it to the GPIO pin.
def test(args):
  c.status = not c.status
  c.led.write(c.status)


I am working on the file "Interview Cake.ipynb".
The first task is:
Fibonacci
Can you write Python code for it?

class Fibber:
    def __init__(self):
        self.memo = {}

    def fib(self, n):
        if n < 0:
            raise Exception('Index was negative. No such thing as a negative index in a series')

        elif n < 2:
            return n;

        if n in self.memo:
            return self.memo[n]

        result = self.fib(n-1) + self.fib(n-2)

        self.memo[n] = result

        return result

def fib(n):

    if n < 0:
        raise Exception("Index was negative. Cannot have a negaitve index in a series")

    if n < 2:
        return n

    prev_prev = 0
    prev = 1
    for _ in xrange(n-1):
        current = prev + prev_prev
        prev_ prev = prev
        prev = current

    return current


I am working on the file "Interview Cake.ipynb".
The first task is:
Find Rotation Point
I flipped through a dictionary, starting from the middle and picked out words that I didn't know. I added those words to a list and continued until I hit the end of the book. Then I started at the beginning of the dictionary and started from there. Now I have a mostly alphabetical list except that there is a rotation point where it flips, find this rotaiton point.
Can you write Python code for it?

# So this clearly looks like a binary search problem except
# I still find myself a little lost as too how to implement
# these, so lets do that tomorrow morning

def find_rotation_point(words):
    first_word = words[0]
    floor_index = 0
    ceiling_index = len(words) -1

    while floor_index < ceiling_index:
        guess_index = floor_index + ((ceiling_index - floor_index) / 2)

        if words[guess_index] >= first_word:
            floor_index = guess_index
        else:
            ceiling_index = guess_index

        if floor_index + 1 == ceiling_index:
            return ceiling_index


I am working on the file "Interview Cake.ipynb".
The first task is:
Inflight Entertainment
Write a function that takes a list of movie times and an integer equal to the length of the flight and returns a boolean indicating whether there is are two numbers in movie_lengths whose sum equals flight_length.
Do not count the same movie twice.
Can you write Python code for it?

def inflight_entertainment(movie_lengths, flight_length):
    movie_lengths_seen = set()

    for first_movie_length in movie_lengths:
        matching_second_movie_length = flight_length - first_movie_length
        if matching_second_movie_length in movie_length_seen:
            return True

        movie_lengths_seen.add(first_movie_length)

    return False


I am working on the file "Interview Cake.ipynb".
The first task is:
Million Gazillion Engine
There is a crawler that keeps a list of all the websites that it visits, now the crawler has been growing for some time- how do we trim the amount of space taken up by visited?
Can you write Python code for it?

I know the answer to this problem is a trie, but I don't really know how to implement a trie - and this is the problem- I'm pulling a cheap time move an just looking ahead to the answer.

You could also use a bloom filter for this answer, with run-length encoding.

### Keys to a Trie
Implement as a highly nested hash map, you can use other things, nodes, or pointers
Use a \* character to represent 'end of entry'

class Trie:

    def __init__(self):
        self.root_node = {}

    def check_present_and_add(self, word):

        current_node = self.root_node
        is_new_word = False

        for char in word:
            if char not in current_node:
                is_new_word = True
                current_node[char] = {}
            current_node = current_node[char]

        if "End of Word" not in current_node:
            is_new_word = True
            current_node["End Of Word"] = {}

        return is_new_word


I am working on the file "Interview Cake.ipynb".
The first task is:
Parenthesis Matching
Usually we would use a stack for storing questions like this.
Can you write Python code for it?

def get_closing_paren(sentence, opening_paren_index):
    open_nested_parens = 0
    position = opening_paren_index + 1

    while position <= len(sentence) -1:
        char = sentence[position]

        if char == '(':
            open_nested_parens += 1
        elif char == ')':
            if open_nested_parens == 0:
                return position
            else:
                open_nested_parens -= 1

        position += 1

    raise Exception('No closing parenthesis')


I am working on the file "Interview+questions.ipynb".
The first task is:
По аналогии требуется написать функцию, которая будет возвращать текущее и следующее значения.
Type your code below

Problem 2. SQL / Python

Есть следующая SQL таблица sample_table:

column name	driver_id	 start_timestamp	 status

data type	(String)	(String)	(String)

1	driver_id_1	2017-01-21 00:05	driving

2	driver_id_1	2017-01-21 00:09	waiting

...	...	...	...

k x n	driver_id_n	2017-01-21 23:49	transporting

driver_id_i -- идентификатор i-го водителя
start_timestamp -- время начала статуса, в котором находился водитель
status -- статус, в котором находился водитель

Для простоты предположим, что по каждому водителю в таблице одинаковое число записей k.

Табличка хранится в СУБД, которая умеет применять к данным функции, написанные на Python. Например, следующий код выполняет функцию ROW_NUMBER():
Can you write Python code for it?

def row_number(driver_id, input_data):
    sorted_data = sorted(input_data, lambda x: x[0]) # сортируем список входных данных по дате
    result = []
    row_number = 0
    while row_number <= range( 0, len(input_data) ):
        row_data = {'row_number': row_number
                    , 'driver_id': driver_id
                    , 'start_timestamp': sorted_data[row_number][0]
                    , 'status': sorted_data[row_number][1]
                    }
        row_number += 1
        result.append(row_data)
    return result

$row_number = Python::row_number(driver_id, input_data);

$raw = (
    SELECT
            driver_id
            , start_timestamp
            , status
    FROM    sample_table
    );

$reduced = (
    REDUCE $raw
       ON  driver_id
    USING  $row_number((start_timestamp, status))
    );

SELECT * FROM $reduced;


I am working on the file "InterviewCake Questions.ipynb".
The first task is:
Apple Stocks
Write an efficient function that takes stock_prices_yesterday and returns the best profit I could have made from 1 purchase and 1 sale of 1 Apple stock yesterday.
Can you write Python code for it?

from IPython.core.display import Image, display
from IPython.display import Image, Markdown
import random

def get_max_profit(stock_prices):
    '''returns the best profit I could have made from 1 purchase and 1 sale of 1 Apple stock yesterday.
       have to buy before selling
       O(n^2) Solution
    '''
    max_profit = 0

    for inner_index in range(len(stock_prices)):
        for outer_index in range(len(stock_prices)):
            earlier_time = min(inner_index, outer_index)
            later_time = max(inner_index, outer_index)

            # Get the earlier/later prices for correct ordering
            earlier_price = stock_prices[earlier_time]
            later_price = stock_prices[later_time]

            potential_profit = later_price - earlier_price
            max_profit = max(max_profit, potential_profit)
    return max_profit

stock_prices_yesterday = []
print(get_max_profit(stock_prices_yesterday) == 0)

stock_prices_yesterday = [6]
print(get_max_profit(stock_prices_yesterday) == 0)

stock_prices_yesterday = [10, 7, 5, 8, 11, 9]
print(get_max_profit(stock_prices_yesterday) == 6)

def get_max_profit(stock_prices):
    '''returns the best profit I could have made from 1 purchase and 1 sale of 1 Apple stock yesterday.
       have to buy before selling
       O(n^2) Solution
    '''
    max_profit = 0

    for inner_index in range(len(stock_prices)):
        for outer_index in range(len(stock_prices[inner_index:])):
            earlier_time = min(inner_index, outer_index)
            later_time = max(inner_index, outer_index)

            # Get the earlier/later prices for correct ordering
            earlier_price = stock_prices[earlier_time]
            later_price = stock_prices[later_time]

            potential_profit = later_price - earlier_price
            max_profit = max(max_profit, potential_profit)
    return max_profit

stock_prices_yesterday = []
print(get_max_profit(stock_prices_yesterday)...


I am working on the file "InterviewCake Questions.ipynb".
The first task is:
Need to do better than O(n^2), so it'll probably be either O(n log n) or O(n). Let's try a greedy approach.
Can you write Python code for it?

def get_max_profit(stock_prices):
    '''returns the best profit I could have made from 1 purchase and 1 sale of 1 Apple stock yesterday.
       have to buy before selling.
       Came up with this on my own! Runtime is O(n)
    '''
    if len(stock_prices) < 2:
        return 0

    min_buy = stock_prices[0]
    max_sell = stock_prices[1]
    print(min_buy, max_sell)
    for time in range(len(stock_prices)-1):

        if time == 0:
            continue

        buy = stock_prices[time]
        sell = stock_prices[time+1]

        min_buy = min(min_buy, buy)
        max_sell = max(max_sell, sell)
        print(min_buy, max_sell)

    return max_sell - min_buy

# stock_prices_yesterday = []
# print(get_max_profit(stock_prices_yesterday) == 0)

# stock_prices_yesterday = [6]
# print(get_max_profit(stock_prices_yesterday) == 0)

stock_prices_yesterday = [10, 7, 5, 8, 11, 9]
print(get_max_profit(stock_prices_yesterday))

# stock_prices_yesterday = [4, 3, 2, 1]
# print(get_max_profit(stock_prices_yesterday))

# stock_prices_yesterday = [4, 4, 4, 4]
# print(get_max_profit(stock_prices_yesterday))

def get_max_profit(stock_prices_yesterday):

    # make sure we have at least 2 prices
    if len(stock_prices_yesterday) < 2:
        raise IndexError('Getting a profit requires at least 2 prices')

    # we'll greedily update min_price and max_profit, so we initialize
    # them to the first price and the first possible profit
    min_price = stock_prices_yesterday[0]
    max_profit = stock_prices_yesterday[1] - stock_prices_yesterday[0]

    for index, current_price in enumerate(stock_prices_yesterday):

        # skip the first (0th) time
        # we can't sell at the first time, since we must buy first,
        # and we can't buy and sell at the same time!
        # if we took this out, we'd try to buy *and* sell at time 0.
        # this would give a profit of 0, which is a problem if our
        # max_profit is supposed to be *negative*--we'd return 0.
     ...


I am working on the file "Intro ML Semcomp/semcomp17_ml/.ipynb_checkpoints/semcomp17_ml_answer-checkpoint.ipynb".
The first task is:
1. Linear regression
1. 1. Univariate linear regression
Let start with the most simple regression example. Firstly, read the data in a file named "house_price_statcrunch.xls".
Can you write Python code for it?


I am working on the file "Intro ML Semcomp/semcomp17_ml/.ipynb_checkpoints/semcomp17_ml_answer-checkpoint.ipynb".
The first task is:
1.3 Gradient descent with momentum
In the latest experiment, our training takes ~4000 iterations to converge. Now let try gradient descent with momentum to speed up the training. We will employ the following formula:
$$v_t = m*v_{t-1} + k\frac{\partial L}{\partial W}$$
$$W = W - v_t$$
Can you write Python code for it?

"""step 4: gradient descent with momentum - update loop"""
W = np.random.rand(1,X_train.shape[0]).astype(np.float32)   #W has size 1xD, randomly initialised
k = 1e-2                                #learning rate
epsilon = 1e-2                            #terminate condition
m = 0.9                                 #momentum
v = 0                                   #initial velocity

#visualisation settings
vis_interval = 10
loss_collections = []
prev_loss = 0
plt.close()
plt.ion()
fig = plt.figure(1,figsize=(16, 4))
axis_loss = fig.add_subplot(131)
#axis_data = fig.add_subplot(132)
while(1):
    Y_train = F_Regression(X_train,W)               #compute hypothesis function aka. predicted values
    loss = Loss_Regression(Y_train, Z_train)         #compute loss
    dLdW = dLdW_Regression(X_train, Y_train, Z_train) #compute gradient
    v = v*m + k*dLdW
    W = W - v                            #update
    loss_collections.append(loss)
    if abs(loss - prev_loss) < epsilon:
        break
    prev_loss = loss
    if (len(loss_collections)+1) % vis_interval==0:
        #print "Iter #", len(loss_collections)
        ml_utils.plot_loss(axis_loss, range(len(loss_collections)),loss_collections, "loss = " + str(loss))
        #ml_utils.plot_scatter_and_line(axis_data, X_train, Z_train, W, "iter #" + str(len(loss_collections)))
        fig.canvas.draw()

print "Learned parameters ", W.squeeze()
print "Learning terminates after {} iterations".format(len(loss_collections))


I am working on the file "Intro ML Semcomp/semcomp17_ml/.ipynb_checkpoints/semcomp17_ml_answer-checkpoint.ipynb".
The first task is:
Confirm that the error measurement on the test set has improved.
1.2 Multivariate regression
So far we assume the house price is affected by the size only. Now let consider also other fields "Bedrooms", "Baths", "lot" (location) and "NW" (whether or not the houses face Nothern West direction).

Important: now your feature vector is multi-dimensional, it is crucial to normalise your training set for gradient descent to converge properly. The code below is almost identical to the previous step 1, except it loads more fields and implements data normalisation.
Can you write Python code for it?

"""step 1: define training set X, Z."""
selected_fields = ['size', 'Bedrooms', 'Baths', 'lot', 'NW']
X_train, Z_train = ml_utils.extract_data(house_data_train, selected_fields, ['price'])
X_test, Z_test = ml_utils.extract_data(house_data_test, selected_fields, ['price'])

Z_train = Z_train/1000.0                        #price has unit x1000 USD
Z_test = Z_test/1000.0

##normalise
X_train, u, scale = ml_utils.normalise_data(X_train)
X_test = ml_utils.normalise_data(X_test, u, scale)

N = Z_train.size                                            #number of training samples
ones_array = np.ones((1,N),dtype=np.float32)
X_train = np.concatenate((X_train, ones_array), axis=0)           #why?
X_test  = np.concatenate((X_test, np.ones((1, Z_test.size), dtype=np.float32)), axis = 0) #same for test data
print "size of X_train ", X_train.shape
print "size of Z_train ", Z_train.shape


I am working on the file "Intro ML Semcomp/semcomp17_ml/.ipynb_checkpoints/semcomp17_ml_answer-checkpoint.ipynb".
The first task is:
For univariate regression, we are interested in the "size" parameter only. Let's extract necessary data and visualise it.
Can you write Python code for it?

X, Z = ml_utils.extract_data(house_data, ['size'], ['price'])
Z = Z/1000.0  #price has unit x1000 USD

plt.plot(X[0],Z[0], '.')
plt.xlabel('size (feet^2)')
plt.ylabel('price (USD x1000)')
plt.title('house data scatter plot')
plt.show()


I am working on the file "Intro ML Semcomp/semcomp17_ml/.ipynb_checkpoints/semcomp17_ml_answer-checkpoint.ipynb".
The first task is:
Now evaluate your learned model using the test set. Measure the total error of your prediction
Can you write Python code for it?

Y_test = F_Regression(X_test, W)
error = Loss_Regression(Y_test, Z_test)
print "Evaluation error: ", error


I am working on the file "Intro ML Semcomp/semcomp17_ml/.ipynb_checkpoints/semcomp17_ml_answer-checkpoint.ipynb".
The first task is:
Now run step 2-4 again. Note the followings:
1. You need not to modify the Loss_Regression and dLdW_Regression functions. They should generalise enough to work with multi-dimensional data
2. Since your training samples are normalised you can now use much higher learning rate e.g. k = 1e-2
3. Note that the plot function plot_scatter_and_line will not work in multivariate regression since it is designed for 1-D input only. Consider commenting it out.

Question: how many iterations are required to pass the threshold $\Delta L < 10^{-2}$ ?

Answer: ~4000 iterations (and it will take a while to complete).
Task 1.5: (a) evaluate your learned model on the test set. (b) Suppose the house you want to sell has a size of 3000 $feet^2$, has 3 bedrooms, 2 baths, lot number 10000 and in NW direction. How much do you think its price would be? Hints: don't forget to normalise the test sample.

Answer: You will get ~150k USD only, much lower than the previous prediction based on size only. Your house has an advantage of size, but other parameters matter too.
Can you write Python code for it?

"""step 4: gradient descent - update loop"""
""" same code but change k = 1e-2"""
W = np.random.rand(1,X_train.shape[0]).astype(np.float32)   #W has size 1xD, randomly initialised
k = 1e-2                                #learning rate
epsilon = 1e-2                            #terminate condition

#visualisation settings
vis_interval = 10
loss_collections = []
prev_loss = 0
plt.close()
plt.ion()
fig = plt.figure(1,figsize=(16, 4))
axis_loss = fig.add_subplot(131)
#axis_data = fig.add_subplot(132)
while(1):
    Y_train = F_Regression(X_train,W)               #compute hypothesis function aka. predicted values
    loss = Loss_Regression(Y_train, Z_train)         #compute loss
    dLdW = dLdW_Regression(X_train, Y_train, Z_train) #compute gradient
    W = W - k*dLdW                            #update
    loss_collections.append(loss)
    if abs(loss - prev_loss) < epsilon:
        break
    prev_loss = loss

    if (len(loss_collections)+1) % vis_interval==0:
        #print "Iter #", len(loss_collections)
        ml_utils.plot_loss(axis_loss, range(len(loss_collections)),loss_collections, "loss = " + str(loss))
        #ml_utils.plot_scatter_and_line(axis_data, X_train, Z_train, W, "iter #" + str(len(loss_collections)))
        fig.canvas.draw()

print "Learned parameters ", W.squeeze()
print "Learning terminates after {} iterations".format(len(loss_collections))

"""apply on the test set"""
Y_test = F_Regression(X_test, W)
error = Loss_Regression(Y_test, Z_test)
print "Evaluation error: ", error

"""test a single sample"""
x = np.array([3000, 3,2, 10000, 1],dtype=np.float32)[...,None]
x = ml_utils.normalise_data(x, u, scale)
x = np.concatenate((x,np.ones((1,1))),axis=0)
print "Price: ", F_Regression(x,W).squeeze()


I am working on the file "Intro ML Semcomp/semcomp17_ml/.ipynb_checkpoints/semcomp17_ml_answer-checkpoint.ipynb".
The first task is:
Now we will perform gradient descent update procedure according to the following formula:
$$W = W - k\frac{\partial L}{\partial W}$$
Here we use fixed number of iterations and learning rate.
Can you write Python code for it?

"""step 4: gradient descent - update loop"""
np.random.seed(0)
W = np.random.rand(1,X_train.shape[0]).astype(np.float32)   #W has size 1xD, randomly initialised
k = 1e-8                                #learning rate
niters = 160                            #number of training iterations

#visualisation settings
vis_interval = niters/50
loss_collections = []
plt.close()
plt.ion()
fig = plt.figure(1,figsize=(16, 4))
axis_loss = fig.add_subplot(131)
axis_data = fig.add_subplot(132)
for i in xrange(niters):
    Y_train = F_Regression(X_train,W)               #compute hypothesis function aka. predicted values
    loss = Loss_Regression(Y_train, Z_train)         #compute loss
    dLdW = dLdW_Regression(X_train, Y_train, Z_train) #compute gradient
    W = W - k*dLdW                            #update
    loss_collections.append(loss)
    if (i+1)% vis_interval == 0:
        ml_utils.plot_loss(axis_loss, range(i+1),loss_collections, "loss = " + str(loss))
        ml_utils.plot_scatter_and_line(axis_data, X_train, Z_train, W, "iter #" + str(i))
        fig.canvas.draw()
print "Learned parameters ", W.squeeze()


I am working on the file "Intro ML Semcomp/semcomp17_ml/.ipynb_checkpoints/semcomp17_ml_answer-checkpoint.ipynb".
The first task is:
Our goal is to build a house price prediction model that will approximate the price of a house given its size. To do it, we need to fit a linear line (y = ax + b) to the data above using linear regression. Remember the procedure:
1. Define training set
2. Define hypothesis function. Here $F(x,W) = Wx$
3. Loss function. Here $L(W) = \frac{1}{2N}{\sum_{i=1}^N{(F(x^{(i)},W)-z)^2}}$
4. Update procedure (gradient descent). $W = W - k\frac{\partial L}{\partial W}$
To speed up computation, you should avoid using loop when working with scripting languges e.g. Python, Matlab. Try using array/matrix instead. Here you are provided code for step 1 and 2. Your will be asked to implement step 3 and 4. Some skeleton code will be provided for your convenience.
Can you write Python code for it?

"""step 1: define training and test set X, Z."""
X_train, Z_train = ml_utils.extract_data(house_data_train, ['size'], ['price'])
X_test, Z_test = ml_utils.extract_data(house_data_test, ['size'], ['price'])

Z_train = Z_train/1000.0                        #price has unit x1000 USD
Z_test = Z_test/1000.0

##normalise data, uncomment for now
#X_train, u, scale = ml_utils.normalise_data(X_train)
#X_test = ml_utils.normalise_data(X_test, u, scale)

N = Z_train.size                                            #number of training samples
ones_array = np.ones((1,N),dtype=np.float32)
X_train = np.concatenate((X_train, ones_array), axis=0)           #why?
X_test  = np.concatenate((X_test, np.ones((1, Z_test.size), dtype=np.float32)), axis = 0) #same for test data
print "size of X_train ", X_train.shape
print "size of Z_train ", Z_train.shape

"""step 2: define hypothesis function"""
def F_Regression(X, W):
    """
    Compute the hypothesis function y=F(x,W) in batch.
    input:  X  input array, must has size DxN (each column is one sample)
            W  parameter array, must has size 1xD
    output: linear multiplication of W*X, size 1xN
    """
    return np.dot(W,X)


I am working on the file "Intro ML Semcomp/semcomp17_ml/.ipynb_checkpoints/semcomp17_ml_answer-checkpoint.ipynb".
The first task is:
Rerun the update loop again with the new loss and gradient functions. Note you may need to change the learning rate accordingly to have proper convergence. Now you have implemented both regularisation and momentum techniques, you can use a standard learning rate value of 0.01 which is widely used in practice.
Can you write Python code for it?

""" gradient descent with regularisation- parameter update loop"""
W = np.random.rand(1,X_train.shape[0]).astype(np.float32)   #W has size 1xD, randomly initialised
k = 0.01                                #learning rate
epsilon = 1e-6                            #terminate condition
m = 0.9                                 #momentum
v = 0                                   #initial velocity

#visualisation settings
vis_interval = 10
loss_collections = []
prev_loss = 0
plt.close()
plt.ion()
fig = plt.figure(1,figsize=(16, 4))
axis_loss = fig.add_subplot(131)
axis_data = fig.add_subplot(132)
for i in range(500):
    Y_train = F_Classification(X_train,W)               #compute hypothesis function aka. predicted values
    loss = Loss_Classification_Reg(Y_train, Z_train, W)         #compute loss
    dLdW = dLdW_Classification_Reg(X_train, Y_train, Z_train, W) #compute gradient
    v = v*m + k*dLdW
    W = W - v                            #update
    loss_collections.append(loss)
    if abs(loss - prev_loss) < epsilon:
        break
    prev_loss = loss
    if (len(loss_collections)+1) % vis_interval==0:
        ml_utils.plot_loss(axis_loss, range(len(loss_collections)),loss_collections, "loss = " + str(loss))
        ml_utils.plot_scatter_with_label_2d(axis_data, X_train, Z_train, W, "student score scatter plot")
        fig.canvas.draw()

print "Learned parameters ", W.squeeze()
print "Learning terminates after {} iterations".format(len(loss_collections))


I am working on the file "Intro ML Semcomp/semcomp17_ml/.ipynb_checkpoints/semcomp17_ml_answer-checkpoint.ipynb".
The first task is:
Task 1.2: compute gradient of the loss function w.r.t parameter W according to the following formula:<br>
$$\frac{\partial L}{\partial W} = \frac{1}{N}\sum_{i=1}^N{(y^{(i)}-z^{(i)})x^{(i)}}$$
Please fill in the skeleton code below.
Can you write Python code for it?

"""step 4: gradient descent - compute gradient"""
def dLdW_Regression(X, Y, Z):
    """
    Compute gradient of the loss w.r.t parameter W.
    input:   X   input array, each column is one sample, has size DxN
             Y   predicted values, has size 1xN
             Z   groundtruth values, has size 1xN
    output:  gradient, has same size as W
    """
    #enter the code here
    N = float(Z.size)
    return 1/N * (Y-Z).dot(X.T)


I am working on the file "Intro ML Semcomp/semcomp17_ml/.ipynb_checkpoints/semcomp17_ml_answer-checkpoint.ipynb".
The first task is:
Task 1.4: The gradient descent in the code above terminates after 100 iterations. You may want it to terminate when improvement in the loss is below a threshold.
$$\Delta L_t = |L_t - L_{t-1}| < \epsilon$$
Edit the code to terminate the loop when the loss improvement is below $\epsilon=10^{-2}$. Re-evaluate your model to see if its performance has improved.
Can you write Python code for it?

"""step 4: gradient descent - update loop"""
W = np.random.rand(1,X_train.shape[0]).astype(np.float32)   #W has size 1xD, randomly initialised
k = 1e-8                                #learning rate
epsilon = 1e-2                            #terminate condition

#visualisation settings
vis_interval = 10
loss_collections = []
prev_loss = 0
plt.close()
plt.ion()
fig = plt.figure(1,figsize=(16, 4))
axis_loss = fig.add_subplot(131)
axis_data = fig.add_subplot(132)
while(1):
    Y_train = F_Regression(X_train,W)               #compute hypothesis function aka. predicted values
    loss = Loss_Regression(Y_train, Z_train)         #compute loss
    dLdW = dLdW_Regression(X_train, Y_train, Z_train) #compute gradient
    W = W - k*dLdW                            #update
    loss_collections.append(loss)
    if abs(loss - prev_loss) < epsilon:
        break
    prev_loss = loss

    if (len(loss_collections)+1) % vis_interval==0:
        #print "Iter #", len(loss_collections)
        ml_utils.plot_loss(axis_loss, range(len(loss_collections)),loss_collections, "loss = " + str(loss))
        ml_utils.plot_scatter_and_line(axis_data, X_train, Z_train, W, "iter #" + str(len(loss_collections)))
        fig.canvas.draw()

print "Learned parameters ", W.squeeze()
print "Learning terminates after {} iterations".format(len(loss_collections))

#run the test
Y_test = F_Regression(X_test, W)
error = Loss_Regression(Y_test, Z_test)
print "Evaluation error: ", error


I am working on the file "Intro ML Semcomp/semcomp17_ml/.ipynb_checkpoints/semcomp17_ml_tutorial-checkpoint.ipynb".
The first task is:
1. Linear regression
1. 1. Univariate linear regression
Let start with the most simple regression example. Firstly, read the data in a file named "house_price_statcrunch.xls".
Can you write Python code for it?


I am working on the file "Intro ML Semcomp/semcomp17_ml/.ipynb_checkpoints/semcomp17_ml_tutorial-checkpoint.ipynb".
The first task is:
Confirm that the error measurement on the test set has improved.
1.2 Multivariate regression
So far we assume the house price is affected by the size only. Now let consider also other fields "Bedrooms", "Baths", "lot" (location) and "NW" (whether or not the houses face Nothern West direction).

Important: now your feature vector is multi-dimensional, it is crucial to normalise your training set for gradient descent to converge properly. The code below is almost identical to the previous step 1, except it loads more fields and implements data normalisation.
Can you write Python code for it?

"""step 1: define training set X, Z."""
selected_fields = ['size', 'Bedrooms', 'Baths', 'lot', 'NW']
X_train, Z_train = ml_utils.extract_data(house_data_train, selected_fields, ['price'])
X_test, Z_test = ml_utils.extract_data(house_data_test, selected_fields, ['price'])

Z_train = Z_train/1000.0                        #price has unit x1000 USD
Z_test = Z_test/1000.0

##normalise
X_train, u, scale = ml_utils.normalise_data(X_train)
X_test = ml_utils.normalise_data(X_test, u, scale)

N = Z_train.size                                            #number of training samples
ones_array = np.ones((1,N),dtype=np.float32)
X_train = np.concatenate((X_train, ones_array), axis=0)           #why?
X_test  = np.concatenate((X_test, np.ones((1, Z_test.size), dtype=np.float32)), axis = 0) #same for test data
print("size of X_train ", X_train.shape)
print("size of Z_train ", Z_train.shape)(


I am working on the file "Intro ML Semcomp/semcomp17_ml/.ipynb_checkpoints/semcomp17_ml_tutorial-checkpoint.ipynb".
The first task is:
Let preserve some data for test. Here we extract 10% for testing.
Can you write Python code for it?

np.random.seed(0)
idx = np.random.permutation(TOTALS)
idx_train = idx[:90]
idx_test = idx[90:]
house_data_train = {}
house_data_test = {}
for field in house_data.keys():
    house_data_test[field] = house_data[field][idx_test]
    house_data_train[field] = house_data[field][idx_train]


I am working on the file "Intro ML Semcomp/semcomp17_ml/.ipynb_checkpoints/semcomp17_ml_tutorial-checkpoint.ipynb".
The first task is:
Let see what is inside by printing out the first few lines.
Can you write Python code for it?

print(" ".join([field.ljust(10) for field in house_data.keys()]))
for i in range(10):
    print(" ".join([str(house_data[field][i]).ljust(10) for field in house_data.keys()]))
TOTALS = len(house_data['House'])
print("...\n\nTotal number of samples: {}".format(TOTALS))


I am working on the file "Intro ML Semcomp/semcomp17_ml/.ipynb_checkpoints/semcomp17_ml_tutorial-checkpoint.ipynb".
The first task is:
Now evaluate your learned model using the test set. Measure the total error of your prediction
Can you write Python code for it?