I am working on the file "Accessing and managing users.ipynb".
The first task is:
The power of Python can also be used to search for specific patterns.
Can you write Python code for it?

# Get a list of all users
all_users = gis.users.search(None, max_users=500)
len(all_users)

# Use list comprehension to create a subset list of disabled users
disabled_users = [user for user in all_users if user.disabled == True]
len(disabled_users)


I am working on the file "Accessing and managing users.ipynb".
The first task is:
You can also search for other users.
Can you write Python code for it?

username = input('Username: ')

user = gis.users.get(username)
user


I am working on the file "Accessing and managing users_with_outputs.ipynb".
The first task is:
Searching for user accounts
Parameters here: http://resources.arcgis.com/en/help/arcgis-rest-api/index.html#//02r30000009v000000
and here: http://resources.arcgis.com/en/help/arcgis-rest-api/#/Search_reference/02r3000000mn000000/
The gis.users.search method allows you to perform search operations using wildcards.
Can you write Python code for it?

tpt_users = gis.users.search('username:tpt_*',max_users=10000)
len(tpt_users)


I am working on the file "Accessing and managing users_with_outputs.ipynb".
The first task is:
The Python time module can be used to display time values in human readable form.
ArcGIS Online stores time in milliseconds since the epoch, but the Python time library is expecting seconds since the epoch (aka Unix time, epoch time), so the AGOL times need to be divided by 1000 when using the time module.
Can you write Python code for it?

import time

created_time = time.localtime(me.created/1000)
print("Created: {}/{}/{}".format(created_time[1], created_time[2], created_time[0]))

last_accessed = time.localtime(me.lastLogin/1000)
print("Last active: {}/{}/{}".format(last_accessed[1], last_accessed[2], last_accessed[0]))


I am working on the file "Accessing and managing users_with_outputs.ipynb".
The first task is:
User properties
There are a number of properties that can be accessed the user resource.  The full list can be found here: User.
Can you write Python code for it?

print("Description: {}\nEmail: {}\nLast Name: {}\nFirst Name: {}\nName: {}\nLevel: {}\nMFA Enabled: {}".format(me.description, me.email, me.firstName, me.lastName, me.fullName, me.level, me.mfaEnabled))

user_groups = me.groups
print("Member of " + str(len(user_groups)) + " groups")


I am working on the file "Accessing and managing users_with_outputs.ipynb".
The first task is:
You can also search for other users.
Can you write Python code for it?


I am working on the file "Accuracy/choose_threshold.ipynb".
The first task is:
Create a sklearn LogisticRegression classifier, and get the scores
Can you write Python code for it?

logreg = LogisticRegression(C=1e5, solver='lbfgs')

# Create an instance of classifier and fit the data.
logreg.fit(X_train, y_train)

# Compute the score of the linear function
score_lr = logreg.decision_function(X_test)

# The threshold for logistic regression
print(logreg.intercept_[0])


I am working on the file "Accuracy/choose_threshold.ipynb".
The first task is:
Derivation of confusion matrix based on parametric conditional densities
Based on a continuous decision threshold $t$, let $\cdf_+(t)$ and $\cdf_−(t)$ denote the resulting cumulative distribution functions of the positive and negative populations of decision values, respectively. Further, let $\alpha \in (0, 1)$ be the fraction of positive examples. The confusion matrix can then be rewritten in a parametric form:
| conf. matrix  | actual $+$ | actual $-$ |
|:--------------|:----------:|:----------:|
| predicted $+$ | $\alpha(1-\cdf_+(t))$ | $(1-\alpha)(1-\cdf_-(t))$ |
| predicted $-$ | $\alpha\cdf_+(t)$ | $(1-\alpha)\cdf_-(t)$ |
| total | $\alpha$ | $1-\alpha $ |
Reference:
Kay H. Brodersen, Cheng Soon Ong, Klaas E. Stephan and Joachim M. Buhmann
The binormal assumption on precision-recall curves
International Conference on Pattern Recognition 2010
Find a threshold that balances false positive and false negative rates
In other words, find $t$ such that
$$(1-\alpha)(1-\cdf_-(t)) = \alpha\cdf_+(t).$$
Rearranging
$$ \frac{\cdf_+(t)}{1-\cdf_-(t)} = \frac{1}{\alpha} - 1 $$
Can you write Python code for it?

from scipy.stats import norm

pos_mean, pos_stddev = gauss_fit(X[pos_idx])
pos_fit = norm(loc=pos_mean, scale=pos_stddev)
neg_mean, neg_stddev = gauss_fit(X[neg_idx])
neg_fit = norm(loc=neg_mean, scale=neg_stddev)


I am working on the file "Accuracy/choose_threshold.ipynb".
The first task is:
Illustration of how the values look like
Can you write Python code for it?

min_x = np.min(X)
max_x = np.max(X)
t_vals = np.arange(min_x, max_x, 0.01)
tail_ratio = pos_fit.cdf(t_vals)/neg_fit.sf(t_vals)

alpha = float(num_pos)/float(num_pos+num_neg)
target_ratio = 1/alpha - 1
print(alpha, target_ratio)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(t_vals, tail_ratio, 'b')
ax.plot([min_x, max_x], [target_ratio, target_ratio], 'r--')
ax.set_ylim([0, 2*target_ratio])
ax.set_xlim([2,3])


I am working on the file "Accuracy/choose_threshold.ipynb".
The first task is:
Investigate the effect of threshold on accuracy
Can you write Python code for it?

def get_conf_matrix(ypred, ytrue):
    """get confusion matrix from [0, 1] labels"""
    ypred = np.array(ypred)
    ytrue = np.array(ytrue)
    tp =  np.sum((ytrue==1) & (ypred==1))
    tn =  np.sum((ytrue==0)  & (ypred==0))
    fp =  np.sum((ytrue==0)  & (ypred==1))
    fn =  np.sum((ytrue==1)  & (ypred==0))

    return np.array([[tp,fp], [fn,tn]])

def accuracy(conf):
    """ compute the accuracy, given a confusion matrix conf
        format: [[TP, FP], [FN, TN]]"""

    return (conf[0,0] + conf[1,1]) / np.sum(conf)

def score2class(score, thres):
    """Given a vector of scores, use threshold to predict 0 or 1
    when below or above the threshold respectively"""
    pred = np.zeros(len(score), dtype=int)
    pos_idx = np.where(score > thres)[0]
    pred[pos_idx] = 1
    return pred

pred_default = score2class(score_lr, logreg.intercept_[0])
pred_gaussfit = score2class(score_lr, threshold)

print('Accuracy with default threshold = {}'.format(accuracy(get_conf_matrix(pred_default, y_test))))
print('Accuracy with Gaussian fit threshold = {}'.format(accuracy(get_conf_matrix(pred_gaussfit, y_test))))


I am working on the file "Accuracy/choose_threshold.ipynb".
The first task is:
Plot the histograms of per class scores. We can see from the histograms that the Gaussian fit is not really a very good assumption.
Can you write Python code for it?

fig = plt.figure()
ax = fig.add_subplot(111)
ax.hist(score_lr[pos_idx], bins=10, color='red', alpha=0.6)
_ = ax.hist(score_lr[neg_idx], bins=20, color='blue', alpha=0.6)


I am working on the file "Accuracy/choose_threshold.ipynb".
The first task is:
Split into training and test sets
Can you write Python code for it?

X_train, X_test, y_train, y_test = train_test_split(X, y)

# positive and negative indices on the test set
pos_idx = np.where(y_test==1)[0]
neg_idx = np.where(y_test==0)[0]


I am working on the file "Accuracy/choose_threshold.ipynb".
The first task is:
Use bisection to find the crossing
Can you write Python code for it?

from scipy.optimize import bisect

def cost(t, pos_fit, neg_fit, alpha):
    target_ratio = 1/alpha - 1
    tail_ratio = pos_fit.cdf(t)/neg_fit.sf(t)
    return tail_ratio - target_ratio

threshold = bisect(cost, neg_mean, pos_mean, args=(pos_fit, neg_fit, alpha))
print(threshold)


I am working on the file "AcousticFWI/Acoustic-wave-equation.ipynb".
The first task is:
A Forward propagation example
Can you write Python code for it?

(rect,ut)=Forward(nt,nx,ny,mt)

fig = plt.figure()
plts = []             # get ready to populate this list the Line artists to be plotted
plt.hold("off")
for i in range(nt):
    r = plt.imshow(ut[i,:,:])   # this is how you'd plot a single line...
    plts.append( [r] )
ani = animation.ArtistAnimation(fig, plts, interval=50,  repeat = False)   # run the animation
plt.show()

fig2 = plt.figure()
plt.hold("off")
shotrec = plt.imshow(rect)   # this is how you'd plot a single line...
#plt.show()


I am working on the file "AcousticFWI/Acoustic-wave-equation.ipynb".
The first task is:
Adjoint test
In ordr to guaranty we have the gradient we need to make sure that the solution of the adjoint wave equation is indeed the true adjoint. Tod os so one should check that
$ <Ax,y> - <x,A^Ty> = 0$
where $A$ is the wave_equation,  $A^T$ is wave_equationA and $x,y$ are any random vectors in the range of each operator. This can however be expensive as this two vector would be of size $N * n_t$. To test our operator we will the relax test by
$ <P_r A P_s^T x,y> - <x,P_SA^TP_r^Ty> = 0$
where $P_r , P_s^T$ are the source and recevier projection operator mapping the source and receiver locations and times onto the full domain. This allow to have only a random source of size $n_t$ at a random postion.
Can you write Python code for it?

(rec0,u0)=Forward(nt,nx,ny,m0)
(srca,v)=Adjoint(nt,nx,ny,m0,rec0)

plts = []             # get ready to populate this list the Line artists to be plotted
plt.hold("off")
for i in range(0,nt):
    r = plt.imshow(v[i,:,:],vmin=-100, vmax=100)   # this is how you'd plot a single line...
    plts.append( [r] )
ani = animation.ArtistAnimation(fig, plts, interval=50,  repeat = False)   # run the animation
plt.show()

shotrec = plt.plot(srca)   # this is how you'd plot a single line...
#plt.show()

# Actual adjoint test
term1=0
for ti in range(0,nt):
    term1=term1+srca[ti]*source(xsrc,ysrc,(ti)*tstep)

term2=LA.norm(rec0)**2

term1,term2,term1-term2,term1/term2


I am working on the file "AcousticFWI/Acoustic-wave-equation.ipynb".
The first task is:
Adjoint test for the gradient
The adjoint of the FWI Gradient is the Born modelling operator, implementing a double propagation forward in time with a wavefield scaled by the model perturbation for the second propagation
$ J dm = - A^{-1}(\frac{d A^{-1}q}{dt^2}) dm $
Can you write Python code for it?

Im2=Gradient(nt,nx,ny,m0,rec0,u0)

du1=Born(nt,nx,ny,m0,dm)

term11=np.dot((rec0).reshape(-1),du1.reshape(-1))
term21=np.dot(Im2.reshape(-1),dm.reshape(-1))

term11,term21,term11-term21,term11/term21


I am working on the file "AcousticFWI/Acoustic-wave-equation.ipynb".
The first task is:
Define the discrete model
Can you write Python code for it?

import matplotlib.pyplot as plt
from matplotlib import animation

hstep=25 #space increment d  = minv/(10*f0);
tstep=2 #time increment dt < .5 * hstep /maxv;
tmin=0.0 #initial time
tmax=300 #simulate until
xmin=-875.0 #left bound
xmax=875.0 #right bound...assume packet never reaches boundary
ymin=-875.0 #left bound
ymax=875.0 #right bound...assume packet never reaches boundary
f0=.010
t0=1/.010
nbpml=10
nx = int((xmax-xmin)/hstep) + 1 #number of points on x grid
ny = int((ymax-ymin)/hstep) + 1 #number of points on x grid
nt = int((tmax-tmin)/tstep) + 2 #number of points on t grid
xsrc=-400
ysrc=0.0
xrec = nbpml+4
#set source as Ricker wavelet for f0
def source(x,y,t):
    r = (np.pi*f0*(t-t0))
    val = (1-2.*r**2)*np.exp(-r**2)
    if abs(x-xsrc)<hstep/2 and abs(y-ysrc)<hstep/2:
        return val
    else:
        return 0.0

def dampx(x):
    dampcoeff=1.5*np.log(1.0/0.001)/(5.0*hstep);
    if x<nbpml:
        return dampcoeff*((nbpml-x)/nbpml)**2
    elif x>nx-nbpml-1:
        return dampcoeff*((x-nx+nbpml)/nbpml)**2
    else:
        return 0.0
def dampy(y):
    dampcoeff=1.5*np.log(1.0/0.001)/(5.0*hstep);
    if y<nbpml:
        return dampcoeff*((nbpml-y)/nbpml)**2
    elif y>ny-nbpml-1:
        return dampcoeff*((y-ny+nbpml)/nbpml)**2
    else:
        return 0.0

