I am working on the file "2D.ipynb".
The first task is:
Are we dealing with form or metric information here?
Can you write Python code for it?

matplotlib.__version__

from matplotlib.cm import magma, inferno, plasma, viridis
# from colormaps import magma, inferno, plasma, viridis

correlation = data['correlation']
excitation = data['excitation energy']
emission = data['emission energy']

plt.imshow(correlation,
           origin='lower',
           cmap=magma)

plt.imshow(correlation,
           origin='lower',
           extent=[excitation.min(), excitation.max(), emission.min(), emission.max()],
           cmap=magma)

f, ax = plt.subplots(1, 1, figsize=(5, 5))
map0 = ax.imshow(correlation,
          origin='lower',
          extent=[excitation.min(), excitation.max(), emission.min(), emission.max()],
          cmap=magma)
ax.plot(excitation, excitation)
ax.set_xlabel('Excitation Energy (eV)')
f.colorbar(map0, ax=ax)


I am working on the file "2D.ipynb".
The first task is:
Revise and edit to best convey the scientific result.
Can you write Python code for it?

f, ax = plt.subplots(1, 1, figsize=(5, 5))
ax.imshow(correlation,
          origin='lower',
          extent=[excitation.min(), excitation.max(), emission.min(), emission.max()],
          cmap=viridis)
ax.plot(excitation, excitation)
ax.set_xlabel('Excitation Energy (eV)')


I am working on the file "2DRodFF_1.ipynb".
The first task is:
4.1.1 Visualizing the section force orbits
We may choose a parameter $\theta$ to traverse the orbit. Then, denoting the first constant as $\mathcal{H}$ and the second as $F^2$, invariant equations are equivalent to
$$N = F \cos\theta$$
$$Q = -F \sin\theta$$
$$M = \pm\sqrt{2EI (\mathcal{H} - F \cos\theta)}$$
The expression for the moment will fail when the root is less than 0. Therefore, we may distinguish the following situations:
1. $\mathcal{H} > |F|$. The orbit will be a closed one with moments of equal sign all along it.
2. $\mathcal{H} = |F|$.
3. $\mathcal{H} < |F|$. The values of $\theta$ are in the interval $(\arccos\frac{\mathcal{H}}{F}, -\arccos\frac{\mathcal{H}}{F})$. We are traversing the orbit in decreasing $\theta$ sense; this chioce determines the side of the (buckled) structure.
Can you write Python code for it?

EI = 5000.   #kN m^2

H = 3600.    #kN m/m
F1 = -2600.   #kN
F2 = -3600.   #kN
F3 = -4600.   #kN

phi = np.linspace(np.pi, -np.pi, 501)
theta0 = np.arccos(H/F3)
phi3 = np.linspace(theta0, - theta0, 501)

N1 = F1*np.cos(phi)
Q1 = -F1*np.sin(phi)
M1 = -np.sqrt(2*EI*(H - N1)) # sign is consistent with the choice of sense for theta

N2 = F2*np.cos(phi)
Q2 = -F2*np.sin(phi)
M2 = -np.sqrt(2*EI*(H - N2))

N3 = F3*np.cos(phi3)
Q3 = -F3*np.sin(phi3)
M3 = -np.sqrt(2*EI*(H - N3))

fig = plt.figure(figsize=(9., 9.))

ax = fig.gca(projection='3d')
ax.plot(N1, Q1, M1, color='r')
ax.plot(N1, Q1, -M1, color='r')
ax.plot(N2, Q2, M2, color='g')
ax.plot(N2, Q2, -M2, color='g')
ax.plot(N3, Q3, M3, color='b')
ax.plot(N3, Q3, -M3, color='b')

ax.set_xlabel('$N$')
ax.set_ylabel('$V$')
ax.set_zlabel('$M$')


I am working on the file "2DRodFF_1.ipynb".
The first task is:
4.1.2 Numerical solution of the equilibrium equations
According to Hairer et al. (2006), p. 247 Lie-Poisson systems may be numerically solved by using splitting methods if the Hamiltonian can be splitted into independent functions (which is indeed the case for the Kirchhoff rod). The resulting method is:
$$N_{n+1} = N_n \cos(h(M_n - h Q_n/4)/EI) + Q_n \sin(h(M_n - h Q_n/4)/EI)$$
$$Q_{n+1} = Q_n \cos(h(M_n - h Q_n/4)/EI) - N_n \sin(h(M_n - h Q_n/4)/EI)$$
$$M_{n+1} = M_n - \frac{h}{2} (Q_n + Q_{n+1})$$
Pinned rod
Let's consider the case of a pinned rod subject to end forces of modulus $F$.
At the start of the rod $M = 0$ and $N^2 + Q^2 = F^2$. Thus, we have the freedom to prescribe the value of the first invariant. Once this is made, the orbit -and also the start orientation of the rod $\theta_0$- is defined and therefore $(N, Q, M)$ triplets on the orbit may be computed by the numerical method introduced above.
We will experiment with the corresponding values for the third case, taking $h = 0.1$
Can you write Python code for it?

print('F= {0:.0f} kN,    H = {1:.0f} kN m/m,    theta_0 = {2:.5f} rad'.format(F3, H, theta0))

sectionForces = []
N0 = F3*np.cos(theta0)
Q0 = -F3*np.sin(theta0)
M0 = 0.
sectionForces.append([N0, Q0, M0])

nEdges = 50
h = 0.1

Nn = N0
Qn = Q0
Mn = M0
for n1 in range(1, nEdges+1):
    Nn1 = Nn*np.cos(h*(Mn - h*Qn/4)/EI) + Qn*np.sin(h*(Mn - h*Qn/4)/EI)
    Qn1 = Qn*np.cos(h*(Mn - h*Qn/4)/EI) - Nn*np.sin(h*(Mn - h*Qn/4)/EI)
    Mn1 = Mn - 0.5*h*(Qn + Qn1)
    sectionForces.append([Nn1, Qn1, Mn1])
    Nn = Nn1
    Qn = Qn1
    Mn = Mn1

axialForce = np.array([force[0] for force in sectionForces])
shearForce = np.array([force[1] for force in sectionForces])
bendingMoment = np.array([force[2] for force in sectionForces])
H = bendingMoment**2/2/EI + axialForce

fig = plt.figure(figsize=(9., 9.))

ax = fig.gca(projection='3d')
ax.plot(N3, Q3, M3, color='b')
ax.plot(N3, Q3, -M3, color='b')
ax.scatter(axialForce, shearForce, bendingMoment, color='r')
ax.set_xlabel('$N$')
ax.set_ylabel('$Q$')
ax.set_zlabel('$M$')

fig = plt.figure(figsize=(7., 7.))

ax = fig.gca()
ax.plot(np.linspace(0, h*nEdges, nEdges+1), H)
ax.set_xlabel('$s$')
ax.set_ylabel('$\mathcal{H}$')


I am working on the file "2DRodFF_1.ipynb".
The first task is:
4.2 Spatial case
The above equations may be expressed in terms of the components of the section forces and moments
- $\frac{1}{2}\Bigl( \frac{1}{GJ}(T)^2 + \frac{1}{EI_2}(M_2)^2 + \frac{1}{EI_3} (M_3)^2\Bigr) + N = constant$
- $N^2 + (Q_2)^2 + (Q_3)^2 = constant$
- $N\,T + Q_2\,M_2 + Q_3\,M_3 = constant$
5 Kinematic relationships
We refer to the field of Discrete Diferential Geometry. Hoffmann, 2008 introduces the following concepts and definitions:

A discrete curve in $\mathbb{R}^n$ is a map $\gamma: I \in \mathbb{Z} \rightarrow \mathbb{R}^n$
The edge tangent vector is defined as the forward difference $\Delta\gamma_k := \gamma_{k+1} - \gamma_k$
A discrete curve is called parametrized by arc-length if $\|\Delta\gamma_k\| \equiv h = const. \neq 0$

5.1 Planar case
For planar curves we may work in the complex plane, i.e. $\gamma: I \in \mathbb{N} \rightarrow \mathbb{R}^2 \cong \mathbb{C}$.

The vertex tangent vector of a planar discrete curve is given by...
The curvature of a arc-length parametrized (planar) discrete curve $\gamma$ is given by $\kappa = \frac{2}{h} \tan\frac{\phi}{2}$
The curvature of a general planar discrete curve $\gamma$ is defined at every vertex as $\kappa_n = \frac{4}{h_{n-1}+h_n} \tan\frac{\phi_n}{2}$

The key result for us is the following:
The discrete curvature function $\kappa$ determines a parametrized discrete curve up to an Euclidean motion. The rotation at every vertex relates the normalized edge segments
$$\frac{\Delta\gamma_n}{h_n} = \frac{\Delta\gamma_{n-1}}{h_{n-1}} e^{i \phi_n}$$
The curve is then determined by the recurrent relation
$$\gamma_{n+1} = \gamma_n + \frac{h_n}{h_{n-1}} \Delta\gamma_{n-1} \, e^{i \phi_n}$$
Note that the rotation operator transforming the direction of the discrete curve at every vertex can be expresed in terms of the curvature at the vertex through the following (exact) quotient
$$e^{i \phi_n} = \frac{1 + i \tan(\phi_n/2)}{1 - i \tan(\phi_n/2)} = \frac{\frac{4}{h_{n-1}+h_n} + i \kappa_n}{\frac{4}{h_{n-1}+h_n} - i \kappa_n}$$
With this...
Can you write Python code for it?

kappa = M / EI
phi = np.zeros(nEdges)
rotor = np.zeros(nEdges) + 1j*np.zeros(nEdges)
for n in range(1, nEdges):
    phi[n] = 2.*np.arctan(kappa[n]*(h[n-1] + h[n])/4.)
    rotor[n] = (4./(h[n-1] + h[n]) + 1j * kappa[n])/(4./(h[n-1] + h[n]) - 1j * kappa[n])
np.sum(phi)/2

phi[0] = theta0
rotor[0] = np.exp(1j*phi[0])

gamma = np.zeros(len(kappa)) + 1j*np.zeros(len(kappa))
gamma[0] = 0.+0j
gamma[1] = gamma[0] + h[0]*rotor[0]
for n in range(1, len(kappa)-1):
    gamma[n+1] = gamma[n] + h[n]/h[n-1] * (gamma[n] - gamma[n-1]) * rotor[n]

fig = plt.figure(figsize=(9,9))

ax = fig.gca(aspect='equal')
ax.plot(gamma.real, gamma.imag, color='b')
ax.set_xlabel('$x$')
ax.set_ylabel('$y$')

print('Total rotation at the orbit: {:.5f}*pi rad'.format((theta[-1] - theta[0])/np.pi))
print('Total rotation computed with DDG: {:.5f}*pi rad'.format((np.sum(phi) - phi[0])/np.pi))


I am working on the file "2DRodFF_1.ipynb".
The first task is:
It is easy to observe that the solution is "rotated" compared to the expected one. The reason of this behavior is that no rotation has been considered at the start node, but there is rotation caused by the non-zero curvature at the start section. Let's modify the code to fix this issue. For this purpose we will add to the initial (reference) angle, half of the angle corresponding to the curvature at the start section, and we will take $h_0$ as averaging length for the curvature
$$\kappa_0 = \frac{2}{h_0} \tan\biggl(\frac{\phi_0^}{2}\biggr)$$
$$\Delta\phi_0 = \frac{\phi_0^}{2} = \arctan\biggl(\frac{h_0}{2} \kappa_0\biggl)$$
Can you write Python code for it?

EI = 5000.   #kN m^2

H = 3600.    #kN m/m
F = -3500.    #kN

nEdges = 50
nVertex = nEdges + 1

if F > H:
    theta0 = np.arccos(H/F)
else:
    theta0 = np.pi
theta = np.linspace(theta0, -theta0, nVertex)

N = F*np.cos(theta)
Q = -F*np.sin(theta)
M = -np.sqrt(2*EI*(H - N))

h = np.zeros(nEdges)
h[:] = -2*(M[1:] - M[0:-1])/(Q[0:-1] + Q[1:])

kappa = M / EI
phi = np.zeros(nVertex-1)
rotor = np.zeros(nVertex-1) + 1j*np.zeros(nVertex-1)

phi[0] = theta0 + np.arctan(h[0]*kappa[0]/2)                  # modified code line
phi[1:] = 2.*np.arctan(kappa[1:-1]*(h[0:-1] + h[1:])/4.)

rotor[0] = np.exp(1j*phi[0])
rotor[1:] = (4./(h[0:-1] + h[1:]) + 1j * kappa[1:-1])/(4./(h[0:-1] + h[1:]) - 1j * kappa[1:-1])

gamma = np.zeros(nVertex) + 1j*np.zeros(nVertex)
gamma[0] = 0.+0j
gamma[1] = gamma[0] + h[0]*rotor[0]
for n in range(1, nVertex-1):
    gamma[n+1] = gamma[n] + h[n]/h[n-1] * (gamma[n] - gamma[n-1]) * rotor[n]

fig = plt.figure(figsize=(9,9))

ax = fig.gca(aspect='equal')
ax.plot(gamma.real, gamma.imag, color='b')
ax.set_xlabel('$x$')
ax.set_ylabel('$y$')

print('The y coordinate at the end section is now  y = {:.7f} m'.format(gamma[-1].imag))


I am working on the file "2DRodFF_1.ipynb".
The first task is:
Let's pack the code into a single tool and do a new experiment:
Can you write Python code for it?

EI = 5000.   #kN m^2

H = 3600.    #kN m/m
F = -4600.    #kN

nEdges = 200
nVertex = nEdges + 1

theta0 = np.arccos(H/F)
theta = np.linspace(theta0, -theta0, nVertex)

N = F*np.cos(theta)
Q = -F*np.sin(theta)
M = -np.sqrt(2*EI*(H - N))

h = np.zeros(nEdges)
h[:] = -2*(M[1:] - M[0:-1])/(Q[0:-1] + Q[1:])    # pythonic looping

print('The length of the rod is {:.3f} m'.format(np.sum(h)))

kappa = M / EI
phi = np.zeros(nVertex-1)
rotor = np.zeros(nVertex-1) + 1j*np.zeros(nVertex-1)

phi[0] = theta0
phi[1:] = 2.*np.arctan(kappa[1:-1]*(h[0:-1] + h[1:])/4.)

rotor[0] = np.exp(1j*phi[0])
rotor[1:] = (4./(h[0:-1] + h[1:]) + 1j * kappa[1:-1])/(4./(h[0:-1] + h[1:]) - 1j * kappa[1:-1])

