I am working on the file "NumberLineModel.ipynb".
The first task is:
Plot distance - error
Can you write Python code for it?

# http://stackoverflow.com/questions/1867861/python-dictionary-how-to-keep-keys-values-in-same-order-as-declared
experiment_keys = ['distance1', 'distance2', 'distance3', 'distance4', 'distance5', 'distance6']
results = []
for key in experiment_keys:
    sum = 0
    for keys in experiment[key]:
        sum += experiment[key][keys]
    average = sum/len(experiment[key])/10 #i.e. /1000*100
    results.append(average)

results

x = np.arange(0, 6)
plt.plot(x,np.array(results),color='#096c99')

plt.xlabel('Distance')
plt.ylabel('Error-Rate in %')
plt.title('Distance Effect')
plt.xticks(np.arange(min(x), max(x)+1, 1.0))
plt.savefig('distanceEffect.png')
plt.show()


I am working on the file "NumberLineModel.ipynb".
The first task is:
Therefore the model doesn't consistently discriminate $16$ from $17$.
$16 \cdot.125 = 2$
therefore we expect that the model consistently discriminates $16$ from $18$
Can you write Python code for it?

sigma_a = 16 * weber
sigma_b = 18 * weber
difference_sigma = sqrt(sigma_a**2+sigma_b**2)
test =  np.random.normal(2, difference_sigma, 1000)
100-len(test[test<0])/10 #/1000*100


I am working on the file "NumberLineModel.ipynb".
The first task is:
We usually decide to call a specific level of performance the ‘threshold’. A good level (for 2AFC) is 75% correct, as it is half way between chance and perfect performance – it’s when the observer can just see the target.
18/16 = 1.125 --> 18 = 16 * 1.125 = 16 * (1+0.125) = 161 + 16.125
Perfection is asyntotical, but note 4/2 = 2 >> .125  and the error rate is always almost 0
Can you write Python code for it?

sigma_a = 2 * weber
sigma_b = 4 * weber
difference_sigma = sqrt(sigma_a**2+sigma_b**2)
test =  np.random.normal(2, difference_sigma, 1000)
100-len(test[test<0])/10 #/1000*100

test_ratio = [(5,5), (6,5), (7,5), (5,3), (7,4), (8,4)]
results = []
for i in test_ratio:
    sigma_a = i[0] * weber
    sigma_b = i[1] * weber
    difference_sigma = sqrt(sigma_a**2+sigma_b**2)
    test = np.random.normal(abs(i[0]-i[1]), difference_sigma, 1000)
    results.append(100-len(test[test<0])/10)

results

x = [5.0/5.0, 6.0/5.0, 7.0/5.0, 5.0/3.0, 7.0/4.0, 8.0/4.0]
plt.plot(x, results)
plt.show()

xvals = np.linspace(1, 2, 1000)
yinterp = np.interp(xvals, x, results)
line2d = plt.plot(xvals, yinterp)
#plt.axvline(x=1.125)
#plt.axhline(y=75)
plt.plot((0.8, 1.148), (75, 75), 'k--')
plt.plot((1.148, 1.148), (50, 75), 'k--')
plt.show()
np.interp(75, results, x)

# http://stackoverflow.com/questions/9850845/how-to-extract-points-from-a-graph
# plot() returns a useful object: [<matplotlib.lines.Line2D object at 0x38c9910>]
xvalues = line2d[0].get_xdata()
yvalues = line2d[0].get_ydata()
idy = np.min(np.where(np.logical_and(yvalues<=76,yvalues>=75)))
# xy = line2d[0].get_xydata()
# xy[2]
print(np.interp(75, results, x))
xvalues[idy]


I am working on the file "NumberLineModel.ipynb".
The first task is:
Wheter the number line is compressive or linear is an empirical issue.
Mathematically the two models are clearly the same.
If plotted on a log scale, the gaussians have a fixed standard deviation, as is more apparent from the normalized log response below
Can you write Python code for it?

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.mlab as mlab
import math
from matplotlib import style
from matplotlib.ticker import ScalarFormatter
#style.use('fivethirtyeight')
style.use('ggplot')

mu = 1
variance = 1
weber = .125
sigma = math.sqrt(variance)
x = np.linspace(1, 14, 1000)

fig = plt.figure()
for mu in range(1,11):
    maximum_activation = mlab.normpdf(1, 1, mu*.125)
    sigma = mu * weber
    #normalized plot, maximum activation one
    #sigma = math.sqrt(variance)
    plt.plot(x,mlab.normpdf(x, mu, sigma)/maximum_activation)

plt.xlabel('Number')
plt.ylabel('Activation')
plt.title('NLM - Normalized Log')
plt.xscale('log')
plt.gca().xaxis.set_major_formatter(ScalarFormatter())
plt.xticks([1,2,3,4,5,6,7,8,9,20])
plt.xlim([1,20])
plt.savefig('NLMnormlog.png')
plt.show()

from scipy import integrate
g = mlab.normpdf(x, 1, sigma)/maximum_activation
f = mlab.normpdf(x, 2, sigma)/maximum_activation

integrate.quad(lambda x: abs(mlab.normpdf(x, 1, sigma)/maximum_activation - mlab.normpdf(x, 2, sigma)/maximum_activation), 1, 14)


I am working on the file "NumberOfDivisor.ipynb".
The first task is:
Divisor
Number of Divisor
Write a function which returns the number of divisor (including 1 and itself).
Example:

Input: 5,  Output: 2 (1, 5)
Input: 12, Output: 6 (1, 2, 3, 4, 6, 12)
Input: 49, Output: 3 (1, 7, 49)
Can you write Python code for it?

# Simple but not efficient answer

def num_div0(n):
    '''
    n: Integer (bigger than 0)
    output: Number of Divisor (Including 1 and n)
    '''

    count_div = 0
    for div in range(1, n+1):   # 1 - n
        if n % div == 0:
            count_div += 1

    return count_div

print (5, num_div0(5))
print (12, num_div0(12))
print (49, num_div0(49))

# This function is not efficient, so it's difficult to handle big input
print (9999999, num_div0(9999999))


I am working on the file "NumberOfDivisor.ipynb".
The first task is:
Java version
```
class num_div {
static int num_div0(int n) {
    int count_div = 0 ;
    int sqrt_n = (int)Math.sqrt((double)n) ;
    for (int div=1; div<=sqrt_n; div++) {
        if (n % div == 0) {
            count_div++ ;
            int qt = n / div ;
            if (qt != div) count_div++ ;
        }
    }
    return count_div ;
}

public static void main(String argv[]) {

    System.out.printf("%d %d\n", 5, num_div0(5)) ;
    System.out.printf("%d %d\n", 12, num_div0(12)) ;
    System.out.printf("%d %d\n", 49, num_div0(49)) ;
}

}
```
Further improvement
Every interger can be represented as prime factorized format. $2^{c0}\cdot 3^{c1}\cdot 5^{c2} \cdot 7^{c3} ... (cx >= 0)$
In that case, number of divisor is calculated as $(c0+1) \cdot (c1+1) \cdot (c2+1) \cdot (c3+1) ...$
Because every divisors are combination of the prime factor of input number.
Example $12=2^2\cdot3^1$, Num of Divisor $(2+1)\cdot(1+1)=6$
Divisor: $2^0\cdot 3^0=1\2^1\cdot 3^0=2\2^2\cdot 3^0=4\2^0\cdot 3^1=3\2^1\cdot 3^1=6\2^2\cdot 3^1=12$
Question
What is the smallest integer that has 100 divisors (including 1 and itself) ?
Can you write Python code for it?


I am working on the file "NumberOfDivisor.ipynb".
The first task is:
Java version of simple algorithm
```
class num_div {
static int num_div0(int n) {
    int count_div = 0 ;
    for (int div=1; div<=n; div++) {
        if (n % div == 0) count_div++ ;
    }
    return count_div ;
}

public static void main(String argv[]) {

    System.out.printf("%d %d\n", 5, num_div0(5)) ;
    System.out.printf("%d %d\n", 12, num_div0(12)) ;
    System.out.printf("%d %d\n", 49, num_div0(49)) ;
}

}
```
How to improve the performance
Hint: Prime number checking
Can you write Python code for it?

# Faster than version 0

def num_div1(n):
    '''
    n: Integer (bigger than 0)
    output: Number of Divisor (Including 1 and n)
    '''

    import sys
    count_div = 0
    sqrt_n = (int)(n**0.5)
    for div in range(1, sqrt_n+1):   # 1 - n
        if n % div == 0:
            count_div += 1
            qt = n // div
            print('(', n, '/', div, '=', qt, ')', end = ' ', file=sys.stderr)
            if n // div != div:  # quatient != div
                count_div += 1

    print('Number of divisor:', count_div, file=sys.stderr)
    return count_div

print (5, num_div1(5))
print (12, num_div1(12))
print (49, num_div1(49))
print (9999999, num_div1(9999999))


I am working on the file "Numbers.ipynb".
The first task is:
Python 3 Alert!
Whoa! What just happened? Last time I checked, 3 divided by 2 is equal 1.5 not 1!
The reason we get this result is because we are using Python 2. In Python 2, the / symbol performs what is known as "classic" division, this means that the decimal points are truncated (cut off). In Python 3 however, a single / performs "true" division. So you would get 1.5 if you had inputed 3/2 in Python 3.
So what do we do if we are using Python 2 to avoid this?
There are two options:
Specify one of the numbers to be a float:
Can you write Python code for it?

# Specifying one of the numbers as a float
3.0/2

# Works for either number
3/2.0


I am working on the file "Numbers.ipynb".
The first task is:
Now if I call a in my Python script, Python will treat it as the number 5.
Can you write Python code for it?


I am working on the file "Numbers.ipynb".
The first task is:
We could also "cast" the type using a function that basically turns integers into floats. This function, unsurprisingly, is called float().
Can you write Python code for it?

# We can use this float() function to cast integers as floats:
float(3)/2


I am working on the file "Numbers.ipynb".
The first task is:
We will go over functions in much more detail later on in this course, so don't worry if you are confused by the syntax here. Consider this a sneak preview.
One more "sneak preview" we can use to deal with classic division in Python 2 is importing from a module called future.
This is a module in Python 2 that has Python 3 functions, this basically allows you to import Python 3 functions into Python 2. We will go over imports and modules later in the course, so don't worry about fully understanding the import statement right now!
Can you write Python code for it?


I am working on the file "Numbers.ipynb".
The first task is:
When you import division from the future you won't need to worry about classic division occurring anymore anywhere in your code!
Arithmetic continued
Can you write Python code for it?

# Powers
2**3

# Can also do roots this way
4**0.5

# Order of Operations followed in Python
2 + 10 * 10 + 3

# Can use parenthesis to specify orders
(2+10) * (10+3)


I am working on the file "Numbers.ipynb".
The first task is:
Yes! Python allows you to write over assigned variable names. We can also use the variables themselves when doing the reassignment. Here is an example of what I mean:
Can you write Python code for it?

# Check
a

# Use A to redefine A
a = a + a

# Check
a


I am working on the file "Numerical Integration Tutorial.ipynb".
The first task is:
An interesting thing to do here is take a look at the phase space, that is, plot only the dependent variables, without respect to time:
Can you write Python code for it?

# `x[0,0]` is the first value (1st line, 1st column), `x[0,1]` is the value of
# the 1st line, 2nd column, which corresponds to the value of P at the initial
# time. We plot just this point first to know where we started:
plot(x[0,0], x[0,1], 'o')
print('Initial condition:', x[0])

# `x[0]` or (equivalently) x[0,:] is the first line, and `x[:,0]` is the first
# column. Notice the colon `:` stands for all the values of that axis. We are
# going to plot the second column (P) against the first (V):
plot(x[:,0], x[:,1])
xlabel('V')
ylabel('P')

# Let's calculate and plot another solution with a different initial condition
x2 = odeint(LV, [10., 4.], t, (r, c, e, d))
plot(x2[:,0], x2[:,1])
plot(x2[0,0], x2[0,1], 'o')