# Velocity models
def smooth10(vel,nx,ny):
    out=np.ones((nx,ny))
    out[:,:]=vel[:,:]
    for a in range(5,nx-6):
        out[a,:]=np.sum(vel[a-5:a+5,:], axis=0) /10
    return out

# True velocity
vel=np.ones((nx,ny)) + 2.0
vel[floor(nx/2):nx,:]=4.5
mt=vel**-2
# Smooth velocity
v0=smooth10(vel,nx,ny)
m0=v0**-2

dm=m0-mt


I am working on the file "AcousticFWI/Acoustic-wave-equation.ipynb".
The first task is:
Gradient test
The last part is to check that the operators are consistent with the problem. There is then two properties to be satisfied
$ \Phi(m + hdm) = \Phi(m) +  \mathcal{O} (h) \
   \Phi(m + h dm) = \Phi(m) + h (J[m]^T\delta |d)dm + \mathcal{O} (h^2) $
which are the linearization conditions for the objective. This is a bit slow to run here but here is the way to test it.
1 - Genrate data for the true model m
2 - Define a smooth initial model $m_0$ and comput the data $d_0$ for this model
3 - You now have $\Phi(m_0)$
4 - Define $ dm = m-m_0$ and $ h = {1,.1,.01,.001,...}$
5 - For each $h$ compute $\Phi(m_0 + h dm)$ by generating data for $m_0 + h dm$ and compute $(J[m_0 + h dm]^T\delta |d) $
6 - Plot in Loglog the two lines of equation above
Can you write Python code for it?

(DT,uT)=Forward(nt,nx,ny,mt)
(D1,u0)=Forward(nt,nx,ny,m0)
F0=.5*LA.norm(D1-DT)**2

g=Gradient(nt,nx,ny,m0,D1-DT,u0)
G=np.dot(g.reshape(-1),dm.reshape(-1));
error21=np.zeros((7))
error22=np.zeros((7))
for i in range(0,7):
    mloc=m0+H[i]*dm
    (D,u)=Forward(nt,nx,ny,mloc)
    error21[i]  = .5*LA.norm(D-DT)**2 -F0
    error22[i]  = .5*LA.norm(D-DT)**2 -F0 - H[i]*G

shotrec = plt.loglog(H,error21,H,H)   # this is how you'd plot a single line...
plt.show()
shotrec = plt.loglog(H,error22,H,hh)   # this is how you'd plot a single line...
plt.show()


I am working on the file "AcousticFWI/Acoustic-wave-equation.ipynb".
The first task is:
Jacobian test
The last part is to check that the operators are consistent with the problem. There is then two properties to be satisfied
$ U(m + hdm) = U(m) +  \mathcal{O} (h) \
   U(m + h dm) = U(m) + h J[m]dm + \mathcal{O} (h^2) $
which are the linearization conditions for the objective. This is a bit slow to run here but here is the way to test it.
1 - Genrate data for the true model m
2 - Define a smooth initial model $m_0$ and comput the data $d_0$ for this model
3 - You now have $U(m_0)$
4 - Define $ dm = m-m_0$ and $ h = {1,.1,.01,.001,...}$
5 - For each $h$ compute $U(m_0 + h dm)$ by generating data for $m_0 + h dm$ and compute $(J[m_0 + h dm]^T\delta |d) $
6 - Plot in Loglog the two lines of equation above
Can you write Python code for it?

H=[1,0.1,0.01,.001,0.0001,0.00001,0.000001]
(D1,u0)=Forward(nt,nx,ny,m0)
dub=Born(nt,nx,ny,m0,dm)
error1=np.zeros((7))
error2=np.zeros((7))
for i in range(0,7):
    mloc=m0+H[i]*dm
    (d,u)=Forward(nt,nx,ny,mloc)
    error1[i]  = LA.norm(d - D1,ord=1)
    error2[i]  = LA.norm(d - D1 - H[i]*dub,ord=1)

hh=np.zeros((7))
for i in range(0,7):
    hh[i]=H[i]*H[i]
shotrec = plt.loglog(H,error1,H,H)   # this is how you'd plot a single line...
plt.show()
shotrec = plt.loglog(H,error2,H,hh)   # this is howyou'd plot a single line...
plt.show()


I am working on the file "AcousticFWI/Acoustic-wave-equation.ipynb".
The first task is:
Time and space  discretization as a Taylor expansion.
The time discretization is define as a second order ( $ O (dt^2)) $) centered finite difference to get an explicit Euler scheme easy to solve by steping in time.
$ \frac{d^2 u(x,t)}{dt^2} \simeq \frac{u(x,t+dt) - 2 u(x,t) + u(x,t-dt)}{dt^2} + O(dt^2) $
And we define the space discretization also as a Taylor serie, with oder chosen by the user. This can either be a direct expansion of the second derivative bulding the laplacian, or a combination of first oder space derivative. The second option can be a better choice in case you would want to extand the method to more complex wave equations involving first order derivatives in chain only.
$ \frac{d^2 u(x,t)}{dt^2} \simeq \frac{1}{dx^2} \sum_k \alpha_k (u(x+k dx,t)+u(x-k dx,t)) + O(dx^k) $
Can you write Python code for it?

dtt=as_finite_diff(p(x,y,z,t).diff(t,t), [t-s,t, t+s])
dt=as_finite_diff(p(x,y,t).diff(t), [t-s, t+s])
# Spacial finite differences can easily be extended to higher order by increasing the list of sampling point in the next expression.
# Be sure to keep this stencil symmetric and everything else in the notebook will follow.
dxx=as_finite_diff(p(x,y,z,t).diff(x,x), [x-h,x, x+h])
dyy=as_finite_diff(p(x,y,z,t).diff(y,y), [y-h,y, y+h])
dzz=as_finite_diff(p(x,y,z,t).diff(z,z), [z-h,z, z+h])
dtt,dxx,dyy,dt


I am working on the file "AcousticFWI/Acoustic3D.ipynb".
The first task is:
PDE
The acoustic wave equation for the square slowness m and a source q is given in 3D by :
\begin{cases}
 &m \frac{d^2 u(x,t)}{dt^2} - \nabla^2 u(x,t) =q  \
 &u(.,0) = 0 \
 &\frac{d u(x,t)}{dt}|_{t=0} = 0
\end{cases}
with the zero initial conditons to guaranty unicity of the solution
Can you write Python code for it?

p=Function('p')
m,s,h = symbols('m s h')
m=M(x,y,z)
q=Q(x,y,z,t)
d=D(x,y,z,t)
e=E(x,y,z)

# Choose dimension (2 or 3)
dim = 3
# Choose order
time_order = 2
space_order = 2

# half width for indexes, goes from -half to half
width_t = int(time_order/2)
width_h = int(space_order/2)
solvep = p(x,y,z,t+width_t*s)
solvepa = p(x,y,z,t-width_t*s)

# Indexes for finite differences
indx = []
indy = []
indz = []
indt = []
for i in range(-width_h,width_h+1):
    indx.append(x + i * h)
    indy.append(y + i * h)
    indz.append(z + i* h)

for i in range(-width_t,width_t+1):
    indt.append(t + i * s)

# Finite differences
dtt=as_finite_diff(p(x,y,z,t).diff(t,t),indt)
dxx=as_finite_diff(p(x,y,z,t).diff(x,x), indx)
dyy=as_finite_diff(p(x,y,z,t).diff(y,y), indy)
dzz=as_finite_diff(p(x,y,z,t).diff(z,z), indz)
dt=as_finite_diff(p(x,y,z,t).diff(t), indt)
lap = dxx + dyy + dzz

arglamb=[]
arglamba=[]
for i in range(-width_t,width_t):
    arglamb.append( p(x,y,z,indt[i+width_t]))
    arglamba.append( p(x,y,z,indt[i+width_t+1]))

for i in range(-width_h,width_h+1):
    arglamb.append( p(indx[i+width_h],y,z,t))
    arglamba.append( p(indx[i+width_h],y,z,t))

for i in range(-width_h,width_h+1):
    arglamb.append( p(x,indy[i+width_h],z,t))
    arglamba.append( p(x,indy[i+width_h],z,t))

for i in range(-width_h,width_h+1):
    arglamb.append( p(x,y,indz[i+width_h],t))
    arglamba.append( p(x,y,indz[i+width_h],t))

arglamb.extend((q , m, s, h, e))
arglamb=tuple(arglamb)
arglamba.extend((q , m, s, h, e))
arglamba=tuple(arglamba)

arglamb=[ii for n,ii in enumerate(arglamb) if ii not in arglamb[:n]]

arglamb

# Forward wave equation
wave_equation = m*dtt- lap - q  + e*dt
stencil = solve(wave_equation,solvep)[0]
ts=lambdify(arglamb,stencil,"numpy")
stencil

# Adjoint wave equation
wave_equationA = m*dtt- lap - d - e*dt
stencilA = solve(wave_equationA,solvepa)[0]
tsA=lambdify(arglamba,stencilA,"numpy")
stencilA


I am working on the file "AcousticFWI/MultiOrder_2d-3d.ipynb".
The first task is:
Forward and adjoint stencil
2D-3D is automatic from the setup
Can you write Python code for it?

# Forward wave equation
wave_equation = m*dtt- lap- q  + e*dt
stencil = solve(wave_equation,solvep)[0]
ts=lambdify(arglamb,stencil,"numpy")
stencil

# Adjoint wave equation
wave_equationA = m*dtt- lap - d - e*dt
stencilA = solve(wave_equationA,solvepa)[0]
tsA=lambdify(arglamba,stencilA,"numpy")
stencilA


I am working on the file "AcousticFWI/OP.ipynb".
The first task is:
Operational intensity of  differential operation
We consder  differential operation on a vector $u$ at a given point in 3D with a 1D stencil size $k$ (number of points in the stencil) for every order, the subindex $i$ represent the dimension number $1$ for z, $2$ for $x$ and 3 for $y$,
First order :
$
\frac{d u}{dx_i}
$
Second order :
$
\frac{d^2 u}{dx_i^2}
$
Second order cross derivative
$
\frac{d^2 u}{dx_i dx_j}
$
Can you write Python code for it?

# Arithmetic operations
k = symbols('k')
s = symbols('s')
# 1D stencil
#                multiplication         addition
AI_dxi   =              k            +    k - 1
AI_dxxi  =           k + 1           +    k - 1
AI_dxxij =    (k+1)**2 - 2*k + 1 + 1 +   (k+1)**2 - 2*k + 1 -1

# I/O operations
#                    load
IO_dxi   =           k
IO_dxxi  =           k
IO_dxxij =    (k+1)**2 - 2*k + 1

# Operational intensity in sing;e precision

print(AI_dxi/(4*IO_dxi))
print(AI_dxxi/(4*IO_dxxi))
print(AI_dxxij/(4*IO_dxxij))

OI_dxi   = lambdify(k,AI_dxi/(4*IO_dxi))
OI_dxxi  = lambdify(k,AI_dxxi/(4*IO_dxxi))
OI_dxxij = lambdify(k,AI_dxxij/(4*IO_dxxij))


I am working on the file "AcousticFWI/Visco-Acoustic-wave-equation.ipynb".
The first task is:
Adjoint test
In ordr to guaranty we have the gradient we need to make sure that the solution of the adjoint wave equation is indeed the true adjoint. Tod os so one should check that
$ <Ax,y> - <x,A^Ty> = 0$
where $A$ is the wave_equation,  $A^T$ is wave_equationA and $x,y$ are any random vectors in the range of each operator. This can however be expensive as this two vector would be of size $N * n_t$. To test our operator we will the relax this test by
$ <P_r A P_s^T x,y> - <x,P_SA^TP_r^Ty> = 0$
where $P_r , P_s^T$ are the source and recevier projection operator mapping the source and receiver locations and times onto the full domain. This allow to have only a random source of size $n_t$ at a random postion.
Can you write Python code for it?

(rec0,u0)=Forward(nt,nx,ny,m0,rho)

(srca,v)=Adjoint(nt,nx,ny,m0,rho,rec0)

plts = []             # get ready to populate this list the Line artists to be plotted
plt.hold("off")
for i in range(0,nt):
    r = plt.imshow(v[i,:,:])   # this is how you'd plot a single line...
    plts.append( [r] )
ani = animation.ArtistAnimation(fig, plts, interval=50,  repeat = False)   # run the animation
#plt.show()

shotrec = plt.plot(srca)   # this is how you'd plot a single line...
plt.show()

# Actual adjoint test
term1=0
for ti in range(0,nt):
    term1=term1+srca[ti]*source(xsrc,ysrc,(ti)*tstep)

term2=LA.norm(rec0)**2

term1,term2,term1-term2,term1/term2

#if abs(term11/term21-1)<1e-9
#    print('Adjoint test passed')