gamma = np.zeros(nVertex) + 1j*np.zeros(nVertex)
gamma[0] = 0.+0j
gamma[1] = gamma[0] + h[0]*rotor[0]
for n in range(1, nVertex-1):
    gamma[n+1] = gamma[n] + h[n]/h[n-1] * (gamma[n] - gamma[n-1]) * rotor[n]

fig = plt.figure(figsize=(9,9))

ax = fig.gca(aspect='equal')
ax.plot(gamma.real, gamma.imag, color='b')
ax.set_xlabel('$x$')
ax.set_ylabel('$y$')

print('Total rotation at the orbit: {:.5f}*pi rad'.format((theta[-1] - theta[0])/np.pi))
print('Total rotation computed with DDG: {:.5f}*pi rad'.format((np.sum(phi) - phi[0])/np.pi))


I am working on the file "2DRodFF_1.ipynb".
The first task is:
The experiment succeeds; if we examin the result for $h$, we see that every formula produces exactly the same results in each step. We have a straightforward and simple method to compute section forces and rod length for a prescribed traction and Hamiltonian.
Can you write Python code for it?

h = []
for n in range(nEdges):
    hn = -2*(M[n+1] - M[n])/(Q[n] + Q[n+1])
    h.append(hn)

print('The length of the rod is {:.3f} m'.format(np.sum(h)))


I am working on the file "2DRodFF_1.ipynb".
The first task is:
The result shows that this numerical method does not preserve the Hamiltonian. Therefore, we try now with the implicit midpoint rule, which according to Hairer et al. (2006), p. 247 preserves quadratic invariants
$$N_{n+1} = N_n + \frac{h}{4EI} (Q_n + Q_{n+1})(M_n + M_{n+1})$$
$$Q_{n+1} = Q_n - \frac{h}{4EI} (N_n + N_{n+1})(M_n + M_{n+1})$$
$$M_{n+1} = M_n - \frac{h}{2} (Q_n + Q_{n+1})$$
Rearranging and dividing the first equation by the second one yields $N_{n+1}^2 + Q_{n+1}^2 = N_n^2 + Q_n^2$ (preservation of the Casimir). Substituting the third into the first shows the preservation of the Hamiltonian.
The drawback is that we end up with an implicit scheme in which the $n+1$ variables can't be isolated. In the next experiment we will advance each step using scipy.optimize.root non-linear solver.
Can you write Python code for it?

import scipy.optimize

def implicitMidpoint(Xn1, Xn, h, EI):
    f = np.empty(3)
    f[0] = Xn1[0] - Xn[0] - h*(Xn[1] + Xn1[1])*(Xn[2] + Xn1[2])/4./EI
    f[1] = Xn1[1] - Xn[1] + h*(Xn[0] + Xn1[0])*(Xn[2] + Xn1[2])/4./EI
    f[2] = Xn1[2] - Xn[2] + h*(Xn[1] + Xn1[1])/2.
    return f

sectionForces = []
N0 = F3*np.cos(theta0)
Q0 = -F3*np.sin(theta0)
M0 = 0.
sectionForces.append([N0, Q0, M0])

nEdges = 50
h = 0.1

Xn = np.array([N0, Q0, M0])
for n1 in range(1, nEdges+1):
    eqSystem = lambda Xn1: implicitMidpoint(Xn1, Xn, h, EI)
    solution = scipy.optimize.root(eqSystem, Xn)
    Xn1 = solution.x
    sectionForces.append(Xn1)
    Xn = Xn1

axialForce = np.array([force[0] for force in sectionForces])
shearForce = np.array([force[1] for force in sectionForces])
bendingMoment = np.array([force[2] for force in sectionForces])
H = bendingMoment**2/2/EI + axialForce

fig = plt.figure(figsize=(9., 9.))

ax = fig.gca(projection='3d')
ax.plot(N3, Q3, M3, color='b')
ax.plot(N3, Q3, -M3, color='b')
ax.scatter(axialForce, shearForce, bendingMoment, color='r')
ax.set_xlabel('$N$')
ax.set_ylabel('$Q$')
ax.set_zlabel('$M$')

fig = plt.figure(figsize=(7., 7.))

ax = fig.gca()
ax.plot(np.linspace(0, h*nEdges, nEdges+1), H)
ax.set_xlabel('$s$')
ax.set_ylabel('$\mathcal{H}$')


I am working on the file "2DRodFF_1.ipynb".
The first task is:
This method yields an excellent result. However, if we are thinking in a formfinding problem of a pinned rod, we can't control that the endpoint of our solution falls on the endpoint of the rod. Therefore we will try another experiment using the recurrent step of the implicit midpoint rule.
The basic idea is to let the interval size be a variable one, and to calculate every value following this procedure (exemplified in a pinned rod):

Prescribe the end force $F$ and the Hamiltonian $\mathcal{H}$ (this fixes the orbit)
Parametrize the orbit through $\theta$. Using the condition $M = 0$ at the endpoints, calculate the start and end values of the parameter.
Calculate the triplets $(N, Q, M)$ at fixed intervals of the parameter (up to this point this is the same procedure we used to draw the orbit.)
(Key step) Calculate the values of $h$ between triplet values. They will be used later on to draw the solution.
Can you write Python code for it?

EI = 5000.   #kN m^2

H = 3600.    #kN m/m
F = -4600.    #kN

theta0 = np.arccos(H/F)
theta = np.linspace(theta0, -theta0, nEdges+1)

N = F*np.cos(theta)
Q = -F*np.sin(theta)
M = -np.sqrt(2*EI*(H - N))


I am working on the file "2DRodFF_1.ipynb".
The first task is:
We are testing if the interval values computed from each formula are consistent
$$h_N = 4EI \frac{N_{n+1} - N_n}{(Q_n + Q_{n+1})(M_n + M_{n+1})}$$
$$h_Q = -4EI \frac{Q_{n+1} - Q_n}{(N_n + N_{n+1})(M_n + M_{n+1})}$$
$$h_M = -2 \frac{M_{n+1} - M_n}{Q_n + Q_{n+1}}$$
Can you write Python code for it?


I am working on the file "2DRodFF_1.ipynb".
The first task is:
We make a new experiment, this time with $F<\mathcal{H}$ which means that there are moments at the end sections
Can you write Python code for it?

EI = 5000.   #kN m^2

H = 3600.    #kN m/m
F = -3500.    #kN

nEdges = 50
nVertex = nEdges + 1

if F > H:
    theta0 = np.arccos(H/F)
else:
    theta0 = np.pi
theta = np.linspace(theta0, -theta0, nVertex)

N = F*np.cos(theta)
Q = -F*np.sin(theta)
M = -np.sqrt(2*EI*(H - N))

h = np.zeros(nEdges)
h[:] = -2*(M[1:] - M[0:-1])/(Q[0:-1] + Q[1:])

kappa = M / EI
phi = np.zeros(nVertex-1)
rotor = np.zeros(nVertex-1) + 1j*np.zeros(nVertex-1)

phi[0] = theta0
phi[1:] = 2.*np.arctan(kappa[1:-1]*(h[0:-1] + h[1:])/4.)

rotor[0] = np.exp(1j*phi[0])
rotor[1:] = (4./(h[0:-1] + h[1:]) + 1j * kappa[1:-1])/(4./(h[0:-1] + h[1:]) - 1j * kappa[1:-1])

gamma = np.zeros(nVertex) + 1j*np.zeros(nVertex)
gamma[0] = 0.+0j
gamma[1] = gamma[0] + h[0]*rotor[0]
for n in range(1, nVertex-1):
    gamma[n+1] = gamma[n] + h[n]/h[n-1] * (gamma[n] - gamma[n-1]) * rotor[n]

fig = plt.figure(figsize=(9,9))

ax = fig.gca(aspect='equal')
ax.plot(gamma.real, gamma.imag, color='b')
ax.set_xlabel('$x$')
ax.set_ylabel('$y$')

print('Total rotation at the orbit: {:.5f}*pi rad'.format((theta[-1] - theta[0])/np.pi))
print('Total rotation computed with DDG: {:.5f}*pi rad'.format((np.sum(phi) - phi[0])/np.pi))


I am working on the file "2DRodFF_2.ipynb".
The first task is:
1.1 Constant energy $\mathcal{H}$, varying force magnitude $F$
Can you write Python code for it?

EI = 5000.   #kN m^2

H = 5000.    #kN m/m
F = np.arange(-5500., -10500., -500.)    #kN

nEdges = 500
nVertex = nEdges + 1

N = np.zeros((len(F), nVertex))
Q = np.zeros((len(F), nVertex))
M = np.zeros((len(F), nVertex))
th0 = np.zeros(len(F))
length = []
gamma = []
for i in range(len(F)):
    (th0[i], N[i], Q[i], M[i]) = getOrbitPoints(EI, H, F[i], nVertex)
    (ph0, l, g) = computeElastica(th0[i], Q[i], M[i], nEdges, nVertex)
    length.append(l)
    gamma.append(g)

fig = plt.figure(figsize=(9,9))

ax = fig.gca(aspect='equal')
for i in range(len(F)):
    ax.plot(gamma[i].real, gamma[i].imag)
ax.set_xlabel('$x$')
ax.set_ylabel('$y$')

fig = plt.figure(figsize=(9,9))

ax = fig.gca(projection='3d')
for i in range(len(F)):
    ax.plot(N[i], Q[i], M[i])

ax.set_xlabel('$N$')
ax.set_ylabel('$Q$')
ax.set_zlabel('$M$')


I am working on the file "2DRodFF_2.ipynb".
The first task is:
1.2.2 $\mathcal{H} > |F|$
Can you write Python code for it?

EI = 5000.   #kN m^2

H = np.arange(5500., 23500., 1000.)   #kN m/m
F = -5000.    #kN

nEdges = 5000
nVertex = nEdges + 1

N = np.zeros((len(H), nVertex))
Q = np.zeros((len(H), nVertex))
M = np.zeros((len(H), nVertex))
th0 = np.zeros(len(H))
length = []
gamma = []
for i in range(len(H)):
    (th0[i], N[i], Q[i], M[i]) = getOrbitPoints(EI, H[i], F, nVertex)
    (ph0, l, g) = computeElastica(th0[i], Q[i], M[i], nEdges, nVertex)
    length.append(l)
    gamma.append(g)

print(length)

fig = plt.figure(figsize=(9,9))

ax = fig.gca(aspect='equal')
for i in range(len(H)):
    ax.plot(gamma[i].real, gamma[i].imag)
ax.set_xlabel('$x$')
ax.set_ylabel('$y$')

fig = plt.figure(figsize=(9,9))

ax = fig.gca(projection='3d')
for i in range(len(H)):
    ax.plot(N[i], Q[i], M[i])

ax.set_xlabel('$N$')
ax.set_ylabel('$Q$')
ax.set_zlabel('$M$')


I am working on the file "2DRodFF_2.ipynb".
The first task is:
2.2 For varying F
We will experiment representing the results for a set of values of $F$ and $\mathcal{H}$ using adimensional variables $-\mathcal{H}/F$ and $d/d_0$
Can you write Python code for it?

EI = 5000.   #kN m^2

fig, ax0 = plt.subplots(figsize=(9, 9))

ax0.grid()
ax0.axvline(x=1., color=(0.5,0.5,0.5))
ax0.axhline(color=(0.5,0.5,0.5))
ax0.set_xlabel(r'$-\mathcal{H}/F$')
ax0.set_ylim(-1.5, 1.5)
ax0.set_ylabel(r'$d/d_0$')

ax1 = ax0.twinx()
ax1.set_ylim(-1.5, 1.5)
ax1.set_ylabel(r'$\theta_0/\pi$')

F = np.arange(-5000., -1000., 1000.)    #kN

nEdges = 1000
nVertex = nEdges + 1

for k in range(len(F)):
    Hinf = np.arange(F[k]+50., -F[k]-50, 100.)   #kN m/m
    Hsup = np.arange(-F[k]+50, 5950., 100)

    Ni = np.zeros((len(Hinf), nVertex))
    Qi = np.zeros((len(Hinf), nVertex))
    Mi = np.zeros((len(Hinf), nVertex))
    th0i = np.zeros(len(Hinf))
    ph0i = []
    gammaInf = []
    for i in range(len(Hinf)):
        (th0i[i], Ni[i], Qi[i], Mi[i]) = getOrbitPoints(EI, Hinf[i], F[k], nVertex)
        (ph0, l, g) = computeElastica(th0i[i], Qi[i], Mi[i], nEdges, nVertex)
        gammaInf.append(g)
        ph0i.append(ph0)

    Ns = np.zeros((len(Hsup), nVertex))
    Qs = np.zeros((len(Hsup), nVertex))
    Ms = np.zeros((len(Hsup), nVertex))
    th0s = np.zeros(len(Hsup))
    ph0s = []
    gammaSup = []
    for i in range(len(Hsup)):
        (th0s[i], Ns[i], Qs[i], Ms[i]) = getOrbitPoints(EI, Hsup[i], F[k], nVertex)
        (ph0, l, g) = computeElastica(th0s[i], Qs[i], Ms[i], nEdges, nVertex)
        gammaSup.append(g)
        ph0s.append(ph0)

    distInf = np.zeros(len(Hinf))
    for i in range(len(Hinf)):
        distInf[i] = gammaInf[i][nVertex-1].real

    distSup = np.zeros(len(Hsup))
    for i in range(len(Hsup)):
        distSup[i] = gammaSup[i][nVertex-1].real

    ph0i = np.asarray(ph0i)
    ph0s = np.asarray(ph0s)

    #print(k)
    #print(ph0i)
    #print(ph0s)

    ax0.plot(-Hinf/F[k], distInf/gammaInf[0][nVertex-1].real, 'b')
    ax0.plot(-Hsup/F[k], distSup/gammaInf[0][nVertex-1].real, 'b')

    ax1.plot(-Hinf/F[k], ph0i/np.pi, 'r')
    ax1.plot(-Hsup/F[k], ph0s/np.pi, 'r')


I am working on the file "2DRodFF_3_test.ipynb".
The first task is:
1 Classical 2D Force density equations
1.1 Connectivity matrix
Following Schek, 1973, the connectivity matrix $[\mathbf{C}_l \;\mathbf{C}_f]$ has $\mathsf{M}$ rows and $\mathsf{N}$ columns, being $\mathsf{N}$ the number of nodes and $\mathsf{M}$ the number of members in the structure. $\mathbf{C}_f$ gathers the columns which correspond to the restrained nodes. The definition of the elements is as follows:
Given a member $m$ with end nodes $j, k$
$$C[m, i] =
\begin{Bmatrix}
+1 \;\text{for}\; i = j \
-1 \;\text{for}\; i = k \
0 \;\text{otherwise}
\end{Bmatrix}$$
Can you write Python code for it?