I am working on the file "Numerical Integration Tutorial.ipynb".
The first task is:
Congratulations: you are now ready to integrate any system of differential equations! (We hope generalizing the above to more than 2 equations won't be very challenging).
Exploring parameters with a simple interface
IPython’s widgets allow to create user interface (UI) controls for exploring your code interactively. To use this resource you have to run the code below in a computer with all the software required (see first section), plus the library ipywidgets.
The interact function provides a quick way to use widgets to explore the parameter space of ODEs. To do this, first create a new function that integrate your ODEs and return a plot. The argumnts of this functions should be the parameters that you want to explore.
Can you write Python code for it?

def LV_plot(r=2, c=0.5, e=0.5, d=1):
    # Time range
    t = arange(0, 50., 0.01)
    # Initial conditions
    x0 = array([1., 3.])
    # The function to be integrated
    def LV(x, t, r, c, e, d):
        return array([ r*x[0] - c * x[0] * x[1],
        e * c * x[0] * x[1] - d * x[1] ])
    #integrating
    y = odeint(LV, x0, t, (r, c, e, d))
    # ploting: use the function show
    show(plot(t, y))


I am working on the file "Numerical Integration Tutorial.ipynb".
The first task is:
Now call your the function you created above within interact. The arguments for the sliders that set each parameter of the equations are (min, max, step).
Can you write Python code for it?

#Libraries to use interact
from __future__ import print_function
from ipywidgets import interact, interactive, fixed
import ipywidgets as widgets

#Now call the function to be integrated within interact
interact(LV_plot, r=(0,5.,0.1), c = (0,1,0.1), e = (0,1,0.1), d = (0,5, 0.1))


I am working on the file "Numerical Integration Tutorial.ipynb".
The first task is:
We get a much better approximation now, the two curves superimpose each other!
Now, what if we wanted to integrate a system of differential equations? Let's take the Lotka-Volterra equations:
$$ \begin{aligned}
\frac{dV}{dt} &= r V - c V P\
\frac{dP}{dt} &= ec V P - dP
\end{aligned}$$
In this case, the variable is no longer a number, but an array [V, P]. We do the same as before, but now x is going to be an array:
Can you write Python code for it?

# we didn't need to do this again: if the cell above was run already,
# the libraries are imported, but we repeat it here for convenience
from numpy import *
from matplotlib.pyplot import *
from scipy.integrate import odeint

t = arange(0, 50., 0.01)

# parameters
r = 2.
c = 0.5
e = 0.1
d = 1.

# initial condition: this is an array now!
x0 = array([1., 3.])

# the function still receives only `x`, but it will be an array, not a number
def LV(x, t, r, c, e, d):
    # in python, arrays are numbered from 0, so the first element
    # is x[0], the second is x[1]. The square brackets `[ ]` define a
    # list, that is converted to an array using the function `array()`.
    # Notice that the first entry corresponds to dV/dt and the second to dP/dt
    return array([ r*x[0] - c * x[0] * x[1],
                   e * c * x[0] * x[1] - d * x[1] ])

# call the function that performs the integration
# the order of the arguments is as below: the derivative function,
# the initial condition, the points where we want the solution, and
# a list of parameters
x = odeint(LV, x0, t, (r, c, e, d))

# Now `x` is a 2-dimension array of size 5000 x 2 (5000 time steps by 2
# variables). We can check it like this:
print('shape of x:', x.shape)

# plot the solution
plot(t, x)
xlabel('t') # define label of x-axis
ylabel('populations') # and of y-axis
legend(['V', 'P'], loc='upper right')


# we didn't need to do this again: if the cell above was run already,
# the libraries are imported, but we repeat it here for convenience
from numpy import *
from matplotlib.pyplot import *
from scipy.integrate import odeint

t = arange(0, 50., 0.1)

# parameters
r = 2.
c = 0.5
e = 0.1
d = 1.

# initial condition: this is an array now!
x0 = array([1., 3.])

# the function still receives only `x`, but it will be an array, not a number
def LV(x, t, r, c, e, d):
    # in python, arrays are numbered from 0, so the first element
    # is x[0], the second is x[1]. The square brackets `[ ]` define a
    # list, that is converted to an array using the function `array()`.
    # Notice that the first entry corresponds to dV/dt and the second to dP/dt
    return array([ r*x[0] - c * x[0] * x[1],
                   e * c * x[0] * x[1] - d * x[1] ])

# call the function that performs the integration
# the order of the arguments is as below: the derivative function,
# the initial condition, the points where we want the solution, and
# a list of parameters
x = odeint(LV, x0, t, (r, c, e, d))

# Now `x` is a 2-dimension array of size 5000 x 2 (5000 time steps by 2
# variables). We can check it like this:
print('shape of x:', x.shape)

# plot the solution
plot(t, x)
xlabel('t') # define label of x-axis
ylabel('populations') # and of y-axis
legend(['V', 'P'], loc='upper right')


I am working on the file "Numerical Integration Tutorial.ipynb".
The first task is:
Why use scientific libraries?
The method we just used above is called the Euler method, and is the simplest one available. The problem is that, although it works reasonably well for the differential equation above, in many cases it doesn't perform very well. There are many ways to improve it: in fact, there are many books entirely dedicated to this. Although many math or physics students do learn how to implement more sophisticated methods, the topic is really deep. Luckily, we can rely on the expertise of lots of people to come up with good algorithms that work well in most situations.
Then, how... ?
We are going to demonstrate how to use scientific libraries to integrate differential equations. Although the specific commands depend on the software, the general procedure is usually the same:

define the derivative function (the right hand side of the differential equation)
choose a time step or a sequence of times where you want the solution
provide the parameters and the initial condition
pass the function, time sequence, parameters and initial conditions to a computer routine that runs the integration.

A single equation
So, let's start with the same equation as above, the logistic equation, now with any parameters for growth rate and carrying capacity:
$$ \frac{dx}{dt} = f(x) = r x \left(1 - \frac{x}{K} \right) $$
with $r=2$, $K=10$ and $x(0) = 0.1$. We show how to integrate it using python below, introducing key language syntax as necessary.
Can you write Python code for it?

# everything after a '#' is a comment

## we begin importing libraries we are going to use
# import all (*) functions from numpy library, eg array, arange etc.
from numpy import *
# import all (*) interactive plotting functions, eg plot, xlabel etc.
from matplotlib.pyplot import *
# import the numerical integrator we will use, odeint()
from scipy.integrate import odeint

# time steps: an array of values starting from 0 going up to (but
# excluding) 10, in steps of 0.01
t = arange(0, 10., 0.01)
# parameters
r = 2.
K = 10.
# initial condition
x0 = 0.1

# let's define the right-hand side of the differential equation
# It must be a function of the dependent variable (x) and of the
# time (t), even if time does not appear explicitly
# this is how you define a function:
def f(x, t, r, K):
    # in python, there are no curling braces '{}' to start or
    # end a function, nor any special keyword: the block is defined
    # by leading spaces (usually 4)
    # arithmetic is done the same as in other languages: + - * /
    return r*x*(1-x/K)

# call the function that performs the integration
# the order of the arguments is as below: the derivative function,
# the initial condition, the points where we want the solution, and
# a list of parameters
x = odeint(f, x0, t, (r, K))

# plot the solution
plot(t, x)
xlabel('t') # define label of x-axis
ylabel('x') # and of y-axis

# plot analytical solution
# notice that `t` is an array: when you do any arithmetical operation
# with an array, it is the same as doing it for each element
plot(t, K * x0 * exp(r*t)/(K+x0*(exp(r*t)-1.)))
legend(['approximation', 'analytical solution'], loc='best') # draw legend


I am working on the file "Numerical Solution to the 1D Steady State Schrödinger Equation.ipynb".
The first task is:
3.2 The infinite well using FD
The well-known result for particles in an infinite potential well at $[0, 1]$ is:
$$\phi_n(x) = \sqrt{2}\sin(n\pi x),\quad E_n = \frac{n^2\pi^2}{2} \quad (6)$$
Can you write Python code for it?

def ϕ_theory(n):
    return lambda x : np.sqrt(2) * np.sin(n * np.pi * x)


I am working on the file "Numerical Solution to the 1D Steady State Schrödinger Equation.ipynb".
The first task is:
A key observation is that the higher the energy level $n$ goes, the larger the discreptancy between the calculated and theoretical energy value. This is because discretization "drops out" high-frequency information of the wavefunction, which is captured by the higher-level modes. In particular, there's no way to obtain information for energy levels $\ge N$, since a rank-$N$ matrix equation can yield at most $N$ eigenvalues.
To see the trend more clearly, we change the number of grid points $N$ and plot the energy levels:
Can you write Python code for it?

V = lambda x : 0
Ns = [10, 20, 50, 100, 200, 500, 1000]
ns = np.arange(1, 1001)
fig = plt.figure(figsize=(12, 9))
plt.plot(ns, np.pi**2 / 2 * ns**2, label='Theory')
for i, N in enumerate(Ns):
    Es, _, _ = finite_difference(V, 0, 1, N)
    plt.plot(np.arange(1, len(Es) + 1), Es, '.', label='N={}'.format(N))
plt.semilogx()
plt.semilogy()
plt.legend(loc=2)
plt.xlabel('n')
plt.ylabel('En')
plt.title('Comupted and Theoretic Energy Levels')
# plt.savefig('energy_fd.png')


I am working on the file "Numerical Solution to the 1D Steady State Schrödinger Equation.ipynb".
The first task is:
Code for carrying out the finite difference scheme:
Can you write Python code for it?

def finite_difference(V, a, b, N):
    '''
    Solve bound states using the finite difference scheme

    Parameters
    ==========
    `V`: the potential funcion
    `a`, `b`: endpoint of the solution region
    `N`: number of grid points (excluding `a` and `b`)

    Returns
    =======
    `Es`: the energy levels, sorted from low to high
    `ϕs`: value of the wavefunction on the grid points (including `a` and `b`)
    `xs`: the grid points (including `a` and `b`)
    '''
    Δ = (b - a) / (N + 1)
    xs = np.linspace(a, b, N + 2)
    H = Hamiltonian_fd(V, a, b, N)
    Es, ϕs = linalg.eigh(H) # routine for calculating eigenvalues and eigenvectors
    # Post processing
    # The eigenvectors are in default complex, but we know from Hermitian property they are all real
    ϕs = np.real(ϕs)
    # Normalize the eigenvectors according to the L2 norm on [a, b]
    ϕs /= np.sqrt(Δ)
    # Add the boundary values
    n_eigs = len(Es)
    boundary = np.zeros(n_eigs)
    ϕs = np.row_stack((boundary, ϕs, boundary))
    return Es, ϕs, xs


I am working on the file "Numerical Solution to the 1D Steady State Schrödinger Equation.ipynb".
The first task is:
First, let's define some potential functions:
Can you write Python code for it?

def gaussian(center, height, sigma):
    '''Gaussian Potential'''
    return lambda x : height * np.exp(-(x - center)**2/(2 * sigma**2))

def square(height, l, r):
    '''Square Well Potential'''
    def V(x):
        if l < x < r:
            return height
        else:
            return 0.0
    return V


I am working on the file "Numerical Solution to the 1D Steady State Schrödinger Equation.ipynb".
The first task is:
Intuitively, the Gaussian potential should decrease the particle density in the central region, which is confirmed by the numerical results. Also note the result for $N = 20$ is almost identical to $N = 10$, which is remarkable. In general, only a few basis functions is needed if the only thing of interest is the ground state. As usual, more functions are needed if we wish to resolve higher energy states.
4.3 Remarks regarding the variational method
In actual usage, we can loosen the requirement that the basis functions $\psi_n(x)$ be orthornormal. The general case of an arbitrary basis gives rise to the generalized eigenvalue problem
$$ \mathrm{H}\mathrm{C} = E\mathrm{S}\mathrm{C} $$
where $\mathrm{S}_{ij} := \langle \psi_i | \psi_j \rangle$ is the metric matrix. The generalized eigenvalue problem can also be solved with ease using existing numerical linear algebra packages. For example, the finite element method uses B-splines as basis functions.
References
[1] Nobel Focus: Chemistry by Computer, Phys. Rev. Focus 2, 19
[2] Anders W. Sandvik, Numerical Solutions of the Schrodinger Equation, PY 502, Computational Physics, Fall 2015 (Boston University)
[3] Philippe de Forcrand, Matthias Troyer, Computational Quantum Physics, ETH Zürich, SS 2008
[4] Numerical solution to Schrödinger equation - eigenvalues, Physics StackExchange
Can you write Python code for it?

import sys
print("Python version = ", sys.version)
print("Numpy version = ", np.version.version)
print("Scipy version = ", scipy.version.version)
print("Matplotlib version = ", matplotlib.__version__)


I am working on the file "Numerical Solution to the 1D Steady State Schrödinger Equation.ipynb".
The first task is:
The code for the variational method is very simple:
Can you write Python code for it?

def var_method(V, N):
    '''
    Returns
    =======
    `Es`: energy levels, sorted from low to high
    `cs`: the corresponding coefficients
    '''
    H = Hamiltonian_var(V, N)
    Es, cs = linalg.eigh(H)
    return Es, cs


I am working on the file "Numerical Solution to the 1D Steady State Schrödinger Equation.ipynb".
The first task is:
The numerical results for each value of $\sigma$ is plotted below:
Can you write Python code for it?

N = 400
xs = np.linspace(-10, 10, N) # Recorded points
fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(18,10), sharey=True)
for i, σ in enumerate(σs):
    V = Vs[i]
    ϕs = scattering_state(E, V, xs)
    # Plotting routines
    ax_V = axes[i//3, i%3]
    ax_ϕ = ax_V.twinx()
    ax_V.plot(xs, [V(x) for x in xs], 'b', label=r'$V$')
    ax_ϕ.plot(xs, abs(ϕs)**2, 'r', label=r'$|\phi|^2$')
    ax_ϕ.set_ylim(0, 5.0)
    ax_V.set_yticklabels(ax_V.get_yticks(), color='b')
    ax_ϕ.set_yticklabels(ax_ϕ.get_yticks(), color='r')
    ax_V.legend(loc=2)
    ax_ϕ.legend()
    ax_V.set_title(r'$\sigma={}$'.format(σ))
fig.suptitle('The delta barrier', fontsize=20)
# plt.savefig('delta_barrier.png')


I am working on the file "Numerical Solution to the 1D Steady State Schrödinger Equation.ipynb".
The first task is:
Thus $|A| \approx 1.234$ and $|B| \approx 0.722$, which gives $|T| \approx 0.657$ and $|R| \approx 0.342$. This is close to the theoretical value predicted in (3).
3. Bound state - finite difference scheme
3.1 Discretization of the Schrödinger Equation
The energy level $E_n$ of bound states are usually not know a priori. Although the methods discussed in Section 2 can still be adapted in this situation, for most choice of $E$ the wavefunction will diverge. We are then forced to tune the value of $E$ incrementally until the solution coverges. This is the so-called "shooting method".
A different approach is to discrete the phase space and approximate derivatives with finite differences, giving rise to the finite difference (FD) scheme. We proceed as follows:

Choose solution domain $[a, b]$ large enough so that most of the particle density is concentrated within the region. Then fix $\phi(a) = \phi(b) = 0$

Discretization of space: let $x_k = a + k\Delta,\,(k = 0, 1, \ldots, N + 1)$ with $\Delta = (b - a) / (N + 1)$. The wave function is now represented by its value on the grid points $\Phi := (\phi(x_1)\ldots\phi(x_N))^T$

Discretization of the Hamiltonian: Taylor expansion gives the approximation $\phi(x_{k-1}) - 2\phi(x_k) + \phi(x_{k+1}) \approx \phi''(x_k)\Delta^2$. Substituting this into (1) gives the matrix equation

$$ \mathrm{H}\Phi = E\Phi \quad (5)$$
where $$\mathrm{H} = \frac{1}{2\Delta}\begin{pmatrix}
2 & -1 & & & \
-1 & 2 & -1 & & \
& \ddots & \ddots & \ddots& \
& & -1 & 2 & -1 \
& & & -1 & 2
\end{pmatrix} + diag{V(x_1),\ldots,V(x_N)}$$
Solving the matrix eigenequation gives the approximate energy levels $E_n$ and eigenvector $\Phi_n$, which could be used to construct the eigen-wavefunctions via interpolation.
Can you write Python code for it?

def Laplacian_fd(N, Δ):
    '''
    The discretized Laplacian operator
    '''
    uu = np.diag(np.ones(N - 1), 1)
    dd = np.diag(-2 * np.ones(N))
    ll = np.diag(np.ones(N - 1), -1)
    return (1/Δ**2) * (uu + dd + ll)

def Hamiltonian_fd(V, a, b, N):
    '''
    The discretized Hamiltonian
    '''
    Δ = (b - a) / (N + 1)
    xx = np.linspace(a, b, N + 2)[1:-1]
    vv = np.array([V(x) for x in xx])
    return -Laplacian_fd(N, Δ) / 2 + np.diag(vv)


I am working on the file "Numerical Stability.ipynb".
The first task is:
(Optional) Differences between round and np.round
There are few differences between built-in Python 2.7 round function and numpy (a)round:

The built in function rounds away from zero
Numpy round rounds to even, which tends to skew the results less and is a commonly accepted rounding method
From my (limited) exerience it looks numpy round is much better behaved in dealing with decimal-to-binary float rounding errors

Note that Python 3 has a different round function that behaves more similarly to numpy round.
Can you write Python code for it?

import numpy as np
for i in range(13):
    x = -3 + 0.5 * i
    print '\t{0:5.1f}\t{1:5.1f}\t{2:5.1f}'.format(x, round(x), np.round(x))


I am working on the file "Numerical Stability.ipynb".
The first task is:
Absolute and relative error
Can you write Python code for it?

def absolute_error(true_value, approx_value):
    return abs(true_value - approx_value)

print 'Absolute error: {0:.9f}'.format(absolute_error(10.951, 10.949))

def relative_error(true_value, approx_value):
    return absolute_error(true_value, approx_value) / abs(true_value)

print 'Relative error: {0:.9f}'.format(relative_error(10.951, 10.949))


I am working on the file "Numerical Stability.ipynb".
The first task is:
Calculations without subtractions are fine, right?
Hint: No, we can still over/under-flow the floating point precision numbers.
Can you write Python code for it?

def naive_e(n):
    return (1 + 1.0 / n)**n

from math import exp
e = exp(1)

for i in range(5, 20):
    print naive_e(10**i) - e

from math import sqrt
def identity(x, n):
    for i in xrange(n):
        x = sqrt(x)
    for i in xrange(n):
        x = x**2
    return x
x = 2
for i in xrange(35, 60):
    print x - identity(x, i)


I am working on the file "Numerical Stability.ipynb".
The first task is:
Examples
Variance Calculation
There are two concepts that we refer to as variance:

A property of a distribution
A characteristic of a set of observations

Variance of a random variable X is defined as:
Var(X) = E[(X - E[X])^2] = ... = E[X^2] - (E[X])^2
Where E[Z] is the expected value of a random variable Z.
The second form (E[X^2] - (E[X])^2) should be avoided when performing calculations on a fixed precision machine. Although it has a nice property that for sample variance it is easy to implement it while traversing the data just once, naive implementations usually suffer from extreme cancellation.
Can you write Python code for it?

# Calculate the variance of X = sum of dice in N throws. The probabilities of each side of dice are given.
import numpy as np

# Dice sides probabilities, p[i] = probability of throwing i. We use fractions to be able to compare our calculations to
# 'exact' values:
from fractions import Fraction as F
from numpy.random import randint as ri
max = 1000000
pf = np.array([0] + [F(ri(max), 1) for _ in range(6)])
pf /= sum(pf)
p = np.array([float(f) for f in pf])

# Number of throws
N = 10000 # 30000

# Dynamic program, we're holding probability of throwing k-dice for each possibility.
# First iteration:
dp = np.ones(1)
for i in range(N):
    dp = np.convolve(dp, p)
print dp

# Let's calculate variance using both ways.

dice = np.arange(len(dp))

# var1 = E[(X - E[X])^2]
ex = (p * np.arange(7)).sum() * N
var1 = (dp * (dice - ex)**2).sum()
print 'Variance calculated using: E[(X - E[X])^2]:   ', var1

# var2 = E[X^2] - (E[X])^2
ex2 = (dice**2 * dp).sum()
var2 = ex2 - ex**2
print 'Variance calculated using: E[X^2] - (E[X])^2: ', var2

print 'E[X^2]:    ', ex2
print '(E[X2])^2: ', ex**2

# There is a simpler way to calculate variance in this particular problem, since all N throws are independent we can simply
# calculate variability of one and multiply it by N. To make sure this calculation is as precise as possible, we use Python
# representation of rational numbers.

# Let's use equation 1: E[(X - E[X])^2]
rangef = np.array([F(i, 1) for i in range(7)])
exf = (rangef * pf).sum()
ex2f = (rangef**2 * pf).sum()
varf = ex2f - exf**2
varf *= N
print 'Variance calculated fractions:                ', float(varf)

print 'Relative error of: E[(X - E[X])^2]:   ', abs((var1 - varf) / varf)
print 'Relative error of: E[X^2] - (E[X])^2: ', abs((var2 - varf) / varf)


I am working on the file "Numerical Stability.ipynb".
The first task is:
Fortunately, we can rewrite f(x) in a form that is less prone to cancellation:
Can you write Python code for it?

def g(x):
    return 0.5 * (2 * np.sin(x / 2) / x)**2
print 'Value of g near zero:', g(near_zero)
x = np.linspace(near_zero, 10, 100)
y = g(x)
import matplotlib.pyplot as plt
%matplotlib inline
plt.ylim([0, 1])
plt.plot(x, y)


I am working on the file "Numerical Stability.ipynb".
The first task is:
Loss of significance
Error in floating point arithmetic when an operation increases relative error substantially more than absolute error
Can you write Python code for it?

x_value = 0.123123
y_value = 0.123000
# We want to learn the value of d = x - y:
d_value = x_value - y_value
print 'Actual d value:',
# Assuming we're apprximating above calculation with a 4 decimal digits precision:
import numpy as np
d_approx = np.round(x_value, 4) - np.round(y_value, 4)
print 'Approx d value:', d_approx
print 'Absolute error: {0:.9f}'.format(abs(d_value - d_approx))
print 'Relative error: {0:.9f}'.format(abs((d_value - d_approx) / d_value))


I am working on the file "Numerical Stability.ipynb".
The first task is:
Measuring Accuracy
Backward and Forward Errors

Forward error - error of the computed value
Backward error:
Let y = f(x), given x we approximate f(x) with y’
Let dx be the smallest quantity where y’ = f(x + dx) in exact computation
Then dx is the backward error
Benefits of using backward error:
Unifies error w/ perturbation in the data
Removes the need to calculate forward error
Forward-backward error:
f(x + dx) = y + dy
Used to define stability of computation where just using backward error isn’t possible, e.g. sin, cos
If rounding errors are dominant source of errors, we call an algorithm numerically stable if it is stable in forward-backward error sense

Condition Number

Condition number of a function with respect to its arguments is used to measure how how much the output of the function will change for a small change in the input
As a rule of thumb, if the condition number kappa(A) = 10^k, then you may lose up to k digits of accuracy on top of what would be lost to the numerical method due to loss of precision from arithmetic methods
A problem with a low condition number is said to be well-conditioned, while a problem with a high condition number is said to be ill-conditioned
For example, the condition number associated with the linear equation Ax = b gives a bound on how inaccurate the solution x will be after approximation. Note that this is before the effects of round-off error are taken into account; conditioning is a property of the matrix, not the algorithm or floating point accuracy of the computer used to solve the corresponding system.

An important example of an ill-conditioned problem is finding roots of a polynomial. Let's look at Wilkinson’s polynomial as an example.
Can you write Python code for it?

# Wilkinson's polynomial is defined as p(x) = (x - 1)(x - 2)...(x - 20)
import numpy as np
x = np.linspace(0, 20, 4000)
y = 1
for i in range(1, 20):
    y *= (x - i)
import matplotlib.pyplot as plt
%matplotlib inline
plt.ylim([-4e11, 4e11])
plt.plot(x, y)


I am working on the file "Numerical Stability.ipynb".
The first task is:
Relative Error for Non-Scalars
For non-scalars, calculating normalized value ||x|| = max / sum, implies smaller components of x are bound by absolute error only. Consider compensative relative error: max(i) |xi - xi’| / |xi|, which puts all components on equal footing.
Can you write Python code for it?

import numpy as np

def relative_error(true_value, approx_value):
    return np.max(np.fabs((true_value - approx_value) / true_value))

x_value = np.array([10000, 0.01])
x_approx = np.array([9999, 0.02])

print relative_error(x_value, x_approx)


I am working on the file "Numerical Stability.ipynb".
The first task is:
Rounding errors are not random
Can you write Python code for it?

import numpy as np

def r(x):
    # Calculate value of a rational function using Horner's rule (https://en.wikipedia.org/wiki/Horner%27s_method)
    # More on the function can be looked up on Wolfram's Alpha:
    # http://www.wolframalpha.com/input/?i=f(x)+%3D+(622.0+-+x+*+(751.0+-+x+*+(324.0+-+x+*+(59.0+-+4+*+x))))+%2F+(112+-+x+*+(151+-+x+*+(72+-+x+*+(14+-+x))))&t=crmtb01
    p = 622.0 - x * (751.0 - x * (324.0 - x * (59.0 - 4 * x)))
    q = 112 - x * (151 - x * (72 - x * (14 - x)))
    return p / q

def calc_f(a):
    t = np.array([a + k * 2**-52 for k in xrange(400)])
    t = r(t)
    t -= t[0]
    t *= 1.0 / max(abs(t))
    return t

import matplotlib.pyplot as plt
%matplotlib inline

def plot(t):
    plt.plot(t, linestyle='--', marker='o')
    plt.show()

for a in [1.606, 4, 8, 16, 32]:
    plot(calc_f(a))


I am working on the file "Numerical Stability.v2.ipynb".
The first task is:
Calculations without subtractions are fine, right?Hint: No, we can still over/under-flow the floating point precision numbers.
Can you write Python code for it?

def naive_e(n):    return (1 + 1.0 / n)**nfrom math import expe = exp(1)for i in range(5, 20):    print naive_e(10**i) - e

from math import sqrtdef identity(x, n):    for i in xrange(n):        x = sqrt(x)    for i in xrange(n):        x = x**2    return xx = 2for i in xrange(35, 60):    print x - identity(x, i)


I am working on the file "Numerical Stability.v2.ipynb".
The first task is:
Fortunately, we can rewrite f(x) in a form that is less prone to cancellation:
Can you write Python code for it?

def g(x):    return 0.5 * (2 * np.sin(x / 2) / x)**2print 'Value of g near zero:', g(near_zero)x = np.linspace(near_zero, 10, 100)y = g(x)import matplotlib.pyplot as plt%matplotlib inlineplt.ylim([0, 1])plt.plot(x, y)


I am working on the file "Numerical Stability.v2.ipynb".
The first task is:
Loss of significanceError in floating point arithmetic when an operation increases relative error substantially more than absolute error
Can you write Python code for it?

x_value = 0.123123y_value = 0.123000# We want to learn the value of d = x - y:d_value = x_value - y_valueprint 'Actual d value:', # Assuming we're apprximating above calculation with a 4 decimal digits precision:import numpy as npd_approx = np.round(x_value, 4) - np.round(y_value, 4)print 'Approx d value:', d_approxprint 'Absolute error: {0:.9f}'.format(abs(d_value - d_approx))print 'Relative error: {0:.9f}'.format(abs((d_value - d_approx) / d_value))


I am working on the file "Numerical Stability.v2.ipynb".
The first task is:
Quoting Wikipedia: "If the coefficient of x19 is decreased from −210 by 2−23 to −210.0000001192, then the polynomial value w(20) decreases from 0 to −6.25×10^17, and the root at x = 20 grows to x ≈ 20.8. The roots at x = 18 and x = 19 collide into a double root at x ≈ 18.62 which turns into a pair of complex conjugate roots at x ≈ 19.5±1.9i as the perturbation increases further." *"Wilkinson's polynomial is often used to illustrate the undesirability of naively computing eigenvalues of a matrix by first calculating the coefficients of the matrix's characteristic polynomial* and then finding its roots, since using the coefficients as an intermediate step may introduce an extreme ill-conditioning even if the original problem was well conditioned"
Sources of Error## Truncation error (discretization error)Error coming from representing a function or continuous variable using finite number of evaluations - outside of scope of this notebook, mentioned for completeness only.## Round-off error Difference between calculated approximation and exact value due to rounding Related to representation error, which is due to representing numbers with finite number of digits
Can you write Python code for it?

import numpy as npx = np.float64(0.1)y = np.float32(0.1)print '32- vs 64-bit representation difference:', abs(x - y)


I am working on the file "Numerical Stability.v2.ipynb".
The first task is:
Rounding Multiple Times Can Accumulate Error
Can you write Python code for it?

import numpy as np# For explanation why we we use np.round rather than default Python 2.7.3 round function, see belowx = 9.945309 print np.round(x, 2), np.round(np.round(x, 2), 1)print np.round(x, 1)


I am working on the file "Numerical Stability.v2.ipynb".
The first task is:
Rounding errors are not random
Can you write Python code for it?

import numpy as npdef r(x):    # Calculate value of a rational function using Horner's rule (https://en.wikipedia.org/wiki/Horner%27s_method)    # More on the function can be looked up on Wolfram's Alpha:    # http://www.wolframalpha.com/input/?i=f(x)+%3D+(622.0+-+x+*+(751.0+-+x+*+(324.0+-+x+*+(59.0+-+4+*+x))))+%2F+(112+-+x+*+(151+-+x+*+(72+-+x+*+(14+-+x))))&t=crmtb01    p = 622.0 - x * (751.0 - x * (324.0 - x * (59.0 - 4 * x)))    q = 112 - x * (151 - x * (72 - x * (14 - x)))    return p / qdef calc_f(a):    t = np.array([a + k * 2**-52 for k in xrange(400)])    t = r(t)    t -= t[0]    t *= 1.0 / max(abs(t))    return timport matplotlib.pyplot as plt%matplotlib inlinedef plot(t):    plt.plot(t, linestyle='--', marker='o')    plt.show()    for a in [1.606, 4, 8, 16, 32]:    plot(calc_f(a))


I am working on the file "Numerical Stability.v2.ipynb".
The first task is:
The last definition avoids issues that the previous two had, but it's still non-ideal
Can you write Python code for it?

# Definition 3: Numbers x and x' match to p-sig. digits if x - x' < half a unit in p-th sig. digit of xx1 = 0.123x2 = 0.127# 0.004 < (0.01 / 2) => x1 and x2 match in 2 significant digits according to this definition wchich may be slightly confusing


I am working on the file "Numerical-Experimentation/Series of N equals in coin tosses.ipynb".
The first task is:
As we will see later the non-integer nature of this expectation value is a residual of the numerical procedure we employed and it could easily be cast to an integer.
Before we get into the mathematical formulation of the problem, let's study the distribution a little bit more. A good way to gain some insight into the distribution is to look at the normalized histogram of the number of total throws until success. To this end we take a look at the raw tosses returned by runKExperimentsAndEvaluate
Can you write Python code for it?

tosses_three_equals = runKExperimentsAndEvaluate(25000, 3, number_of_maximum_tosses=1000)[2]
tosses_four_equals = runKExperimentsAndEvaluate(25000, 4, number_of_maximum_tosses=1000)[2]
tosses_five_equals = runKExperimentsAndEvaluate(25000, 5, number_of_maximum_tosses=1000)[2]

bin_range = range(0,150, 2)
plt.hist(tosses_three_equals, bins=bin_range, normed=True)
plt.hist(tosses_four_equals, bins=bin_range, normed=True)
plt.hist(tosses_five_equals, bins=bin_range, normed=True)
plt.xlim([0,100])


I am working on the file "Numerical-Experimentation/Series of N equals in coin tosses.ipynb".
The first task is:
Finally we want to run M experiments and evaluate for the expected number of throws.
Can you write Python code for it?

def runKExperimentsAndEvaluate(m_experiments, number_of_equals, number_of_maximum_tosses=500, prob = [0.5,0.5]):
    number_of_tosses = []
    for idx in range(m_experiments):
        number_of_tosses.append(runSingleExperiment(number_of_maximum_tosses, number_of_equals, prob))

    return np.mean(number_of_tosses), np.std(number_of_tosses), number_of_tosses


I am working on the file "Numerical-Experimentation/Series of N equals in coin tosses.ipynb".
The first task is:
From the plot aboe we see that the number of tosses until we have $N$ equals grows exponential! (Observe the logarithmic scale). For $N=20$ heads in a row we need on the order of 2 million successive throws for a fair coin. If we could manually throw a coin every second, it would take us about 23 days of uniterupted coin tossing until we could expect 20 heads in a row to appear... but remember: The distribution has a heavy tail and the variance is also on the order of 23 days!
Summary
We did some numerical experiments to investigate the question how many times we need to throw a coin before we can expect $N$ heads in a row. The distribution of this problem is surprisingly heavy tailed and becomes heavier the more heads are required. We saw that the variance is as large as the expectation value, which grows exponential with the number of required equals as we proved mathematically.
Enjoy this notebook! Contributions and/or feedback are welcome as always!
Stylesheet
Can you write Python code for it?

from IPython.core.display import HTML

def css_styling():
    styles = open("../styles/custom.css", "r").read()
    return HTML(styles)
css_styling()


I am working on the file "Numerical-Experimentation/Series of N equals in coin tosses.ipynb".
The first task is:
Maybe surprisingly the distribution is not very well localized. In fact trying to fit it with an exponential function given the calculated mean fails. Increasing the number of required equals makes the curve flatter and more heavy tailed. Thus the variance itself is also large. In fact it is of the same order as the mean!
Mathematical calculation of the expectation value
Let us now turn towards the mathematical calculation of the expectation value. Note that we define success as the event $X$ where we have $n$ heads in a row. It is then easy to see that in whenever we observe tails we essentially waisted all tries before and need to start over again. However, we need to keep track of the wasted throws. If $p$ is the probability of having the coin land on head then the expectation value can be expressed simply by
\begin{align}
E(X) = np^n + (1-p) \left[E(X) +1\right] + p(1-p) \left[E(X) +2\right] +  p^2(1-p) \left[E(X) +3\right]...  = np^n + (1-p)\sum\limits_{i=0}^{n-1}p^i\left[E(X)+i+1\right)
\end{align}
Evaluating the sum we find the simple expression:
\begin{align}
E(X) = \frac{1-p^n}{p^n(1-p)}
\end{align}
To gain some understanding why above formula works let us look closer at the case for two eqaual tosses. In this case we have success if $X=HH$. To calculate the probability of this event happening we condition on the first toss, i.e. we calculate
\begin{align}
E(X) = E(X|H) p(H) + E(X|T) p(T)
\end{align}
where the condition is the result of the first toss. Expanding this some more we get
\begin{align}
E(X) = p^2 + (1-p) \left[E(X) + 1\right]
\end{align}
where we employed the same argument as above.
Can you write Python code for it?

def expectationValueForNumberOfTosses(p, number_of_equals):
    return int(float(1 - np.power(p, number_of_equals))/float(np.power(p, number_of_equals) * (1-p)))

equals = np.linspace(1,20, 20)
y = []
for x in equals:
    y.append(expectationValueForNumberOfTosses(0.5, x))

plt.semilogy( equals, y, 'o')


I am working on the file "Numerical-Experimentation/Series of N equals in coin tosses.ipynb".
The first task is:
Next we need to check if the last N throws have been equal to the category we want to observe. To do this we construct a set of the last N tosses. If the size of the set is 1 and the category in the set is the one we are looking for we found a sequence of N equal tosses.
Can you write Python code for it?

def areLastNTossesEqualy(tosses, N, category):
    subset = set(tosses[-N:])
    if ((len(subset) == 1) and (category in subset)):
        return True
    else:
        return False


I am working on the file "Numerical-Experimentation/Series of N equals in coin tosses.ipynb".
The first task is:
So for 3 heads in a row, what's the expected number of tosses to observe this event"
Can you write Python code for it?

print "We expect to observe 3 heads after %3.2f tosses" % runKExperimentsAndEvaluate(5000, 3)[0]


I am working on the file "Numerical-Experimentation/The Monty Hall Problem.ipynb".
The first task is:
As a result of these choices, Monty opens another door
Can you write Python code for it?

def montyOpensDoor(contestantChoice, carBehindDoor, pref_monty = [0.5, 0.5]):
    list_of_doors = ["A", "B", "C"]

    # Monty will not open the car door, so we remove it
    list_of_doors.remove(carBehindDoor)

    # If the contestant chooses the door which contains the car,
    # Monty can choose which door to open
    if contestantChoice is carBehindDoor:
        return str(choice(list_of_doors, p = pref_monty))

    # If the car and contestant door do not coincide,
    # Monty has only a single choice
    elif contestantChoice is list_of_doors[0]:
        return list_of_doors[1]
    else:
        return list_of_doors[0]


I am working on the file "Numerical-Experimentation/The Monty Hall Problem.ipynb".
The first task is:
Let's first define some helper function for chosing the doors behind which the car lives and which the contestant chooses
Can you write Python code for it?

def carBehindDoor(prob = [1./3, 1./3, 1./3]):
    list_of_doors = ["A", "B", "C"]
    return str(choice(list_of_doors, p = prob))

def contestantChoosesRandomDoor(prob = [1./3, 1./3, 1./3]):
    list_of_doors = ["A", "B", "C"]
    return str(choice(list_of_doors, p = prob))


I am working on the file "Numerical-Experimentation/The Monty Hall Problem.ipynb".
The first task is:
Let's string together the experiment
Can you write Python code for it?

def runExperiment(n_trials):
    success = 0
    for k in range(n_trials):
        car_behind_door = carBehindDoor()
        contestant_door = contestantChoosesRandomDoor()
        monty_door = montyOpensDoor(contestant_door, car_behind_door)
        if successUponSwitch(car_behind_door, contestant_door, monty_door):
            success += 1
    return float(success)/float(n_trials)


I am working on the file "Numerical-Experimentation/The Monty Hall Problem.ipynb".
The first task is:
Note that in the return argument we returned the ratio between the counts and not the probabilities. This is correct since we get the probabilities by normalizing with the number of trials. However, this normalization drops out when calculating the ratio.
What do we get?
Can you write Python code for it?

p_monty = .75
print "We succeed with a probability: %0.3f" % runConditionalExperiment(10000, 'B', p_monty)


I am working on the file "Numerical-Experimentation/The Monty Hall Problem.ipynb".
The first task is:
Surprise! We do get some additional advantage if we ask the question what our success rate is given Monty opens a specific door. How does this probability depend on his preferences?
Can you write Python code for it?

p_monty = np.linspace(0, 1, 100)
cond_probabilities = []
for prob in p_monty:
    cond_probabilities.append(runConditionalExperiment(10000, 'B', prob))

plt.plot(p_monty, cond_probabilities)


I am working on the file "Numerical-Experimentation/The Monty Hall Problem.ipynb".
The first task is:
This is quite surprising! If Monty never opens door $2$ we have no advantage and win the car only half of the time. However, if Monty opens door $2$ always, we are guaranteed to win the car every single time!
To understand this in more formal terms we make use of Bayes rule and find
\begin{align}
P(C_3 | M_2) \,=\, \frac{P(M_2 | C_3) P(C_3)}{P(M_2)}
\end{align}
Let's look at the different probabilities individually:

$P(M_2 | C_3)=1$, since Monty is forced to open door $2$ in this constellation (the contestant has to choose door $1$ or the strategy fails)
$P(C_j) = 1/3$, since the contestant has no prior knowledge of where the car is
$P(M_2)$ is slightly more complicated. To simplify this we expand this into disjoint cases using the law of total probability. We then have (given the contestant chooses door $1$):
\begin{align}
P(M_2) = P(M_2|C_1) P(C_1) + P(M_2|C_2) P(C_2) + P(M_2|C_3) P(C_3)
 = (1-p) \cdot \frac{1}{3} + 0 + 1\cdot \frac{1}{3}
\end{align}

Putting this all together we find
\begin{align}
P(C_3 | M_2) \,=\, \frac{1}{2-p}
\end{align}
which is exactly what we found in the graph above! Note that in the symmetric case $p=0.5$ we obtain the result of total probability, meaning that at this point the events are independent.
Summary
The Monty Hall problem has several surprising and non-intuitive twists. First of all, we can come up with a dominant stragegy that always beats Monty, by simply switching the doors independent of Monty's choice. This is surprising as naively one would think the chances are equal. Second, when conditioning on Monty's behavior, we do see a dependence of our success strategy on Monty's preference of doors. If we stick with our original choice always, then Monty can come up with a strategy that will equalize our chances.
Enjoy this notebook and have a lot of fun playing some more with it, such as exploring different amount of doors or a different strategy. Contributions are more than welcome!
Miscellenea
References
are given throughout the text
Stylesheet
Can you write Python code for it?


I am working on the file "Numerical-Experimentation/The Two Envelope Paradox.ipynb".
The first task is:
Implementing the actual strategy
The strategy contains of four steps:

create two envelopes containing iid amount of money
choose one of them at random
check for the money and switch if it $X<T$ where $T$ is draw from a different distribution
evaluate if the strategy was successful
Can you write Python code for it?

def singleExperiment(money_distribution, threshold_distribution):

    # create two identical envelopes with a random amount of money
    envelope_contents = {'a': money_distribution.random().item(),
                         'b': money_distribution.random().item()}

    #choose an envelope
    chosen_envelope = choice(['a','b'])

    #check for the amount and switch if necessary
    if (threshold_distribution.random().item() >= envelope_contents[chosen_envelope]):
        chosen_envelope = switch_envelope(chosen_envelope)

    #evaluate whether strategy was a success
    return isLargerAmount(chosen_envelope, envelope_contents)


I am working on the file "Numerical-Experimentation/The Two Envelope Paradox.ipynb".
The first task is:
It seems strategy 2 from above is correct! The probability is significantly higher than $0.5$ and switching seems like a good choice. How is that possible?
Resolution of the paradox
Looking back at strategy 2, we silently dropped the conditions when evaluating the expectation values. In fact if we let $I$ be an indicator random variable of the event $Y=2X$, then we have
\begin{align}
E( Y | Y = 2X) = E(2X | I = 1) \neq E(2X)
\end{align}
There is no reason we can drop the condition $I=1$ without ensuring that these events are independent. In fact, the above simulation shows, that they are not independent. This leads us to the surprising result that observing the result of one envelope gives us information about the amount of money in the second envelope! It also shows that a naive assumption of symmetry can lead to false intuition. The symmetry argument of strategy 1 secretly assumes independence of the observations, which is clearly violated.
Mathematical resolution of the paradox
To base the resolution is a little more formal description, we denote the observed amount of money of as $x_a$ and $x_b$ and can safely assume $x_a<x_b$. We switch the envelopes in the event $T>x_a$. Let us denote the success of the strategy as event $S$ and choosing envelope $a$ as event $A$. Assuming $T\sim Expo(1)$, we have $P(S|A) = P(T>x_a) = 1-(1-e^{-x_a}) = e^{-x_a}$ and correspondingly $P(S|A^c) = P(T<x_b) = 1- e^{-x_b}$. Adding the events correctly we obtain
\begin{align}
\frac{1}{2} P(S|A) + \frac{1}{2} P(S|A^c) = \frac{1}{2} +  \frac{1}{2} \left( e^{-x_a} - e^{-x_b}\right) > \frac{1}{2}
\end{align}
Dependency on the choice of the threshold
The above example was very specific on the choice of our threshold. However, it is now easy to investigate how our success rate depends on the rate parameter $\lambda$ of our distribution $T\sim Expo(\lambda)$ or on the choice of the money amounts in the envelopes.
In this section we use the global money distribution
Can you write Python code for it?

money = pm.DiscreteUniform('money', 100, 100000)


I am working on the file "Numerical-Experimentation/The Two Envelope Paradox.ipynb".
The first task is:
Let us run the experiment $n_{trials}$ times. Since we return values $1$ and $0$ for success and failure, respectively, we can find a good approximation of the success probability by computing the mean.
Can you write Python code for it?

def run_n_experiments(n_trials, money, threshold):
    lst = []
    for idx in range(n_trials):
        lst.append(singleExperiment(money, threshold))
    return np.mean(lst)

print 'The success probability is approximately p = %0.3f' % run_n_experiments(100, money, threshold)


I am working on the file "Numerical-Experimentation/The Two Envelope Paradox.ipynb".
The first task is:
Our advantage is gone! The fact that we do know something about the money distribution mysteriously decorrelates the envelopes and renders our strategy obsolete. We might as well stick with any envelope independent of the amount of money we find in it. However, this might not be a bad trade, since we now have a rough idea of what is in either envelope before we even open it.
Summary
The two-envelope paradox is quite surprising. In a first hunch we would assume that the situation is quite symmetric and that there is no chance we could potentially gain knowledge from opening an envelope. However, it turns out, that we can devise a strategy that allows us to make an optimal choice whether to switch envelopes or not. The two envelopes are correlated despite what meets the eye! However, our optimal choice hinges on the range and type of distribution. We only gain an advantage if we have zero prior knowledge of the money distribution, i.e. for a uniform. If we gain knowledge in the form of the money distribution, as is the case for the normal, our strategy is rendered useless. This might not be too bad as it probably eases our minds by making the choice easier.
I hope you enjoy this notebook and have a lot of fun playing some more with it, such as exploring different distributions and maybe strategies. Contributions are more than welcome!
Miscellenea
References
Stylesheets and idea from
- [1] Probabilistic Programming and Bayesian Methods for Hackers
Stylesheet
Can you write Python code for it?


I am working on the file "Numerical-Experimentation/The Two Envelope Paradox.ipynb".
The first task is:
Performing the experiment
To keep it simple we draw the monetary values from a uniform distribution, whereas our random threshold is exponentially distributed.
Can you write Python code for it?

money = pm.DiscreteUniform('money', 100, 100000)
threshold = pm.Exponential("threshold", 0.00005)


I am working on the file "Numerical-Experimentation/The Two Envelope Paradox.ipynb".
The first task is:
We also need some helper methods to evaluate the success of our strategy
Can you write Python code for it?

'''Find out if the final envelope _actually_ contains the highest value'''
def isLargerAmount(chosen_envelope, envelope_contents):
    inverted_contents = create_inverted_envelope(envelope_contents)
    if chosen_envelope == inverted_contents.get(np.max(inverted_contents.keys())):
        return 1 # success
    else:
        return 0 # failure

'''We need an inverse lookup table to associate the highest value with an envelope'''
def create_inverted_envelope(envelope_contents):
    dct = {}
    for key in envelope_contents.keys():
        dct[envelope_contents.get(key)] = key
    return dct


I am working on the file "Numerical-Experimentation/The Two Envelope Paradox.ipynb".
The first task is:
We can clearly see that with increasing the spread of the distribution the maximum is moving towars smaller rates. In fact you can roughly say that the maximum occurs at at a rate close to the inverse of the upper boundary. This makes sense since the expectation value $E(T) = 1/\lambda$ and thus an optimal threshold choice is given by the inverse of the upper limit of the money distribution.
Different distributions
The previous choices of the money distribution have been motivated by the idea that we have no knowledge about the value of the money in the envelope at all. This means that every value is equally likely and the natural choice is a uniform distribution.
However, what happens if we do gain some insight to the distribution? A good assumption is that the person handing out the money will do so according to a normal distribution $X\sim N(\mu, \sigma^2)$. Let's see what happens:
Can you write Python code for it?

money_1 = pm.Normal('money', np.power(10,5), 100)
money_2 = pm.Normal('money', np.power(10,5), 500)
money_3 = pm.Normal('money', np.power(10,5), 10000)

array_of_rates = np.logspace(-8, 0, num=100)
plt.semilogx(array_of_rates, createSuccessValuesWithMoneyRangeFrom(array_of_rates, money_1),
             array_of_rates, createSuccessValuesWithMoneyRangeFrom(array_of_rates, money_2),
             array_of_rates, createSuccessValuesWithMoneyRangeFrom(array_of_rates, money_3))


I am working on the file "Numerical-Experimentation/t-SNE and the KL Divergence.ipynb".
The first task is:
Histogram of the Bernoulli variable
... looks pretty normal, I would say
Can you write Python code for it?

from scipy.stats import norm

fig = plt.figure(figsize=(10, 6.5))
data = perform_bernoulli_experiment(0.02, 10000, 10000)
hist = plt.hist(data , bins=99, normed= True, label = 'n-fold Bernoulli distribution');
plt.xlabel('number of successes', fontsize = 20);
plt.ylabel('number of occurences', fontsize = 20);
plt.tick_params(axis='both', which='major', labelsize=16);
plt.tick_params(axis='both', which='minor', labelsize=12);

mu, std = norm.fit(data)

x = np.linspace(min(data), max(data), 100)
p = norm.pdf(x, mu, std)
plt.plot(x, p, '--k', linewidth=2, label='Gaussian fit')

plt.xlim([min(data), max(data)])
plt.legend(loc='best', fontsize=16)


I am working on the file "Numerical-Experimentation/t-SNE and the KL Divergence.ipynb".
The first task is:
KL curve with uncertainty and mean
Can you write Python code for it?

p = 0.02
n_bern = 10000
sample_sizes = [100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, 50000]

lst_kl_values = []
for idx in range(10):
    kl_values = []
    for n_samples in sample_sizes:
        kl_values.append(calculate_full_kullback_leibler(p, n_samples, n_bern))
    lst_kl_values.append(kl_values)
kl_array = np.asarray(lst_kl_values)

fig = plt.figure(figsize=(10, 6.5))
p1 = plt.loglog(sample_sizes, kl_array.mean(axis=0), '--o', markersize=10, linewidth = 3, label = 'mean');

p2 = plt.fill_between(sample_sizes,
                 kl_array.mean(axis=0) + 3* kl_array.std(axis=0),
                 kl_array.mean(axis=0) - 3* kl_array.std(axis=0),
                 color='green',
                 alpha='0.35',
                 label= 'sigma range')
plt.xlabel('number of realizations', fontsize = 20);
plt.ylabel('Kullback-Leibler divergence', fontsize = 20);
plt.xlim([min(sample_sizes), max(sample_sizes)])
plt.tick_params(axis='both', which='major', labelsize=16);
plt.tick_params(axis='both', which='minor', labelsize=12);


I am working on the file "Numpy Tutorial.ipynb".
The first task is:
Create an np array. You can pass any type of python seq: list, tuples, etc
Can you write Python code for it?


I am working on the file "Numpy Tutorial.ipynb".
The first task is:
Do Some Vector Math Not for Loop Math
Can you write Python code for it?


I am working on the file "Numpy Tutorial.ipynb".
The first task is:
Line magic uses 1 '%'
Example Using Functional Programming
Remove class definition
Can you write Python code for it?

def random_walk_f(n):
    position = 0
    walk = [position]
    for i in range(n):
        position = 2 * random.randint(0,1) - 1
        walk.append(position)
    return walk

%%%timeit
walk = random_walk_f(10000)


I am working on the file "Numpy Tutorial.ipynb".
The first task is:
Multidimensional array using list of lists
Can you write Python code for it?

m = np.array([[1,2,3], [4,5,6]])

m.shape

ad = a.data
list(ad)

# what type is a
type(a)

# what is the numerica type of the elements in the array
a.dtype

# What shape (dimensions) is the array
a.shape

# Bytes per element. 32bit integers should be 4 bytes
a.itemsize

# Total size in bytes of the array
a.nbytes

# Beware of type coercion
# a holds dtypes int32
print(a)
a[0] = 10.38383
print(a)

x = np.array([0,1,1.5,3])
y = np.array([1,2,3,1])


I am working on the file "Numpy Tutorial.ipynb".
The first task is:
Statistical Analysis
Can you write Python code for it?

data_set = random.random((2,3))
print(data_set)

# example of namespace....cant access np.max and builtin max is being used

max(data_set[0])


I am working on the file "Numpy Tutorial.ipynb".
The first task is:
small improvement in time
Vectorized Approach Like When You Did Things in MATLAB :(
Get rid of the loop
Can you write Python code for it?

from itertools import accumulate
def random_walker_v(n):
    steps = random.sample([1, -1] * n, n)
    return list(accumulate(steps))

%%%timeit
walk = random_walker_v(10000)


I am working on the file "Numpy e Pandas/Modulo 2 - Introdução à Lib Pandas.ipynb".
The first task is:
* Atribuição de Valores em DataFrames
Can you write Python code for it?

""" Atribuir Valores em 'slices' levanta warnings """
df["Unidade"][[0, 2]] = "Pacote"
df

""" Deve-se usar 'loc' ou 'iloc' """
df.loc["Carne", "Unidade"] = "Kilograma"
df.iloc[3, 1] = "Litro"
df


I am working on the file "Numpy e Pandas/Modulo 2 - Introdução à Lib Pandas.ipynb".
The first task is:
* Cálculo do Preço Total por Item
Can you write Python code for it?

df["Preço Total"] = df["Preço Unitário"] * df["Quantidade"]
df


I am working on the file "Numpy e Pandas/Modulo 2 - Introdução à Lib Pandas.ipynb".
The first task is:
* Cálculo do Preço por Kg
Can you write Python code for it?

df["Preço Médio Por Kg"] = np.nan
df

mask = df["Unidade"] == "Kilograma"
df[mask]

df.loc[mask, "Preço Médio Por Kg"] = (df.loc[mask, "Preço Unitário"] / df.loc[mask, "Quantidade"]).sum()
df


I am working on the file "Numpy e Pandas/Modulo 2 - Introdução à Lib Pandas.ipynb".
The first task is:
* DataFame
Can you write Python code for it?

""" atribuição de valores em uma cópia """
df_tmp = df[["Preço p/ Ud", "Preço Total"]].copy()
df_tmp

""" mask """
mask = (df_tmp > 5) & (df_tmp < 10)
mask

df_tmp[mask]

tmp2 = df_tmp.copy()
tmp2[mask] = "?"
tmp2

s_tmp[s_tmp == 2] = 3
s_tmp


I am working on the file "Numpy e Pandas/Modulo 2 - Introdução à Lib Pandas.ipynb".
The first task is:
* Desconto de 10% no Preço Unitário
Can you write Python code for it?


I am working on the file "Numpy e Pandas/Modulo 2 - Introdução à Lib Pandas.ipynb".
The first task is:
* Média: apenas valores numéricos
Can you write Python code for it?


I am working on the file "Numpy e Pandas/Modulo 2 - Introdução à Lib Pandas.ipynb".
The first task is:
* Preenchendo NaNs
Can you write Python code for it?


I am working on the file "Numpy e Pandas/Modulo 2 - Introdução à Lib Pandas.ipynb".
The first task is:
* Soma : apenas valores numéricos
Can you write Python code for it?


I am working on the file "Numpy e Pandas/Modulo 2 - Introdução à Lib Pandas.ipynb".
The first task is:
Acessando valores
Definição das Variáveis
Can you write Python code for it?

index = pd.Index(data=["Leite", "Ovos", "Carne", "Arroz", "Feijão"], name="Itens")
index

sq = pd.Series(index=index,  data=[2, 12, 1, 5, 2]).sort_values()
sq

su = pd.Series(index=index,  data=["L", "Ud", "Kg", "Kg", "Kg"]).sort_index()
su

df = pd.DataFrame({"Quantidade": sq, "Unidade": su}).sort_values(by="Unidade")
df

df["Preço p/ Ud"] = [5.00, 29.99, 6.50, 3.30, 0.50]
df["Preço Total"] = [25.00, 29.99, 13.00, 6.60, 6.00]
df


I am working on the file "Numpy e Pandas/Modulo 2 - Introdução à Lib Pandas.ipynb".
The first task is:
Construção
Series
Can you write Python code for it?

""" Construtor padrão """
pd.Series(
    name="Compras",
    index=["Leite", "Ovos", "Carne", "Arroz", "Feijão"],
    data=[2, 12, 1, 5, 2]
)

""" Construtor padrão: dados desconhecidos """
pd.Series(
    name="Compras",
    index=["Leite", "Ovos", "Carne", "Arroz", "Feijão"]
)

""" Construtor padrão: valor padrão """
pd.Series(
    name="Compras",
    index=["Leite", "Ovos", "Carne", "Arroz", "Feijão"],
    data="fill here"
)

""" Recebendo um Dicionário"""
s = pd.Series({"Leite": 2, "Ovos": 12, "Carne": 1, "Arroz": 5, "Feijão": 2})
s.name = "Compras"
s

""" Recebendo uma Lista"""
s = pd.Series([2, 12, 1, 5, 2])
s

""" editando parâmetros"""
s.name="Compras"
s.index=["Leite", "Ovos", "Carne", "Arroz", "Feijão"]
s

""" Ordenação: Índices """
s.sort_index()

""" Ordenação: Dados """
s.sort_values(ascending=False)


I am working on the file "Numpy e Pandas/Modulo 2 - Introdução à Lib Pandas.ipynb".
The first task is:
DataFrame
Can you write Python code for it?

df

df["Unidade"]

df.Quantidade

""" Uma Coluna do DataFrame é uma Series"""
df["Preço Total"][2]

""" Acesso a mais de uma Coluna """
df[["Preço Total", "Quantidade"]]

""" acesso às Linhas: método 'loc' """
df.loc["Leite"]

""" acesso ao item: método 'loc' """
df.loc["Ovos", "Preço Total"]

""" acesso ao item: método 'iloc' """
df.iloc[4, 3]

""" acesso por slice: método 'loc' """
df.loc["Leite":, "Preço p/ Ud":]

""" acesso por slice: método 'iloc' """
df.iloc[3:, 2:]


""" Construtor padrão """
pd.DataFrame(
    index=["Leite", "Ovos", "Carne", "Arroz", "Feijão"],
    columns=["quantidade", "unidade"],
    data=[
        [ 2,  "L"],
        [12, "Ud"],
        [ 1, "Kg"],
        [ 5, "Kg"],
        [ 2, "Kg"]
    ]
)

""" Construtor padrão: dados desconhecidos """
pd.DataFrame(
    index=["Leite", "Ovos", "Carne", "Arroz", "Feijão"],
    columns=["quantidade", "unidade"]
)

""" Construtor padrão: valor padrão """
pd.DataFrame(
    index=["Leite", "Ovos", "Carne", "Arroz", "Feijão"],
    columns=["quantidade", "unidade"],
    data="?"
)

""" Recebendo um Dicionário"""
pd.DataFrame(
    {
        "quantidade": {
            "Leite": 2,
            "Ovos": 12,
            "Carne": 1,
            "Arroz": 5,
            "Feijão": 2
        },
        "unidade": {
            "Leite": "L",
            "Ovos": "Ud",
            "Carne": "Kg",
            "Arroz": "Kg",
            "Feijão": "Kg"
        }
    }
)

""" Recebendo um Dicionário de Series"""
index = ["Leite", "Ovos", "Carne", "Arroz", "Feijão"]
pd.DataFrame(
    {
        "quantidade": pd.Series(index=index,  data=[2, 12, 1, 5, 2]),
        "unidade": pd.Series(index=index,  data=["L", "Ud", "Kg", "Kg", "Kg"])
    }
)

""" Recebendo um vetor de Series"""
index = ["Leite", "Ovos", "Carne", "Arroz", "Feijão"]
df = pd.DataFrame(
    [
        pd.Series(name="quantidade", index=index,  data=[2, 12, 1, 5, 2]),
        pd.Series(name="unidade", index=index,  data=["L", "Ud", "Kg", "Kg", "Kg"])
    ]
)
df

""" Transpondo para ajustar a Tabela """
df = df.T
df

""" editando parâmetros"""
df.index = ["Leite tipo A", "Ovos Orgânicos", "Patinho", "Arroz Arbóreo", "Feijão Preto"]
df.columns = ["Quantidade", "Unidade"]
df

""" Ordenação: Índices """
df.sort_index()

""" Ordenação: Dados """
df.sort_values(by="Unidade", ascending=False)


I am working on the file "Numpy e Pandas/Modulo 2 - Introdução à Lib Pandas.ipynb".
The first task is:
Dataset Codificado:
Can you write Python code for it?


I am working on the file "Numpy e Pandas/Modulo 2 - Introdução à Lib Pandas.ipynb".
The first task is:
Descrição dos dados
Can you write Python code for it?

""" descrevendo as distribuições dos dados """
df.describe()

""" mesma coisa, manipulando os percentis  """
df.describe(percentiles=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])

""" Verificando os valores únicos de C3 """
df.c3.unique()

""" Verificando a frequencia dos valores únicos de C3 """
df.c3.value_counts()


I am working on the file "Numpy e Pandas/Modulo 2 - Introdução à Lib Pandas.ipynb".
The first task is:
Manipulações Numéricas
* Incrementando o Preço Unitário
Can you write Python code for it?


I am working on the file "Numpy e Pandas/Modulo 2 - Introdução à Lib Pandas.ipynb".
The first task is:
Masks
Conceito
Can you write Python code for it?

""" mask => array de bool """
sq > 2

""" mask => array de bool """
df > 2


I am working on the file "Numpy e Pandas/Modulo 2 - Introdução à Lib Pandas.ipynb".
The first task is:
O Acumulador
O acumulador é um DataFrame usado para acumular as transformações feitas em cima do dataset original. Cada transformação será armazenada em uma coluna cujo nome descreve a transformação feita sobre os dados.
Insira o dataset criado na coluna de nome original.
Can you write Python code for it?

accum = pd.DataFrame(
    index=range(2600),
    columns=["original"],
    data=serie
)
accum.head().append(accum.tail())


I am working on the file "Numpy e Pandas/Modulo 2 - Introdução à Lib Pandas.ipynb".
The first task is:
[ B ]  Elementos de outra distribuição

N = 100
média = 400
sigma = 100
coluna = "outliers_adicionados"
Can you write Python code for it?


I am working on the file "Numpy e Pandas/Modulo 2 - Introdução à Lib Pandas.ipynb".
The first task is:
[ C ]  Elementos Próximos à média

N = 1000
média = 150
sigma = 0.1
coluna = "elementos_prox_a_media"
Can you write Python code for it?


I am working on the file "Numpy e Pandas/Modulo 3 - Leitura e Escrita em Arquivos + Combinando Tabelas.ipynb".
The first task is:
Arquivo de Resposta
Can you write Python code for it?

output_file = os.path.join(path, "dataset.csv")


I am working on the file "Numpy e Pandas/Modulo 3 - Leitura e Escrita em Arquivos + Combinando Tabelas.ipynb".
The first task is:
Construção do Dataset

Junte todos os DataFrames usando os métodos apropriados
O dataset final deve conter apenas as seguintes colunas:
order_id
user_id
order_dow
department
aisle
product_name
Can you write Python code for it?


I am working on the file "Numpy e Pandas/Modulo 3 - Leitura e Escrita em Arquivos + Combinando Tabelas.ipynb".
The first task is:
Construção do Dataset
Referência:
* The Instacart Online Grocery Shopping Dataset 2017”, Accessed from https://www.instacart.com/datasets/grocery-shopping-2017 on October 20, 2017.
* Descrição dos Campos: <a href="https://gist.github.com/jeremystan/c3b39d947d9b88b3ccff3147dbcf6c6b" target=_blank> https://gist.github.com/jeremystan/c3b39d947d9b88b3ccff3147dbcf6c6b </a>
Leitura dos Arquivos

sep = ',' para todos os arquivos
o nome da variável deverá ser o mesmo do dicionário. Exemplo:
aisles = pd.read_csv(input_file["aisles"])
carregar do aqrquivo apenas as colunas especificadas em cada item

orders

colunas:
order_id
user_id
order_dow

sep: ','
Can you write Python code for it?


I am working on the file "Numpy e Pandas/Modulo 3 - Leitura e Escrita em Arquivos + Combinando Tabelas.ipynb".
The first task is:
Dataset orders.csv
Can you write Python code for it?


I am working on the file "Numpy e Pandas/Modulo 3 - Leitura e Escrita em Arquivos + Combinando Tabelas.ipynb".
The first task is:
Dataset product.csv
Can you write Python code for it?

""" nome do arquivo """
file_name = os.path.join("data", "tutorial", "products.csv")

""" criação do DataFrame """
df = pd.DataFrame(
    columns=["product_id", "product_name"],
    data=[
        [1, "Leite"],
        [2, "Ovos"],
        [3, "Arroz"],
        [4, "Feijão"],
        [5, "Carne"],
        [6, "Frango"],
        [7, "Peixe"]
    ]
).set_index("product_id")  # set_index: usa a coluna especificada como índice
df

""" salvando o DataFrame """
df.to_csv(
    file_name,          # nome do arquivo
    sep=";",            # separador ;
    encoding="utf-8"    # encoding: garante formato correto de strings no arquivo
)


I am working on the file "Numpy e Pandas/Modulo 3 - Leitura e Escrita em Arquivos + Combinando Tabelas.ipynb".
The first task is:
Dataset stores.csv
Can you write Python code for it?

""" nome do arquivo """
file_name = os.path.join("data", "tutorial", "stores.csv")

""" criação do DataFrame """
df = pd.DataFrame(
    columns=["store_id", "store_name"],
    data=[
        [1, "Pão de Açúcar"],
        [2, "Dia"],
        [3, "Extra"],
    ]
).set_index("store_id")
df

""" salvando o DataFrame """
df.to_csv(
    file_name,          # nome do arquivo
    sep="\t",           # separador TAB
    encoding="utf-8"    # encoding: garante formato correto de strings no arquivo
)


I am working on the file "Numpy e Pandas/Modulo 3 - Leitura e Escrita em Arquivos + Combinando Tabelas.ipynb".
The first task is:
Desafio
Objetivo:
Construir um dataset a partir de tabelas de diferentes formatos contidas em arquivos separados.
Solução
Arquivos das Tabelas
Download dos Arquivos

Link: <a href="https://drive.google.com/open?id=0BzHr4OwCpy_haWdqWlBFODVKOHc" target=_blank> https://drive.google.com/open?id=0BzHr4OwCpy_haWdqWlBFODVKOHc </a>

Arquivos de Referência
Can you write Python code for it?

path = os.path.join("data", "desafio")

input_file = {
    "aisles": os.path.join(path, "aisles.csv"),
    "departments": os.path.join(path, "departments.csv"),
    "order_products": os.path.join(path, "order_products.csv"),
    "orders": os.path.join(path, "orders.csv"),
    "products": os.path.join(path, "products.csv")
}


I am working on the file "Numpy e Pandas/Modulo 3 - Leitura e Escrita em Arquivos + Combinando Tabelas.ipynb".
The first task is:
Estruturas Homogêneas
Adicionando Linhas

método: append
concatena DataFrames no sentido das linhas
Can you write Python code for it?

""" novos dados de 'orders' """
new_orders = pd.DataFrame(
    columns=["user_id", "store_id", "product_id"],
    data=[
        [1, 1, 1],
        [2, 1, 2],
        [3, 2, 1],
        [2, 1, 3],
        [1, 2, 1],
    ]
)

new_orders

""" adicionando no 'orders' original """
temp_orders = orders.append(new_orders)

temp_orders


I am working on the file "Numpy e Pandas/Modulo 3 - Leitura e Escrita em Arquivos + Combinando Tabelas.ipynb".
The first task is:
Leitura de Arquivos
Parâmetros default
Can you write Python code for it?

file_name = os.path.join("data", "tutorial", "orders.csv")

pd.read_csv(file_name)


I am working on the file "Numpy e Pandas/Modulo 3 - Leitura e Escrita em Arquivos + Combinando Tabelas.ipynb".
The first task is:
Observação:
Em DataFrames com nomes de coluna iguais, deve-se especificar sufixos para diferenciar as colunas.
Can you write Python code for it?

new_stores.join(new_stores, lsuffix="_original")

new_stores.join(new_stores, rsuffix="_copycat")

new_stores.join(new_stores, lsuffix="_original", rsuffix="_copycat")


I am working on the file "Numpy e Pandas/Modulo 3 - Leitura e Escrita em Arquivos + Combinando Tabelas.ipynb".
The first task is:
Observações:
* append não verifica índices, podendo repetir existentes
* Caso um dos DataFrames tenha uma coluna a mais, o outro replica essa coluna, preenchida com NaNs
Can you write Python code for it?

new_orders.append(
    pd.DataFrame(
        columns=list(new_orders.columns) + ["new_column"],
        data=[
            [1, 2, 1, 51],
            [1, 2, 1, 15]
        ]
    )
)


I am working on the file "Numpy e Pandas/Modulo 3 - Leitura e Escrita em Arquivos + Combinando Tabelas.ipynb".
The first task is:
Operações com Arquivos
Criando os Arquivos
Criando Estrutura de Pastas
Can you write Python code for it?

try:
    os.makedirs(os.path.join("data", "tutorial"))
    print("Pasta criada.")
except OSError:
    print("Pasta já existe!")


I am working on the file "Numpy e Pandas/Modulo 3 - Leitura e Escrita em Arquivos + Combinando Tabelas.ipynb".
The first task is:
Parâmetro header

define a linha que contém as colunas
valores esperados: inteiro ou lista de inteiros ou None
valor default: 0
Can you write Python code for it?

""" Usando header = None """
pd.read_csv(file_name, header=None)

""" Usando outra linha como header; nota-se que as linhas acima são eliminadas """
pd.read_csv(file_name, header=1)

""" Usando múltiplass linhas como header """
pd.read_csv(file_name, header=[0,1,2])


I am working on the file "Numpy e Pandas/Modulo 3 - Leitura e Escrita em Arquivos + Combinando Tabelas.ipynb".
The first task is:
Parâmetro index_col

define a coluna que contém o index
valores esperados: inteiro ou lista de inteiros
valor default: None
Can you write Python code for it?

""" definindo index_col para a primeira coluna """
pd.read_csv(file_name, index_col=0)

""" definindo index_col para a segunda coluna """
pd.read_csv(file_name, index_col=1)


I am working on the file "Numpy e Pandas/Modulo 3 - Leitura e Escrita em Arquivos + Combinando Tabelas.ipynb".
The first task is:
Parâmetro sep

define o separador de colunas.
valores esperados: caractere
valor default: ","
Can you write Python code for it?

""" Modificando o parâmetro sep"""
pd.read_csv(file_name, sep=";")

""" Usando explicitamente o valor default de sep """
pd.read_csv(file_name, sep=",")


I am working on the file "Numpy e Pandas/Modulo 3 - Leitura e Escrita em Arquivos + Combinando Tabelas.ipynb".
The first task is:
Salvando o Dataset
Can you write Python code for it?


I am working on the file "Numpy e Pandas/Modulo 3 - Leitura e Escrita em Arquivos + Combinando Tabelas.ipynb".
The first task is:
Visualização do Dataset
Can you write Python code for it?


I am working on the file "Numpy e Pandas/Modulo 3 - Leitura e Escrita em Arquivos + Combinando Tabelas.ipynb".
The first task is:
department

colunas:
department_id
department

sep: ','
Can you write Python code for it?


I am working on the file "Numpy e Pandas/Modulo 4 - Transformações e Information Retrieval.ipynb".
The first task is:
DataFrame
Variável de Teste
Can you write Python code for it?

""" DataFrame a ser transformado"""
df = pd.DataFrame(
    index=pd.Index(data=["Leite", "Ovos", "Carne", "Arroz", "Feijão"], name="Itens"),
    columns=["Quantidade",  "Preço Unitário"],
    data=np.array([
        [4, 3, 1, 5, 2],
        [3.00, 6.50, 25.90, 5.00, 3.80]
    ]).T,
)
df


I am working on the file "Numpy e Pandas/Modulo 4 - Transformações e Information Retrieval.ipynb".
The first task is:
Desafio
Objetivo:
Analisar o dataset gerado no Módulo 3 e responder às questões em cada item.
Dataset
Can you write Python code for it?

file_name = os.path.join("data", "desafio", "dataset.csv")

dataset = pd.read_csv(filename, sep=",", index_col=0, encoding="utf-8")
dataset.head()


I am working on the file "Numpy e Pandas/Modulo 4 - Transformações e Information Retrieval.ipynb".
The first task is:
Itens
[ Item A ]
Quantos usuários únicos fizeram pedidos?
Can you write Python code for it?


I am working on the file "Numpy e Pandas/Modulo 4 - Transformações e Information Retrieval.ipynb".
The first task is:
Linhas e Colunas
Can you write Python code for it?

""" Função de Transformação """
def fn(x):
    return x.product()

""" Transformação elemento a elemento """
df.apply(fn)

""" Transformação elemento a elemento """
df.apply(fn, axis=1)


I am working on the file "Numpy e Pandas/Modulo 4 - Transformações e Information Retrieval.ipynb".
The first task is:
Propagação de Funções
Series
Variável de Teste
Can you write Python code for it?

""" Série a ser transformada"""
s = pd.Series(
    name="Compras",
    index=["Leite", "Ovos", "Carne", "Arroz", "Feijão"],
    data=[2, 12, 1, 5, 2]
)
s


I am working on the file "Numpy e Pandas/Modulo 4 - Transformações e Information Retrieval.ipynb".
The first task is:
Transformações
Variável de Teste
Can you write Python code for it?


I am working on the file "Numpy e Pandas/Modulo 4 - Transformações e Information Retrieval.ipynb".
The first task is:
[ Item D ]
Qual o dia da semana em que ocorrem mais pedidos de produtos para cada departmento (department)?
Can you write Python code for it?


I am working on the file "Numpy e Pandas/Modulo 4 - Transformações e Information Retrieval.ipynb".
The first task is:
crosstab
Can you write Python code for it?

pd.crosstab?

pd.crosstab(df.store_name, df.product_name)


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


I am working on the file "Numpy.ipynb".
The first task is:
Ako želimo napraviti novu kopiju, koristimo funkciju copy:
Can you write Python code for it?

B = copy(A)

v = array([1,2,3,4])

for element in v:
    print (element)

M = array([[1,2], [3,4]])

for row in M:
    print ("redak {}".format(row))

    for element in row:
        print (element)


I am working on the file "Numpy.ipynb".
The first task is:
Eksplicitno pretvaranje podataka. Uvijek stvara novi niz.
Can you write Python code for it?

M.dtype

M2 = M.astype(float)
M2

M2.dtype

M3 = M.astype(bool)
M3

from verzije import *
from IPython.display import HTML
HTML(print_sysinfo()+info_packages('numpy,matplotlib'))


I am working on the file "Numpy.ipynb".
The first task is:
Format je: godina, mjesec, dan, prosječna dnevna temperatura, najniža, najviša, lokacija.
Recimo da nas zanimaju samo temperature u veljači.
Can you write Python code for it?

# mjeseci su 1.,..., 12.
unique(dataStockholm[:,1])

maska_velj = dataStockholm[:,1] == 2

mean(dataStockholm[maska_velj,3])


I am working on the file "Numpy.ipynb".
The first task is:
Funkcija choose:
Can you write Python code for it?


I am working on the file "Numpy.ipynb".
The first task is:
Funkcija flatten radi kopiju.
Can you write Python code for it?

B = A.flatten()
B

B[0:5] = 10
B

A # A je sad ostao isti

v = array([1,2,3])

shape(v)

# pretvorimo v u matricu
v[:, newaxis]

v[:,newaxis].shape

v[newaxis,:].shape

a = array([[1, 2], [3, 4]])

# ponovimo svaki element tri puta
repeat(a, 3)

tile(a, 3)

b = array([[5, 6]])

concatenate((a, b), axis=0)

concatenate((a, b.T), axis=1)

vstack((a,b))

hstack((a,b.T))


I am working on the file "Numpy.ipynb".
The first task is:
Funkcije na nizovima
Can you write Python code for it?

indeksi = where(maska)
indeksi

x[indeksi]

print(A)
diag(A)

diag(A, -1)

v2 = arange(-3,3)
v2

indeksi_redaka = [1, 3, 5]
v2[indeksi_redaka]

v2.take(indeksi_redaka)


I am working on the file "Numpy.ipynb".
The first task is:
Još neke funkcije: inner, outer, cross, kron, tensordot.
Can you write Python code for it?

C = matrix([[1j, 2j], [3j, 4j]])
C

conjugate(C)


I am working on the file "Numpy.ipynb".
The first task is:
Koja je razlika između numpy.ndarray tipa i standardnih lista u Pythonu?

liste u Pythonu mogu sadržavati bilo kakve vrste objekata, to nije slučaj s numpy.ndarray
numpy.ndarray nisu dinamički objekti: pri kreiranju im je određen tip
za numpy.ndarray implementirane su razne efikasne metode važne u numerici
de facto sva računanja se odvijaju u C-u i Fortranu pomoću BLAS rutina

dtype (data type) nam daje informaciju o tipu podataka u nizu:
Can you write Python code for it?


I am working on the file "Numpy.ipynb".
The first task is:
Kopiranje nizova
Can you write Python code for it?

A = array([[1, 2], [3, 4]])
A

# B je isto što i A (bez kopiranja podataka)
B = A


I am working on the file "Numpy.ipynb".
The first task is:
Kreiranje nizova pomoću numpy modula
Can you write Python code for it?

v = array([1,2,3,4])
v

M = array([[1, 2], [3, 4]])
M

type(v), type(M)

v.shape

M.shape

v.size, M.size


I am working on the file "Numpy.ipynb".
The first task is:
Matrice mogu biti i višedimenzionalne
Can you write Python code for it?

a = random.rand(8,13,13)
b = random.rand(8,13,13)
matmul(a, b).shape


I am working on the file "Numpy.ipynb".
The first task is:
Možemo koristiti i funkcije numpy.shape, numpy.size
Can you write Python code for it?


I am working on the file "Numpy.ipynb".
The first task is:
Možemo koristiti i tzv. maske: ako je maska numpy niz tipa bool, tada se izabiru oni elementi koji u maski odgovaraju vrijednosti True.
Can you write Python code for it?

B = array([n for n in range(5)])
B

maska = array([True, False, True, False, False])
B[maska]

maska = array([1,0,1,0,0], dtype=bool)
B[maska]


I am working on the file "Numpy.ipynb".
The first task is:
Naravno, možemo koristiti i : operator:
Can you write Python code for it?


I am working on the file "Numpy.ipynb".
The first task is:
Naravno, sve ove operacije možemo raditi na dijelovima nizova.
Can you write Python code for it?


I am working on the file "Numpy.ipynb".
The first task is:
Oblik niza se može promijeniti bez da se dira memorija, dakle mogu se primijenjivati i na veliku količinu podataka.
Can you write Python code for it?

A

n, m = A.shape

B = A.reshape((1,n*m))
B

B[0,0:5] = 5 # promijenili smo B
B

A # a time smo promijenili i A


I am working on the file "Numpy.ipynb".
The first task is:
Postoji i tip matrix. Kod njega operacije +, -, * se ponašaju onako kako smo navikli.
Can you write Python code for it?

M = matrix(A)
v = matrix(v1).T # da bi dobili stupčasti vektor

v

M*M

M*v

# skalarni produkt
v.T * v

v + M*v


I am working on the file "Numpy.ipynb".
The first task is:
Prosječna dnevna temperatura u Stockholmu u zadnjiih 200 godina je bila 6.2 C.
Can you write Python code for it?

std(dataStockholm[:,3]), var(dataStockholm[:,3])

dataStockholm[:,3].min()

dataStockholm[:,3].max()

d = arange(0, 10)
d

sum(d)

prod(d+1)

# kumulativa suma
cumsum(d)

# kumulativan produkt
cumprod(d+1)

# isto što i: diag(A).sum()
trace(A)


I am working on the file "Numpy.ipynb".
The first task is:
Rad s nizovima
Indeksiranje funkcionira standardno.
Can you write Python code for it?


I am working on the file "Numpy.ipynb".
The first task is:
Rad s višedimenzionalnim podacima
Can you write Python code for it?

m = rand(3,3)
m

m.max()

# max u svakom stupcu
m.max(axis=0)

# max u svakom retku
m.max(axis=1)


I am working on the file "Numpy.ipynb".
The first task is:
S negativnim indeksima računamo od kraja niza:
Can you write Python code for it?

A = array([1,2,3,4,5])

A[-1] # zadnji element niza

A[-3:] # zadnja tri elementa


I am working on the file "Numpy.ipynb".
The first task is:
Tipično dtype su: int, float, complex, bool, object, itd.
Ali možemo biti i eksplicitni vezano za veličinu registra: int64, int16, float128, complex128.
Funkcije koje generiraju nizove
Can you write Python code for it?

x = arange(0, 10, 1) # argumenti: početak, kraj, korak

x # 10 nije u nizu!

x = arange(-1, 1, 0.1)
x

# ovdje su i početak i kraj uključeni!
linspace(0, 10, 25)

logspace(0, 10, 10, base=e)

x, y = mgrid[0:5, 0:5] # slično kao meshgrid u MATLAB-u

x

y

from numpy import random

# uniformna distribucija na [0,1]
random.rand(5,5)

# standardna normalna distribucija
random.randn(5,5)

# dijagonalna matrica
diag([1,2,3])

# matrica sa sporednom dijagonalom
diag([1,2,3], k=1)

zeros((3,3))

ones((3,3))


I am working on the file "Numpy.ipynb".
The first task is:
To smo mogli napraviti i ručno.
Can you write Python code for it?

def Theta(x):
    """
    Vektorska verzija step funkcije.
    """
    return 1 * (x >= 0)

Theta(array([-3,-2,-1,0,1,2,3]))

# radi naravno i za skalare
Theta(-1.2), Theta(2.6)

M

if (M > 5).any():
    print ("barem jedan element iz M je veći od 5")
else:
    print ("svi elementi iz M su manji ili jednaki od 5")

if (M > 5).all():
    print ("svi elementi iz M su veći od 5")
else:
    print ("barem jedan element je manji ili jednak od 5")


I am working on the file "Numpy.ipynb".
The first task is:
U sljedećem primjeru take djeluje na listu, a izlaz je array:
Can you write Python code for it?


I am working on the file "Numpy.ipynb".
The first task is:
Uz numpy.savetxt možemo napraviti i obrnuto.
Can you write Python code for it?

M = random.rand(3,3)
M

savetxt("random-matrix.csv", M)

!cat random-matrix.csv

savetxt("random-matrix.csv", M, fmt='%.5f') # s fmt specificiramo format

!cat random-matrix.csv


I am working on the file "Numpy.ipynb".
The first task is:
Za izvlačenje realnog, odnosno imaginarnog dijela: real i imag:
Can you write Python code for it?

real(C) # isto što i C.real

imag(C) # isto što i C.imag

angle(C+1) # u MATLAB-u je to funkcija arg, dakle argument (faza) kompleksnog broja

abs(C)

from numpy.linalg import inv, det
inv(C) # isto što i C.I

C.I * C

det(C)

det(C.I)


I am working on the file "Numpy.ipynb".
The first task is:
Zanimljiviji primjer:
Can you write Python code for it?

x = arange(0, 10, 0.5)
x

maska = (5 < x) * (x < 7.5)
maska

x[maska]


I am working on the file "Numpy/NumpyEx01.ipynb".
The first task is:
Use vizarray to visualize a checkerboard of size=20 with a block size of 10px.
Can you write Python code for it?

va.set_block_size(10)
va.vizarray(checkerboard(20))

assert True


I am working on the file "Numpy/NumpyEx01.ipynb".
The first task is:
Use vizarray to visualize a checkerboard of size=27 with a block size of 5px.
Can you write Python code for it?

va.set_block_size(5)
va.vizarray(checkerboard(27))

assert True


I am working on the file "Numpy/NumpyEx02.ipynb".
The first task is:
Use the %timeit magic to time both versions of this function for an argument of 50. The syntax for %timeit is:
python
%timeit -n1 -r1 function_to_time()
Can you write Python code for it?

print("Argument of 50")
%timeit -n1 -r1 np_fact(50)
%timeit -n1 -r1 loop_fact(50)

print("Argument of 90000")
%timeit -n1 -r1 np_fact(90000)
%timeit -n1 -r1 loop_fact(90000)


I am working on the file "Numpy/NumpyEx02.ipynb".
The first task is:
Write a function that computes the factorial of small numbers using a Python loop.
Can you write Python code for it?

def loop_fact(n):
    result = 1
    """Compute n! using a Python for loop."""
    if n == 0:
        return 1
    else:
        while n > 0:
            result = result*n
            n-=1
    return result

assert loop_fact(0)==1
assert loop_fact(1)==1
assert loop_fact(10)==3628800
assert [loop_fact(i) for i in range(0,11)]==[1,1,2,6,24,120,720,5040,40320,362880,3628800]


I am working on the file "Numpy/NumpyEx03.ipynb".
The first task is:
Use np.diff to compute the changes at each step of the motion, dW, and then compute the mean and standard deviation of those differences.
Can you write Python code for it?

dW = np.diff(W)
mean = dW.mean()
std = dW.std()
print "mean: " + str(mean)
print "std: " + str(std)

assert len(dW)==len(W)-1
assert dW.dtype==np.dtype(float)


I am working on the file "Numpy/NumpyEx03.ipynb".
The first task is:
Use your function to simulate geometric brownian motion, $X(t)$ for $X_0=1.0$, $\mu=0.5$ and $\sigma=0.3$ with the Wiener process you computed above.
Visualize the process using plt.plot with t on the x-axis and X(t) on the y-axis. Label your x and y axes.
Can you write Python code for it?

geo_brownian(t, W, 1, .5, .3)
plt.plot(t, geo_brownian(t, W, 1, .5, .3))
plt.xlabel('time')
plt.ylabel('X(t)')

assert True # leave this for grading


I am working on the file "Numpy/NumpyEx03.ipynb".
The first task is:
Write a function that takes $W(t)$ and converts it to geometric Brownian motion using the equation:
$$
X(t) = X_0 e^{((\mu - \sigma^2/2)t + \sigma W(t))}
$$
Use Numpy ufuncs and no loops in your function.
Can you write Python code for it?

def geo_brownian(t, W, X0, mu, sigma):
    "Return X(t) for geometric brownian motion with drift mu, volatility sigma."""
    # This returns an array for every value of x(t) at a specific
    return X0*np.exp(((mu-.5*sigma**2)*t)+sigma*W)

assert True # leave this for grading


I am working on the file "Numpy/NumpyEx04.ipynb".
The first task is:
The Laplacian Matrix is a matrix that is extremely important in graph theory and numerical analysis. It is defined as $L=D-A$. Where $D$ is the degree matrix and $A$ is the adjecency matrix. For the purpose of this problem you don't need to understand the details of these matrices, although their definitions are relatively simple.
The degree matrix for $K_n$ is an $n \times n$ diagonal matrix with the value $n-1$ along the diagonal and zeros everywhere else. Write a function to compute the degree matrix for $K_n$ using NumPy.
Can you write Python code for it?

def complete_deg(n):
    return np.identity(n,dtype=np.int)*(n-1)

D = complete_deg(5)
assert D.shape==(5,5)
assert D.dtype==np.dtype(int)
assert np.all(D.diagonal()==4*np.ones(5))
assert np.all(D-np.diag(D.diagonal())==np.zeros((5,5),dtype=int))


I am working on the file "Numpy/NumpyEx04.ipynb".
The first task is:
The adjacency matrix for $K_n$ is an $n \times n$ matrix with zeros along the diagonal and ones everywhere else. Write a function to compute the adjacency matrix for $K_n$ using NumPy.
Can you write Python code for it?

def complete_adj(n):
    #I used diagflat to create an identity matrix of size n. I also could have just used np.identity
    return np.ones((n,n),np.dtype(int)) - np.diagflat([1]*n)

A = complete_adj(5)
assert A.shape==(5,5)
assert A.dtype==np.dtype(int)
assert np.all(A+np.eye(5,dtype=int)==np.ones((5,5),dtype=int))


I am working on the file "Numpy/numpyclass.ipynb".
The first task is:
Array creation:
NumPy offers several functions to create arrays with initial placeholder content.
numpy.empty
Syntax: numpy.empty(shape, dtype = float, order = 'C')
Shape : Shape of an empty array in int or tuple of int
Dtype  : Desired output data type. Optional
Order   :'C' for C-style row-major array, 'F' for FORTRAN style column-major array
Can you write Python code for it?


I am working on the file "Numpy/numpyclass.ipynb".
The first task is:
Broadcasting :
The term broadcasting refers to the ability of NumPy to treat arrays of different shapes during arithmetic operations.
Can you write Python code for it?

#normal example
a = np.array([1,2,3,4])
b = np.array([10,20,30,40])

print(a.shape)

print(b.shape)

c = a * b
print (c)

#Broadcasting

x = np.arange(4)
y = np.ones(5)
xb=x.reshape(4,1)
print(xb)
#bd
print(xb + y)
(xb + y).shape


I am working on the file "Numpy/numpyclass.ipynb".
The first task is:
CS-2
Topics
Data types,Array creation,
Numeric Ranges,Indexing and slicing.
dtype:
A dtype object is constructed using the following
syntax −
numpy.dtype(object, align, copy)
Object − To be converted to data type object
Align − If true, adds padding to the field to make it similar to C-struct
Copy − Makes a new copy of dtype object. If false, the result is reference to builtin data type object.
Can you write Python code for it?