I am working on the file "AcousticFWI/Visco-Acoustic-wave-equation.ipynb".
The first task is:
Create functions for the PDE
The Gradient/Born are here so that everything is at the correct place, it is described later
Can you write Python code for it?

def Forward(nt,nx,ny,m,rho):
    u=np.zeros((nt,nx,ny))
    rec=np.zeros((nt,ny-2))
    for ti in range(0,nt):
        for a in range(1,nx-1):
            for b in range(1,ny-1):
                src = source(xmin+a*hstep,ymin+b*hstep,tstep*ti)
                damp=dampx(a)+dampy(b)
                if ti==0:
                    u[ti,a,b]=ts(0,0,0,0,0,0,rho[a-1,b],rho[a,b],rho[a+1,b],rho[a,b-1],rho[a,b+1],
src,m[a,b],rho[a,b]**-1,tstep,hstep,damp)
                elif ti==1:
                    u[ti,a,b]=ts(0,u[ti-1,a-1,b],u[ti-1,a,b],u[ti-1,a+1,b],u[ti-1,a,b-1],u[ti-1,a,b+1],rho[a-1,b],rho[a,b],rho[a+1,b],rho[a,b-1],rho[a,b+1],
src,m[a,b],rho[a,b]**-1,tstep,hstep,damp)
                else:
                    u[ti,a,b]=ts(u[ti-2,a,b],u[ti-1,a-1,b],u[ti-1,a,b],u[ti-1,a+1,b],u[ti-1,a,b-1],u[ti-1,a,b+1],rho[a-1,b],rho[a,b],rho[a+1,b],rho[a,b-1],rho[a,b+1],
src,m[a,b],rho[a,b]**-1,tstep,hstep,damp)
                if a==xrec :
                    rec[ti,b-1]=u[ti,a,b]
    return rec,u

def Adjoint(nt,nx,ny,m,rho,rec):
    v=np.zeros((nt,nx,ny))
    srca=np.zeros((nt))
    for ti in  range(nt-1, -1, -1):
        for a in range(1,nx-1):
            for b in range(1,ny-1):
                if a==xrec:
                    resid=rec[ti,b-1]
                else:
                    resid=0
                damp=dampx(a)+dampy(b)
                if ti==nt-1:
                    v[ti,a,b]=tsA(0,0,0,0,0,0,rho[a-1,b],rho[a,b],rho[a+1,b],rho[a,b-1],rho[a,b+1],
resid,m[a,b],rho[a,b]**-1,tstep,hstep,damp)
                elif ti==nt-2:
                    v[ti,a,b]=tsA(0,v[ti+1,a-1,b],v[ti+1,a,b],v[ti+1,a+1,b],v[ti+1,a,b-1],v[ti+1,a,b+1],rho[a-1,b],rho[a,b],rho[a+1,b],rho[a,b-1],rho[a,b+1],
resid,m[a,b],rho[a,b]**-1,tstep,hstep,damp)
                else:
                    v[ti,a,b]=tsA(v[ti+2,a,b],v[ti+1,a-1,b],v[ti+1,a,b],v[ti+1,a+1,b],v[ti+1,a,b-1],v[ti+1,a,b+1],rho[a-1,b],rho[a,b],rho[a+1,b],rho[a,b-1],rho[a,b+1],
resid,m[a,b],rho[a,b]**-1,tstep,hstep,damp)
                if abs(xmin+a*hstep-xsrc)<hstep/2 and...


I am working on the file "AcousticFWI/Visco-Acoustic-wave-equation.ipynb".
The first task is:
Define the discrete model
Can you write Python code for it?

import matplotlib.pyplot as plt
from matplotlib import animation

hstep=25 #space increment d  = minv/(10*f0);
tstep=2 #time increment dt < .5 * hstep /maxv;
tmin=0.0 #initial time
tmax=600 #simulate until
xmin=-500.0 - 10*hstep #left bound
xmax=500.0 + 10*hstep #right bound...assume packet never reaches boundary
ymin=-500.0 - 10*hstep #left bound
ymax=500.0 + 10*hstep #right bound...assume packet never reaches boundary
f0=.010
t0=1/.010
nbpml=10
nx = int((xmax-xmin)/hstep) + 1 #number of points on x grid
ny = int((ymax-ymin)/hstep) + 1 #number of points on x grid
nt = int((tmax-tmin)/tstep) + 2 #number of points on t grid
xsrc=-400
ysrc=0.0
xrec = nbpml+4
#set source as Ricker wavelet for f0
def source(x,y,t):
    r = (np.pi*f0*(t-t0))
    val = (1-2.*r**2)*np.exp(-r**2)
    if abs(x-xsrc)<hstep/2 and abs(y-ysrc)<hstep/2:
        return val
    else:
        return 0.0

def dampx(x):
    dampcoeff=1.5*np.log(1.0/0.001)/(5.0*hstep);
    if x<nbpml:
        return dampcoeff*((nbpml-x)/nbpml)**2
    elif x>nx-nbpml-1:
        return dampcoeff*((x-nx+nbpml)/nbpml)**2
    else:
        return 0.0
def dampy(y):
    dampcoeff=1.5*np.log(1.0/0.001)/(5.0*hstep);
    if y<nbpml:
        return dampcoeff*((nbpml-y)/nbpml)**2
    elif y>ny-nbpml-1:
        return dampcoeff*((y-ny+nbpml)/nbpml)**2
    else:
        return 0.0

# Velocity and density models
def smooth10(vel,nx,ny):
    out=np.ones((nx,ny))
    out[:,:]=vel[:,:]
    for a in range(5,nx-6):
        out[a,:]=np.sum(vel[a-5:a+5,:], axis=0) /10
    return out

# True velocity
vel=np.ones((nx,ny)) + 2.0
rho=np.ones((nx,ny))
vel[floor(nx/2):nx,:]=4.5
rho[floor(nx/2):nx,:]=2.0
rho=rho**-1
mt=vel**-2
# Smooth velocity
v0=smooth10(vel,nx,ny)
m0=v0**-2

dm=m0-mt


I am working on the file "AcousticFWI/Visco-Acoustic-wave-equation.ipynb".
The first task is:
Gradient test
The last part is to check that the operators are consistent with the problem. There is then two properties to be satisfied
$ \Phi(m + hdm) = \Phi(m) +  \mathcal{O} (h) \
   \Phi(m + h dm) = \Phi(m) + h (J[m]^T\delta |d)dm + \mathcal{O} (h^2) $
which are the linearization conditions for the objective. This is a bit slow to run here but here is the way to test it.
1 - Genrate data for the true model m
2 - Define a smooth initial model $m_0$ and comput the data $d_0$ for this model
3 - You now have $\Phi(m_0)$
4 - Define $ dm = m-m_0$ and $ h = {1,.1,.01,.001,...}$
5 - For each $h$ compute $\Phi(m_0 + h dm)$ by generating data for $m_0 + h dm$ and compute $(J[m_0 + h dm]^T\delta |d) $
6 - Plot in Loglog the two lines of equation above
Can you write Python code for it?

(DT,uT)=Forward(nt,nx,ny,mt,rho)
(D1,u0)=Forward(nt,nx,ny,m0,rho)
F0=.5*LA.norm(D1-DT)**2

g=Gradient(nt,nx,ny,m0,rho,D1-DT,u0)
G=np.dot(g.reshape(-1),dm.reshape(-1));
error21=np.zeros((7))
error22=np.zeros((7))
for i in range(0,7):
    mloc=m0+H[i]*dm
    (D,u)=Forward(nt,nx,ny,mloc,rho)
    error21[i]  = .5*LA.norm(D-DT)**2 -F0
    error22[i]  = .5*LA.norm(D-DT)**2 -F0 - H[i]*G

shotrec = plt.loglog(H,error21,H,H)   # this is how you'd plot a single line...
plt.show()
shotrec = plt.loglog(H,abs(error22),H,hh) # this is how you'd plot a single line...
plt.show()

error22


I am working on the file "AcousticFWI/Visco-Acoustic-wave-equation.ipynb".
The first task is:
Jacobian test
The last part is to check that the operators are consistent with the problem. There is then two properties to be satisfied
$ U(m + hdm) = U(m) +  \mathcal{O} (h) \
   U(m + h dm) = U(m) + h J[m]dm + \mathcal{O} (h^2) $
which are the linearization conditions for the objective. This is a bit slow to run here but here is the way to test it.
1 - Genrate data for the true model m
2 - Define a smooth initial model $m_0$ and comput the data $d_0$ for this model
3 - You now have $U(m_0)$
4 - Define $ dm = m-m_0$ and $ h = {1,.1,.01,.001,...}$
5 - For each $h$ compute $U(m_0 + h dm)$ by generating data for $m_0 + h dm$ and compute $(J[m_0 + h dm]^T\delta |d) $
6 - Plot in Loglog the two lines of equation above
Can you write Python code for it?

H=[1,0.1,0.01,.001,0.0001,0.00001,0.000001]
(D1,u0)=Forward(nt,nx,ny,m0,rho)
dub=Born(nt,nx,ny,m0,rho,dm)
error1=np.zeros((7))
error2=np.zeros((7))
for i in range(0,7):
    mloc=m0+H[i]*dm
    (d,u)=Forward(nt,nx,ny,mloc,rho)
    error1[i]  = LA.norm(d - D1,ord=1)
    error2[i]  = LA.norm(d - D1 - H[i]*dub,ord=1)

hh=np.zeros((7))
for i in range(0,7):
    hh[i]=H[i]*H[i]
shotrec = plt.loglog(H,error1,H,H)   # this is how you'd plot a single line...
plt.show()
shotrec = plt.loglog(H,error2,H,hh)   # this is howyou'd plot a single line...
plt.show()


I am working on the file "AcousticFWI/Visco-Acoustic-wave-equation.ipynb".
The first task is:
Time and space  discretization as a Taylor expansion.
The time discretization is define as a second order ( $ O (dt^2)) $) centered finite difference to get an explicit Euler scheme easy to solve by steping in time.
$ \frac{d^2 u(x,t)}{dt^2} \simeq \frac{u(x,t+dt) - 2 u(x,t) + u(x,t-dt)}{dt^2} + O(dt^2) $
And we define the space discretization also as a Taylor serie, with oder chosen by the user. This can either be a direct expansion of the second derivative bulding the laplacian, or a combination of first oder space derivative. The second option can be a better choice in case you would want to extand the method to more complex wave equations involving first order derivatives in chain only.
$ \frac{d^2 u(x,t)}{dt^2} \simeq \frac{1}{dx^2} \sum_k \alpha_k (u(x+k dx,t)+u(x-k dx,t)) + O(dx^k) $
Can you write Python code for it?

dtt=as_finite_diff(p(x,y,t).diff(t,t), [t-s,t, t+s])
dt=as_finite_diff(p(x,y,t).diff(t), [t-s, t+s])
# Spacial finite differences can easily be extended to higher order by increasing the list of sampling point in the next expression.
# Be sure to keep this stencil symmetric and everything else in the notebook will follow.
dxx=as_finite_diff(p(x,y,t).diff(x,x), [x-h,x, x+h])
dyy=as_finite_diff(p(x,y,t).diff(y,y), [y-h,y, y+h])
dy=as_finite_diff(p(x,y,t).diff(y), [y-h, y+h])
dx=as_finite_diff(p(x,y,t).diff(x), [x-h, x+h])
dyr=as_finite_diff(b(x,y).diff(y), [y-h, y+h])
dxr=as_finite_diff(b(x,y).diff(x), [x-h, x+h])
dtt,dxx,dyy,dt,dx,dy

# In practice, compute rho. grad(rho) upfront (this is a constant) and just compute the following expression, it avoids
# the recomputation of the gradient at every time step. int the following X and Y are the component of rho.grad(rho) in X and Y respectively

gradgrad=X*dx+Y*dy
expand(gradgrad)

lap=(dxx+dyy)
expand(lap + gradgrad)


I am working on the file "Acoustics.ipynb".
The first task is:
In this chapter we consider our first system of hyperbolic conservation laws.  We study the acoustics equations that were introduced briefly in Introduction.  We first describe the physical context of this system and then investigate its characteristic structure and the solution to the Riemann problem.  This system is described in more detail in Chapter 3 of (LeVeque 2002).
If you wish to examine the Python code for this chapter, please see:

exact_solvers/acoustics.py ...
   on github,
exact_solvers/acoustics_demos.py ...
   on github.

Physical setting
The linear acoustic equations describe the propagation of small perturbations in a fluid.  In Advection we derived the one-dimensional continuity equation, which describes mass conservation:
\begin{align} \label{Ac:continuity}
    \rho_t + (\rho u)_x & = 0.