I am working on the file "2DRodFF_3_test.ipynb".
The first task is:
1.3 Restrained nodes
We are using complex numbers for working in the 2D plane
Can you write Python code for it?


I am working on the file "2DRodFF_3_test.ipynb".
The first task is:
2 Force density for active bending members
The equilibrium equation for node $J$ with active bending members $IJ$ and $JK$ and tension (compression) member $JL$ is
$$N_{IJ} (\mathbf{x}I - \mathbf{x}_J)/d{IJ} + V_{IJ} \mathbf{J}(\mathbf{x}I - \mathbf{x}_J)/d{IJ} +
N_{JK} (\mathbf{x}K - \mathbf{x}_J)/d{KJ} + V_{JK} \mathbf{J}(\mathbf{x}K - \mathbf{x}_J)/d{IJ} +
N_{JL} (\mathbf{x}L - \mathbf{x}_J)/d{JL} + \mathbf{f}J = \mathbf{0}$$
where
$$ \mathbf{J} =
\begin{bmatrix}
0 & -1 \
1 & \;\;0
\end{bmatrix}$$
Rearranging the equilibrium equation,
$$n{IJ} (\mathbf{x}J - \mathbf{x}_I) + v{IJ} \mathbf{J}(\mathbf{x}J - \mathbf{x}_I) +
n{JK} (\mathbf{x}J - \mathbf{x}_K) + v{JK} \mathbf{J}(\mathbf{x}J - \mathbf{x}_K) +
n{JL} (\mathbf{x}_J - \mathbf{x}_L) = \mathbf{f}_J$$
Defining
$$\mathbf{q}{IJ} = \frac{N{IJ}}{d_{IJ}}\mathbf{1} + \frac{V_{IJ}}{d_{IJ}}\mathbf{J} =
\begin{bmatrix}
n_{IJ} & -v_{IJ} \
v_{IJ} & \;\;n_{IJ}
\end{bmatrix}$$
we can rearrange the equilibrium equation in the following way
$$\begin{bmatrix}
\mathbf{1} & \mathbf{1} & \mathbf{1}
\end{bmatrix}
\begin{bmatrix}
\mathbf{q}{IJ} & \mathbf{0} & \mathbf{0} \
\mathbf{0} & \mathbf{q}{JK} & \mathbf{0} \
\mathbf{0} & \mathbf{0} & \mathbf{q}_{KL}
\end{bmatrix}
\begin{bmatrix}
-\mathbf{1} & \mathbf{1} & \mathbf{0} & \mathbf{0} \
\mathbf{0} & \mathbf{1} & -\mathbf{1} & \mathbf{0} \
\mathbf{0} & \mathbf{1} & \mathbf{0} & -\mathbf{1}
\end{bmatrix}
\begin{bmatrix}
\mathbf{x}_I \
\mathbf{x}_J \
\mathbf{x}_K \
\mathbf{x}_L
\end{bmatrix} = \mathbf{f}_J
$$
Therefore, the system of equations for the complete structure has the same form as in the original case. Only the force density matrix has the new structure indicated above, where tension/compression force densities are complemented by shear densities in the appropriate positions.
2.1 An elegant alternate way of expressing the equations
In 2D we may choose to express the product $\mathbf{J} (\mathbf{x}J - \mathbf{x}_I)$ using complex numbers as
$$\exp{(i \frac{\pi}{2})}(\gamma_J - \gamma_I) = i \,(\gamma_J -...
Can you write Python code for it?

NN = 6
NF = 4
MM = 5
C = np.zeros((MM, NN))

C[0, 0] = 1; C[0, 2] = -1    # element 1
C[1, 0] = 1; C[1, 1] = -1    # element 2
C[2, 1] = 1; C[2, 5] = -1    # element 3
C[3, 0] = 1; C[3, 3] = -1    # element 4
C[4, 1] = 1; C[4, 4] = -1    # element 5

CL = C[:, 0:(NN - NF)]
CF = C[:, (NN - NF):]

print(C)

gammaF = np.array([-5. + 0.*1j, 0. + 2.5*1j, 0 - 2.5*1j, 5. + 0.*1j])

q = [-5. + 1.35*1j, -5. + -5.*1j, -5. + 1.35*1j, 0., 0.]    # force densities in kN/m corresponding to every element
qQ = np.diagflat(q)
print(qQ)

fL = np.array([0. + (0.)*1j])

gammaL = np.zeros(NN - NF)
DL = np.zeros((NN - NF, NN - NF))
DF = np.zeros((NN - NF, NF))

DL = np.dot(np.transpose(CL), np.dot(qQ, CL))
DF = np.dot(np.transpose(CL), np.dot(qQ, CF))

gammaL = np.linalg.solve(DL, fL - np.dot(DF, gammaF))
print(gammaL)

fig = plt.figure(figsize=(9,9))

ax = fig.gca(aspect='equal')
ax.scatter(gammaF.real, gammaF.imag, color='b')
ax.scatter(gammaL.real, gammaL.imag, color='r')
ax.set_xlabel('$x$')
ax.set_ylabel('$y$')

gamma = np.concatenate((gammaL, gammaF), axis=0)
d = np.absolute((np.dot(C, gamma)))
f = q*d
V = np.imag(f)
DM = -V*d
print(DM)


I am working on the file "2DRodFF_4.ipynb".
The first task is:
3 Further experiments
3.1 Travelling along the elastica
Can you write Python code for it?

EI = 5000.
F = -5000.
H = 4900.

absMmax = np.sqrt(2*EI*(H - F))

nVertex = 201
nEdges = nVertex - 1

N = 10
dist = []
length = []
DeltaX = []
DeltaM = []

for ii in np.linspace(0., 1., 11):
    tI = ii*np.pi
    tJ = (ii+0.5)*np.pi

    (t, theta, N, Q, M) = getOrbitPoints(EI, H, F, tI, tJ, nVertex)
    (phi, h, gamma) = computeElastica(theta, Q, M, nEdges, nVertex)

    DeltaM.append(M[nVertex-1]+M[0])
    dist.append(np.absolute(gamma[nVertex-1] - gamma[0]))
    DeltaX.append(gamma[nVertex-1].real - gamma[0].real)
    length.append(np.sum(h))

fig, ax0 = plt.subplots(figsize=(9,9))
ax1 = ax0.twinx()

ax0.scatter(np.linspace(0., 1., 11), dist, color='b')
ax0.grid()
ax0.set_xlabel('$n / \pi$')
ax0.set_ylabel('$d$')

ax1.scatter(np.linspace(0., 1., 11), length, color='r')
ax1.set_ylabel('$DeltaX$')


I am working on the file "2DRodFF_4.ipynb".
The first task is:
3.2 Moment diagrams in terms of different parameters
Can you write Python code for it?

EI = 5000.
F = -5000.
H = 4900.

nVertex = 201
nEdges = nVertex - 1

tI = 0*np.pi
tJ = 1*np.pi

(t, theta, N, Q, M) = getOrbitPoints(EI, H, F, tI, tJ, nVertex)
(phi, h, gamma) = computeElastica(theta, Q, M, nEdges, nVertex)
s = np.zeros(nVertex)
for i in range(1, len(s)):
    s[i] = s[i-1] + h[i-1]

fig = plt.figure(figsize=(9,9))

ax = fig.gca(aspect='equal')
ax.plot(gamma.real, gamma.imag, color='b')
ax.grid()
ax.set_xlabel('$x$')
ax.set_ylabel('$y$')

fig, (ax0, ax1, ax2) = plt.subplots(ncols=3, figsize=(15, 5))

ax0.plot(t, M, color='b')
ax0.grid()
ax0.set_xlabel(r'$t$')
ax0.set_ylabel(r'$M$')

ax1.plot(theta, M, color='b')
ax1.grid()
ax1.set_xlabel(r'$\theta$')
ax1.set_ylabel(r'$M$')

ax2.plot(s, M, color='b')
ax2.grid()
ax2.set_xlabel(r'$s$')
ax2.set_ylabel(r'$M$')


I am working on the file "2DRodFF_4.ipynb".
The first task is:
3.4 Relation between arc-length $s$ and $d$
Can you write Python code for it?

EI = 5000.   #kN m^2

fig, ax0 = plt.subplots(figsize=(9, 9))

ax0.grid()
ax0.axvline(x=1., color=(0.5,0.5,0.5))
ax0.axhline(color=(0.5,0.5,0.5))
ax0.set_xlabel(r'$-\mathcal{H}/F$')
ax0.set_ylim(-1.5, 3.0)
ax0.set_ylabel(r'$d/d_0$')

ax1 = ax0.twinx()
ax1.set_ylim(-1.5, 3.0)
#ax1.set_ylabel(r'$\theta_0/\pi$')
ax1.set_ylabel(r'$d/s$')

F = np.arange(-5000., -1000., 1000.)    #kN

nEdges = 100
nVertex = nEdges + 1

tA = 0*np.pi
tB = 1*np.pi

for k in range(len(F)):
    Hinf = np.arange(F[k]+50., -F[k]-50., 100.)   #kN m/m
    Hsup = np.arange(-F[k]+50., 5950., 100.)

    ti = np.zeros((len(Hinf), nVertex))
    thi = np.zeros((len(Hinf), nVertex))
    Ni = np.zeros((len(Hinf), nVertex))
    Qi = np.zeros((len(Hinf), nVertex))
    Mi = np.zeros((len(Hinf), nVertex))
    phiInf = []
    hInf = []
    gammaInf = []
    for i in range(len(Hinf)):
        (ti[i], thi[i], Ni[i], Qi[i], Mi[i]) = getOrbitPoints(EI, Hinf[i], F[k], tA, tB, nVertex)
        (phi, h, gamma) = computeElastica(thi[i], Qi[i], Mi[i], nEdges, nVertex)
        phiInf.append(phi)
        hInf.append(h)
        gammaInf.append(gamma)

    ts = np.zeros((len(Hsup), nVertex))
    ths = np.zeros((len(Hsup), nVertex))
    Ns = np.zeros((len(Hsup), nVertex))
    Qs = np.zeros((len(Hsup), nVertex))
    Ms = np.zeros((len(Hsup), nVertex))
    phiSup = []
    hSup = []
    gammaSup = []
    for i in range(len(Hsup)):
        (ts[i], ths[i], Ns[i], Qs[i], Ms[i]) = getOrbitPoints(EI, Hsup[i], F[k], tA, tB, nVertex)
        (phi, h, gamma) = computeElastica(ths[i], Qs[i], Ms[i], nEdges, nVertex)
        phiSup.append(phi)
        hSup.append(h)
        gammaSup.append(gamma)

    distInf = np.zeros(len(Hinf))
    phi0Inf = np.zeros(len(Hinf))
    sInf = np.zeros(len(Hinf))
    for i in range(len(Hinf)):
        distInf[i] = gammaInf[i][nVertex-1].real
        phi0Inf[i] = phiInf[i][0]
        sInf[i] = np.sum(hInf[i])

    distSup = np.zeros(len(Hsup))
    phi0Sup = np.zeros(len(Hsup))
    sSup = np.zeros(len(Hsup))
    for i...


I am working on the file "2DRodFF_4.ipynb".
The first task is:
The first expression establishes a explicit relation between end moments and end rotations. We can rearrange the expression to reach the following invariant relation
$$ \frac{M/F}{d_0} = \pm \frac{1}{\pi} \sqrt{2 \,\biggl( \cos\theta - \frac{\mathcal{H}}{F} \biggr)}$$
With $d_0$, critical length of the rod buckling with $F$
$$d_0^2 = -\pi^2 EI / F \;\;\;\;(F < 0)$$
Let's represent it with the help of the parameter $t$.
Can you write Python code for it?

EI = 5000.
F = -5000.
H = 4950.

nVertex = 101
nEdges = nVertex - 1

tI = 0.*np.pi
tJ = 2.*np.pi

(t, theta, N, Q, M) = getOrbitPoints(EI, H, F, tI, tJ, nVertex)
(phi, h, gamma) = computeElastica(theta, Q, M, nEdges, nVertex)

dcrit = np.pi* np.sqrt(-EI / F)

# Plots
fig, ax = plt.subplots(figsize=(8, 8))
ax.scatter(theta, M/F/dcrit, color='b')
ax.grid()
ax.set_xlabel(r'$\theta$')
ax.set_ylabel(r'$\frac{M/F}{d_0}$')

fig = plt.figure(figsize=(8, 8))
ax = fig.gca(aspect='equal')
ax.plot(gamma.real, gamma.imag, color='b')
ax.grid()
ax.set_xlabel(r'$x$')
ax.set_ylabel(r'$y$')


I am working on the file "2DRodFF_4.ipynb".
The first task is:
We may also plot the adimensional moment in terms of the normalizad arc-length $s/d_{crit}$.
Can you write Python code for it?

s = np.zeros(nVertex)
for i in range(1, len(s)):
    s[i] = s[i-1] + h[i-1]

fig, ax = plt.subplots(figsize=(8, 8))
ax.scatter(s/dcrit, M/F/dcrit, color='b')
ax.grid()
ax.set_xlabel(r'$s/d_{crit}$')
ax.set_ylabel(r'$\frac{M/F}{d_0}$')

fig = plt.figure(figsize=(8, 8))
ax = fig.gca(aspect='equal')
ax.plot(gamma.real, gamma.imag, color='b')
ax.grid()
ax.set_xlabel(r'$x$')
ax.set_ylabel(r'$y$')


I am working on the file "2D_Plots.ipynb".
The first task is:
A scatter plot is also available:
Can you write Python code for it?


I am working on the file "2D_Plots.ipynb".
The first task is:
Let's get fancy and draw a figure with subplots.
The trick is to create a figure then use the add_subplot command to include each subplot. Each subplot can have its own labels, colours, etc.
The number 131 (in subplot) tells us that there will be 1 row and 3 columns in the figure, and we are inserting the 1st subplot.
Can you write Python code for it?

x = linspace(-3,3)

fig = figure(figsize=figaspect(0.2))

ax = fig.add_subplot(131)
ax.plot(x,cos(x),color='b')
ax.set_title('Cosine')

ax = fig.add_subplot(132)
ax.plot(x,sin(x),color='r')
ax.set_title('Sine')

ax = fig.add_subplot(133)
ax.plot(x,cos(x),'b',x,sin(x),'r')
ax.set_title('Both')


I am working on the file "2D_Plots.ipynb".
The first task is:
Scatter plots
Scatter plots are a whole lot more useful when we are comparing sets of real data.
Here we have 3 random data sets $(x,y)$, showing positive, negative, and zero correlation.
Using the subplot command, we can plot all three together in one figure.
Can you write Python code for it?

fig, ax = subplots()
num = 1000
s = 121
x1 = linspace(-0.5,1,num) + (0.5 - random.rand(num))
y1 = linspace(-5,5,num) + (0.5 - random.rand(num))
x2 = linspace(-0.5,1,num) + (0.5 - random.rand(num))
y2 = linspace(5,-5,num) + (0.5 - random.rand(num))
x3 = linspace(-0.5,1,num) + (0.5 - random.rand(num))
y3 = (0.5 - random.rand(num))
ax.scatter(x1, y1, color='r', s=2*s, marker='^', alpha=.4)
ax.scatter(x2, y2, color='b', s=s/2, marker='o', alpha=.4)
ax.scatter(x3, y3, color='g', s=s/3, marker='s', alpha=.4)


I am working on the file "2D_Plots.ipynb".
The first task is:
You can also plot y versus x values, as follows:
Can you write Python code for it?


I am working on the file "2D_Plots.ipynb".
The first task is:
You can use various modifiers to get different styles of plots.
Can you write Python code for it?


I am working on the file "2D_Widgets.ipynb".
The first task is:
First we define a simple function that the widgets will activate.
Can you write Python code for it?


I am working on the file "2D_Widgets.ipynb".
The first task is:
First we define the update function which will plot something interesting for us. In this case we plot the function $$f(x) = x^k$$ for various values of $k$.
The show command in the function is important, to get the plot to display.
Can you write Python code for it?

x=linspace(0,1)

def update(k=0):
    plot(x,x**k)
    show()


I am working on the file "2D_Widgets.ipynb".
The first task is:
Next We create the widgets by calling the interact function, using an argument type for x. The choice of x will give use sliders, check boxes, or text entry boxes.
Can you write Python code for it?

interact(f,x=(0,10))

interact(f,x=(0.0,10.0))

interact(f,x=True)

interact(f,x='Hello World')


I am working on the file "2D_Widgets.ipynb".
The first task is:
Other graphics
You are not limited to simple 2D plots. The widgets can control more interesting graphics, as in this network example. (from github.com/jupyter-widgets/ipywidgets )
Can you write Python code for it?

import networkx as nx

# wrap a few graph generation functions so they have the same signature

def random_lobster(n, m, k, p):
    return nx.random_lobster(n, p, p / m)

def powerlaw_cluster(n, m, k, p):
    return nx.powerlaw_cluster_graph(n, m, p)

def erdos_renyi(n, m, k, p):
    return nx.erdos_renyi_graph(n, p)

def newman_watts_strogatz(n, m, k, p):
    return nx.newman_watts_strogatz_graph(n, k, p)

def plot_random_graph(n, m, k, p, generator):
    g = generator(n, m, k, p)
    nx.draw(g)
    show()

interact(plot_random_graph, n=(2,30), m=(1,10), k=(1,10), p=(0.0, 1.0, 0.001),
         generator={
             'lobster': random_lobster,
             'power law': powerlaw_cluster,
             'Newman-Watts-Strogatz': newman_watts_strogatz,
             u'Erdős-Rényi': erdos_renyi,
         });


I am working on the file "2D_Widgets.ipynb".
The first task is:
Plot animations
Now let's do some more interesting examples where we get the widget to plot something interesting.
We will import some utility functions for plotting and numerics.
Can you write Python code for it?

%matplotlib inline
from numpy import *
from matplotlib.pyplot import *


I am working on the file "2D_Widgets.ipynb".
The first task is:
We can do more complex plots with many sliders. For instance, let's explore the sine funciton as it ofter appears in signal processing. It can have an amplitude, frequency and phase. We define the appropriate function and include sliders for this.
$$t\mapsto A\sin(2\pi f t + \phi ).$$
When we define the function, we can include some default values in the argument list.
Can you write Python code for it?

t = linspace(0,1,1000)

def plot_sine(A=4,f=2,phi=0):
    plot(t,A*sin(2*pi*f*t + phi))
    axis([0,1,-6,6])
    show()


I am working on the file "2D_Widgets.ipynb".
The first task is:
When calling the interact function, makes sure the list of parameters have the same name as the action function.
Can you write Python code for it?


I am working on the file "2S2018/04 Gerando imagens sinteticas.ipynb".
The first task is:
Imagem da função "sela"
A função "sela" bidimensional é uma função dada pelo produto de suas coordenadas r e c.
Observe que, implementando desta forma, é possível fazer com que o código Python/NumPy fique
muito próximo à equação matemática, colocada a seguir.
Vamos gerar uma função sela, onde os valores para as linhas serão valores inteiros entre
-75 e 75 e os valores para as colunas, inteiros no intervalo [-100,100]:
$$    f(r,c) = r \ c $$
$$    \text{para} \ r  \in  [-75,75] $$
$$                  c  \in  [-100,100]$$
No exemplo a seguir é utilizado a função arange para gerar os vetores de coordenadas. Para melhorar
a visualização foi utilizada a função ia636:iaisolines iaisolines que permite visualizar os
pixels de mesmo valores (isolinhas) da imagem gerada com uma cor destacada.
Can you write Python code for it?

r,c = np.meshgrid(np.arange(-75,75), np.arange(-100,100), indexing='ij')
f = r * c

%matplotlib inline
import matplotlib.pyplot as plt
plt.title('Ponto de sela')
plt.imshow(f, cmap='gray')


I am working on the file "2S2018/05 Histograma da imagem.ipynb".
The first task is:
Histograma x informação de localização
O histograma contém informações importantes da imagem e é bastante utilizado para diversos algoritmos de processamento, como por exemplo, segmentação por limiarização. Porém, ao computar a quantidade de pixels para cada valor de nível de cinza, o histograma não leva em consideração a localização dos pixels. Isso pode ser visto no exemplo abaixo, onde embaralhamos os pixels de uma imagem, e o histograma permanece o mesmo.
Can you write Python code for it?

f = mpimg.imread('../data/cameraman.tif')

fs = f.copy()
np.random.shuffle(fs) # embaralha os pixels da imagem os deixando em outra ordem

plt.figure()
plt.imshow(f, cmap = 'gray')
plt.colorbar()

plt.figure()
plt.imshow(fs, cmap = 'gray')
plt.colorbar()

h, bins = np.histogram(f)
plt.figure()
plt.plot(h)
plt.title('histograma imagem original')

hshuffle, binshuffle = np.histogram(fs)
plt.figure()
plt.plot(hshuffle)
plt.title('histograma imagem shuffle')


I am working on the file "2S2018/05 Histograma da imagem.ipynb".
The first task is:
O que temos no retorno da função np.histogram é a contagem do número de pixels com valores em uma determinada faixa. No exemplo acima, a imagem do cameramen possui 10618 pixels com valor entre 0 e 12,75. Podemos visualizar o histograma através da função plot do matplotlib.
Can you write Python code for it?


I am working on the file "2S2018/05 Histograma da imagem.ipynb".
The first task is:
Veja que a visualização acima não é muito adequada, já que o histograma não é uma função contínua. Uma forma melhor de visualizarmos o histograma é usando um gráfico de barras. E, no eixo das abscissas, queremos mostrar o valor do nível de cinza computado (bin center) e não os limites de cada faixa (bin edges), como o retornado pela função np.histogram.
Can you write Python code for it?

w=255./nbins
bin_centers = bin_edges[1:]-(w/2)
plt.bar(bin_centers, h, width=w)
plt.title('Historama da imagem')


I am working on the file "2S2018/07 Filtragem no dominio espacial.ipynb".
The first task is:
Filtro Gaussiano
Assim como o filtro da média, o filtro gaussiano é composto por uma máscara quadrada, onde os elementos são pesos de uma gaussiana bidimensional. Para que o filtro gaussiano não altere a intensidade da imagem, divide-se a máscara gaussiana pelo somatória de todos os elementos da máscara.
Can you write Python code for it?

g1 = ia.gaussian((3,3), np.transpose([[1,1]]), [[2,0],[0,2]])
g1 = g1/g1.sum() # normalização
print('g1=', g1.round(3))
G = ia.conv(f,g1)
plt.imshow(f,cmap='gray')
plt.imshow(G,cmap='gray')
fgauss = G[1:-1,1:-1] # tratamento de borda


I am working on the file "2S2018/07 Filtragem no dominio espacial.ipynb".
The first task is:
Filtro suavizante
Filtros suavizantes são aqueles que removem a nitidez da imagem fazendo uma média, aritmética ou ponderada, na janela do núcleo. O filtro ponderado suavizante mais comum é o da gaussiana. Filtros suavizantes são muito utilizados para eliminar ruído de uma imagem.
Filtro da Média
Filtro da média
O filtro da média é composto por uma máscara quadrada com pesos iguais. Os pesos são divididos pelo número de elementos da máscara, para não alterar a intensidade da imagem. Para realizar a operação de filtragem, depois de criarmos a máscara, utilizamos a função função iaconv da toolbox, que faz a varredura da imagem pelo filtro.
Can you write Python code for it?

%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

import numpy as np
import sys,os
ia898path = os.path.abspath('../../')
if ia898path not in sys.path:
    sys.path.append(ia898path)
import ia898.src as ia

f = mpimg.imread('../data/retina.tif')
s = 3
k = np.ones((s,s))/(s*s)
print('kernel=\n',k)

fconv = ia.conv(f,k)
plt.imshow(f, cmap='gray')


I am working on the file "2S2018/09 Translacao periodica.ipynb".
The first task is:
2. Usando indexação
Também é possível realizar a translação periódica usando indexação por arrays. A operação que permite a periodicidade no cálculo dos índices é o módulo, implementado pelo operador % do NumPy. Esta é a forma que a função está implementada na toolbox ia898: ptrans.
Can you write Python code for it?

def ptrans(f,t):
    import numpy as np
    g = np.empty_like(f)
    if f.ndim == 1:
      W = f.shape[0]
      col = np.arange(W)
      g = f[(col-t)%W]
    elif f.ndim == 2:
      H,W = f.shape
      rr,cc = t
      row,col = np.indices(f.shape)
      g = f[(row-rr)%H, (col-cc)%W]
    elif f.ndim == 3:
      Z,H,W = f.shape
      zz,rr,cc = t
      z,row,col = np.indices(f.shape)
      g = f[(z-zz)%Z, (row-rr)%H, (col-cc)%W]
    return g

def ptrans2d(f,t):
    rr,cc = t
    H,W = f.shape

    r = rr%H
    c = cc%W

    g = np.empty_like(f)

    g[:r,:c] = f[H-r:H,W-c:W]
    g[:r,c:] = f[H-r:H,0:W-c]
    g[r:,:c] = f[0:H-r,W-c:W]
    g[r:,c:] = f[0:H-r,0:W-c]

    return g

f = mpimg.imread('../data/cameraman.tif')
f5=ptrans(f, np.array(f.shape)//3)
plt.imshow(f,cmap='gray')
plt.title('Original 2D image - Cameraman')
plt.imshow(f5,cmap='gray')
plt.title('Cameraman periodically translated')


I am working on the file "2S2018/11 Teorema da Convolucao.ipynb".
The first task is:
Agora, vamos calcular a transformada de Fourier $F(f)$ da imagem e $F(h)$ do kernel. Antes de mais nada, precisamos garantir que a imagem $f$ e o kernel $h$ sejam periódicos e tenham o mesmo tamanho.
Can you write Python code for it?

# Aumentando h para o tamanho de f
aux = np.zeros(f.shape)
r,c = h.shape
aux[:r,:c] = h

# Calculando a Transformada de Fourier da f e h
F = fft2(f)
H = fft2(aux)

# Multiplicando-se as Tranformadas
G = F * H

# Calculando a Transformada inversa
gg = ifft2(G)

print("Result gg: \n",np.around(gg))


I am working on the file "2S2018/11 Teorema da Convolucao.ipynb".
The first task is:
Pelo teorema da convolução, gg e g deveriam ser iguais:
Can you write Python code for it?

print('O teorema da convolução funcionou?', np.allclose(gg.real,g))


I am working on the file "2S2018/11 Teorema da Convolucao.ipynb".
The first task is:
Teorema da convolução
O teorema da convolução diz que
$$ F(f * g) = F(f) \cdot F(g) $$
$$ F(f\cdot g) = F(f) * F(g) $$
onde $F$ indica o operador da transformada de Fourier, ou seja, $F(f)$ and $F(g)$ são as transformdas de $f$ e $g$. É importante perceber que a convolução usada aqui é a convolução periódica.
Vamos ilustrar o Teorema da Convolução com um exemplo numérico. Primeiro, vamos calcular a convolução periódica de uma imagem $f$ com um kernel $h$
Can you write Python code for it?

fr = np.linspace(-1,1,6)
f  = np.array([fr,2*fr,fr,fr])
print(f)

hh = np.array([-1,0,+1])
h = np.array([hh,2*hh,hh])
print(h)

g = ia.pconv(f,h)
print(g)


I am working on the file "2S2018/12 Filtros em frequencia.ipynb".
The first task is:
Para entender os efeitos observados na filtragem com o filtro ideal, vamos visualizar o filtro ideal no espaço.
Can you write Python code for it?

hpb_ideal = ifft2(HPB_ideal)

plt.imshow(np.log(np.abs(ia.ptrans(hpb_ideal,(x//2,y//2))+1)),cmap='gray');
plt.title('hpb_ideal')


I am working on the file "2S2018/13 Correlacao de fase.ipynb".
The first task is:
Identificando a rotação entre 2 imagens

Calcular a Transformada de Fourier das 2 imagens que se quer comparar;
Converter as imagens obtidas para coordenadas polares
Calcular a correlação de fase usando a função phasecorr
Encontrar o ponto de máximo do mapa de correlação resultante
Can you write Python code for it?

f = mpimg.imread('../data/cameraman.tif')

# Inserindo uma borda de zeros para permitir a rotação da imagem
t = np.zeros(np.array(f.shape)+100,dtype=np.uint8)
t[50:f.shape[0]+50,50:f.shape[1]+50] = f
f = t

t1 = np.array([
             [1,0,-f.shape[0]/2.],
             [0,1,-f.shape[1]/2.],
             [0,0,1]]);

t2 = np.array([
             [1,0,f.shape[0]/2.],
             [0,1,f.shape[1]/2.],
             [0,0,1]]);

# Rotacionando a imagem 30 graus
theta = np.radians(30)
r1 = np.array([
        [np.cos(theta),-np.sin(theta),0],
        [np.sin(theta),np.cos(theta),0],
        [0,0,1]]);

T = t2.dot(r1).dot(t1)
f_rot = ia.affine(f,T,0)

plt.figure(1,(10,10))
plt.subplot(1,2,1)
plt.imshow(f, cmap='gray')
plt.title('Imagem original')

plt.subplot(1,2,2)
plt.imshow(f_rot, cmap='gray')
plt.title('Imagem rotacionada')

W,H = f.shape
f_polar = ia.polar(f,(150,200),2*np.pi)
f_rot_polar = ia.polar(f_rot,(150,200),2*np.pi)

plt.figure(1,(10,10))
plt.subplot(1,2,1)
plt.imshow(f_polar, cmap='gray')
plt.title('Imagem original (coord. polar)')

plt.subplot(1,2,2)
plt.imshow(f_rot_polar, cmap='gray')
plt.title('Imagem rotacionada (coord. polar)')

# Calculando a correlação de fase
g = ia.phasecorr(f_polar,f_rot_polar)

# Encontrando o ponto de máxima correlação
i = np.argmax(g)
corr = np.unravel_index(i,g.shape)

# Calculate the angle
ang = (float(corr[1])/g.shape[1])*360
print('Ponto de máxima correlação: ',ang)


I am working on the file "2S2018/Ex03 Transformacao de intensidade.ipynb".
The first task is:
Exercício 4
Você é capaz de transformar a imagem para identificar o número da placa do carro apresentado na imagem abaixo?
Can you write Python code for it?

f = mpimg.imread('../figures/fusca.png')
plt.imshow(f,cmap='gray')


I am working on the file "2S2018/Ex03 Transformacao de intensidade.ipynb".
The first task is:
Exercício 5
Escreva uma função que implemente a tranformação de brilho e contraste de uma imagem em níveis de cinza, baseada na construçao de três segmentos de retas:

Primeiro segmento de reta: 'x' de '0' a 'x1', com angulo 'alpha';
Segundo segmento de reta: 'x' de 'x1' a 'x2', com angulo 'beta';
Terceiro segmento de reta: 'x' de 'x2' a '255';
Retorno: mapeamento de brilho & contraste da 'imagem' de entrada.
Can you write Python code for it?

from PIL import Image
ang = Image.open('../figures/bc_angulos.png')
ang


I am working on the file "2S2018/Ex03 Transformacao de intensidade.ipynb".
The first task is:
Os valores calculados parecem corretos? Vejamos o valor da média, por exemplo, usando a função np.mean:
Can you write Python code for it?


I am working on the file "2S2018/Ex03 Transformacao de intensidade.ipynb".
The first task is:
Porque os valores de média calculados pela h2stats (5.7777778) e pela np.mean(1.2222222222) são diferentes? Muito provavelmente o problema é a diferença entre a função ia.histogram e np.histogram.
Exercício 1
Pensando que gostaríamos de usar a função h2stats com a função np.histogram, descubram se há uma forma correta de usar a função np.histogram para obter os valores corretos das estatísticas sem alterar a função h2stats. Caso vocês não obtenham sucesso, alterem a função h2stats para que os cálculos fiquem corretos. Testem com arrays numéricos pequenos. Para checar se as estatísticas retornadas estão corretas, usem funções do Numpy e, eventualmente, do Scipy para confirmar (por exemplo: np.mean( ), scipy.stats.skew( )).
Parte 2 - Transformação de Intensidade
É a categoria de funções que apenas alteram o valor do pixel aplicando-se uma função ou tabela de mapeamento. Estudem atentamente os tutoriais sobre Transformada de intensidade e sobre Equalização de histograma. O último tutorial mostra que é possível fazer a equalização da imagem utilizando também o conceito usado para construir um mosaico com tons de cinza igualmente distribuídos.
Exercício 2
Exercite transformações de intensidade em uma imagem.
Exercício 3
Veja a imagem abaixo. Escolha um valor de limiar (threshold) para separar as bolachas do fundo da imagem. Dica: Visualize o histograma da imagem para escolher o valor. Crie primeiro uma função de transformação T e depois aplique esta transformação para obter o objeto de interesse (cookies) pintados de branco e o fundo em preto.
Can you write Python code for it?

import numpy as np
import matplotlib.image as mpimg
import matplotlib.pyplot as plt

%matplotlib inline
f = mpimg.imread('../figures/cookies.pgm')
plt.imshow(f,cmap='gray')


I am working on the file "2S2018/Ex06 Preparando para a Transformada de Fourier.ipynb".
The first task is:
OBS: estou usando a função adshow no lugar da implot pois percebi um efeito quadriculado na visualização (vejam abaixo).
Can you write Python code for it?


I am working on the file "2S2018/Ex06 Preparando para a Transformada de Fourier.ipynb".
The first task is:
Parte 2 - Calculando a translação periódica
A próxima atividade consiste em entender as diferentes implementações da translação periódica. Procure comparar as 3 versões (translação periódica).

Primeiro, transforme a primeira implementação em uma função que recebe parâmetros para realizar a translação (rr, cc).
Implemente a terceira forma indicada no link acima (usando slicing).
Compare as 3 formas, em termos de resultado e de tempo. É provável que a última opção seja a mais eficiente de todas, pois somente utiliza fatiamento.

Um dos cuidados nas implementações é observar que os parâmetros da translação podem ser negativos ou ainda bem maiores que a largura ou altura da imagem.
Parte 3 - Calculando a Transformada de Fourier de uma onda senoidal
Antes de continuar, leia sobre a Transformada Discreta de Fourier (DFT) e sobre a Transformada Rápida de Fourier (FFT). Além dos slides colocados no Google Calssroom, dê uma olhada em alguns links interessantes:

demo
explicação intuitiva

No exemplo abaixo, a partir da onda senoidal bidimensional gerada na parte 1, calculamos a Transformada de Fourier usando a função fft2 do numpy. Para a visualização, é necessário fazer a translação periódica, para colocar a origem do espectro (0,0) no centro da imagem. Também é preciso usar o valor da magnitude do espectro resultante, já que o resultado da transformação é uma matriz complexa. E por fim, costumamos visualizar o log do resultado.
Can you write Python code for it?

faux = f
F = fft2(faux)
H,W = faux.shape
ia.adshow(ia.normalize(np.log(np.abs(ia.ptrans(F,(H//2,W//2))+1))), [0,255])


I am working on the file "2S2018/Ex09 Tecnicas de segmentacao.ipynb".
The first task is:
Parte 2 - Demonstração da limiarização por Otsu
Tente melhorar a demonstração da limiarização por Otsu (Demo).
Can you write Python code for it?

def otsu(f):
     n = np.product(np.shape(f))
     h = 1.*np.bincount(f.ravel()) / n
     if len(h) == 1: return 1,1
     x = np.arange(np.product(np.shape(h)))
     w0 = np.cumsum(h)
     w1 = 1 - w0
     eps = 1e-10
     m0 = np.cumsum(x * h) / (w0 + eps)
     mt = m0[-1]
     m1 = (mt - m0[0:-1]*w0[0:-1]) / w1[0:-1]
     sB2 = w0[0:-1] * w1[0:-1] * ((m0[0:-1] - m1)**2)
     t = np.argmax(sB2)
     v = sB2[t]
     st2 = sum((x-mt)**2 * h)
     eta = v / st2
     return t, eta


I am working on the file "2S2018/Seminarios/B_Correlação_Cruzada_Scipy.ipynb".
The first task is:
Encontrar a correspondência, através da correlação cruzada

calcular a média do array da imagem alvo e subtraí-la da imagem alvo
calcular a média do template e subtraí-la do template
Utilizar a função signal.correlate2d do Scipy para calcular a correlação cruzada entre a imagem alvo e o template, passando como parâmetro: a imagem alvo, o template, boundary='symm' que indica condições de contorno simétricas, mode='same' que indica que a saída é do mesmo tamanho que a imagem alvo e centralizada em relação à saída "completa".
Encontrar as coordenadas onde está localizada a correspondencia, através do índices dos valores máximos ao longo do eixo x e y.
Can you write Python code for it?

arr = arr - arr.mean()
template -= template.mean()
#arr = arr + np.random.randn(*arr.shape) * 100  # add noise
corr = signal.correlate2d(arr, template, boundary='fill', mode='same')
y, x = np.unravel_index(np.argmax(corr), corr.shape)  # Encontrar a correspondencia: Converte um índice simples ou uma matriz de índices simples em uma tupla de matrizes de coordenadas
print(np.argmax(corr))
print(corr.shape)
print(y,x)
#print(template)
#print(corr)
#plt.imshow(template, cmap='gray')


I am working on the file "2S2018/Seminarios/B_Correlação_Cruzada_Scipy.ipynb".
The first task is:
Plotando os resultados

Exibir a corrêspondecia através de um marcador vermelho das coordenadas x e y calculadas acima
Can you write Python code for it?

fig, (ax_orig, ax_template, ax_corr, ax_result) = plt.subplots(4, 1, figsize=(6, 15))
ax_orig.imshow(arr, cmap='gray')
ax_orig.set_title('Original')
#ax_orig.set_axis_off()

ax_template.imshow(template, cmap='gray')
ax_template.set_title('Template')
ax_template.set_axis_off()

ax_corr.imshow(corr, cmap='gray')
ax_corr.set_title('Cross-correlation')
ax_corr.set_axis_off()

ax_result.imshow(img, cmap='gray')
ax_result.set_title('Resultado da correspondência')
ax_result.set_axis_off()

ax_result.plot(x, y, 'ro')
fig.show()

%matplotlib inline
from mpl_toolkits import mplot3d
from mpl_toolkits.mplot3d import Axes3D
h1,g2=np.histogram(arr,bins=255)
h2,g2=np.histogram(corr,bins=255)
plt.figure(figsize=(15,5))
plt.subplot(1,2,1)
plt.plot(h1)
plt.subplot(1,2,2)
plt.plot(h2)
plt.show()
plt.axes(projection='3d')

import timeit
t = timeit.Timer('char in text', setup='text = "sample string"; char = "g"')
t.timeit()

%timeit 1+1


I am working on the file "2S2018/Seminarios/Dithering.ipynb".
The first task is:
A imagem gerada pelo algorítmo de Floyde-Steinberg é incrívelmente melhor que a quantizada para 1 bit, já que é possível ver detalhes do cubo que na imagem quantizada nós não vemos.
Algorítmo de Jarvis, Judice, and Ninke (JJN)
Esse algorítmo possui o mesmo princípio de difusão de erro que o anterior. O que os difere é a porcentagem de erro que é propagado para cada pixel e o número de pixels que recebem esse erro. Aqui, pixels a duas linhas e colunas de distancia, para cada direção (menos para cima), recebem uma porcentagem de erro. A matriz de erro agora é:
                                                     X   7   5
                                             3   5   7   5   3
                                             1   3   5   3   1

                                                   (1/48)

Sendo assim, agora devemos acrescentar duas colunas de zeros nas extremidades e duas linhas de zeros ao final da matriz, para que possamos processar os pixels dos cantos da imagem.
Can you write Python code for it?

def function_jjn_dither(img_array):
    M = img_array.shape[0]
    N = img_array.shape[1]

    #linhas de zeros adicionadas na imagem para poder processar as bordas corretamente
    array_col = np.zeros((2,M)).reshape(M, 2)
    array_lin = np.zeros((2,N+4))

    img_jjn = np.hstack((img2_array,array_col))
    img_jjn = np.hstack((array_col,img_jjn))
    img_jjn = np.vstack((img_jjn,array_lin))

    jjn_dither = np.zeros((M,N))
    limiar = 127

    for lin in range(M):
        for col in range(2,N+1):
            if img_jjn[lin,col] >255:
                jjn_dither[lin,col-2]=255
                erro=0
            else:
                var = limiar - img_jjn[lin,col]
                if var>=0:
                    erro = img_jjn[lin,col]
                else:
                    jjn_dither[lin,col-2]=255
                    erro = img_jjn[lin,col] - 255
            img_jjn[lin,col+1] = (7/48)*erro+img_jjn[lin,col+1]
            img_jjn[lin,col+2] = (5/48)*erro+img_jjn[lin,col+2]
            img_jjn[lin+1,col-2] = (3/48)*erro+img_jjn[lin+1,col-2]
            img_jjn[lin+1,col-1] = (5/48)*erro+img_jjn[lin+1,col-1]
            img_jjn[lin+1,col] = (7/48)*erro+img_jjn[lin+1,col]
            img_jjn[lin+1,col+1] = (5/48)*erro+img_jjn[lin+1,col+1]
            img_jjn[lin+1,col+2] = (3/48)*erro+img_jjn[lin+1,col+2]
            img_jjn[lin+2,col-2] = (1/48)*erro+img_jjn[lin+2,col-2]
            img_jjn[lin+2,col-1] = (3/48)*erro+img_jjn[lin+2,col-1]
            img_jjn[lin+2,col] = (5/48)*erro+img_jjn[lin+2,col]
            img_jjn[lin+2,col+1] = (3/48)*erro+img_jjn[lin+2,col+1]
            img_jjn[lin+2,col+2] = (1/48)*erro+img_jjn[lin+2,col+2]

    return jjn_dither

jjn_dither = function_jjn_dither(img2_array)

jjn_img = Image.fromarray(np.uint8(jjn_dither))
print('Imagem com dithering por difusão de erro tipo JJN')
display(jjn_img)


I am working on the file "2S2018/Seminarios/Dithering.ipynb".
The first task is:
Dithering Ordenado
Neste método, uma matriz conhecida como matriz de limiar é adotada para se fazer a comparação com o valor inicial do pixel. A imagem é normalizada para um range que equivale ao número de elementos da matriz de limiar, em seguida ,cada pixel é comparado com o valor de limiar correspondente à posição na matriz de limiar. Se o valor do pixel for maior que o limiar, o pixel é setado para 255, se não, para 0. A matriz de limiar pode ser de vários tamanhos, mas o algorítmo mais conhecido para gerar essas matrizes (Bayer matrix) gera somente matrizes com tamanhos proporcionais à potencias de 2.
Can you write Python code for it?

img2 = Image.open('../seminario/imagens/cube_gray.png')
img2_quant = img2.quantize(2,1)
img2_array= np.array(img2)

plt.figure(figsize=(15,8))
plt.subplot(121)
plt.imshow(img2_array,cmap='gray',vmin=0,vmax=255)
plt.title('Imagem em escala de cinza')
plt.subplot(122)
plt.imshow(img2_quant,cmap='gray',vmin=0,vmax=255)
plt.title('Imagem quantizada para 1 bit');


I am working on the file "2S2018/Seminarios/Dithering.ipynb".
The first task is:
Neste exemplo, usaremos a imagem do cubo mostrada acima, pois ela mostra um resultado mais didático. Mostraremos a implementação do dithering ordenado utilizando matrizes de Bayer com potencias de 2, 4 e 8. Para melhor visualização do resultado, os arrays foram transformados para imagem, pois o 'plt.imshow' muda as configurações do arquivo para poder gerar a imagem.
Can you write Python code for it?

m_limiar_2x2 = np.array([[0,2],
                        [3,1]])
m_limiar_4x4 = np.array([[0,8,2,10],
                                [12,4,14,6],
                                [3,11,1,9],
                                [15,7,13,5]])
m_limiar_8x8 = np.array([[0,48,12,60,3,51,15,63],
                                [32,16,44,28,35,19,47,31],
                                [8,56,4,52,11,59,7,55],
                                [40,24,36,20,43,27,39,23],
                                [2,50,14,62,1,49,13,61],
                                [34,18,46,30,33,17,45,29],
                                [10,58,6,54,9,57,5,53],
                                [42,26,38,22,41,25,37,21]])

from IPython.display import display

def function_dithering_ordered(img_array,m_limiar):
    M = img_array.shape[0]
    N = img_array.shape[1]
    ordered_dither = np.zeros((M2,N2))

    mat=m_limiar
    n = mat.shape[0]

    norm = np.amax(img_array)/n**2
    img_norm = img_array/norm

    for col in range(N):
        for lin in range(M):
            if img_norm[lin,col] >=mat[(lin%n),(col%n)]:
                ordered_dither[lin,col] = 255
    return ordered_dither

ordered_dither = function_dithering_ordered(img2_array,m_limiar_2x2)

ordered_img_8x8 = Image.open('../seminario/imagens/cube_ordered_8x8.png')
ordered_img_4x4 = Image.open('../seminario/imagens/cube_ordered_4x4.png')
ordered_img = Image.fromarray(np.uint8(ordered_dither))

print('Imagem com dithering ordenado tipo Bayer 2x2')
display(ordered_img)
print('Imagem com dithering ordenado tipo Bayer 4x4')
display(ordered_img_4x4)
print('Imagem com dithering ordenado tipo Bayer 8x8')
display(ordered_img_8x8)


I am working on the file "2S2018/tutorial_numpy_1_5.ipynb".
The first task is:
Confirme que a principal diferença entre os dois que pode ser verificada nos exemplos acima é que
no linspace o limite superior da distribuição é inclusivo (intervalo fechado),
enquanto no arange isso não ocorre (intervalo semi-aberto).
Funções indices e meshgrid
As funções indices e meshgrid são extremamente úteis na geração de imagens sintéticas e o seu aprendizado permite também
entender as vantagens de programação matricial, evitando-se a varredura seqüencial da imagem muito usual na programação na linguagem C.
Operador indices em pequenos exemplos numéricos
A função indices recebe como parâmetros uma tupla com as dimensões (H,W) das matrizes a serem criadas. No exemplo a seguir, estamos
gerando matrizes de 5 linhas e 10 colunas. Esta função retorna uma tupla de duas matrizes que podem ser obtidas fazendo suas atribuições
como no exemplo a seguir onde criamos as matrizes r e c, ambas de tamanho (5,10), isto é, 5 linhas e 10 colunas:
Can you write Python code for it?

r,c = np.indices( (5, 10) )
print('r=\n', r )
print('c=\n', c )


I am working on the file "2S2018/tutorial_numpy_1_5.ipynb".
The first task is:
Imprimindo arrays binários
Array booleanos são impressos com as palavras True e False, como no exemplo a seguir:
Can you write Python code for it?

A = np.random.rand(5,10) > 0.5
print('A = \n', A )


I am working on the file "2S2018/tutorial_numpy_1_5.ipynb".
The first task is:
Já na função arange, define-se o intervalo semi-aberto [inicio,fim) e o passo que será dado entre um elemento e outro.
Dessa forma, para gerar
um numpy.array entre 0 e 1 com 10 elementos, temos que calcular o passo (0.1) e passar esse passo como parâmetro.
Can you write Python code for it?

# gera um numpy.array linearmente espaçados entre 0 a 1 com passo 0.1

print(np.arange(0, 1.0, 0.1) )


I am working on the file "2S2018/tutorial_numpy_1_5.ipynb".
The first task is:
Linspace e Arange
As funções do numpy linspace e arange tem o mesmo objetivo: gerar numpy.arrays linearmente
espaçados em um intervalo indicado como parâmetro.
A diferença primordial entre essas funções é como será realizada a divisão no intervalo especificado.
Na função linspace essa divisão é feita através da definição do intervalo fechado [inicio,fim], isto é, contém o
início e o fim, e da quantidade de
elementos que o numpy.array final terá. O passo portanto é calculado como (fim - inicio)/(n - 1).
Dessa forma, se queremos gerar um numpy.array entre 0 e 1 com 10 elementos, utilizaremos o linspace da seguinte forma
Can you write Python code for it?

# gera um numpy.array de 10 elementos, linearmente espaçados entre 0 a 1
print(np.linspace(0, 1.0, num=10).round(2) )


I am working on the file "2S2018/tutorial_numpy_1_5.ipynb".
The first task is:
Multiplicação de array por escalar: b x 5
Can you write Python code for it?


I am working on the file "2S2018/tutorial_numpy_1_5.ipynb".
The first task is:
Multiplicação de matrizes: a x c
A multiplicação de matrizes é feita através do operador dot.
Para que a multiplicação seja possível é importante que o número de
colunas do primeiro ndarray seja igual ao número de linhas do
segundo. As dimensões do resultado será o número de linhas do
primeiro ndarray pelo número de colunas do segundo ndarray. Confira:
Can you write Python code for it?

ac = a.dot(c)

print('a.shape:',a.shape )
print('c.shape:',c.shape )
print('a=\n',a )
print('c=\n',c )
print('ac=\n', ac )
print('ac.shape:',ac.shape )


I am working on the file "2S2018/tutorial_numpy_1_5.ipynb".
The first task is:
Ou ainda a função $f(r,c) = (r + c) \% 2$ onde % é operador módulo. Esta função retorna 1 se a soma das coordenadas for ímpar e 0 caso contrário.
É uma imagem no estilo de um tabuleiro de xadrez de valores 0 e 1:
Can you write Python code for it?


I am working on the file "2S2018/tutorial_numpy_1_5.ipynb".
The first task is:
Ou ainda a função de uma reta $f(r,c) = (r = \frac{1}{2}c)$:
Can you write Python code for it?


I am working on the file "2S2018/tutorial_numpy_1_5.ipynb".
The first task is:
Ou ainda a função diferença entre a coordenada linha e coluna $f(r,c) = r - c$:
Can you write Python code for it?


I am working on the file "2S2018/tutorial_numpy_1_5.ipynb".
The first task is:
Ou ainda a função parabólica dada pela soma do quadrado de suas coordenadas $f(r,c) = r^2 + c^2$:
Can you write Python code for it?


I am working on the file "2S2018/tutorial_numpy_1_5.ipynb".
The first task is:
Para facilitar a visualização destes arrays, é possível converter os valores para inteiros utilizando
o método astype(int):
Can you write Python code for it?


I am working on the file "2S2018/tutorial_numpy_1_5.ipynb".
The first task is:
Soma de arrays: a + b
Can you write Python code for it?


I am working on the file "2S2018/tutorial_numpy_1_5.ipynb".
The first task is:
Transposta de uma matriz: a.T
A transposta de uma matriz, troca os eixos das coordenadas. O elemento que
estava na posição (r,c) vai agora estar na posição (c,r). O shape
da matriz resultante ficará portanto com os valores trocados. A operação
de transposição é feita através de cópia rasa, portanto é uma operação
muito eficiente e deve ser utilizada sempre que possível.
Veja o exemplo a seguir:
Can you write Python code for it?

at = a.T
print('a.shape=',a.shape )
print('a.T.shape=',a.T.shape )
print('a=\n', a )
print('at=\n', at )


I am working on the file "2S2018/tutorial_numpy_1_5.ipynb".
The first task is:
É possível diminuir o número de casas decimais e suprimir a notação exponencial utilizando
a função set_printoption do numpy:
Can you write Python code for it?

np.set_printoptions(suppress=True, precision=3)

print('A: \n', A )


I am working on the file "2StockEmotion/3. 主成份分析（PCA）（伦敦）.ipynb".
The first task is:
取一个主成分， 解释方差0.917864
Can you write Python code for it?

pca = PCA(n_components=8)
pca.fit(tdf)
np.set_printoptions(precision=6, suppress=True)

print('各主成份方差贡献占比：', end=' ')
print(pca.explained_variance_ratio_)

emotion_score = pd.DataFrame(pca.transform(tdf))
# 第一个主成份
pd.concat([df, emotion_score.loc[:, 0]], axis=1, join='inner').rename(index=str, columns={0: 'emotion_score'}).to_csv('London_score_raw.txt', index=None, sep='\t')


I am working on the file "2StockEmotion/3. 主成份分析（PCA）（曼哈顿）.ipynb".
The first task is:
取一个主成分， 解释方差0.917864
Can you write Python code for it?

pca = PCA(n_components=8)
pca.fit(tdf)
np.set_printoptions(precision=6, suppress=True)

print('各主成份方差贡献占比：', end=' ')
print(pca.explained_variance_ratio_)

emotion_score = pd.DataFrame(pca.transform(tdf))
emotion_score.rename(columns={'0': 'emotion_score'}, inplace=True)
# 第一个主成份
pd.concat([df, emotion_score.loc[:, 0]], axis=1, join='inner').rename(index=str, columns={0: 'emotion_score'}).to_csv('Manhattan_score_raw.txt', index=None, sep='\t')


I am working on the file "2_1-Kontinuumsstroemungen_Einfuehrung.ipynb".
The first task is:
Als Beispiel verwenden wir das Vektorfeld
$$\overrightarrow{v} =
\begin{pmatrix}
0 \
x \
0 \
\end{pmatrix},$$
so dass sich die Rotation zu
$$\text{rot}~\overrightarrow{v} =
\begin{pmatrix}
0 \
0 \
1 \
\end{pmatrix}$$
ergibt. D.h. die Rotation ist im gesamten Feld konstant und steht senkrecht auf der $x$-$y$-Ebene.
Can you write Python code for it?

x = y = z = np.linspace(-1.,1.,11)
xi, yi = np.meshgrid(x, y)

ui = np.zeros_like(xi)
vi = xi

fig = plt.figure(figsize=(4, 4))
plt.quiver(xi, yi, ui, vi, color='r');

rotv = curl2d(xi, yi, ui, vi)
print(rotv)


I am working on the file "2_1-Kontinuumsstroemungen_Einfuehrung.ipynb".
The first task is:
Gradient eines Vektorfeldes
In der Fluidmechanik (z.B. in den Navier-Stokes-Gleichungen) wird häufig auch der Gradient eines Vektorfeldes benötigt:
$$\text{grad}~\overrightarrow{v} = \nabla \overrightarrow{v} = \nabla \otimes\overrightarrow{v} =  \frac{\partial v_i}{\partial x_j} =
\begin{pmatrix}
\frac{\partial v_x}{\partial x} & \frac{\partial v_x}{\partial y} & \frac{\partial v_x}{\partial z} \
\frac{\partial v_y}{\partial x} & \frac{\partial v_y}{\partial y} & \frac{\partial v_y}{\partial z} \
\frac{\partial v_z}{\partial x} & \frac{\partial v_z}{\partial y} & \frac{\partial v_z}{\partial z}\
\end{pmatrix}$$
Hintergrund ist die Fragestellung, wie groß die Änderung $\text{d}\overrightarrow{v}$ des Vektorfeldes ist, wenn wir uns um den infinitesimalen Vektor $\text{d}r$ fortbewegen. Genau wie bei einem Skalar, ist die Änderung (das Totale Differential):
$$\text{d}\overrightarrow{v}=\frac{\partial \overrightarrow{v}}{\partial x}\text{d}x + \frac{\partial \overrightarrow{v}}{\partial y}\text{d}y + \frac{\partial \overrightarrow{v}}{\partial z}\text{d}z$$
oder mit dem Vektor $\text{d}\overrightarrow{r} = \text{d}x \overrightarrow{e}_x + \text{d}y \overrightarrow{e}_y + \text{d}z \overrightarrow{e}_z$:
$$\text{d}\overrightarrow{v} = \left(\overrightarrow{e}_x \text{d}x +  \overrightarrow{e}_y \text{d}y +  \overrightarrow{e}_z \text{d}z \right)\cdot \left(\overrightarrow{e}_x \frac{\partial}{\partial x} +  \overrightarrow{e}_y \frac{\partial}{\partial y} +  \overrightarrow{e}_z \frac{\partial}{\partial z} \right) \overrightarrow{v}$$
$$\text{d}\overrightarrow{v} = \text{d}\overrightarrow{r}\cdot\nabla\overrightarrow{v} = \left(\text{d}\overrightarrow{r}\cdot\nabla\right)\overrightarrow{v}$$
Für den Ausdruck $\nabla \otimes\overrightarrow{v}$ allein, der sog. Dyade, gibt es leider keine anschauliche geometrische Bedeutung.
Divergenz eines Vektorfeldes
Die Divergenz eines Vektorfelds ergibt ein Skalarfeld, aus dem man ablesen kann, ob und wo es im Vektorfeld Quellen (positive Divergenz) oder Senken (negative...
Can you write Python code for it?

def div2d(x,y,u,v):
    return ((np.diff(u,axis=1)/np.diff(x,axis=1))[:-1,:]
            + (np.diff(v,axis=0)/np.diff(y,axis=0))[:,:-1])


I am working on the file "2_1-Kontinuumsstroemungen_Einfuehrung.ipynb".
The first task is:
Rotation eines Vektorfeldes
Die Rotation eines Vektorfeldes ist durch das Kreuzprodukt des Nabla-Operators mit dem Vektorfeld definiert:
$$\text{rot}~\overrightarrow{v} = \nabla \times \overrightarrow{v} = \begin{pmatrix}
\frac{\partial v_z}{\partial y} - \frac{\partial v_y}{\partial z} \
\frac{\partial v_x}{\partial z} - \frac{\partial v_z}{\partial x} \
\frac{\partial v_y}{\partial x} - \frac{\partial v_x}{\partial y} \
\end{pmatrix}$$
In Python gibt es keine vorgefertigte Funktion zur Berechnung der Rotation. Wir basteln uns deshalb selbst eine (hier für 2D):
Can you write Python code for it?

def curl2d(x, y, u, v):
    return ((np.diff(v,axis=1)/np.diff(x,axis=1))[:-1,:]
            - (np.diff(u,axis=0)/np.diff(y,axis=0))[:,:-1])


I am working on the file "2_1-Kontinuumsstroemungen_Einfuehrung.ipynb".
The first task is:
Substantielle Ableitung (Transporttheorem, Eulersche und Lagrangesche Betrachtung)
Die absolute Änderung einer Größe $\phi$, die ein im Strömungsfeld mitbewegter Beobachter sieht setzt sich zusammen aus der Änderung von $\phi$ mit der Zeit und der Änderung von $\phi$ mit dem Ort. Mathematisch ausgedrückt:
$$\underbrace{\frac{\text{D}\phi}{\text{D} t}}{\substack{\text{Änderung, die ein} \ \text{mitbewegter Beobachter} \ \text{sieht}}} = \underbrace{\frac{\partial \phi}{\partial t}}{\substack{\text{Änderung} \ \text{mit der Zeit}}} + \underbrace{\overrightarrow{v}\cdot \nabla \phi}_{\substack{\text{Änderung} \ \text{mit dem Ort}}} = \frac{\partial \phi}{\partial t} + v_i \frac{\partial \phi}{\partial x_i}$$
$\phi$ kann sowohl ein Skalar (z.B. die Temperatur) als auch ein Vektor (z.B. die Geschwindigkeit) sein.
Als Beispiel betrachten wir einen See, dessen Temperatur sich durch Sonneneinstrahlung um 3 K/s erhöht. Außerdem ist die Temperatur im See nicht überall gleich, sondern abhängig vom Ort. Insgesamt kann die Temperatur wie folgt beschrieben werden:
$$\phi = T(x,y,z) = 300 \text{K} + 1 \frac{\text{K}}{\text{m}}\cdot x + 2 \frac{\text{K}}{\text{m}}\cdot y + 3 \frac{\text{K}}{\text{s}}\cdot t$$
Ein ortsfester Beobachter würde an jeder Stelle des Sees eine Temperaturänderung
$$\frac{\partial \phi}{\partial t} = 3 \frac{\text{K}}{\text{s}}$$
sehen. Ein Beobachter, der sich z.B. in einem Boot mit der Geschwindigkeit $\overrightarrow{v} =
\begin{pmatrix}
3 \
1 \
\end{pmatrix} \frac{\text{m}}{\text{s}}$ über den See bewegt würde jedoch folgende Änderung detektieren:
$$\frac{\text{D}\phi}{\text{D} t} = 3 \frac{\text{K}}{\text{s}} + \begin{pmatrix}
3 \
1 \
\end{pmatrix} \frac{\text{m}}{\text{s}}
\cdot
\begin{pmatrix}
1 \
2 \
\end{pmatrix} \frac{\text{K}}{\text{m}} = 3 \frac{\text{K}}{\text{s}} + (3\cdot 1+ 1\cdot 2) \frac{\text{K}}{\text{s}} = 8 \frac{\text{K}}{\text{s}}$$
Diese Betrachtungsweise nennt man auch Langrangesche Betrachtungsweise.
Hier geht es weiter mit den Navier-Stokes-Gleichungen.

Copyright (c)...
Can you write Python code for it?

from IPython.core.display import HTML
def css_styling():
    styles = open('TFDStyle.css', 'r').read()
    return HTML(styles)
css_styling()


I am working on the file "2_Array_manipulation_routines_Solutions.ipynb".
The first task is:
Q10. Let x be an array [1, 2, 3, ..., 9]. Split x into 3 arrays, each of which has 4, 2, and 3 elements in the original order.
Can you write Python code for it?

x = np.arange(1, 10)
print np.split(x, [4, 6])


I am working on the file "2_Array_manipulation_routines_Solutions.ipynb".
The first task is:
Q11. Let x be an array

[[[  0.,   1.,   2.,   3.],

  [  4.,   5.,   6.,   7.]],

[[  8.,   9.,  10.,  11.],

  [ 12.,  13.,  14.,  15.]]].

Split it into two such that the first array looks like

[[[  0.,   1.,   2.],

  [  4.,   5.,   6.]],

[[  8.,   9.,  10.],

  [ 12.,  13.,  14.]]].

and the second one look like:

[[[  3.],

  [  7.]],

[[  11.],

  [ 15.]]].

Can you write Python code for it?

x = np.arange(16).reshape(2, 2, 4)
out1 = np.split(x, [3],axis=2)
out2 = np.dsplit(x, [3])
assert np.allclose(out1[0], out2[0])
assert np.allclose(out1[1], out2[1])
print out1


I am working on the file "2_Array_manipulation_routines_Solutions.ipynb".
The first task is:
Q12. Let x be an array

[[  0.,   1.,   2.,   3.],

 [  4.,   5.,   6.,   7.],

 [  8.,   9.,  10.,  11.],

 [ 12.,  13.,  14.,  15.]].

Split it into two arrays along the second axis.
Can you write Python code for it?

x = np.arange(16).reshape((4, 4))
out1 = np.hsplit(x, 2)
out2 = np.split(x, 2, 1)
assert np.allclose(out1[0], out2[0])
assert np.allclose(out1[1], out2[1])
print out1


I am working on the file "2_Array_manipulation_routines_Solutions.ipynb".
The first task is:
Q13. Let x be an array

[[  0.,   1.,   2.,   3.],

 [  4.,   5.,   6.,   7.],

 [  8.,   9.,  10.,  11.],

 [ 12.,  13.,  14.,  15.]].

Split it into two arrays along the first axis.
Can you write Python code for it?

x = np.arange(16).reshape((4, 4))
out1 = np.vsplit(x, 2)
out2 = np.split(x, 2, 0)
assert np.allclose(out1[0], out2[0])
assert np.allclose(out1[1], out2[1])
print out1


I am working on the file "2_Array_manipulation_routines_Solutions.ipynb".
The first task is:
Q14. Let x be an array [0, 1, 2]. Convert it to

[[0, 1, 2, 0, 1, 2],

 [0, 1, 2, 0, 1, 2]].
Can you write Python code for it?

x = np.array([0, 1, 2])
out1 = np.tile(x, [2, 2])
out2 = np.resize(x, [2, 6])
assert np.allclose(out1, out2)
print out1


I am working on the file "2_Array_manipulation_routines_Solutions.ipynb".
The first task is:
Q15. Let x be an array [0, 1, 2]. Convert it to <br/>
[0, 0, 1, 1, 2, 2].
Can you write Python code for it?

x = np.array([0, 1, 2])
print np.repeat(x, 2)


I am working on the file "2_Array_manipulation_routines_Solutions.ipynb".
The first task is:
Q16. Let x be an array [0, 0, 0, 1, 2, 3, 0, 2, 1, 0].<br/>
remove the leading the trailing zeros.
Can you write Python code for it?

x = np.array((0, 0, 0, 1, 2, 3, 0, 2, 1, 0))
out = np.trim_zeros(x)
print out


I am working on the file "2_Array_manipulation_routines_Solutions.ipynb".
The first task is:
Q17. Let x be an array [2, 2, 1, 5, 4, 5, 1, 2, 3]. Get two arrays of unique elements and their counts.
Can you write Python code for it?

x = np.array([2, 2, 1, 5, 4, 5, 1, 2, 3])
u, indices = np.unique(x, return_counts=True)
print u, indices


I am working on the file "2_Array_manipulation_routines_Solutions.ipynb".
The first task is:
Q18. Lex x be an array

[[ 1 2]

 [ 3 4].

Flip x along the second axis.
Can you write Python code for it?

x = np.array([[1,2], [3,4]])
out1 = np.fliplr(x)
out2 = x[:, ::-1]
assert np.allclose(out1, out2)
print out1


I am working on the file "2_Array_manipulation_routines_Solutions.ipynb".
The first task is:
Q19. Lex x be an array

[[ 1 2]

 [ 3 4].

Flip x along the first axis.
Can you write Python code for it?

x = np.array([[1,2], [3,4]])
out1 = np.flipud(x)
out2 = x[::-1, :]
assert np.allclose(out1, out2)
print out1


I am working on the file "2_Array_manipulation_routines_Solutions.ipynb".
The first task is:
Q2. Let x be array [[1, 2, 3], [4, 5, 6]]. Convert it to [1 4 2 5 3 6].
Can you write Python code for it?

x = np.array([[1, 2, 3], [4, 5, 6]])
out1 = np.ravel(x, order='F')
out2 = x.flatten(order="F")
assert np.allclose(out1, out2)
print out1


I am working on the file "2_Array_manipulation_routines_Solutions.ipynb".
The first task is:
Q4. Let x be an arbitrary 3-D array of shape (3, 4, 5). Permute the dimensions of x such that the new shape will be (4,3,5).
Can you write Python code for it?

x = np.zeros((3, 4, 5))
out1 = np.swapaxes(x, 1, 0)
out2 = x.transpose([1, 0, 2])
assert out1.shape == out2.shape
print out1.shape


I am working on the file "2_Array_manipulation_routines_Solutions.ipynb".
The first task is:
Q5. Let x be an arbitrary 2-D array of shape (3, 4). Insert a nex axis such that the new shape will be (3, 1, 4).
Can you write Python code for it?

x = np.zeros((3, 4))
print np.expand_dims(x, axis=1).shape


I am working on the file "2_Array_manipulation_routines_Solutions.ipynb".
The first task is:
Q6. Let x be an arbitrary 3-D array of shape (3, 4, 1). Remove a single-dimensional entries such that the new shape will be (3, 4).
Can you write Python code for it?

x = np.zeros((3, 4, 1))
print np.squeeze(x).shape


I am working on the file "2_Array_manipulation_routines_Solutions.ipynb".
The first task is:
Q7. Lex x be an array

[[ 1 2 3]

[ 4 5 6].

and y be an array

[[ 7 8 9]

[10 11 12]].

Concatenate x and y so that a new array looks like
[[1, 2, 3, 7, 8, 9],
[4, 5, 6, 10, 11, 12]].
Can you write Python code for it?

x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.array([[7, 8, 9], [10, 11, 12]])
out1 = np.concatenate((x, y), 1)
out2 = np.hstack((x, y))
assert np.allclose(out1, out2)
print out2


I am working on the file "2_Array_manipulation_routines_Solutions.ipynb".
The first task is:
Q8. Lex x be an array

[[ 1 2 3]

[ 4 5 6].

and y be an array

[[ 7 8 9]

[10 11 12]].

Concatenate x and y so that a new array looks like
[[ 1  2  3]

 [ 4  5  6]

 [ 7  8  9]

 [10 11 12]]
Can you write Python code for it?

x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.array([[7, 8, 9], [10, 11, 12]])
out1 = np.concatenate((x, y), 0)
out2 = np.vstack((x, y))
assert np.allclose(out1, out2)
print out2


I am working on the file "2_Classification/Classification I-student.ipynb".
The first task is:
Finally, to analyze the differences of the above classifiers, let's plot their classification boundaries.
The following function let you plot a classification boundary and classification regions for a given classifier. Please, use it to plot the classification regions of the above classifiers.
Can you write Python code for it?

# Plot the decision boundary
import matplotlib.pyplot as plt

def plot_boundary(clf, X, Y, plt):
    """Plot the classification regions for a given classifier.

    Args:
        clf: scikit-learn classifier object.
        X (numpy dnarray): training or test data to be plotted (number data x number dimensions). Only frist two
                            dimensions are ploted
        Y (numpy dnarray): labels of the training or test data to be plotted (number data x 1).
        plt: graphic object where you wish to plot

    """

    plot_colors = "brymc"
    plot_step = 0.02
    n_classes = np.unique(Y).shape[0]
    # Plot the decision regions
    x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    xx, yy = np.meshgrid(np.arange(x_min, x_max, plot_step),
                        np.arange(y_min, y_max, plot_step))

    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    cs = plt.contourf(xx, yy, Z, cmap=plt.cm.Paired)

    plt.xlabel('Feature 1')
    plt.ylabel('Feature 2')
    plt.axis("tight")

    # Plot the training points
    for i, color in zip(range(n_classes), plot_colors):
        idx = np.where(Y == i)
        plt.scatter(X[idx, 0], X[idx, 1], c=color, cmap=plt.cm.Paired)

    plt.axis("tight")

plt.figure(1,figsize=(6, 6))
plt.subplot(3,2,1)
plot_boundary(clf_hinge, X_train, Y_train, plt)
plt.subplot(3,2,3)
plot_boundary(clf_sqhinge, X_train, Y_train, plt)
plt.subplot(3,2,5)
plot_boundary(clf_log, X_train, Y_train, plt)
plt.subplot(3,2,2)
plot_boundary(clf_hinge, X_test, Y_test, plt)
plt.subplot(3,2,4)
plot_boundary(clf_sqhinge, X_test, Y_test, plt)
plt.subplot(3,2,6)
plot_boundary(clf_log, X_test, Y_test, plt)
plt.show()


I am working on the file "2_Classification/Classification I-student.ipynb".
The first task is:
Obtain the test accuracies of each classifier and, for the classifier with l1 regularization, analyze the values of their coefficients (check the variables of the object clf_l1reg).
Can you write Python code for it?

acc_noreg = clf_noreg.score(X_test,Y_test)
acc_l2reg = clf_l2reg.score(X_test,Y_test)
acc_l1reg = clf_l1reg.score(X_test,Y_test)
coef_l1reg =  clf_l1reg.coef_

print("The test accuracy without regularization is %2.2f%%" %(100*acc_noreg))
print("The test accuracy with l2 regularization is %2.2f%%" %(100*acc_l2reg))
print("The test accuracy with l1 regularization loss is %2.2f%%" %(100*acc_l1reg))
print("The classifier coefficients with l1 regularization are (%2.2f, %2.2f)" %(coef_l1reg[0][0], coef_l1reg[0][1]))

###########################################################
# TEST CELL
###########################################################
from test_helper import Test

# TEST accuracy values
Test.assertEquals(np.round(acc_noreg,2), 0.95, 'incorrect result: Accuracy of the classifier without regularization is uncorrect')
Test.assertEquals(acc_l2reg, 0.97, 'incorrect result: Accuracy of the classifier with l2 regularization is uncorrect')
Test.assertEquals(acc_l1reg, 0.9775, 'incorrect result: Accuracy of the classifier with l1 regularization is uncorrect')
Test.assertEquals(coef_l1reg[0][0], 0, 'incorrect result: Coefficients the classifier with l1 regularization are uncorrect')


I am working on the file "2_Classification/Classification I-student.ipynb".
The first task is:
Part 2: The effect of the regularization
Adding a regularization penalty in the classification cost function let you control the generalization capability of the classifier. Furthermore, some especial regularization terms provide additional properties such as the sparsity of the solution.
Here, we are going to analyze this effect by including a regularization term in the SGDClassifier used in the previous section. For this exercise, select the squared_hinge loss function and train the classifier:
* Without any regularization (penalty = 'none')
* Including an l2 regularization (penalty = 'l2')
* Including an l1 regularization (penalty = 'l1')
For two last cases, set the regularization factor to 1 (alpha=1).
Can you write Python code for it?

###########################################################
# TODO: Replace <FILL IN> with appropriate code
###########################################################

clf_noreg = SGDClassifier(loss='squared_hinge', penalty = 'none', alpha= 0, learning_rate='constant', eta0=0.001, n_iter=100)
clf_noreg.fit(X_train, Y_train)

clf_l2reg = #<FILL IN>

clf_l1reg = #<FILL IN>

print 'Classifiers sucessfully trained'


I am working on the file "2_Classification/Classification I-student.ipynb".
The first task is:
Part 3: From binary classifiers to multiclass formulations
When we have to face a multiclass problems, some classifiers are inherently multiclass (for instance, K-Nearest Neighbours); however, other ones have been designed for the binary case, so we have to extend them to the multiclass case. More common strategies relies on applying a 1 vs. 1 approach or a 1 vs. rest scheme.
By default, Scikit-learn implements all classifiers in a one vs. all (or one vs. rest) fashion (except for the linear SVM implemented by the SVC() method). However, to let the user select the multiclass implementation to be used, it also includes wrapper functions that work over the classifier to force the multiclass strategy to be applied: OneVsRestClassifier( ) and OneVsOneClassifier( ).
In this part of the lab session, we are going to learn to use these multiclass implementations. So, we are going to work with a multiclass dataset and we will use a linear SVM classifier as baseline method to analyze the different multiclass strategies.
 3.1 Load and prepare the data
The following code builds a bidimensional problem with four classes and 500 samples, it creates the training and testing partitions with the 60% and 40% of the original data and, it normalizes the data to zero mean and unitary standard deviation.
Can you write Python code for it?

import numpy as np
from sklearn import datasets
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import StandardScaler

# Initialize the random generator seed to compare results
np.random.seed(0)

# Generating artifial data
X, Y = datasets.make_classification(n_samples=500, n_features=2, n_classes = 4, n_clusters_per_class=1,
                                    class_sep=2, n_redundant=0, flip_y =0.01)

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=.4)

scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)


I am working on the file "2_Classification/Classification I-student.ipynb".
The first task is:
Part 4.2: Confusion matrix
In multiclass problems, the confusion matrix provides the percentage of well classified data over each class. Use the metrics.confusion_matrix() method to compute it over the performance of above SVM classifier.
Can you write Python code for it?


I am working on the file "2_Classification/Classification I-student.ipynb".
The first task is:
Part 4: Performance evaluation
Until now, we have evaluated the classifier performance by means of its accuracy over the test data, i.e., number of test samples correctly classified. Here, we are going to explore other useful measurements.
For the sake of simplicity, here we are going to work with the multiclass problem generated in the previous section and the standard multiclass SVC classifier (the one provided by the SVC( ) method).
 Part 4.1: Accuracy
We normally use the .score( ) method of the object classifier to compute the accuracy of a classifier. Use the metrics.accuracy_score( ) function to calculate it and check that you obtain the same value. Note that you need to obtain the classifier output for each test data to able to run the metrics.accuracy_score() function; review the method .predict() of the classifier object to compute this output.
Can you write Python code for it?

###########################################################
# TODO: Replace <FILL IN> with appropriate code
###########################################################

from sklearn.svm import SVC
clf = SVC(C=100, kernel='linear').fit(X_train, Y_train)

# Compute the test accuracy by means of .score()
acc_1 = clf.score(X_test,Y_test)

# Compute the test accuracy by means of accuracy_score()
from sklearn.metrics import accuracy_score
Y_pred = #<FILL IN>
acc_2 = accuracy_score(#<FILL IN>)

print("The test accuracy provided by score method is  %2.2f%%" %(100*acc_1))
print("The test accuracy provided by accuracy_score function is %2.2f%%" %(100*acc_2))

###########################################################
# TEST CELL
###########################################################
from test_helper import Test

# TEST accuracy values
Test.assertEquals(acc_1, acc_2, 'incorrect result: different accuracy values')


I am working on the file "2_Classification/Classification I-student.ipynb".
The first task is:
Review the methods of the objects clf_hinge, clf_sqhinge and clf_log classifiers and use the appropriate one to compute their classification accuracies over the text data.
Trick: if, for example, you write down "clf_hinge." plus tab key, you can check all the methods belonging to the object.
Can you write Python code for it?

###########################################################
# TODO: Replace <FILL IN> with appropriate code
###########################################################

# Compute the test accuracies
acc_hinge = clf_hinge.#<FILL IN>
acc_sqhinge = clf_sqhinge.#<FILL IN>
acc_log = clf_log.#<FILL IN>

print("The test accuracy with hinge loss is %2.2f%%" %(100*acc_hinge))
print("The test accuracy with squared hinge loss is %2.2f%%" %(100*acc_sqhinge))
print("The test accuracy with log loss is %2.2f%%" %(100*acc_log))

###########################################################
# TEST CELL
###########################################################
from test_helper import Test

# TEST accuracy values
Test.assertEquals(acc_hinge, 0.995, 'incorrect result: Accuracy of the classifier with hinge loss is uncorrect')
Test.assertEquals(np.round(acc_sqhinge,2), 0.95, 'incorrect result: Accuracy of the classifier with squared hinge loss is uncorrect')
Test.assertEquals(acc_log, 0.975, 'incorrect result: Accuracy of the classifier with log loss is uncorrect')


I am working on the file "2_Classification/Classification I-student.ipynb".
The first task is:
Use this code as example to obtain two different multiclass configurations: one vs. all and one vs. one. For this purpose, you can use the functions: OneVsRestClassifier( ) and OneVsOneClassifier( ). Check the number of classifiers that are built by each configuration (you can use access to the .estimators_ parameter of the resulting classifier object). Finally, compute their classification accuracies.
When you complete the code successfully, you can use the last cell to plot the classification boundaries.
Can you write Python code for it?

###########################################################
# TODO: Replace <FILL IN> with appropriate code
###########################################################

# Train a linear SVM with a 1 vs. all configuration
from sklearn.multiclass import OneVsRestClassifier
# 1 vs. rest
clf_1vsall = #<FILL IN>

# Train a linear SVM with a 1 vs. 1 configuration
from sklearn.multiclass import OneVsOneClassifier
# 1 vs. 1
clf_1vs1 = #<FILL IN>

# Check number of classifiers
num_clf_1vsall = #<FILL IN>
num_clf_1vs1 = #<FILL IN>
print("The number of classfiers in 1 vs. all configuration is %2.2f" %(num_clf_1vsall))
print("The number of classfiers 1 vs. all configuration is %2.2f" %(num_clf_1vs1))

# Compute classifier accuracies
acc_1vsall = #<FILL IN>
acc_1vs1 = #<FILL IN>
print("The test accuracy in 1 vs. all configuration is %2.2f%%" %(100*acc_1vsall))
print("The test accuracy in 1 vs. all configuration is %2.2f%%" %(100*acc_1vs1))

###########################################################
# TEST CELL
###########################################################
from test_helper import Test

# TEST accuracy values
Test.assertEquals(num_clf_1vsall, 4, 'incorrect result: Number of classfiers in 1 vs. all configuration is uncorrect')
Test.assertEquals(num_clf_1vs1, 6, 'incorrect result: Number of classfiers in 1 vs. 1 configuration is uncorrect')
Test.assertEquals(acc_1vsall, 0.955, 'incorrect result: Accuracy in 1 vs. all configuration is uncorrect')
Test.assertEquals(acc_1vs1, 0.975, 'incorrect result: Accuracy in 1 vs. 1 configuration is uncorrect')

# Plot classification boundaries
plt.figure(1,figsize=(8, 6))
plt.subplot(2,2,1)
plot_boundary(clf_1vsall, X_train, Y_train, plt)
plt.subplot(2,2,2)
plot_boundary(clf_1vs1, X_train, Y_train, plt)
plt.subplot(2,2,3)
plot_boundary(clf_1vsall, X_test, Y_test, plt)
plt.subplot(2,2,4)
plot_boundary(clf_1vs1, X_test, Y_test, plt)
plt.show()


I am working on the file "2_Classification/Classification II-student.ipynb".
The first task is:
1.3: Plotting K-NN classification boundary
To finish this section, we are going to plot classification boundary of the K-NN classifier. For this purpose, next cell contains the plot_boundary function used in the first lab session. Use this function to plot the classification boundary of the cross validated K-NN classifier over training and test data.
Can you write Python code for it?

# Plot the decision boundary
import matplotlib.pyplot as plt

def plot_boundary(clf, X, Y, plt):
    """Plot the classification regions for a given classifier.

    Args:
        clf: scikit-learn classifier object.
        X (numpy dnarray): training or test data to be plotted (number data x number dimensions). Only frist two
                            dimensions are ploted
        Y (numpy dnarray): labels of the training or test data to be plotted (number data x 1).
        plt: graphic object where you wish to plot

    """

    plot_colors = "brymc"
    plot_step = 0.02
    n_classes = np.unique(Y).shape[0]
    # Plot the decision regions
    x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    xx, yy = np.meshgrid(np.arange(x_min, x_max, plot_step),
                        np.arange(y_min, y_max, plot_step))

    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    cs = plt.contourf(xx, yy, Z, cmap=plt.cm.Paired)

    plt.xlabel('Feature 1')
    plt.ylabel('Feature 2')
    plt.axis("tight")

    # Plot the training points
    for i, color in zip(range(n_classes), plot_colors):
        idx = np.where(Y == i)
        plt.scatter(X[idx, 0], X[idx, 1], c=color, cmap=plt.cm.Paired)

    plt.axis("tight")

plt.figure(figsize=(8, 4))
# Plot classification boundary over training data
plt.subplot(1,2,1)
plot_boundary(clf, X_train, Y_train, plt)

# Plot classification boundary over test data
plt.subplot(1,2,2)
plot_boundary(clf, X_test, Y_test, plt)

plt.show()


I am working on the file "2_Classification/Classification II-student.ipynb".
The first task is:
2.3. SVM with polynomial kernel
Can you write Python code for it?

###########################################################
# TODO: Replace <FILL IN> with appropriate code
###########################################################

rang_d=np.arange(1,5)
tuned_parameters = [{'C': rang_C, 'degree': rang_d}]

# Train an SVM with polynomial kernel and adjust by CV the parameter C
clf_base =  svm.SVC(kernel='poly')
poly_svc  = # <FILL IN>
poly_svc.# <FILL IN>

# Save the values of C and degree selected and compute the final accuracy
C_opt = # <FILL IN>
d_opt = # <FILL IN>

print "The C value selected is " + str(C_opt)
print "The degree value selected is " + str(d_opt)
acc_poly_svc = poly_svc.score(X_test, Y_test)
print("The test accuracy of the polynomial SVM is %2.2f" %(100*acc_poly_svc))

###########################################################
# TEST CELL
###########################################################
from test_helper import Test

# TEST accuracy values
Test.assertEquals(np.round(C_opt, 2), 10 , 'incorrect result: The value of C_opt is uncorrect')
Test.assertEquals(np.round(d_opt, 2), 3, 'incorrect result: The value of d_opt is uncorrect')
Test.assertEquals(np.round(acc_poly_svc, 4), 0.9111, 'incorrect result: Test accuracy of the polynomial SVM after CV process is uncorrect')


I am working on the file "2_Classification/Classification II-student.ipynb".
The first task is:
2.4. Plot the classification boundaries and support vectors
The SVM decision function depends on some subset of the training data, called the support vectors. In this section we are going to compare the boundaries provided by the different kernel functions and study which training points are support vectors.
As in previous sections, you can use the plot_boundary( ) function to display the decision regions. To obtain the support vectors, you can access to the parameters "support_vectors_" of the svm classifier object. Add the necessary code to include them in the figure.
Can you write Python code for it?

###########################################################
# TODO: Replace <FILL IN> with appropriate code
###########################################################

plt.figure(1,figsize=(10, 4))

# Linear SVM
plt.subplot(1,2,1)
plot_boundary(lin_svc , X_train, Y_train, plt)
plt.title ('SVM boundary and training data')
plt.subplot(1,2,2)
plot_boundary(lin_svc , X_train, Y_train, plt)
SVs=# <FILL IN>
plt.scatter(# <FILL IN> )
plt.title ('SVM boundary, training data and SVs')

plt.figure(2,figsize=(10, 4))
# RBF SVM
plt.subplot(1,2,1)
plot_boundary(rbf_svc , X_train, Y_train, plt)
plt.title ('SVM boundary and training data')
plt.subplot(1,2,2)
plot_boundary(rbf_svc , X_train, Y_train, plt)
SVs=# <FILL IN>
plt.scatter(# <FILL IN> )
plt.title ('SVM boundary, training data and SVs')

plt.figure(3,figsize=(10, 4))
# Polynomial SVM
plt.subplot(1,2,1)
plot_boundary(poly_svc , X_train, Y_train, plt)
plt.title ('SVM boundary and training data')
plt.subplot(1,2,2)
plot_boundary(poly_svc , X_train, Y_train, plt)
SVs=# <FILL IN>
plt.scatter(# <FILL IN> )
plt.title ('SVM boundary, training data and SVs')

plt.show()