\end{align}
For more realistic fluid models, we need another equation that determines the velocity $u$.  This typically takes the form of a conservation law for the momentum $\rho u$.  Momentum, like density, is transported by fluid motion with corresponding flux $\rho u^2$.  Additionally, any difference in pressure will also lead to a flux of momentum that is proportional to the pressure difference.  Thus the momentum equation takes the form
\begin{align} \label{Ac:mom_cons}
(\rho u)_t + (\rho u^2 + P(\rho))_x & = 0,
\end{align}
where the pressure $P$ is is given by the equation of state $P(\rho)$; here we have assumed the pressure depends only on the density. A more general equation of state will be considered, along with fully nonlinear fluid motions, in Euler.  The linear acoustics equations focus on the behavior of small perturbations in the system above.
In order to derive the equations of linear acoustics, observe that equations (\ref{Ac:continuity})-(\ref{Ac:mom_cons}) form a hyperbolic system $q_t+f(q)_x=0$ with
\begin{align}
q & = \begin{bmatrix} \rho \ \rho u \end{bmatrix} &
f(q) & = \begin{bmatrix} \rho u \ \rho u^2 + P(\rho)...
Can you write Python code for it?

%matplotlib inline

%config InlineBackend.figure_format = 'svg'
import numpy as np
from exact_solvers import acoustics, acoustics_demos
from IPython.display import IFrame

acoustics_demos.decompose_q_interactive()


I am working on the file "Acoustics.ipynb".
The first task is:
One way to visualize the Riemann solution for a system of two equations is by looking at the  $p-u$ phase plane. In the figure below, we show the two initial conditions of the Riemann problem $q_\ell$ and $q_r$ as points in the phase space; the lines passing through these points correspond to the eigenvectors, $r_1$ and $r_2$.
The middle state $q_m$ is simply the intersection of the line in the direction $r_1$ passing through $q_\ell$ and the line in the direction $r_2$ passing through $q_r$. The structure of this solution becomes evident from equation (\ref{eq:acussol}).  The dashed lines correspond to a line in the direction $r_2$ passing through $q_\ell$ and a line in the direction $r_1$ passing through $q_r$; these also intersect, but cannot represent a Riemann solution since they would involve a wave going to the right but connected to $q_\ell$ and a wave going to the left but connected to $q_r$.
In the live notebook, the cell below allows you to interactively adjust the initial conditions the material parameters as well as the plot range, so that you can explore how the structure of the solution in the phase plane is affected by these quantities.
Can you write Python code for it?


I am working on the file "Acoustics.ipynb".
The first task is:
The three constant states are related by the jumps:
\begin{align}
q_m = q_\ell + \Delta w_1 r_1 = q_r - \Delta w_2 r_2.
\label{eq:acussol}
\end{align}
The jumps in pressure and velocity for each propagating discontinuity are related in a particular way, since each jump is a multiple of one of the eigenvectors of $A$.  More generally, the eigenvectors of the coefficient matrix of a linear hyperbolic system reveal the relation between jumps in the conserved variables across a wave propagating with speed given by the corresponding eigenvalue.  For acoustics, the impedance is the physical parameter that determines this relation.
A simple solution
Here we provide some very simple initial data, and determine the Riemann solution, which consists of three states $q_\ell$, $q_m$ and $q_r$, and the speeds of the two waves.
Can you write Python code for it?

# Initial data for Riemann problem
rho = 0.5               # density
bulk = 2.            # bulk modulus
ql = np.array([3,2])   # Left state
qr = np.array([3,-2])  # Right state
# Calculated parameters
c = np.sqrt(bulk/rho)  # calculate sound speed
Z = np.sqrt(bulk*rho)  # calculate impedance
print("With density rho = %g,  bulk modulus K = %g" \
      % (rho,bulk))
print("We compute: sound speed c = %g, impedance Z = %g \n" \
      % (c,Z))

# Call and print Riemann solution
states, speeds, reval = \
    acoustics.exact_riemann_solution(ql ,qr, [rho, bulk])

print("The states ql, qm and qr are: ")
print(states, "\n")
print("The left and right wave speeds are:")
print(speeds)


I am working on the file "Acoustics.ipynb".
The first task is:
We again show the Riemann solution in space and in the phase plane, where the symmetry is also evident.
Disregarding the left half of the domain ($x<0$), one can view this as a solution to the problem of an acoustic wave impacting a solid wall. The result is a reflected wave that moves away from the wall; notice that the velocity vanishes at the wall, as it must.
This type of Riemann solution is important when simulating waves in a domain with reflecting boundaries. The reflecting condition can be imposed by the use of fictitious ghost cells that lie just outside the domain and whose state is set by reflecting the interior solution with the symmetry just described (equal pressure, negated velocity).
In reality, at a material boundary only part of a wave is reflected while the rest is transmitted.  This can be accounted for by including the spatial variation in $\rho, K$ and solving a variable-coefficient Riemann problem.
Interactive phase plane with solution at fixed time
For a more general exploration of the solution to the acoustics equation, we now show an interactive solution of the acoustics equations. The initial states $q_\ell$ and $q_r$ can be modified by dragging and dropping the points in the phase plane plot (in the notebook version, or on this webpage).
Can you write Python code for it?

IFrame(src='phase_plane/acoustics_small_notitle.html',
       width=980, height=340)


I am working on the file "Acoustics_heterogeneous.ipynb".
The first task is:
Examples
We will show some examples of where this Riemann problem becomes relevant. As in the previous case, we will begin by defining a function to do the interactive plotting for the different cases.
Problem 3: Shock tube with interface
We repeat the shock tube problem for acoustics but now with two materials. The material properties in the acoustic equations are defined by the density and bulk modulus. Therefore, we can solve the acoustics Riemann problem for two materials by simply choosing different densities and bulk modulus and on the left and on the right. This Riemann problem is fundamental to model acoustic wave propagation across interfaces or heterogenous materials. Note the symmetry of the wave speeds is lost since the eigenvalues are the sound speed and the sound speed depends on the material, i.e. on the density and bulk modulus, like shown in equation (\ref{eq:achetero}). Also note the characteristics bend when crossing the origin. This is a consequence of having differente materials on the left and right sides since different materials will yield different sound speeds.
Can you write Python code for it?

ql = np.array([5,0])
qr = np.array([1,0])
rhol, rhor = 1.0, 20.0 # left and right density
bulkl, bulkr = 4.0, 15.0  # left and right bulk modulus
auxl = [rhol, bulkl]
auxr = [rhor, bulkr]
interact(acoustics_heterogeneous.riemann_plot_func(ql,qr,auxl, auxr), t=widgets.FloatSlider(value=0.0,min=0,max=1.0),
                    which_char=widgets.Dropdown(options=[None,1,2],description='Show characteristics'));


I am working on the file "Acoustics_heterogeneous.ipynb".
The first task is:
The solution in the phase plane is
Can you write Python code for it?


I am working on the file "Acoustics_heterogeneous.ipynb".
The first task is:
We would like to model acoustic waves propagation through heterogeneous media, like non-homogeneous materials, layered media or any kind of interface, like walls. The materials in acoustic equations are modeled by the density and bulk modulus coefficients. The natural generalization of constant-coefficient acoustic equations is to have spatially dependent coefficients, so the equations take the form $q_t+A(x)q_x=0$. The explicit form is given by
\begin{align}
 \left[ \begin{array}{c}
p \
u
\end{array} \right]_t
+ \left[ \begin{array}{cc}
0 & K(x) \
1/\rho(x) & 0  \
\end{array} \right]
\left[ \begin{array}{c}
p \
u \end{array} \right]_x = 0,
\end{align}
with $p$ and $u$ the pressure and velocity and $\rho(x)$ and $K(x)$ the spatially dependent density and bulk modulus of compressibility. Note this equation is in non-conservative form. An acoustics equation for heterogeneous media in conservative form can be derived in terms of the momentum and strain. However, that is a particular case of the elasticity equations, which are explored in detail in other sections of this book. Furthermore, it is convenient to use the pressure and velocity as variables since they are continuous at interfaces between materials, and they are also more physically intuitively. It is also important to recognize that more complicated systems emerging in applications might not be written in conservation form. Therefore, studying these problems might provide insight and algorithms on how to solve more complicated cases.
We proceed to do the usual analysis. The eigenvalues of the coefficient matrix $A$ are $\pm c(x)$, and the matrix of column eigenvalues is
\begin{align}
R(x) =
\left[ \begin{array}{ccccc}
-Z(x) & Z(x) \
 1   & 1  \
\end{array} \right].
\end{align}
where
\begin{align}
c(x) &= \sqrt{\frac{K(x)}{\rho(x)}}, \
Z(x) &= \rho(x) c(x) = \sqrt{K(x)\rho(x)}
\end{align}
are the spatially dependent sound speed and impedance.
In order to solve these equations, we need to do a numerical discretization. Following the spirit of finite...
Can you write Python code for it?

# Initial states [pressure, velocity]
ql = [25.0, 15.0]
qr = [10.0,-15.0]
# Acoustic eq. parameters [rho, bulk(K)]
paramsl = [1.0, 0.5]
paramsr = [5.0, 3.0]

acoustics_heterogeneous_demos.interactive_phase_plane(ql,qr,paramsl,paramsr)


I am working on the file "Acpype_API_Jupyter.ipynb".
The first task is:
For an example of ACPYPE amb2gmx feature:
Can you write Python code for it?

mol = MolTopol(acFileXyz="tests/glycam_exe.inpcrd", acFileTop="tests/glycam_exe.prmtop", debug=True, amb2gmx=True)

mol.writeGromacsTopolFiles()


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

n_experiments = 100  # @param
n_steps = 100  # @param
num_policies = 50  # @param
use_fqe = True  # @param
use_prior = False

if not use_fqe:
  use_prior = True

# GP optimizer
optimizer_config = dict(
    optimizer_name='Adam',
    learning_rate=0.001,
    steps_per_update=1000,
    kwargs=dict(
        beta1=0.9,
        beta2=0.999,
    ),
)

# Priors.
arm_kwargs = [{
    'prior_mean': 0.0,
    'prior_std': 1000.,
    'alpha': 1.,
    'beta': 1000.,
    'sample': False,
    'steps': 10,
    'burnin': 0
} for _ in range(num_policies)]
prior = dict(use_prior=use_prior, alpha=1., beta=200.)

sim_regrets = None
regrets_fqe = []


I am working on the file "Active_ops_experiment.ipynb".
The first task is:
Load information about the policies: their ids, FQE estimates and GT performance.
Can you write Python code for it?

with open('./data/ope_values.pkl', 'rb') as f:
  ope_values = pkl.load(f)


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

plt.rcParams.update(plt.rcParamsDefault)

plt.rc('font', size=8.0)
plt.rc('figure', figsize=(2, 1.5))
plt.rc('axes', linewidth=0.5, titlesize=6.0)
plt.rc('legend', fontsize=7.0, frameon=False)
plt.rc('lines', markersize=1, linewidth=0.5)
plt.rc('xtick', direction='out')
plt.rc('ytick', direction='out')

n_steps = len(sim_regrets['Ind+Uniform+OPE'][0])
line_colors = {
    'Ind+Uniform+OPE': plt.cm.Paired(3),
    'Ind+UCB+OPE': plt.cm.Paired(3),
    'GP+Uniform+OPE': plt.cm.Paired(9),
    'A-ops': plt.cm.Paired(9)
}
fill_colors = {
    'Ind+Uniform+OPE': plt.cm.Paired(2),
    'Ind+UCB+OPE': plt.cm.Paired(3),
    'GP+Uniform+OPE': plt.cm.Paired(8),
    'A-ops': plt.cm.Paired(9)
}
line_styles = {
    'Ind+Uniform+OPE': '--',
    'Ind+UCB+OPE': '-',
    'GP+Uniform+OPE': '--',
    'A-ops': '-'
}

fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1])

# Plot OPE performance with standard deviation
regret_fqe = np.mean(np.array(regrets_fqe))
regret_std = np.std(np.array(regrets_fqe))
plt.plot([0, n_steps - 1], [regret_fqe, regret_fqe],
         '-.',
         lw=1,
         color='black',
         label='OPE')
plt.fill_between([0, n_steps - 1],
                 [regret_fqe - regret_std / 2, regret_fqe - regret_std / 2],
                 [regret_fqe + regret_std / 2, regret_fqe + regret_std / 2],
                 color='k',
                 alpha=0.05,
                 linewidth=0.0)

# Plot agent's performance with standard deviation
for agent_name in sim_regrets:
  regret_agent = np.mean(np.array(sim_regrets[agent_name]), axis=0)
  regret_std = np.std(np.array(sim_regrets[agent_name]), axis=0)
  plt.plot(
      range(n_steps),
      regret_agent,
      line_styles[agent_name],
      lw=1,
      color=line_colors[agent_name],
      label=agent_name)
  plt.fill_between(
      range(n_steps),
      regret_agent - regret_std / 2,
      regret_agent + regret_std / 2,
      facecolor=fill_colors[agent_name],
      alpha=0.3,
      linewidth=0.0)

plt.xlabel('# trajectories')
plt.ylabel('simple...


I am working on the file "Active_ops_experiment.ipynb".
The first task is:
Run the experiment!
Please note that it may take several hours to run the full experiment. For experimentation purposes, in order to get the results faster, the number of runs and the number of steps may be reduced. To verify that the code runs you can start with 10 experiments (n_experiments) with 10 steps (n_steps).
Can you write Python code for it?

for _ in range(n_experiments):
  print(_)
  # Make a world with randomly selected policies.
  np.random.seed(seed=805 + _)
  # selected_policies_fqe = task_data.iloc[np.random.choice(len(task_data), num_policies, replace=False)]
  selected_policies_fqe = dict(
      random.sample(ope_values.items(), k=num_policies))

  world = bandit.MAB(selected_policies_fqe)
  world.load_reward_samples(full_reward_samples_dict)

  true_mean = np.array([np.mean(rs) for rs in world._rewards])
  best_mean = true_mean.max()

  # FQE instead of a-ops.
  regret_fqe = best_mean - true_mean[np.argmax(world.opes)]
  regrets_fqe.append(regret_fqe)

  # Independent arm model.

  model_ind = multiarm_model.IndependentMultiArmModel(
      world.num_arms,
      arm_model.SingleBayesArm,
      arm_args=None,
      arm_kwargs=arm_kwargs)
  if use_fqe:
    for a in range(len(world.opes)):
      model_ind.update(a, world.opes[a])

  # Kernel for GP.
  experiment_distances = get_experiment_distances(
      policies=selected_policies_fqe)
  kernel = kernel_classes.ActionDistanceMatern12(
      experiment_distances,
      lengthscale=np.median(experiment_distances),
      bias_variance=10.,
      variance_prior=prior)

  # GP as arm model.
  model_gp = multiarm_model.MVNormal(
      num_arms=world.num_arms,
      offset=0.,
      kernel=kernel,
      observation_noise_variance=1000.,
      optimizer_config=optimizer_config,
      observation_noise_variance_prior=prior)
  if use_fqe:
    for a in range(len(world.opes)):
      model_gp.update(a, world.opes[a])

  # Make agents.
  agent_ind_uniform = agent_classes.UniformAgent(copy.deepcopy(model_ind))
  agent_gp_uniform = agent_classes.UniformAgent(copy.deepcopy(model_gp))
  if use_fqe:
    agent_ind_ucb = agent_classes.UCBAgent(
        copy.deepcopy(model_ind), exploration_coef=5)
    agent_gp_ucb = agent_classes.UCBAgent(
        copy.deepcopy(model_gp), exploration_coef=5)
  # If no FQE is available at the start, first sample a few datapoints.
  else:
    agent_ind_ucb =...


I am working on the file "Activity05/.ipynb_checkpoints/Mushroom Edibility Classification - Naive Bayes Bernoulli-checkpoint.ipynb".
The first task is:
Added column names from the UCI documentation
Can you write Python code for it?

#manually add column names from documentation (1st col is class: e=edible,p=poisonous; rest are attributes)
df.columns = ['class','cap-shape','cap-surface','cap-color','bruises','odor','gill-attachment',
             'gill-spacing','gill-size','gill-color','stalk-shape','stalk-root',
             'stalk-surf-above-ring','stalk-surf-below-ring','stalk-color-above-ring','stalk-color-below-ring',
             'veil-type','veil-color','ring-number','ring-type','spore-color','population','habitat']

print("Example values:\n")
print(df.iloc[3984]) #this one has a ? value - how are those treated by classifier?


I am working on the file "Activity05/.ipynb_checkpoints/Mushroom Edibility Classification - Naive Bayes Bernoulli-checkpoint.ipynb".
The first task is:
Let's see  how well our classifier can identify poisonous mushrooms by combinations of features
Can you write Python code for it?

#put the features into X (everything except the 0th column)
X = pd.DataFrame(df, columns=df.columns[1:len(df.columns)], index=df.index)
#put the class values (0th column) into Y
Y = df['class']

#encode the class labels as numeric
from sklearn import preprocessing
le = preprocessing.LabelEncoder()
le.fit(Y)
#print(le.classes_)
#print(np.array(Y))
#Y values now boolean values; poison = 1
y = le.transform(Y)
#print(y_train)

#have to initialize or get error below
x = pd.DataFrame(X,columns=[X.columns[0]])

#encode each feature column and add it to x_train (one hot encoder requires numeric input?)
for colname in X.columns:
    le.fit(X[colname])
    #print(colname, le.classes_)
    x[colname] = le.transform(X[colname])

#encode the feature labels using one-hot encoding
from sklearn import preprocessing
oh = preprocessing.OneHotEncoder(categorical_features='all')
oh.fit(x)
xo = oh.transform(x).toarray()
#print(xo)

print('\nEncoder Value Counts Per Column:')
print(oh.n_values_)
print('\nExample Feature Values - row 1 in X:')
print(X.iloc[1])
print('\nExample Encoded Feature Values - row 1 in xo:')
print(xo[1])
print('\nClass Values (Y):')
print(np.array(Y))
print('\nEncoded Class Values (y):')
print(y)

#split the dataset into training and test sets
from sklearn.cross_validation import train_test_split
x_train, x_test, y_train, y_test = train_test_split(xo, y, test_size=0.33)

#initialize and fit the naive bayes classifier
from sklearn.naive_bayes import BernoulliNB
skbnb = BernoulliNB()
skbnb.fit(x_train,y_train)
train_predict = skbnb.predict(x_train)
#print(train_predict)

#see how accurate the training data was fit
from sklearn import metrics
print("Training accuracy:",metrics.accuracy_score(y_train, train_predict))

#use the trained model to predict the test values
test_predict = skbnb.predict(x_test)
print("Testing accuracy:",metrics.accuracy_score(y_test, test_predict))

print("\nClassification Report:")
print(metrics.classification_report(y_test, test_predict,...


I am working on the file "Activity05/.ipynb_checkpoints/Mushroom Edibility Classification - Naive Bayes Bernoulli-checkpoint.ipynb".
The first task is:
The dataset doesn't include column names, and the values are text characters
Can you write Python code for it?

#read in data. it's comma-separated with no column names.
df = pd.read_csv('agaricus-lepiota.data', sep=',', header=None,
                 error_bad_lines=False, warn_bad_lines=True, low_memory=False)
# set pandas to output all of the columns in output
pd.options.display.max_columns = 25
#show the first 5 rows
print(df.sample(n=5))


I am working on the file "Activity05/.ipynb_checkpoints/Mushroom Edibility Classification - Naive Bayes Bernoulli-checkpoint.ipynb".
The first task is:
The dataset is split fairly evenly between the edible and poison classes
Can you write Python code for it?

#show plots in notebook
%matplotlib inline

#bar chart of classes using pandas plotting
print(df['class'].value_counts())
#df['class'].value_counts().plot(kind='bar')


I am working on the file "Activity05/Mushroom Edibility Classification - Naive Bayes Bernoulli.ipynb".
The first task is:
The dataset is split fairly evenly between the edible and poison classes
Can you write Python code for it?


I am working on the file "Activity05/Mushroom Edibility Classification - Naive Bayes.ipynb".
The first task is:
Added column names from the UCI documentation
Can you write Python code for it?


I am working on the file "Activity05/Mushroom Edibility Classification - Naive Bayes.ipynb".
The first task is:
Edibility by Mushroom Gill Color
note that buff gills (b) appear to always indicate poison, and the others aren't as clear-cut
Can you write Python code for it?

g = sns.factorplot("class", col="gill-color", data=df_forplot,
                    kind="count", size=2.5, aspect=.8, col_wrap=6)


I am working on the file "Activity05/Mushroom Edibility Classification - Naive Bayes.ipynb".
The first task is:
Let's see  how well our classifier can identify poisonous mushrooms by combinations of features
Can you write Python code for it?

#put the features into X (everything except the 0th column)
X = pd.DataFrame(df, columns=df.columns[1:len(df.columns)], index=df.index)
#put the class values (0th column) into Y
Y = df['class']

#encode the text category labels as numeric
from sklearn import preprocessing
le = preprocessing.LabelEncoder()
le.fit(Y)
#print(le.classes_)
#print(np.array(Y))
#Y values now boolean values; poison = 1
y = le.transform(Y)
#print(y_train)

#have to initialize or get error below
x = pd.DataFrame(X,columns=[X.columns[0]])
#encode each feature column and add it to x_train
for colname in X.columns:
    le.fit(X[colname])
    print(colname, le.classes_)
    x[colname] = le.transform(X[colname])

print('\nExample Feature Values - row 1 in X:')
print(X.iloc[1])
print('\nExample Encoded Feature Values - row 1 in x:')
print(x.iloc[1])
print('\nClass Values (Y):')
print(np.array(Y))
print('\nEncoded Class Values (y):')
print(y)

#split the dataset into training and test sets
from sklearn.cross_validation import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.33)

#initialize and fit the naive bayes classifier
from sklearn.naive_bayes import GaussianNB
skgnb = GaussianNB()
skgnb.fit(x_train,y_train)
train_predict = skgnb.predict(x_train)
#print(train_predict)

#see how accurate the training data was fit
from sklearn import metrics
print("Training accuracy:",metrics.accuracy_score(y_train, train_predict))

#use the trained model to predict the test values
test_predict = skgnb.predict(x_test)
print("Testing accuracy:",metrics.accuracy_score(y_test, test_predict))

print("\nClassification Report:")
print(metrics.classification_report(y_test, test_predict, target_names=['edible','poisonous']))
print("\nConfusion Matrix:")
skcm = metrics.confusion_matrix(y_test,test_predict)
#putting it into a dataframe so it prints the labels
skcm = pd.DataFrame(skcm, columns=['predicted-edible','predicted-poisonous'])
skcm['actual'] = ['edible','poisonous']
skcm = skcm.set_index('actual')

#NOTE: NEED TO...


I am working on the file "Activity05/Mushroom Edibility Classification - Naive Bayes.ipynb".
The first task is:
The dataset doesn't include column names, and the values are text characters
Can you write Python code for it?


I am working on the file "Activity05/Mushroom Edibility Classification - Naive Bayes.ipynb".
The first task is:
The dataset is split fairly evenly between the edible and poison classes
Can you write Python code for it?

#show plots in notebook
%matplotlib inline

#bar chart of classes using pandas plotting
print(df['class'].value_counts())
df['class'].value_counts().plot(kind='bar')


I am working on the file "Adaboost/Adaboost_Final note.ipynb".
The first task is:
As discussed, this time around the dim = 169.
We have imported the csv module to open the image.
Can you write Python code for it?

temp = np.zeros(N, dtype=np.int64)

# Returns error and calculated labels corresponding to

def weakClassifier_error(i, j, k, x, weight, label):
                                                # threshold i
                                                # dimension j
                                                # sign k on dataset x.
                                                # Original labels are stored in
                                                # label

    j_row = j / 13
    j_col = j % 13
    temp_err = np.float64(0)
    # Initialise actual and expected labels to a perfect match( 0 = match , 1
    # = not a match)
    y = np.zeros(N, dtype=np.int64)

    if(k == 1):
        temp = (x[j_row, j_col:13000:13] >= i)
    else:
        temp = (x[j_row, j_col:13000:13] < i)

    temp = np.int64(temp)
    temp[np.where(temp == 0)] = -1
    y = np.int64(temp != label)
    # Calculate error of this weak classifier on the weighted dataset
    temp_err = np.sum(y * weight)

    return [temp_err, y]


I am working on the file "Adaboost/Adaboost_Final note.ipynb".
The first task is:
Compare this codeblock with the code for our toy example. You will notice that the only difference is that we have an additional for p in range(10).
So basically, because now we have to classify digits, we will build a seperate strong classifiers for each digit from 0 to 9. That is why we are running our Adaboost algorithm 10 times.
In each of theses iterations, we are storing the strong classifier in the arrays final_alpha and final_h.
Can you write Python code for it?

with open('images_training.txt', 'rb') as csvfile:
    reader = csv.reader(csvfile, delimiter=',', quotechar='|')
    x = list(reader)

x = np.array(x, dtype=np.float64)

temp_sum = np.zeros((10, N), dtype=np.float64)
temp = np.zeros(N, dtype=np.float64)
final_label = np.zeros((10, N), dtype=np.float64)
misshits = np.zeros(T)

for p in range(10):
    label[100 * p: 100 * p + 100] = p

label = np.int64(label)
all_label = np.full(N, -1, dtype=np.int64)

for t in range(T):  # Calculate final labels
    for p in range(10):
        row = final_h[p][t][1] / 13
        col = final_h[p][t][1] % 13
        temp = final_h[p][t][2] * \
            np.sign(x[row, col: 13000: 13] - final_h[p][t][0])
        temp_sum[p] = np.float64(temp_sum[p] + final_alpha[p][t] * temp)
        final_label[p] = np.sign(temp_sum[p])
    for p in range(10):
        all_label[np.where(final_label[p] == 1)] = p
    misshits[t] = np.sum(np.float64(all_label != label)) / N

plt.figure()
plt.plot(misshits)
plt.ylabel('Miss hists')
plt.show()

print("--- %s seconds ---" % (time.time() - start_time))


I am working on the file "Adaboost/Adaboost_Final note.ipynb".
The first task is:
Now we can finally take a look at the actual code for the Adaboost algorithm.
We define some variables that we need:
-> h: Tx3 array that stores the weak classifiers selected after each iteration:
       h[index][0]= threshold
       h[index][1]= dim (data dimension)
       h[index][2]= pos (the sign of the classifier, +1/-1)
-> alpha: T x 1 array that stores the weight of each weak classifier chosen to
            make up the final classifier.
The point of 'h' is obvious. However, 'alpha' needs an explanation:
We said that the final classifier will be a combination of all the weak classifiers that we have selected over the iterations.
But what does combination mean exactly?
In Adaboost, the final classifier is a weighted sum of the weak classifiers that we have selected.
Now don't get confused: we have talked about weights before. We assigned weights to each datapoint, over each iteration, so that we could select a new classifier. These weigths are stored in weights[].
And the error rate was a weighted sum of the weights.
What's beautiful about Adaboost is that it generates the final output in an analogous manner.
The weigths we are talking about here are assigned to the weak classifiers themselves.
These weights are stored in alpha[].
The final output is the weighted sum of the outputs of each weak classifier.
Alright: so now we have two sets of weights: One set for the datapoints and one set for the weak classifiers themselves.
How are these weights determined?
We said that the weigths corresponding to the datapoints were incremented or decremented based on whether or not they were correctly classified,  by the best weak classifier of that iteration.
How is this done mathematically?
First we calculate alpha[t] (refer to the code). Then we use it to reassign weights.
The expression for alpha[t] isn't very revealing at first glance. But it is what it is, and it works. We will try and take a look at it in more detail later.
The observant reader will immediately point out and maybe puzzled by the fact...
Can you write Python code for it?

# Actual program begins

# h and alpha together completely specify the final strong classifier
h = np.zeros([T, 3], dtype=np.float64)
alpha = np.zeros(T, dtype=np.float64)

threshold = np.arange(-3.0, 3.0, 0.1)

weight = np.ones(N, dtype=np.float64) / (N)  # Initialise weights

# Initially set error to infinity, to allow comparing with error of classifiers
err = np.ones(T, dtype=np.float64) * np.inf

for t in range(T):
    for i in threshold:
        for j in range(dim):
            for k in [-1, 1]:
                [tmpe, y] = weakClassifier_error(i, j, k, x, weight, label)
                if(tmpe < err[t]):  # storing the better classifier in h
                    err[t] = tmpe
                    y0 = y
                    h[t][0] = i
                    h[t][1] = j
                    h[t][2] = k

    if(err[t] > 0.5):
        T = t
        # We have run out of weak classifiers! So truncate the no: of
        # iterations used
        print t, "Error!"
        break

    alpha[t] = 0.5 * np.log((1.0 - err[t]) / err[t])

    # y0=0 corresponded to correctly labelled datapoints. To reassign weights,
    y0[np.where(y0 == 0)] = -1
    # we need -1 and not 0 at these positions

    weight = np.float64(weight * np.exp(alpha[t] * y0))  # Reassign weights

    weight = weight / np.sum(weight)  # Normalise reassigned weights


I am working on the file "Adaboost/Adaboost_Final note.ipynb".
The first task is:
That's all of it.
We have described all the mathematical details of Adaboost through our code. But with math comes proofs, and we haven't actually proven the convergence of the Adaboost algorithm. Neither have we derived the expression for alpha[t].
Proving convergence is pretty difficult. But you must know that the adaboost algorithm is guaranteed to converge to a strong classifier with an arbitrarily good success rate, given enough iterations(and some other caveats).
For some intuition into where that weird expression of alpha came from, refer to this link.
Application of Adaboost: Digit recognition
We have learnt how Adaboost works and have implemented the code for it as well.
With a few modifications, our code can be tailored into a full-blown application of Adaboost in a real and relevant problem; that of digit recognition. The problem is simple to state: create a program that takes an image of a digit from 0-9 as input and gives as output, the digit in the picture.
So how can we use Adaboost to solve this problem?
First, some specifics: The 'data' that we will use comprises of images that are 13 pixels wide and 13 pixels tall i.e 13x13. The training set will have 1000 images, as will the testing/validation set. Each image is stored as a 13x13 matrix. Each element of this matrix is a number from 0 to 1. Can you guess what the numbers correspond to? As is obvious, they represent the intensity of light in that particular pixel. Meaning that a value of zero corresponds to a black pixel and a value of 1 is a white pixel and any value in between will be varying shades of grey. This is the greyscale representation of an image.
The data files can be found here.
The next step is to define our weak classifiers. How do we do that?
Well, in our implementation, we will make an obvious choice: Each image comprises 13x13= 169 pixels. We have stored a greyscale for each pixel.
So the index for the weak classifier will simply be the index of the pixel that we are looking at. The weak classifier's threshold will be a...
Can you write Python code for it?

"""
All matrices used are implemented via numpy.
The following variables are used:
-> N: The number of samples or data-points.
-> T: The number of iterations in our boosting algorithm.
-> dim: The number of parameters recorded for each data-point.
        (for an image we can choose RGB intensities as features and then dim=3)
-> x: The data. It is an N x dim matrix.
-> label: N x 1 array that stores the known labels for each data-point.
-> final_label: Nx1 array that stores the labels generated for each data-point
                by the final strong classifier.
-> weight: Nx1 array that stores the weight for each data-point.
-> h: Tx3 array that stores the weak classifiers selected after each iteration:
       h[index][0]= threshold
       h[index][1]= dim (data dimension)
       h[index][2]= pos (the sign of the classifier, +1/-1)
-> alpha: T x 1 array that stores the weight of each weak classifier chosen to
            make up the final classifier.
-> final_alpha: Stores the weights for all the digits.
-> final_h: Stores the classifiers for all the digits.

"""
import numpy as np
import matplotlib.pyplot as plt
import time
import csv
start_time = time.time()

with open('images_training.txt', 'rb') as csvfile:
    reader = csv.reader(csvfile, delimiter=',', quotechar='|')
    x = list(reader)

x = np.array(x, dtype=np.float64)
# this array is of size 13x13000, for all the 1000 13x13 images 100 for
# each digit 0 to 9

T = 20
dim = 169
N = 1000


I am working on the file "Adaboost/Adaboost_Final note.ipynb".
The first task is:
We discussed how a classifier can be defined by selecting a parameter and a threshold for this parameter.
The output of this classifier that we have defined would be +1 or -1 depending on whether the input's value for this parameter is greater than or smaller than the threshold, as we have discussed before.
But we had also talked about generating an error rate for weak classifiers in every iteration, on the weighted dataset.
The following lines of code define a function weak_Classifier_error() that takes a weak classifier, a labelled and weighted dataset as input.
It's output is the error rate for this weak classifier on the dataset for the given weights.
And the error rate, as mentioned before, just a weighted sum of the errors.
The weigths are: 0 if it is correctly classified and 1 if incorrectly classified.
This simplfies to simply:
Error rate= Sum of weigths of datapoints that were classified incorrectly
But wait you say: The arguments for this function are threshold, dimension, sign, weight and label. Where is the weak classifier? Remember, how we said that a classifier is uniquely defined by it's parameter and threshold values? So we just input these.
Also, the use of 'sign' is simple: It basically flips the classifier on it's head. If sign is +1, then it will classify data as +1 if it is greater than threshold. If sign is -1, it will classify data as +1 if it is smaller than threshold.
Can you write Python code for it?

temp = np.zeros(N, dtype=np.int64)

# Returns error and calculated labels corresponding to
def weakClassifier_error(i, j, k, x, weight, label):
                                                # threshold i
                                                # dimension j
                                                # sign k on dataset x.
                                                # Original labels are stored in
                                                # label

    temp_err = np.float64(0)
    # Initialise actual and expected labels to a perfect match( 0 = match , 1
    # = not a match)
    y = np.zeros(N, dtype=np.int64)

    if(k == 1):
        temp = (x[:, j] >= i)
    else:
        temp = (x[:, j] < i)

    temp = np.int64(temp)
    temp[np.where(temp == 0)] = -1
    y = np.int64(temp != label)
    # Calculate error of this weak classifier on the weighted dataset
    temp_err = np.sum(y * weight)

    return [temp_err, y]


I am working on the file "Adaboost/Adaboost_Final note.ipynb".
The first task is:
We have finished selecting all the weak classifiers and their corresponding weights. This is all that we need to generate the final output.
In our code, we are not feeding it a new dataset. Instead, we are inputting the training dataset itself. This is an excellent way to see if our algorithm actually works. Because we can compare the original labels directly with the generated lables.
Alright, so we now proceed to generate the final output. We said that the final output is the weighted sum of the outputs of all the weak classifiers. The weights, as we know, are stored in alpha[]
We then proceed to plot the generated labels also.
We also plot 'misshits', which is a measure of how the accuracy of our final classifier improves when we add the weak classifiers one by one.
Can you write Python code for it?

temp_sum = np.zeros(N, dtype=np.float64)
temp = np.zeros(N, dtype=np.float64)
final_label = np.zeros(N, dtype=np.float64)
misshits = np.zeros(T)

for t in range(T):  # Calculate final labels
    temp = h[t][2] * np.sign(x[:, h[t][1]] - h[t][0])
    temp_sum = np.float64(temp_sum + alpha[t] * temp)
    final_label = np.sign(temp_sum)
    misshits[t] = np.sum(np.float64(final_label != label)) / N

# Now plot the generated labels
pos1 = np.where(final_label == 1)
pos2 = np.where(final_label == -1)

plt.figure()
plt.plot(x[pos1, 0], x[pos1, 1], 'b*')
plt.plot(x[pos2, 0], x[pos2, 1], 'r*')
plt.axis([-3, 3, -3, 3])
plt.title("Generated data")
plt.show()

# Plot miss hits when more and more weak learners are used
plt.figure()
plt.plot(misshits)
plt.ylabel('Miss hists')

print("--- %s seconds ---" % (time.time() - start_time))


I am working on the file "Adaline.ipynb".
The first task is:
Classificação
Porta AND/OR
Can you write Python code for it?


I am working on the file "Adaline.ipynb".
The first task is:
Exercício de Classificação
Can you write Python code for it?

x, y = make_blobs(n_samples=100, n_features=2, centers=2, random_state=1234)

print(x.shape, y.shape)
plt.scatter(x[:,0], x[:,1], c=y.ravel(), cmap='bwr')

def plot_linear_classifier(x, y, w, b):
    x1_min, x1_max = x[:,0].min(), x[:,0].max()
    x2_min, x2_max = x[:,1].min(), x[:,1].max()

    x1, x2 = np.meshgrid(np.linspace(x1_min-1, x1_max+1,100), np.linspace(x2_min-1, x2_max+1, 100))
    x_mesh = np.array([x1.ravel(), x2.ravel()]).T

    plt.scatter(x[:,0], x[:,1], c=y.ravel(), cmap='bwr')

    y_mesh = np.dot(x_mesh, np.array(w).reshape(1, -1).T) + b
    y_mesh = np.where(y_mesh < 0.5, 0, 1)

    plt.contourf(x1, x2, y_mesh.reshape(x1.shape), cmap='bwr', alpha=0.5)
    plt.xlim(x1_min-1, x1_max+1)
    plt.ylim(x2_min-1, x2_max+1)


I am working on the file "Adaline.ipynb".
The first task is:
Introdução
Poucos meses após a publicação do teorema da convergência do Perceptron por Rosenblatt, os engenheiros da Universidade de Stanford, Bernard Widrow e Marcian Hoff, publicaram um trabalho descrevendo uma rede neural muito parecida com o Perceptron, a Adaline (do inglês ADAptive LINear Element). Porém, ao invés de utilizar a função step como função de ativação, a Adaline utiliza função de ativação linear e tem uma nova regra de aprendizado supervisionado, conhecida como regra de Widrow-Hoff (ou regra delta, ou ainda regra LMS).
De fato, tanto o Perceptron quanto o Adaline possuem muitas características semelhantes e é comum ver o pessoal confundindo o Perceptron com o Adaline. Entre as principais semelhanças, podemos destacar:
- Ambos possuem apenas um neurônio de N entradas e apenas uma saída. Não há camadas escondidas.
- Ambos são classificadores lineares binários por definição, mas podemos adaptá-los para efetuar regressão linear, da mesma forma como vimos no notebook sobre o Perceptron. Na verdade, o código para treinar um Adaline para regressão é o mesmo de um Perceptron.
- Ambos tem o método de aprendizagem online. Isto é, a atualização dos pesos é efetuada amostra por amostra.
- Ambos tem uma função step para classificação. Porém, ao contrário do Perceptron, na Adaline ela não é utilizada na atualização dos pesos. Nós veremos por que a seguir.
Porém, a principal diferença entre o Perceptron e a Adaline é que o Perceptron utiliza os labels das classes para fazer a atualização dos pesos, enquanto a Adaline utiliza o resultado da função de ativação (linear) como valor contínuo de predição. Isto é, ao invés da saída ser discreta como no Perceptron (0 ou 1), na Adaline a saída pode ser qualquer valor contínuo. Essa diferença fica mais clara quando vemos a figura a seguir:
<img src="images/comparacao_perceptron_adaline.png">
Fonte
Repare, como dito, que ambos têm a função step. No Perceptron, ela é utilizada como função de ativação. No Adaline, por sua vez, a função de ativação é linear e a funcão...
Can you write Python code for it?

df = pd.read_csv('data/notas.csv')

print(df.shape)
df.head(10)

x = df[['prova1', 'prova2', 'prova3']].values
y = df['final'].values.reshape(-1, 1)

print(x.shape, y.shape)

minmax = MinMaxScaler(feature_range=(-1,1))
x = minmax.fit_transform(x.astype(np.float64))

D = x.shape[1]
w = [2*random() - 1 for i in range(D)]
b = 2*random() - 1

learning_rate = 1e-2

for step in range(2001):
    cost = 0
    for x_n, y_n in zip(x, y):
        y_pred = sum([x_i*w_i for x_i, w_i in zip(x_n, w)]) + b
        error = y_n - y_pred
        w = [w_i + learning_rate*error*x_i for x_i, w_i in zip(x_n, w)]
        b = b + learning_rate*error
        cost += error**2

    if step%200 == 0:
        print('step {0}: {1}'.format(step, cost))

print('w: ', w)
print('b: ', b)


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

D = x.shape[1]
w = 2*np.random.random(size=D)-1
b = 2*np.random.random()-1

learning_rate = 1.0 # <- use a mesma learning rate do python

for step in range(1): # <- use a mesma #epochs do python
    cost = 0
    for x_n, y_n in zip(x, y):
        y_pred = np.dot(x_n, w) + b
        error = y_n - y_pred
        w = w + learning_rate*np.dot(error, x_n)
        b = b + learning_rate*error
        cost += error**2

    if step%100 == 0:
        print('step {0}: {1}'.format(step, cost))

print('w: ', w)
print('b: ', b)

plot_linear_classifier(x, y, w, b)


D = x.shape[1]
w = 2*np.random.random(size=D)-1
b = 2*np.random.random()-1

learning_rate = 1.0 # <- use a mesma learning rate do python

for step in range(101):
    cost = 0
    for x_n, y_n in zip(x, y):
        # qual linha devemos remover para transformar o Perceptron num Adaline?
        y_pred = np.dot(x_n, w) + b
        y_pred = np.where(y_pred > 0, 1, 0)
        error = y_n - y_pred
        w = w + learning_rate*np.dot(error, x_n)
        b = b + learning_rate*error
        cost += error**2

    if step%10 == 0:
        print('step {0}: {1}'.format(step, cost))

print('w: ', w)
print('b: ', b)
print('y_pred: {0}'.format(np.dot(x, w)+b))


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

D = x.shape[1]
w = [2*random() - 1 for i in range(D)]
b = 2*random() - 1

learning_rate = 1.0 # <- tente estimar a learning_rate

for step in range(1): # <- tente estimar a #epochs
    cost = 0
    for x_n, y_n in zip(x, y):
        y_pred = sum([x_i*w_i for x_i, w_i in zip(x_n, w)]) + b
        error = y_n - y_pred
        w = [w_i + learning_rate*error*x_i for x_i, w_i in zip(x_n, w)]
        b = b + learning_rate*error
        cost += error**2

    if step%100 == 0:
        print('step {0}: {1}'.format(step, cost))

print('w: ', w)
print('b: ', b)

plot_linear_classifier(x, y, w, b)


I am working on the file "Adding Context to Word Frequency Counts.ipynb".
The first task is:
Another feature of the NLTK frequency distribution function is the generation of a list of hapaxes. These are words that appear only once in the entire corpus. While not meaningful for this study, it's an interesting way to explore the data.
Can you write Python code for it?


I am working on the file "Adding Context to Word Frequency Counts.ipynb".
The first task is:
Finally, we will use the NLTK module to create a graph that shows the top 50 most frequent words in the Hansard corpus. Although privacy will not appear in the graph, it's always interesting to see what types of words are most common, and what their distribution is. NLTK will be introduced with more detail in the next section featuring concordance outputs, but here all we need to know is that we assign our variable large to the NLTK function Text in order to work with the corpus data. From there we can determine the frequency distribution for the whole text.
Can you write Python code for it?

text = nltk.Text(large)
fd = nltk.FreqDist(text)


I am working on the file "Adding Context to Word Frequency Counts.ipynb".
The first task is:
Here is a screenshot of some of the raw data. We can see that there are 97 files in 2006, 117 in 2007 and 93 in 2008. The rest of the data is also correct.

Now we can compare the amount of occurences of privacy with the number of debates occuring in each dataset.
Can you write Python code for it?

for names, files, words in zip(filenames, corpus_1, corpus):
    print("In", names, "there were", len(files), "debates. The word privacy was said", \
    count_in_list('privacy', words), "times.")


I am working on the file "Adding Context to Word Frequency Counts.ipynb".
The first task is:
Here we recreate our list from the last exercise, counting the instances of the word privacy in each file.
Can you write Python code for it?

for words, names in zip(corpus, filenames):
    print("Instances of the word \'privacy\' in", names, ":", count_in_list("privacy", words))


I am working on the file "Adding Context to Word Frequency Counts.ipynb".
The first task is:
Here we will assign the frequency distribution to the plot function to produce a graph. While it's a little hard to read, the most commonly used word in the Hansard corpus is the, with a frequency just over 400,000 occurences. The next most frequent word is to, which only has a frequency of about 225,000 occurences, almost half of the first most common word. The first 10 most frequent words appear with a much greater frequency than any of the other words in the corpus.
Can you write Python code for it?


I am working on the file "Adding Context to Word Frequency Counts.ipynb".
The first task is:
Next we use the len function to count the total number of words in each file.
Can you write Python code for it?

for files, names in zip(corpus, filenames):
    print("There are", len(files), "words in", names)


I am working on the file "Adding Context to Word Frequency Counts.ipynb".
The first task is:
Now we can calculate the ratio of the word privacy to the total number of words in the file. To accomplish this we simply divide the two numbers.
Can you write Python code for it?

print("Ratio of instances of privacy to total number of words in the corpus:")
for words, names in zip(corpus, filenames):
    print('{:.6f}'.format(float(count_in_list("privacy", words))/(float(len(words)))),":",names)


I am working on the file "Adding Context to Word Frequency Counts.ipynb".
The first task is:
Now we can divide the types by the tokens to determine the ratio.
Can you write Python code for it?

print("The type/token ratio is:", ('{:.6f}'.format(len(set(large))/(float(len(large))))), "or",\
'{:.3f}'.format(len(set(large))/(float(len(large)))*100),"%")


I am working on the file "Adding Context to Word Frequency Counts.ipynb".
The first task is:
Now, we can count the number of files in each dataset. This is also an important activity for error-checking. While it is easy to trust the numerical output of the code when it works sucessfully, we must always be sure to check that the code is actually performing in exactly the way we want it to. In this case, these numbers can be cross-referenced with the original XML data, where each transcript exists as its own file. A quick check of the directory shows that the numbers are correct.
Can you write Python code for it?

for files, names in zip(corpus_1, filenames):
    print("There are", len(files), "files in", names)


I am working on the file "Adicionais/derivada.ipynb".
The first task is:
Exercício
Calcule:
$\frac{d^3}{d x^3}\left(\frac{x^2 - 1 + \text{sen}\,x}{x^3 - 3x + 1}\right)$.
Can you write Python code for it?


I am working on the file "Adicionais/derivada.ipynb".
The first task is:
Exercício
Referente ao problema de otimização que acabamos de discutir, responda:
(a) Os pontos $x_1$, $x_2$ e $x_3$ calculados acima são os únicos pontos críticos de $f(x)$? Justifique sua resposta.
(b) Quais são os pontos de mínimo e máximo globais de $f(x)$?
(c) Qual é o maior valor que $f(x)$ assume? E qual é o menor?
Can you write Python code for it?


I am working on the file "Adicionais/derivada.ipynb".
The first task is:
Exercício:
Sendo $g(x) = x^2 + \frac{1}{2}$ calcule $g'(1)$.
Can you write Python code for it?


I am working on the file "Adicionais/derivada.ipynb".
The first task is:
Observando os gráficos, vemos que os pontos críticos de $f(x)$ têm máximos locais próximos ao ponto $x=-1$ e $x=10$ e um mínimo local próximo do ponto $x=1$. Logo, podemos usar estes valores como parâmetro da função $\verb+nsolve+$:
Can you write Python code for it?

fl = diff(f(x),x) #calcula $f'(x)$
x1 = nsolve(fl,x,-1) #calcula ponto crítico próx. de x=-1
x2 = nsolve(fl,x,1) #calcula ponto crítico próx. de x=1
x3 = nsolve(fl,x,10) #calcula ponto crítico próx. de x=10
print("x1=",x1)
print("x2=",x2)
print("x3=",x3)


I am working on the file "Adicionais/derivada.ipynb".
The first task is:
Para fixar as ideias, vamos continuar trabalhando com a função:
$f(x) = (x^3 - 3x + 2)e^{-x/4} - 1$.
Can you write Python code for it?


I am working on the file "Adicionais/derivada.ipynb".
The first task is:
Retas Tangente
Aqui, vamos ver como calcular a reta tangente ao gráfico da função $f(x)$ no ponto $x_0 = -\frac{1}{2}$. Lembremos que tal reta terá equação:
$y = f'(x_0)(x - x_0) + f(x_0)$.
Desta forma, vamos calcular $f'(x_0)$:
Can you write Python code for it?

x0 = -1/2
fl = diff(f(x),x).subs(x,-0.5) #f'(x_0)
print("coef. angular = ")
fl


I am working on the file "Adicionais/derivada.ipynb".
The first task is:
Vejamos os gráficos de $f(x)$ e da reta tangente calculada.
Can you write Python code for it?

%matplotlib inline
p1 = plot(f(x),(x,-2,2),show=False,line_color='b')
p2 = plot(r(x),(x,-1.5,1),show=False,line_color='r')
p1.extend(p2)
p1.show()


I am working on the file "AdmissionsProject/Part2/starter-code/Flautt-project2-submission.ipynb".
The first task is:
Question 12. What did you find?
Answer:
    The strongest, most interesting correlation that exists between two variables exist between variables 'admit' and 'prestige'. The two variables are negatively correlated (-0.241). This would imply that as the prestige of your school increases by one unit your likelyhood of admission to UCLA decreases by a factor of -0.241 (or -25%), holding all other variables constant.
    GPA and GRE variables are positively correlated in that as your GPA increases, your GRE score increases by a factor of 0.382408.
Question 13. Write an analysis plan for exploring the association between grad school admissions rates and prestige of  undergraduate schools.
Answer:
    I will examine the relationship between variables 'prestige' and 'admit' using the admissions.csv database in order to determine if the two varibles are correlated and if they are causally linked. Further, I will determine if this relationship is statistically significant.
Question 14. What is your hypothesis?
Answer:
H1 = There exists a statistically significant relationship between undergraduate school prestige ('prestige') and admission ('admit').
H0 = There exists an insignificant relationship between variables of undergraduate school prestige ('prestige') and admission ('admit').
Bonus/Advanced
1. Bonus: Explore alternatives to dropping obervations with missing data
2. Bonus: Log transform the skewed data
3. Advanced: Impute missing data
Can you write Python code for it?

#utilized this stackoverflow.com resource to attempt to impute missing data
#(http://stackoverflow.com/questions/21050426/pandas-impute-nans)

#data imputation for variable 'admit'

#first commented out line of code will not run. Had errors with "keys" in ...df_raw.groupby('keys')...
#df_raw['admit'].fillna(df_raw.groupby('keys')['admit'].transform('mean'), inplace = True)
df_raw['admit'].fillna(df_raw['admit'].mean(), inplace = True)


I am working on the file "AdmissionsProject/Part2/starter-code/Flautt-project2-submission.ipynb".
The first task is:
Question 7. What do this plots show?
Answer:
GRE Boxplot:
    The mean for this variable lies just south of 600 (around 580) and the interquartile range lies between 650 and 510 as indicated by the blue square. The box plot displays a significant outlier at 300 which has not been included into the range as it falls well outside the acceptable standard deviation from the mean. Further, this value is below the lower extreme of variable GPA.
GPA Boxplot:
    The mean GPA value falls right at ~3.40 with the interquartile range falling between ~3.64 at the upper quartile and ~3.18 at the lower quartile. The lower extreme of this data is right at 2.4 while the upper extreme extends beyond 4.00 despite the maximum of this data being 4.00.
Question 8. Describe each distribution
Can you write Python code for it?

# distribution plot of 'admit' variable with mean
df_raw.admit.plot(kind = 'density', sharex = False, sharey = False, figsize = (10,4));plt.legend(loc='best')
#

plt.vlines(df_raw.admit.mean(),     # Plot black line at mean
           ymin=0,
           ymax=2.0,
           linewidth=4.0)

# distribution plot of 'gre' variable with mean
df_raw.gre.plot(kind = 'density', sharex = False, sharey = False, figsize = (10,4));plt.legend(loc='best')
#

plt.vlines(df_raw.gre.mean(),     # Plot black line at mean
           ymin=0,
           ymax=0.0035,
           linewidth=4.0)

# distribution plot of 'gpa' variable with mean
df_raw.gpa.plot(kind = 'density', sharex = False, sharey = False, figsize = (10,4));plt.legend(loc='best')
#

plt.vlines(df_raw.gpa.mean(),     # Plot black line at mean
           ymin=0,
           ymax=1.0,
           linewidth=4.0)

# distribution plot of 'prestige' variable with mean
df_raw.prestige.plot(kind = 'density', sharex = False, sharey = False, figsize = (10,4));plt.legend(loc='best')
#

plt.vlines(df_raw.prestige.mean(),     # Plot black line at mean
           ymin=0,
           ymax=0.6,
           linewidth=4.0)


I am working on the file "AdmissionsProject/Part2/starter-code/Flautt-project2-submission.ipynb".
The first task is:
Step 2: Read in your data set
Can you write Python code for it?

#Read in data from source
df_raw = pd.read_csv("../assets/admissions.csv")
print df_raw.head()


I am working on the file "AdmissionsProject/Part3/starter-code/Flautt-project3-submission.ipynb".
The first task is:
2.2 When modeling our class variables, how many do we need?
Answer: When modeling dummy or class variables in general, n-1 variables are required. We assume prestige value '4' to be the base case as this, comparatively, the lowest ranked. For modeling purposes of this model, we will need three (3) class variables
Part 3. Hand calculating odds ratios
Develop your intuition about expected outcomes by hand calculating odds ratios.
Can you write Python code for it?

cols_to_keep = ['admit', 'gre', 'gpa']
handCalc = df[cols_to_keep].join(dummy_ranks.ix[:, 'prestige_1':])
print handCalc.head()

# crosstab prestige 1 admission
# frequency table cutting prestige and whether or not someone was admitted
print handCalc.columns
pd.crosstab(handCalc['prestige_1.0'],handCalc['admit'])


I am working on the file "AdmissionsProject/Part3/starter-code/Flautt-project3-submission.ipynb".
The first task is:
3.1 Use the cross tab above to calculate the odds of being admitted to grad school if you attended a #1 ranked college
Can you write Python code for it?


I am working on the file "AdmissionsProject/Part3/starter-code/Flautt-project3-submission.ipynb".
The first task is:
3.6 Calculate the OR
3.7 Write this finding in a sentence
Answer: For every 12 times an prestige_4.0 individual gets into UCLA, an individual from prestige_4.0 will not get accepted 114 times.
Part 4. Analysis
Can you write Python code for it?

# create a clean data frame for the regression
cols_to_keep = ['admit', 'gre', 'gpa']
data = df[cols_to_keep].join(dummy_ranks.ix[:, 'prestige_2':])
print data.head()


I am working on the file "AdmissionsProject/Part3/starter-code/Flautt-project3-submission.ipynb".
The first task is:
4.1 Set the covariates to a variable called train_cols
Can you write Python code for it?

train_cols = ['prestige_2.0','prestige_3.0','prestige_4.0','gre', 'gpa']


I am working on the file "AdmissionsProject/Part3/starter-code/Flautt-project3-submission.ipynb".
The first task is:
4.2 Fit the model
Can you write Python code for it?

data_predictors = data[train_cols]
print data_predictors.head(2)
logit = sm.Logit(data['admit'], data_predictors)
results = logit.fit()


I am working on the file "AdmissionsProject/Part3/starter-code/Flautt-project3-submission.ipynb".
The first task is:
4.3 Print the summary results
Can you write Python code for it?


I am working on the file "AdmissionsProject/Part3/starter-code/Flautt-project3-submission.ipynb".
The first task is:
4.4 Calculate the odds ratios of the coeffiencents and their 95% CI intervals
hint 1: np.exp(X)
hint 2: conf['OR'] = params
       conf.columns = ['2.5%', '97.5%', 'OR']
Can you write Python code for it?

print np.exp(-0.9562)
print np.exp(-1.5375)
print np.exp(-1.8699)
print np.exp(0.0014)
print np.exp(-0.1323)


I am working on the file "AdmissionsProject/Part3/starter-code/Flautt-project3-submission.ipynb".
The first task is:
4.5 Interpret the OR of Prestige_2
Answer: Prestige_2.0 decreases your odds by 0.38 or a probability of 20 percent (p=0.38/0.83+1). This is statistically significant due to the fact that 0 is not included in the CI.
4.6 Interpret the OR of GPA
Answer: GPA decreases your odds but we cannot say that it is statistically significant becayse the CI encompases 0
Part 5: Predicted probablities
As a way of evaluating our classifier, we're going to recreate the dataset with every logical combination of input values. This will allow us to see how the predicted probability of admission increases/decreases across different variables. First we're going to generate the combinations using a helper function called cartesian (above).
We're going to use np.linspace to create a range of values for "gre" and "gpa". This creates a range of linearly spaced values from a specified min and maximum value--in our case just the min/max observed values.
Can you write Python code for it?

def cartesian(arrays, out=None):
    """
    Generate a cartesian product of input arrays.
    Parameters
    ----------
    arrays : list of array-like
        1-D arrays to form the cartesian product of.
    out : ndarray
        Array to place the cartesian product in.
    Returns
    -------
    out : ndarray
        2-D array of shape (M, len(arrays)) containing cartesian products
        formed of input arrays.
    Examples
    --------
    >>> cartesian(([1, 2, 3], [4, 5], [6, 7]))
    array([[1, 4, 6],
           [1, 4, 7],
           [1, 5, 6],
           [1, 5, 7],
           [2, 4, 6],
           [2, 4, 7],
           [2, 5, 6],
           [2, 5, 7],
           [3, 4, 6],
           [3, 4, 7],
           [3, 5, 6],
           [3, 5, 7]])
    """

    arrays = [np.asarray(x) for x in arrays]
    dtype = arrays[0].dtype

    n = np.prod([x.size for x in arrays])
    if out is None:
        out = np.zeros([n, len(arrays)], dtype=dtype)

    m = n / arrays[0].size
    out[:,0] = np.repeat(arrays[0], m)
    if arrays[1:]:
        cartesian(arrays[1:], out=out[0:m,1:])
        for j in xrange(1, arrays[0].size):
            out[j*m:(j+1)*m,1:] = out[0:m,1:]
    return out

# instead of generating all possible values of GRE and GPA, we're going
# to use an evenly spaced range of 10 values from the min to the max
gres = np.linspace(data['gre'].min(), data['gre'].max(), 10)
print gres
# array([ 220.        ,  284.44444444,  348.88888889,  413.33333333,
#         477.77777778,  542.22222222,  606.66666667,  671.11111111,
#         735.55555556,  800.        ])
gpas = np.linspace(data['gpa'].min(), data['gpa'].max(), 10)
print gpas
# array([ 2.26      ,  2.45333333,  2.64666667,  2.84      ,  3.03333333,
#         3.22666667,  3.42      ,  3.61333333,  3.80666667,  4.        ])

# enumerate all possibilities
combos = pd.DataFrame(cartesian([gres, gpas, [1, 2, 3, 4], [1.]]))


I am working on the file "AdmissionsProject/Part3/starter-code/Flautt-project3-submission.ipynb".
The first task is:
5.2 Make predictions on the enumerated dataset
Can you write Python code for it?

logit = sm.Logit(training_data['admit'], pred_cols)


I am working on the file "AdmissionsProject/Part3/starter-code/Flautt-project3-submission.ipynb".
The first task is:
Part 1. Frequency Tables
1. Let's create a frequency table of our variables
Can you write Python code for it?

# frequency table for prestige and whether or not someone was admitted
print df.columns # finding columns for dataframe
pd.crosstab(df['admit'],df['prestige']) # displaying var presitge over var admit in a pandas crosstab function


I am working on the file "AdmissionsProject/Part3/starter-code/Flautt-project3-submission.ipynb".
The first task is:
Part 2. Return of dummy variables
2.1 Create class or dummy variables for prestige
Can you write Python code for it?

# creating dummy variables for var 'prestige' using pandas get_dummies function
dummy_ranks = pd.get_dummies(df['prestige'],prefix = 'prestige')
print dummy_ranks.head(5)
# still need to append these dummy values to df using .join function; see part 3


I am working on the file "AdmissionsProject/Part3/starter-code/Flautt-project3-submission.ipynb".
The first task is:
We're going to add a constant term for our Logistic Regression. The statsmodels function we're going to be using requires that intercepts/constants are specified explicitly.
Can you write Python code for it?

# manually add the intercept
data['intercept'] = 1


I am working on the file "Adult Dataset - Income Prediction.ipynb".
The first task is:
As we thought, our model seems to have overfit, but the accuracy is actually pretty good. However, while our accuracy rate is close to our target of 85.95%, it is still lower. Let's view classification metrics:
Can you write Python code for it?

predictions = rf.predict(X_test_selected)
plot_confusion_matrix(confusion_matrix(y_test, predictions))

print(classification_report(y_test, predictions))


I am working on the file "Adult Dataset - Income Prediction.ipynb".
The first task is:
Data Preprocessing
We get our raw data then standardize continuous variables and dummy code categorical variables.
Can you write Python code for it?

headers = ['Age', 'Work Class', 'Final Weight', 'Education', 'Education-Num', 'Marial Status', 'Occupation', 'Relationship', 'Race', 'Sex', 'Capital Gain', 'Capital Loss', 'Hours per week', 'Native Country', 'Income']

raw_train_df = pd.read_csv('http://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data', names = headers)

raw_train_df.head()

raw_test_df = pd.read_csv('http://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.test', names = headers, header=0)

raw_test_df.head()

raw_train_df.info()

raw_test_df.info()


I am working on the file "Adult Dataset - Income Prediction.ipynb".
The first task is:
Modeling
Based on the documentation, the best result generated by prior studies is an error rate of 14.05 (FSS Naive Bayes), which would be equivalent to an accuracy of 85.95. My goal will be to beat this accuracy rate.
Can you write Python code for it?

rf.fit(X_train_selected, y_train)

rf.score(X_train_selected, y_train)


I am working on the file "Adult Dataset - Income Prediction.ipynb".
The first task is:
No missing data. We can now dummy-code categorical and scale continuous variables.
Can you write Python code for it?

from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
continuous_columns = list(raw_train_df.select_dtypes(include=['int64']).columns)
train_df = raw_train_df
train_df[continuous_columns] = scaler.fit_transform(raw_train_df[continuous_columns])
test_df = raw_test_df
test_df[continuous_columns] = scaler.transform(raw_test_df[continuous_columns])

train_df = pd.get_dummies(train_df, drop_first=True)
test_df = pd.get_dummies(test_df, drop_first=True)

train_df.columns.difference(test_df.columns)

test_df['Native Country_ Holand-Netherlands'] = 0

train_df.head()

test_df.head()


I am working on the file "Adult Dataset - Income Prediction.ipynb".
The first task is:
Now we can prepare the train and test sets:
Can you write Python code for it?

X_train = train_df.drop('Income_ >50K', axis=1)
X_test = test_df.drop('Income_ >50K.', axis=1)
y_train = train_df['Income_ >50K']
y_test = test_df['Income_ >50K.']


I am working on the file "Adult Dataset - Income Prediction.ipynb".
The first task is:
Our cross-validation scores are already better than our previous model. Let's check classification metrics:
Can you write Python code for it?

plot_confusion_matrix(confusion_matrix(y_test, revised_rf.predict(X_test_selected)))

print(classification_report(y_test, revised_rf.predict(X_test_selected)))


I am working on the file "Adult Dataset - Income Prediction.ipynb".
The first task is:
With a recall for the 'Income >50K' class of 0.61, it is obvious that our model is too lax on declaring that one is in the negative class. Let's do a 10-fold cross validation on the training data.
Can you write Python code for it?

from sklearn.model_selection import cross_val_score

crossval = cross_val_score(rf, X_train_selected, y_train, cv=10)
print('Cross-validation scores:', crossval, '\n')
print('Mean cross validation score:', np.mean(crossval))
print('Cross validation standard deviation:', np.std(crossval))