I am working on the file "Prototype Notebook/Sandstone Project_legacy.ipynb".
The first task is:
BIF Series
Can you write Python code for it?

sandstone.compute_potential_field("BIF_Series", verbose=1)
sandstone.plot_potential_field_2D(direction = "y", cell_pos = 12, figsize=(7,6), contour_lines = 100,
                                  potential_field = True)

sandstone.potential_interfaces, sandstone.layers[0].shape;

%matplotlib qt4
block = np.ones_like(sandstone.Z_x)
block[sandstone.Z_x>sandstone.potential_interfaces[0]] = 0
block[(sandstone.Z_x<sandstone.potential_interfaces[0]) * (sandstone.Z_x>sandstone.potential_interfaces[-1])] = 1
block[sandstone.Z_x<sandstone.potential_interfaces[-1]] = 2
block = block.reshape(40,40,80)
plt.imshow(block[:,13,:].T, origin = "bottom", aspect = "equal", extent = (sandstone.xmin, sandstone.xmax,
                                                                           sandstone.zmin, sandstone.zmax),
          interpolation = "none")


I am working on the file "Prototype Notebook/Sandstone Project_legacy.ipynb".
The first task is:
Defining Series
Can you write Python code for it?

sandstone.set_series({"EarlyGranite_Series":sandstone.formations[-1],
                      "BIF_Series":(sandstone.formations[0], sandstone.formations[1]),
                      "SimpleMafic_Series":sandstone.formations[2]},
                       order = ["EarlyGranite_Series",
                              "BIF_Series",
                              "SimpleMafic_Series"])
sandstone.series


I am working on the file "Prototype Notebook/Sandstone Project_legacy.ipynb".
The first task is:
Early granite
Can you write Python code for it?

sandstone.compute_potential_field("EarlyGranite_Series", verbose = 1)
sandstone.plot_potential_field_2D(direction = "y", cell_pos = 13, figsize=(7,6), contour_lines = 20,
                                  potential_field = True)

sandstone.potential_interfaces;

%matplotlib qt4
block = np.ones_like(sandstone.Z_x)
block[sandstone.Z_x>sandstone.potential_interfaces[0]] = 0
block[sandstone.Z_x<sandstone.potential_interfaces[-1]] = 1
block = block.reshape(40,40,80)
#block = np.swapaxes(block, 0, 1)
plt.imshow(block[:,8,:].T, origin = "bottom", aspect = "equal", extent = (sandstone.xmin, sandstone.xmax,
                                                                           sandstone.zmin, sandstone.zmax),
           interpolation = "none")


I am working on the file "Prototype Notebook/Sandstone Project_legacy.ipynb".
The first task is:
Export vtk
Can you write Python code for it?

"""Export model to VTK

Export the geology blocks to VTK for visualisation of the entire 3-D model in an
external VTK viewer, e.g. Paraview.

..Note:: Requires pyevtk, available for free on: https://github.com/firedrakeproject/firedrake/tree/master/python/evtk

**Optional keywords**:
    - *vtk_filename* = string : filename of VTK file (default: output_name)
    - *data* = np.array : data array to export to VKT (default: entire block model)
"""
vtk_filename = "noddyFunct2"

extent_x = 10
extent_y = 10
extent_z = 10

delx = 0.2
dely = 0.2
delz = 0.2
from pyevtk.hl import gridToVTK
# Coordinates
x = np.arange(0, extent_x + 0.1*delx, delx, dtype='float64')
y = np.arange(0, extent_y + 0.1*dely, dely, dtype='float64')
z = np.arange(0, extent_z + 0.1*delz, delz, dtype='float64')

# self.block = np.swapaxes(self.block, 0, 2)
gridToVTK(vtk_filename, x, y, z, cellData = {"geology" : sol})


I am working on the file "Prototype Notebook/Sandstone Project_legacy.ipynb".
The first task is:
GPU
Can you write Python code for it?

%%timeit
# Reset the block
sandstone.block.set_value(np.zeros_like(sandstone.grid[:,0]))

# Compute the block
sandstone.compute_block_model([0,1,2], verbose = 0)

sandstone.block_export.profile.summary()


I am working on the file "Prototype Notebook/Sandstone Project_legacy.ipynb".
The first task is:
Loading data from geomodeller
So there are 3 series, 2 of one single layer and 1 with 2. Therefore we need 3 potential fields, so lets begin.
Can you write Python code for it?

sandstone.load_data_csv("foliations", os.pardir+"/input_data/a_Foliations.csv")
sandstone.load_data_csv("interfaces", os.pardir+"/input_data/a_Points.csv")
pn.set_option('display.max_rows', 25)
sandstone.Foliations;

sandstone.Foliations


I am working on the file "Prototype Notebook/Sandstone Project_legacy.ipynb".
The first task is:
Performance Analysis
CPU
Can you write Python code for it?

%%timeit
sol =  interpolator.geoMigueller(dips,dips_angles,azimuths,polarity, rest, ref)[0]

sandstone.block_export.profile.summary()


I am working on the file "Prototyping semi-supervised.ipynb".
The first task is:
Measuring effectiveness.
Can you write Python code for it?

from sklearn.metrics import precision_score, accuracy_score, f1_score, recall_score

t_pred = label_prop_model.predict(x_test)

print("Metrics based on 50 hold-out points")

print("Macro")
print("accuracy: %f" % accuracy_score(t_test, t_pred))
print("precision: %f" % precision_score(t_test, t_pred, average='macro'))
print("recall: %f" % recall_score(t_test, t_pred, average='macro'))
print("f1: %f" % f1_score(t_test, t_pred, average='macro'))
print("\n\nMicro")
print("accuracy: %f" % accuracy_score(t_test, t_pred))
print("precision: %f" % precision_score(t_test, t_pred, average='micro'))
print("recall: %f" % recall_score(t_test, t_pred, average='micro'))
print("f1: %f" % f1_score(t_test, t_pred, average='micro'))

from sklearn import metrics
import matplotlib.pyplot as pl

labels = ["English", "French", "Greek", "Latin","Norse", "Other"]
labels_digits = [0, 1, 2, 3, 4, 5]
cm = metrics.confusion_matrix(t_test, t_pred, labels_digits)

fig = pl.figure()
ax = fig.add_subplot(111)
cax = ax.matshow(cm)
pl.title("Label Spreading with KNN kernel (k=7)")
fig.colorbar(cax)
ax.set_xticklabels([''] + labels)
ax.set_yticklabels([''] + labels)
pl.xlabel('Predicted')
pl.ylabel('True')

pl.show()


I am working on the file "Prototyping semi-supervised.ipynb".
The first task is:
PCA: Let's see what it looks like
Performing PCA
Can you write Python code for it?

supervised_vectors

import matplotlib.pyplot as pl

u, s, v = np.linalg.svd(supervised_vectors.toarray())
pca = np.dot(u[:,0:2], np.diag(s[0:2]))

english = np.empty((0,2))
french = np.empty((0,2))
greek = np.empty((0,2))
latin = np.empty((0,2))
norse = np.empty((0,2))
other = np.empty((0,2))

for i in range(pca.shape[0]):
    if targets[0].iloc[i] == "English":
        english = np.vstack((english, pca[i]))
    elif targets[0].iloc[i] == "French":
        french = np.vstack((french, pca[i]))
    elif targets[0].iloc[i] == "Greek":
        greek = np.vstack((greek, pca[i]))
    elif targets[0].iloc[i] == "Latin":
        latin = np.vstack((latin, pca[i]))
    elif targets[0].iloc[i] == "Norse":
        norse = np.vstack((norse, pca[i]))
    elif targets[0].iloc[i] == "Other":
        other = np.vstack((other, pca[i]))

pl.plot( english[:,0], english[:,1], "ro",
          french[:,0],  french[:,1], "bs",
           greek[:,0],   greek[:,1], "g+",
           latin[:,0],   latin[:,1], "c^",
           norse[:,0],   norse[:,1], "mD",
           other[:,0],   other[:,1], "kx")
pl.axis([-5,0,-2, 5])
pl.show()

print (s)


I am working on the file "Prototyping semi-supervised.ipynb".
The first task is:
Prepare Vectors
Can you write Python code for it?

# set up targets for the human-categorized data
targets = pd.DataFrame.from_dict(human_categorized, 'index')
targets[0] = pd.Categorical(targets[0])
targets['code'] = targets[0].cat.codes
# form: | word (label) | language | code (1-5)

tmp_dict = {}
for key in human_categorized:
    tmp_dict[key] = tsvopener.etymdict[key]
supervised_sents = pd.DataFrame.from_dict(tmp_dict, 'index')

all_sents = pd.DataFrame.from_dict(tsvopener.etymdict, 'index')
vectorizer = CountVectorizer(stop_words='english', max_features=10000)
all_sents.index.get_loc("anyways (adv.)")

# vectorize the unsupervised vectors.

vectors = vectorizer.fit_transform(all_sents.values[:,0])

print(vectors.shape)
# supervised_vectors = vectorizer.fit_transform(supervised_data.values[:,0])

# add labels

# initialize to -1
all_sents['code'] = -1

supervised_vectors = csr_matrix((len(human_categorized),
                                 vectors.shape[1]),
                                dtype=vectors.dtype)

j = 0
for key in supervised_sents.index:
    all_sents.loc[key]['code'] = targets.loc[key]['code']
    i = all_sents.index.get_loc(key)
    supervised_vectors[j] = vectors[i]
    j += 1

# supervised_vectors = csr_matrix((len(human_categorized),
#                                  unsupervised_vectors.shape[1]),
#                                 dtype=unsupervised_vectors.dtype)

# j = 0
# for key in supervised_data.index:
#     i = unsupervised_data.index.get_loc(key)
#     supervised_vectors[j] = unsupervised_vectors[i]
#     j += 1

all_sents.loc['dicky (n.)']


I am working on the file "Prototyping semi-supervised.ipynb".
The first task is:
Use Scikit's semisupervised learning
There are two semisupervised methods that scikit has. Label Propagation and Label Spreading. The difference is in how they regularize.
Can you write Python code for it?

num_points = 1000
num_test = 50

x = vstack([vectors[:num_points], supervised_vectors]).toarray()
t = all_sents['code'][:num_points].append(targets['code'])

x_test = x[-num_test:]
t_test = t[-num_test:]
x = x[:-num_test]
t = t[:-num_test]

label_prop_model = LabelSpreading(kernel='knn')
from time import time

print("fitting model")
timer_start = time()
label_prop_model.fit(x, t)
print("runtime: %0.3fs" % (time()-timer_start))

print("done!")

# unsupervised_data['code'].iloc[:1000]

import pickle

# with open("classifiers/labelspreading_knn_all_but_100.pkl", 'bw') as writefile:
#     pickle.dump(label_prop_model, writefile)

import smtplib

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("trevortds3@gmail.com", "Picardy3")

msg = "Job's done!"
server.sendmail("trevortds3@gmail.com", "trevortds@gmail.com", msg)
server.quit()

targets


I am working on the file "Prova1GustavoRev1.ipynb".
The first task is:
Exercicio 2 - Juros Compostos
O regime de juros compostos é o mais comum no sistema financeiro e portanto, o mais útil para cálculos de problemas do dia-a-dia. Os juros gerados a cada período são incorporados ao principal para o cálculo dos juros do período seguinte:
$$montante \cdot (1+\frac{taxa}{100}))^{meses}$$
Escreva a função JurosCompostos, cujas entradas são: o valor do montante original, a taxa de juros ao mês da aplicação (em %) e o número de meses da aplicação. A função deve retornar o montante total corrigido e o valor total da taxa dos juros compostos aplicados sobre o montante original (em %).
Can you write Python code for it?

def JurosCompostos(montante,taxa,meses):
    correcao = 1
    for i in range(meses):
        correcao *= (1+(taxa/100))
    return montante*correcao, (correcao - 1)*100
print(JurosCompostos(1000,1,12))


I am working on the file "Prova1GustavoRev1.ipynb".
The first task is:
Exercicio 3 - Imposto de renda
Sobre o salário bruto de um trabalhador incide contribuções e impostos. Os mais comuns são a contribuição para a aposentadoria e o imposto de renda. A base de cálculo do imposto de renda considera o salário bruto descontado das contribuições da aposentadoria. Considere que a alíquota da contribuição para aposentadoria seja de 11% sobre o total do salário bruto, independentemente do valor recebido durante o ano. Considere ainda que sobre a base de cálculo anual para o imposto de renda, para o valor de até 24 mil reais por ano, o trabalhador é isento de Imposto de renda. Somente sobre o que ultrapassar esse valor, será cobrado 15% a título de imposto de renda.
Escreva a função ImpostoRenda cuja entrada é o valor do salário bruto anual e cuja saída é o valor líquido recebido durante o ano.
Can you write Python code for it?

def ImpostoRenda(salario):
    salario = salario * 0.89
    if (salario <= 24000):
        return salario
    else:
        return salario - (0.15 * (salario-24000))
print(ImpostoRenda(30000))


I am working on the file "Prova1GustavoRev1.ipynb".
The first task is:
Exercício 4 - Somatória
Crie a função Soma que permita o calculo da seguinte somatória:
−1 + 2 − 3 + 4 − 5 + 6 + · · · + N
A função soma deve ter como parâmetro de entrada o número N e a saída igual a somatória acima.
Can you write Python code for it?

def Soma(n):
    soma = 0
    for i in range(1,n+1):
        soma = soma + i*( (-1)**i)
    return soma
print(Soma(10))


I am working on the file "Prova1GustavoRev1.ipynb".
The first task is:
Exercício 6 - Menor e maior da lista
Crie a função MaiorMenor, que dada uma lista de números como entrada, retorne o maior e menor elemento dessa lista.
Can you write Python code for it?

def MaiorMenor(lista):
    menor = lista[0]
    maior = lista[0]
    for elemento in lista:
        if elemento > maior:
            maior = elemento
        if elemento < menor:
            menor = elemento
    return maior, menor
lista = [1,2,4,16,32,64,-128]
print(MaiorMenor(lista))


I am working on the file "Prova1GustavoRev1.ipynb".
The first task is:
Exercício 7 - N-ímpares consecutivos
Sabe-se que um número da forma $n^3$ é igual a soma de $n$ ímpares consecutivos:
$$1^3 = 1,
2^3 = 3+5,
3^3 = 7+9+11,
4^3 = 13+15+17+19,...$$
Crie uma função que determine os $n$ ímpares consecutivos de $n^3$.
Can you write Python code for it?

def Consecutivos(n):
    soma = 0
    for i in range(1,n+1):
        soma += i
    impares = [x for x in range(2*soma) if x%2!=0 ]
    return impares[soma-n:soma]

print(Consecutivos(4), sum(Consecutivos(4)), 4**3)

type( (1,2))


I am working on the file "Prova2.ipynb".
The first task is:
Carregando dados de feriados: cdi e bmf.
Fonte: anbima
Can you write Python code for it?

feriado_arq = 'feriados.xlsx'
feriado_cdi = pd.read_excel(feriado_arq,'cdi')
feriado_bmf = pd.read_excel(feriado_arq,'bmf')


I am working on the file "Prova2.ipynb".
The first task is:
Definindo a função para cálculo do portfolio sem fazer o hedging:
Can you write Python code for it?

def calc_portf_semhedge(start,months,usdbrl,cdi,fwd_tenor,vol_tenor,swpprecdi_tenor):

    #init data
    df=portf_initdf(start,months,usdbrl,cdi,fwd_tenor,vol_tenor,swpprecdi_tenor)

    #caixa do prêmio
    df['cfwprem']=-df['premium'][0]*df['cdiprod']

    #portfolio
    df['portf']=df['premium']+df['cfwprem']

    return df


I am working on the file "Prova2.ipynb".
The first task is:
Definindo a função para inicializar o DataFrame com as séries de acordo com o prazo (tenor)
Can you write Python code for it?

def portf_initdf(start,months,usdbrl,cdi,fwd_tenor,vol_tenor,swpprecdi_tenor):
    term=relativedelta(months=months)
    end=start+term
    s=pd.Series(usdbrl['PX_LAST'][start:end])
    df=pd.DataFrame(s.index)
    df['t']=[(end.toordinal()-i.toordinal())/365 for i in df['Date']]
    df['tenor']=[i+term for i in s.index]
    df['usdbrl']=usdbrl['PX_LAST'][start:end].values
    df['cdi']=cdi['PX_LAST'][start:end].values/100
    df['cdi']=cdi['PX_LAST'][start:end].values/100
    cdiprod=[reduce(operator.mul,(1+df['cdi'][0:i])**(1/252),1) for i in range(1,len(df))]
    cdiprod.insert(0,1)
    df['cdiprod']=cdiprod
    df['fwd_tenor']=fwd_tenor[months][start:end].values
    df['vol_tenor']=vol_tenor[months][start:end].values/100
    df['pre_tenor']=swpprecdi_tenor[months][start:end].values/100
    df['strike']=df['fwd_tenor'][0]

    #interpolações
    if(months==1):
        tenor_short=df['Date']
        fwd_short=df['usdbrl']
        vol_short=df['vol_tenor']
        pre_short=df['cdi']

    if(months==3):
        tenor_short=[i+relativedelta(months=1) for i in s.index]
        fwd_short=fwd[1][start:end]
        vol_short=vol[1][start:end]/100
        pre_short=pre[1][start:end]/100

    if(months==12):
        tenor_short=[i+relativedelta(months=3) for i in s.index]
        fwd_short=fwd[3][start:end]
        vol_short=vol[3][start:end]/100
        pre_short=pre[3][start:end]/100

    df['fwd']=[fwd_short[i]+(df['fwd_tenor'][i]-fwd_short[i])*((end-tenor_short[i])/(df['tenor'][i]-tenor_short[i])) for i in df['Date'].index]
    df['vol']=[interpolaVol(tenor_short[i],vol_short[i],df['tenor'][i],df['vol_tenor'][i],end) for i in df['Date'].index]
    df['discount']=[pre_short[i]+(df['pre_tenor'][i]-pre_short[i])*((end-tenor_short[i])/(df['tenor'][i]-tenor_short[i])) for i in df['Date'].index]
    df['discount']=(1+df['discount'])**[-(end.toordinal()-i.toordinal())/365 for i in df['Date']]
    df=df.join(pd.DataFrame([bsv(call,df['fwd'][i],df['strike'][i],df['discount'][i],df['vol'][i],df['t'][i]) for...


I am working on the file "Prova2.ipynb".
The first task is:
Definindo função para fazer a interpolação da vol:
Can you write Python code for it?

def interpolaVol(dt0,v0,dt1,v1,dt):
    vv0=v0**2
    vv1=v1**2
    d0=dt0.toordinal()
    d1=dt1.toordinal()
    d=dt.toordinal()

    vv=((1/(d1-d0))*(vv1*d1*(d-d0)+vv0*d0*(d1-d)))/d
    return np.sqrt(vv)


I am working on the file "Prova2.ipynb".
The first task is:
d) O quarto comprou uma opção de 3m e fez o delta hedge até o final.
e) O quinto comprou uma opção de 12m e carregou sem hedge até o final.
Can you write Python code for it?


I am working on the file "Proyecto de Grado Ingeniería Electrónica/Workspace/RL/TowardsRL/.ipynb_checkpoints/reinforcement_q_learning-checkpoint.ipynb".
The first task is:
Input extraction
^^^^^^^^^^^^^^^^
The code below are utilities for extracting and processing rendered
images from the environment. It uses the torchvision package, which
makes it easy to compose image transforms. Once you run the cell it will
display an example patch that it extracted.
Can you write Python code for it?

resize = T.Compose([T.ToPILImage(),
                    T.Scale(40, interpolation=Image.CUBIC),
                    T.ToTensor()])

# This is based on the code from gym.
screen_width = 600

def get_cart_location():
    world_width = env.x_threshold * 2
    scale = screen_width / world_width
    return int(env.state[0] * scale + screen_width / 2.0)  # MIDDLE OF CART

def get_screen():
    screen = env.render(mode='rgb_array').transpose(
        (2, 0, 1))  # transpose into torch order (CHW)
    # Strip off the top and bottom of the screen
    screen = screen[:, 160:320]
    view_width = 320
    cart_location = get_cart_location()
    if cart_location < view_width // 2:
        slice_range = slice(view_width)
    elif cart_location > (screen_width - view_width // 2):
        slice_range = slice(-view_width, None)
    else:
        slice_range = slice(cart_location - view_width // 2,
                            cart_location + view_width // 2)
    # Strip off the edges, so that we have a square image centered on a cart
    screen = screen[:, :, slice_range]
    # Convert to float, rescare, convert to torch tensor
    # (this doesn't require a copy)
    screen = np.ascontiguousarray(screen, dtype=np.float32) / 255
    screen = torch.from_numpy(screen)
    # Resize, and add a batch dimension (BCHW)
    return resize(screen).unsqueeze(0)

env.reset()
plt.imshow(get_screen().squeeze(0).permute(1, 2, 0).numpy(), interpolation='none')
plt.show()


I am working on the file "Proyecto de Grado Ingeniería Electrónica/Workspace/RL/TowardsRL/.ipynb_checkpoints/reinforcement_q_learning-checkpoint.ipynb".
The first task is:
Now, let's define our model. But first, let quickly recap what a DQN is.
DQN algorithm
Our environment is deterministic, so all equations presented here are
also formulated deterministically for the sake of simplicity. In the
reinforcement learning literature, they would also contain expectations
over stochastic transitions in the environment.
Our aim will be to train a policy that tries to maximize the discounted,
cumulative reward
$R_{t_0} = \sum_{t=t_0}^{\infty} \gamma^{t - t_0} r_t$, where
$R_{t_0}$ is also known as the return. The discount,
$\gamma$, should be a constant between $0$ and $1$
that ensures the sum converges. It makes rewards from the uncertain far
future less important for our agent than the ones in the near future
that it can be fairly confident about.
The main idea behind Q-learning is that if we had a function
$Q^*: State \times Action \rightarrow \mathbb{R}$, that could tell
us what our return would be, if we were to take an action in a given
state, then we could easily construct a policy that maximizes our
rewards:
\begin{align}\pi^(s) = \arg!\max_a \ Q^(s, a)\end{align}
However, we don't know everything about the world, so we don't have
access to $Q^$. But, since neural networks are universal function
approximators, we can simply create one and train it to resemble
$Q^$.
For our training update rule, we'll use a fact that every $Q$
function for some policy obeys the Bellman equation:
\begin{align}Q^{\pi}(s, a) = r + \gamma Q^{\pi}(s', \pi(s'))\end{align}
The difference between the two sides of the equality is known as the
temporal difference error, $\delta$:
\begin{align}\delta = Q(s, a) - (r + \gamma \max_a Q(s', a))\end{align}
To minimise this error, we will use the Huber
loss <https://en.wikipedia.org/wiki/Huber_loss>__. The Huber loss acts
like the mean squared error when the error is small, but like the mean
absolute error when the error is large - this makes it more robust to
outliers when the estimates of $Q$ are very noisy. We calculate
this over a batch of transitions,...
Can you write Python code for it?

class DQN(nn.Module):

    def __init__(self):
        super(DQN, self).__init__()
        self.conv1 = nn.Conv2d(3, 16, kernel_size=5, stride=2)
        self.bn1 = nn.BatchNorm2d(16)
        self.conv2 = nn.Conv2d(16, 32, kernel_size=5, stride=2)
        self.bn2 = nn.BatchNorm2d(32)
        self.conv3 = nn.Conv2d(32, 32, kernel_size=5, stride=2)
        self.bn3 = nn.BatchNorm2d(32)
        self.head = nn.Linear(448, 2)

    def forward(self, x):
        x = F.relu(self.bn1(self.conv1(x)))
        x = F.relu(self.bn2(self.conv2(x)))
        x = F.relu(self.bn3(self.conv3(x)))
        return self.head(x.view(x.size(0), -1))


I am working on the file "Proyecto de Grado Ingeniería Electrónica/Workspace/RL/TowardsRL/.ipynb_checkpoints/reinforcement_q_learning-checkpoint.ipynb".
The first task is:
Reinforcement Learning (DQN) tutorial
Author: Adam Paszke <https://github.com/apaszke>_
This tutorial shows how to use PyTorch to train a Deep Q Learning (DQN) agent
on the CartPole-v0 task from the OpenAI Gym <https://gym.openai.com/>__.
Task
The agent has to decide between two actions - moving the cart left or
right - so that the pole attached to it stays upright. You can find an
official leaderboard with various algorithms and visualizations at the
Gym website <https://gym.openai.com/envs/CartPole-v0>__.
.. figure:: /_static/img/cartpole.gif
   :alt: cartpole
cartpole
As the agent observes the current state of the environment and chooses
an action, the environment transitions to a new state, and also
returns a reward that indicates the consequences of the action. In this
task, the environment terminates if the pole falls over too far.
The CartPole task is designed so that the inputs to the agent are 4 real
values representing the environment state (position, velocity, etc.).
However, neural networks can solve the task purely by looking at the
scene, so we'll use a patch of the screen centered on the cart as an
input. Because of this, our results aren't directly comparable to the
ones from the official leaderboard - our task is much harder.
Unfortunately this does slow down the training, because we have to
render all the frames.
Strictly speaking, we will present the state as the difference between
the current screen patch and the previous one. This will allow the agent
to take the velocity of the pole into account from one image.
Packages
First, let's import needed packages. Firstly, we need
gym <https://gym.openai.com/docs>__ for the environment
(Install using pip install gym).
We'll also use the following from PyTorch:

neural networks (torch.nn)
optimization (torch.optim)
automatic differentiation (torch.autograd)
utilities for vision tasks (torchvision - a separate
   package <https://github.com/pytorch/vision>__).
Can you write Python code for it?

import gym
import math
import random
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from collections import namedtuple
from itertools import count
from copy import deepcopy
from PIL import Image
from __future__ import print_function, division

import torch
import torch.nn as nn
import torch.optim as optim
import torch.autograd as autograd
import torch.nn.functional as F
import torchvision.transforms as T

env = gym.make('CartPole-v0')
env = env.unwrapped

is_ipython = 'inline' in matplotlib.get_backend()
if is_ipython:
    from IPython import display


I am working on the file "Proyecto de Grado Ingeniería Electrónica/Workspace/RL/TowardsRL/.ipynb_checkpoints/reinforcement_q_learning-checkpoint.ipynb".
The first task is:
Replay Memory
We'll be using experience replay memory for training our DQN. It stores
the transitions that the agent observes, allowing us to reuse this data
later. By sampling from it randomly, the transitions that build up a
batch are decorrelated. It has been shown that this greatly stabilizes
and improves the DQN training procedure.
For this, we're going to need two classses:

Transition - a named tuple representing a single transition in
   our environment
ReplayMemory - a cyclic buffer of bounded size that holds the
   transitions observed recently. It also implements a .sample()
   method for selecting a random batch of transitions for training.
Can you write Python code for it?

Transition = namedtuple('Transition',
                        ('state', 'action', 'next_state', 'reward'))

class ReplayMemory(object):

    def __init__(self, capacity):
        self.capacity = capacity
        self.memory = []
        self.position = 0

    def push(self, *args):
        """Saves a transition."""
        if len(self.memory) < self.capacity:
            self.memory.append(None)
        self.memory[self.position] = Transition(*args)
        self.position = (self.position + 1) % self.capacity

    def sample(self, batch_size):
        return random.sample(self.memory, batch_size)

    def __len__(self):
        return len(self.memory)


I am working on the file "Proyecto de Grado Ingeniería Electrónica/Workspace/RL/TowardsRL/.ipynb_checkpoints/reinforcement_q_learning-checkpoint.ipynb".
The first task is:
Training
Hyperparameters and utilities
This cell instantiates our model and its optimizer, and defines some
utilities:

Variable - this is a simple wrapper around
   torch.autograd.Variable that will automatically send the data to
   the GPU every time we construct a Variable.
select_action - will select an action accordingly to an epsilon
   greedy policy. Simply put, we'll sometimes use our model for choosing
   the action, and sometimes we'll just sample one uniformly. The
   probability of choosing a random action will start at EPS_START
   and will decay exponentially towards EPS_END. EPS_DECAY
   controls the rate of the decay.
plot_durations - a helper for plotting the durations of episodes,
   along with an average over the last 100 episodes (the measure used in
   the official evaluations). The plot will be underneath the cell
   containing the main training loop, and will update after every
   episode.
Can you write Python code for it?

BATCH_SIZE = 128
GAMMA = 0.999
EPS_START = 0.9
EPS_END = 0.05
EPS_DECAY = 200
USE_CUDA = torch.cuda.is_available()

model = DQN()
memory = ReplayMemory(10000)
optimizer = optim.RMSprop(model.parameters())

if USE_CUDA:
    model.cuda()

class Variable(autograd.Variable):

    def __init__(self, data, *args, **kwargs):
        if USE_CUDA:
            data = data.cuda()
        super(Variable, self).__init__(data, *args, **kwargs)

steps_done = 0

def select_action(state):
    global steps_done
    sample = random.random()
    eps_threshold = EPS_END + (EPS_START - EPS_END) * \
        math.exp(-1. * steps_done / EPS_DECAY)
    steps_done += 1
    if sample > eps_threshold:
        return model(Variable(state, volatile=True)).data.max(1)[1].cpu()
    else:
        return torch.LongTensor([[random.randrange(2)]])

episode_durations = []

def plot_durations():
    plt.figure(1)
    plt.clf()
    durations_t = torch.Tensor(episode_durations)
    plt.xlabel('Episode')
    plt.ylabel('Duration')
    plt.plot(durations_t.numpy())
    # Take 100 episode averages and plot them too
    if len(durations_t) >= 100:
        means = durations_t.unfold(0, 100, 1).mean(1).view(-1)
        means = torch.cat((torch.zeros(99), means))
        plt.plot(means.numpy())
    if is_ipython:
        display.clear_output(wait=True)
        display.display(plt.gcf())


I am working on the file "Proyecto de Grado Ingeniería Electrónica/Workspace/RL/TowardsRL/.ipynb_checkpoints/reinforcement_q_learning-checkpoint.ipynb".
The first task is:
Training loop
Finally, the code for training our model.
Here, you can find an optimize_model function that performs a
single step of the optimization. It first samples a batch, concatenates
all the tensors into a single one, computes $Q(s_t, a_t)$ and
$V(s_{t+1}) = \max_a Q(s_{t+1}, a)$, and combines them into our
loss. By defition we set $V(s) = 0$ if $s$ is a terminal
state.
Can you write Python code for it?

last_sync = 0

def optimize_model():
    global last_sync
    if len(memory) < BATCH_SIZE:
        return
    transitions = memory.sample(BATCH_SIZE)
    # Transpose the batch (see http://stackoverflow.com/a/19343/3343043 for
    # detailed explanation).
    batch = Transition(*zip(*transitions))

    # Compute a mask of non-final states and concatenate the batch elements
    non_final_mask = torch.ByteTensor(
        tuple(map(lambda s: s is not None, batch.next_state)))
    if USE_CUDA:
        non_final_mask = non_final_mask.cuda()
    # We don't want to backprop through the expected action values and volatile
    # will save us on temporarily changing the model parameters'
    # requires_grad to False!
    non_final_next_states = Variable(torch.cat([s for s in batch.next_state
                                                if s is not None]),
                                     volatile=True)
    state_batch = Variable(torch.cat(batch.state))
    action_batch = Variable(torch.cat(batch.action))
    reward_batch = Variable(torch.cat(batch.reward))

    # Compute Q(s_t, a) - the model computes Q(s_t), then we select the
    # columns of actions taken
    state_action_values = model(state_batch).gather(1, action_batch)

    # Compute V(s_{t+1}) for all next states.
    next_state_values = Variable(torch.zeros(BATCH_SIZE))
    next_state_values[non_final_mask] = model(non_final_next_states).max(1)[0]
    # Now, we don't want to mess up the loss with a volatile flag, so let's
    # clear it. After this, we'll just end up with a Variable that has
    # requires_grad=False
    next_state_values.volatile = False
    # Compute the expected Q values
    expected_state_action_values = (next_state_values * GAMMA) + reward_batch

    # Compute Huber loss
    loss = F.smooth_l1_loss(state_action_values, expected_state_action_values)

    # Optimize the model
    optimizer.zero_grad()
    loss.backward()
    for param in model.parameters():
        param.grad.data.clamp_(-1, 1)
    optimizer.step()


I am working on the file "Proyecto de Grado Ingeniería Electrónica/Workspace/RL/TowardsRL/reinforcement_q_learning.ipynb".
The first task is:
Below, you can find the main training loop. At the beginning we reset
the environment and initialize the state variable. Then, we sample
an action, execute it, observe the next screen and the reward (always
1), and optimize our model once. When the episode ends (our model
fails), we restart the loop.
Below, num_episodes is set small. You should download
the notebook and run lot more epsiodes.
Can you write Python code for it?

num_episodes = 10
for i_episode in xrange(num_episodes):
    # Initialize the environment and state
    env.reset()
    last_screen = get_screen()
    current_screen = get_screen()
    state = current_screen - last_screen
    for t in count():
        # Select and perform an action
        action = select_action(state)
        _, reward, done, _ = env.step(action[0, 0])
        reward = torch.Tensor([reward])

        # Observe new state
        last_screen = current_screen
        current_screen = get_screen()
        if not done:
            next_state = current_screen - last_screen
        else:
            next_state = None

        # Store the transition in memory
        memory.push(state, action, next_state, reward)

        # Move to the next state
        state = next_state

        # Perform one step of the optimization (on the target network)
        optimize_model()

        if done:
            episode_durations.append(t + 1)
            plot_durations()
            break


I am working on the file "Proyecto de Grado Ingeniería Electrónica/Workspace/RL/TowardsRL/reinforcement_q_learning.ipynb".
The first task is:
Reinforcement Learning (DQN) tutorial
Author: Adam Paszke <https://github.com/apaszke>_
This tutorial shows how to use PyTorch to train a Deep Q Learning (DQN) agent
on the CartPole-v0 task from the OpenAI Gym <https://gym.openai.com/>__.
Task
The agent has to decide between two actions - moving the cart left or
right - so that the pole attached to it stays upright. You can find an
official leaderboard with various algorithms and visualizations at the
Gym website <https://gym.openai.com/envs/CartPole-v0>__.
.. figure:: /_static/img/cartpole.gif
   :alt: cartpole
cartpole
As the agent observes the current state of the environment and chooses
an action, the environment transitions to a new state, and also
returns a reward that indicates the consequences of the action. In this
task, the environment terminates if the pole falls over too far.
The CartPole task is designed so that the inputs to the agent are 4 real
values representing the environment state (position, velocity, etc.).
However, neural networks can solve the task purely by looking at the
scene, so we'll use a patch of the screen centered on the cart as an
input. Because of this, our results aren't directly comparable to the
ones from the official leaderboard - our task is much harder.
Unfortunately this does slow down the training, because we have to
render all the frames.
Strictly speaking, we will present the state as the difference between
the current screen patch and the previous one. This will allow the agent
to take the velocity of the pole into account from one image.
Packages
First, let's import needed packages. Firstly, we need
gym <https://gym.openai.com/docs>__ for the environment
(Install using pip install gym).
We'll also use the following from PyTorch:

neural networks (torch.nn)
optimization (torch.optim)
automatic differentiation (torch.autograd)
utilities for vision tasks (torchvision - a separate
   package <https://github.com/pytorch/vision>__).
Can you write Python code for it?


I am working on the file "Proyecto de Grado Ingeniería Electrónica/Workspace/RL/TowardsRL/reinforcement_q_learning.ipynb".
The first task is:
Replay Memory
We'll be using experience replay memory for training our DQN. It stores
the transitions that the agent observes, allowing us to reuse this data
later. By sampling from it randomly, the transitions that build up a
batch are decorrelated. It has been shown that this greatly stabilizes
and improves the DQN training procedure.
For this, we're going to need two classses:

Transition - a named tuple representing a single transition in
   our environment
ReplayMemory - a cyclic buffer of bounded size that holds the
   transitions observed recently. It also implements a .sample()
   method for selecting a random batch of transitions for training.
Can you write Python code for it?


I am working on the file "Proyecto de Grado Ingeniería Electrónica/Workspace/RL/TowardsRL/reinforcement_q_learning.ipynb".
The first task is:
Training
Hyperparameters and utilities
This cell instantiates our model and its optimizer, and defines some
utilities:

Variable - this is a simple wrapper around
   torch.autograd.Variable that will automatically send the data to
   the GPU every time we construct a Variable.
select_action - will select an action accordingly to an epsilon
   greedy policy. Simply put, we'll sometimes use our model for choosing
   the action, and sometimes we'll just sample one uniformly. The
   probability of choosing a random action will start at EPS_START
   and will decay exponentially towards EPS_END. EPS_DECAY
   controls the rate of the decay.
plot_durations - a helper for plotting the durations of episodes,
   along with an average over the last 100 episodes (the measure used in
   the official evaluations). The plot will be underneath the cell
   containing the main training loop, and will update after every
   episode.
Can you write Python code for it?


I am working on the file "Proyecto de Grado Ingeniería Electrónica/Workspace/RL/TowardsRL/reinforcement_q_learning.ipynb".
The first task is:
Training loop
Finally, the code for training our model.
Here, you can find an optimize_model function that performs a
single step of the optimization. It first samples a batch, concatenates
all the tensors into a single one, computes $Q(s_t, a_t)$ and
$V(s_{t+1}) = \max_a Q(s_{t+1}, a)$, and combines them into our
loss. By defition we set $V(s) = 0$ if $s$ is a terminal
state.
Can you write Python code for it?


I am working on the file "Proyectos/Proyecto1.ipynb".
The first task is:
<span style = "color: red"> EJERCICIO 1 </span>

Escribe los metodos __repr__ y __str__ para la clase Array de forma que se imprima legiblemente como en numpy arrays.

2. Un validador
Ok esta es la parte aburrida... Los voy a ayudar un poco. Pero no deberia ser cualquier cosa un array, pues todas las filas deberian ser del mismo tamano no? Ademas, las filas deben tener entradas solo numericas... Por ultimo, cuando en vez de listas de listas solo hay una lista, conviene pensarlo como un vector (una columna... por eso yo prefiero llenar por columnas, pero esa es la forma python...).
Todo esto se puede lograr mejorando un poco el validador.
 Una nota al margen  Para hacer el validador voy a usar una de las herramientas mas poderosas de Python, listas de comprehension. Son formas faciles y rapidas de iterar en un solo comando. Un ejemplo:
Can you write Python code for it?

some_list = [1,2,3, 4, 5, 6]
[i**2 for i in some_list] # Elevar al cuadrado con listas de comprehension


I am working on the file "Proyectos/Proyecto1.ipynb".
The first task is:
<span style = "color: red"> EJERCICIO 2 </span>

Escribe el metodo __setitem__ para que el codigo A[i,j] = new_value cambie el valor de la entrada (i,j) del array. El esqueleto de la funcion es
python
class Array:
    #
    #
    def __setitem__(self, idx, new_value):
        "Ejercicio"
    #

4. Iniciar una matriz en ceros
Este no es un metodo de la clase, mas bien conviene que sea una funcion que crea un objeto de la clase en ceros. Vamos! Es el ejercicio mas facil, se los dejo a ustedes. Queremos que el resultado sea similar a la siguiente la funcion zeros de numpy
Can you write Python code for it?


I am working on the file "Proyectos/Proyecto1.ipynb".
The first task is:
<span style = "color: red"> EJERCICIO 3 </span>

Implementa una funcion de zeros para crear arrays "vacios"
python
def zeros(shape):
    "Implementame por favor"
Hint, encontraras utiles las listas de comprehension, por ejemplo el codigo [0. for x in range(5)] crea una lista de 5 ceros.
Implementa una funcion eye(n) que crea la matriz identidad de $n\times n$ (es decir, la matriz que tiene puros ceros y unos en la diagonal). El nombre eye es tradicional en software de algebra lineal, aunque no es muy intuitivo.

5. Transposicion
Otro ejercicio muy facil! Debe funcionar igual que numpy!
Can you write Python code for it?


I am working on the file "Proyectos/Proyecto1.ipynb".
The first task is:
<span style = "color: red"> EJERCICIO 4 </span>

Implementa la funcion de transposicion
python
class Array:
    ###
    ###
    ###
    def transpose(self):
        "Implementame :)"
        ###

6. Suma
Ok aqui llegamos al primer punto dificil! Pongan mucha atencion porque ustedes van a hacer la multiplicacion!!!!
Muchas clases tienen una nocion de suma, como vimos la suma en listas y en strings de Python es concatenacion, pero la suma de Arrays debe ser suma entrada por entrada, como en los arrays de numpy. Como logramos eso? Definiendo metodos para __add__ que reciben como argumentos self y other
Can you write Python code for it?

"hola " + "tu"

[1,2,3] + [2,3,4]

np.array([1,2,3]) + np.array([2,3,4])

np.array([1,2,3]) + 10 # Broadcasted sum, es muy util

class Array:
    "Una clase minima para algebra lineal"
    def __init__(self, list_of_rows):
        "Constructor y validador"
        # obtener dimensiones
        self.data = list_of_rows
        nrow = len(list_of_rows)
        #  ___caso vector: redimensionar correctamente
        if not isinstance(list_of_rows[0], list):
            nrow = 1
            self.data = [[x] for x in list_of_rows]
        # ahora las columnas deben estar bien aunque sea un vector
        ncol = len(self.data[0])
        self.shape = (nrow, ncol)
        # validar tamano correcto de filas
        if any([len(r) != ncol for r in self.data]):
            raise Exception("Las filas deben ser del mismo tamano")

    def __add__(self, other):
        "Hora de sumar"
        if isinstance(other, Array):
            if self.shape != other.shape:
                raise Exception("Las dimensiones son distintas!")
            rows, cols = self.shape
            newArray = Array([[0. for c in range(cols)] for r in range(rows)])
            for r in range(rows):
                for c in range(cols):
                    newArray.data[r][c] = self.data[r][c] + other.data[r][c]
            return newArray
        elif isinstance(2, (int, float, complex)): # en caso de que el lado derecho sea solo un numero
            rows, cols = self.shape
            newArray = Array([[0. for c in range(cols)] for r in range(rows)])
            for r in range(rows):
                for c in range(cols):
                    newArray.data[r][c] = self.data[r][c] + other
            return newArray
        else:
            return NotImplemented # es un tipo de error particular usado en estos metodos

A = Array([[1,2], [3,4]])
B = Array([[5,6], [7,8]])
C = A + B
C.data

D = A + 10

D.data


I am working on the file "Proyectos/Proyecto1.ipynb".
The first task is:
 EJERCICIO 5

(dificil) En nuestra clase Array la expresion A + 1 tiene sentido para un Array A, sin embargo la expresion inversa falla, por ejemplo
python
1 + Array([[1,2], [3,4]])
entrega un error. Investiga como implementar el metodo de clase __radd__ para resolver este problema.
Nuestro metodo de suma no sabe restar, implementa el metodo de clase __sub__ similar a la suma para poder calcular expresiones como A - B para A y B arrays o numeros.

7. Multiplicacion Matricial
Ahora si su ejercicio mas dificil y mas importante. Queremos que A * B sea multiplicacion matricial y a*A ser multiplicacion escalar cuando a es un numero.
NOTA IMPORTANTE!!!! Esta es la primera cosa que no esta implementada tal cual como numpy, pero es mucho mas padre hacerlo asi para algebra lineal. Numpy, al igual que las matrices de R, asumen que la multiplicacion debe ser entrada por entrada como += la suma. Otros lenguages como Julia y Matlab disenados para computo cientifico asumen que la multiplicacion de matrices es la default.
 EJERCICIO 6
Implementa las funciones __mul__ y __rmul__ para hacer multiplicacion matricial (y por un escalar). Hint. Entiende y modifica el codigo de suma del punto anterior
8. Vectores (Herencia!!!)
La clase de Arrays ya hace todo lo necesario para hacer algebra lineal, pero es un poco incomoda para trabajar con vectores. Quisieramos tener una clase Vector que heredara el comportamiento de los Arrays pero con facilidades adicionales para trabajar con vectores. Este es el concepto de "herencia" en programacion orientada a objetos. Lo poderoso de la herencia es que permite trabajar con objetos mas especializados, que pueden tener campos o metodos adicionales a los de su clase padre, pero heredan el comportamiento. Tambien puede hacer un override de metodos de los padres.
Can you write Python code for it?

class Vector(Array): # declara que Vector es un tipo de Array
    def __init__(self, list_of_numbers):
        self.vdata = list_of_numbers
        list_of_rows = [[x] for x in list_of_numbers]
        return Array.__init__(self, list_of_rows)
    def __repr__(self):
        return "Vector(" + str(self.vdata) + ")"
    def __str__(self):
        return str(self.vdata)
    def __add__(self, other):
        new_arr = Array.__add__(self, other)
        return Vector([x[0] for x in new_arr.data])

Vector([1,2,3]).__dict__

Vector([1,2,3])

Vector([1,2,3]) + Vector([5,-2,0])

Vector([1,2,3]) + 10


I am working on the file "Proyectos/Proyecto1.ipynb".
The first task is:
Comenzando desde cero...
Can you write Python code for it?

class Array:
    "Una clase minima para algebra lineal"
    def __init__(self, list_of_rows):
        "Constructor"
        self.data = list_of_rows
        self.shape = (len(list_of_rows), len(list_of_rows[0]))

A = Array([[1,2,3], [4,5,6]])
A.__dict__ # el campo escondido __dict__ permite acceder a las propiedades de clase de un objeto

A.data

A.shape


I am working on the file "Proyectos/Proyecto1.ipynb".
The first task is:
El campo data de un Array almacena la lista de listas del array. Necesitamos implementar algunos metodos para que sea funcional como una clase de algebra lineal.

Un metodo para imprimir una matriz de forma mas agradable
Validador. Un metodo para validar que la lista de listas sea valida (columnas del mismo tamano y que las listas interiores sean numericas
Indexing Hacer sentido a expresiones A[i,j]
Iniciar matriz vacia de ceros Este metodos es muy util para preacolar espacio para guardar nuevas matrices
Transposicion B.transpose()
Suma A + B
Multiplicacion escalar y matricial 2 * A y A*B
Vectores (Opcional)

Con esto seria posible hacer algebra lineal
Metodos especiales de clase...
Para hacer esto es posible usar metodos especiales de clase __getitem, __setitem__, __add__, __mul__, __str__. Teoricamente es posible hacer todo sin estos metodos especiales, pero, por ejemplo, es mucho mas agradable escribir A[i,j] que A.get(i,j) o A.setitem(i,j,newval) que A[i,j] = newval.
1. Un metodo para imprimir mejor...
Necesitamos agregar un metodo de impresion. Noten que un array de numpy se imprime bonito comparado con el nuestro
Can you write Python code for it?


I am working on the file "Proyectos/Proyecto1.ipynb".
The first task is:
En el validador uso la lista de comprehension para verificar que todas las filas tengo el mismo tamano. Tambien creo un error si no se cumple con el comando raise. Este es un uso avanzado, entonces si no tienen experiencia en Python no se preocupen mucho por los detalles.
Checar que todas las entradas son numericas lo dejamos como un ejericio optativo... no vale la pena deternos mucho en eso...
Can you write Python code for it?

class Array:
    "Una clase minima para algebra lineal"
    def __init__(self, list_of_rows):
        "Constructor y validador"
        # obtener dimensiones
        self.data = list_of_rows
        nrow = len(list_of_rows)
        #  ___caso vector: redimensionar correctamente
        if not isinstance(list_of_rows[0], list):
            nrow = 1
            self.data = [[x] for x in list_of_rows]
        # ahora las columnas deben estar bien aunque sea un vector
        ncol = len(self.data[0])
        self.shape = (nrow, ncol)
        # validar tamano correcto de filas
        if any([len(r) != ncol for r in self.data]):
            raise Exception("Las filas deben ser del mismo tamano")

    def __repr__(self):
        "Ejercicio"
        pass

    def __str__(self):
        "Ejercicio"
        pass

Array([[1,2,3], [4,5]])

vec = Array([1,2,3])
vec.data


I am working on the file "Proyectos/Proyecto1.ipynb".
The first task is:
En numpy podemos saber la dimension de un array con el campo shape de numpy
Can you write Python code for it?


I am working on the file "Proyectos/Proyecto1.ipynb".
The first task is:
Es posible sumar matrices y multiplicarlas por escalares
Can you write Python code for it?

B + 2*B # Python sabe sumar y multiplicar arrays como algebra lineal


I am working on the file "Proyectos/Proyecto1.ipynb".
The first task is:
Las matrices de numpy se pueden multiplicar con la funcion matmul dentro de numpy
Can you write Python code for it?


I am working on the file "Proyectos/Proyecto1.ipynb".
The first task is:
Los arrays the numpy pueden accesarse con indices y slices
Una entrada especifica:
Can you write Python code for it?


I am working on the file "Proyectos/Proyecto1.ipynb".
The first task is:
Para poder acceder a un index un metodo __getitem__.
Can you write Python code for it?

class Array:
    "Una clase minima para algebra lineal"
    def __init__(self, list_of_rows):
        "Constructor y validador"
        # obtener dimensiones
        self.data = list_of_rows
        nrow = len(list_of_rows)
        #  ___caso vector: redimensionar correctamente
        if not isinstance(list_of_rows[0], list):
            nrow = 1
            self.data = [[x] for x in list_of_rows]
        # ahora las columnas deben estar bien aunque sea un vector
        ncol = len(self.data[0])
        self.shape = (nrow, ncol)
        # validar tamano correcto de filas
        if any([len(r) != ncol for r in self.data]):
            raise Exception("Las filas deben ser del mismo tamano")

    def __getitem__(self, idx):
        return self.data[idx[0]][idx[1]]

A = Array([[1,2],[3,4]])
A[0,1]


I am working on the file "Proyectos/Proyecto1.ipynb".
The first task is:
Una columna entera:
Can you write Python code for it?


I am working on the file "Proyectos/Proyecto1.ipynb".
The first task is:
Una fila entera:
Can you write Python code for it?


I am working on the file "Práctica 0 - Problema 6 - Inciso 2.ipynb".
The first task is:
l=1
Can you write Python code for it?

l=1
rvals=np.linspace(.01,20,100)
besselvals=[sf.sph_jn(l,r)[0][l] for r in rvals]
coulomblowk=[coulomb(r,.2,l) for r in rvals]
coulombhighk=[coulomb(r,4.,l) for r in rvals]
coulombkone=[coulomb(r,1.,l) for r in rvals]

plt.plot(rvals,coulomblowk,label="k r_0=.2")
plt.plot(rvals,coulombkone,label="k r_0=1")
plt.plot(rvals,coulombhighk,label="k r_0=4")
plt.plot(rvals,besselvals, label="J_0 (kr_0->\infty)")
plt.xlabel("kr")
plt.legend()
plt.title("l="+str(l))
plt.ylim(-.5,1.)
plt.show()


I am working on the file "Práctica 0 - Problema 6 - Inciso 2.ipynb".
The first task is:
l=6
Can you write Python code for it?

l=6
rvals=np.linspace(.01,20,100)
besselvals=[sf.sph_jn(l,r)[0][l] for r in rvals]
coulomblowk=[coulomb(r,.2,l) for r in rvals]
coulombhighk=[coulomb(r,4.,l) for r in rvals]
coulombkone=[coulomb(r,1.,l) for r in rvals]

plt.plot(rvals,coulomblowk,label="k r_0=.2")
plt.plot(rvals,coulombkone,label="k r_0=1")
plt.plot(rvals,coulombhighk,label="k r_0=4")
plt.plot(rvals,besselvals, label="J_0 (kr_0->\infty)")
plt.xlabel("kr")
plt.legend()
plt.title("l="+str(l))
plt.ylim(-.25,.25)
plt.show()


I am working on the file "Pseudospectra of matrices.ipynb".
The first task is:
1. Jordan block
$$A_1 = \begin{pmatrix}
    0 & 1 &  &  &  & \
     & 0 & 1 &  &  & \
     &  & 0 & 1 &  & \
     &  &  & 0 & 1 & \
     &  &  &  & 0 & 1\
     &  &  &  &  & 0
\end{pmatrix}$$
Can you write Python code for it?

x1, y1 = np.mgrid[-1:1:npts*1j, -1:1:npts*1j]
A1 = diags([1.], [1], shape=(N, N))

ps_spec1 = pseudo_spec(x1, y1, A1)

plt.figure(figsize=(8,8))
plot_pseudo_spec(x1, y1, A1, ps_spec1)
plt.show()


I am working on the file "Pseudospectra of matrices.ipynb".
The first task is:
2. Limacon
$$A_2 = \begin{pmatrix}
    0 & 1 & 1 &  &  & \
     & 0 & 1 & 1 &  & \
     &  & 0 & 1 & 1 & \
     &  &  & 0 & 1 & 1\
     &  &  &  & 0 & 1\
     &  &  &  &  & 0
\end{pmatrix}$$
Can you write Python code for it?

x2, y2 = np.mgrid[-2:2:npts*1j, -2:2:npts*1j]
A2 = diags([1.], [1], shape=(N, N))

ps_spec2 = pseudo_spec(x2, y2, A2)

plt.figure(figsize=(8,8))
plot_pseudo_spec(x2, y2, A2, ps_spec2)
plt.show()


I am working on the file "Pseudospectra of matrices.ipynb".
The first task is:
4. Wilkinson matrix
$$A_4 = \begin{pmatrix}
    \frac{1}{N} & 1 &  &  &  & \
    & \frac{2}{N} & 1 &  &  & \
    &  & \frac{3}{N} & 1 &  & \
    &  &  & \frac{4}{N} & 1 & \
    &  &  &  & \ddots & 1\
    &  &  &  &  & 1
\end{pmatrix}$$
Can you write Python code for it?

x4, y4 = np.mgrid[-1:2:npts*1j, -1.5:1.5:npts*1j]
diag_0 = [k/N for k in range(1,N+1)]
diag_1 = [1 for k in range(N-1)]
A4 = diags([diag_0, diag_1], [0, 1], shape=(N, N))

ps_spec4 = pseudo_spec(x4, y4, A4)

plt.figure(figsize=(8,8))
plot_pseudo_spec(x4, y4, A4, ps_spec4)
plt.show()

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


I am working on the file "PuPPy Dec 2015-Parts of Speech in Python.ipynb".
The first task is:
3. create similar word list (stemming + synsets)
Can you write Python code for it?

from nltk.corpus import wordnet as wn
from pattern.en import parsetree
from pattern.search import taxonomy, WordNetClassifier, search

taxonomy.classifiers.append(WordNetClassifier())

def get_parts(word, pos, recursive=False):
    parts = [word, ]
    parts += taxonomy.children(word, pos=pos, recursive=recursive)
    parts += taxonomy.parents(word, pos=pos, recursive=recursive)
    return parts

def word_search(t, word, pos):
    parts = get_parts(word, pos)
    results = search(pos, t)
    for result in results:
        #  print result.string, parts
        if any(x in result.string.split() for x in parts):
            return True
    return False

def run_a_rule(sent, word, pos):
    t = parsetree(sent)
    return word_search(t, word, pos)


I am working on the file "PuPPy Dec 2015-Parts of Speech in Python.ipynb".
The first task is:
Alice's Yelp Data
Can you write Python code for it?

bad_sounds =['The sound in the place is terrible.',
            'dining with clatter and the occasional smell of BMW exausts',
            'Also, the acoustics are not conducive to having any sort of conversation.']
not_bad_sounds = ["not to sound like a snob",
                  "at your table and you can tune the sound to whichever game you're interested in",
                  "oh god I sound old!"]


I am working on the file "PuPPy Dec 2015-Parts of Speech in Python.ipynb".
The first task is:
Pattern
Can you write Python code for it?

from pattern.en import parse, pprint

s = parse(sent,
     tokenize = True,  # Tokenize the input, i.e. split punctuation from words.
         tags = True,  # Find part-of-speech tags.
       chunks = True,  # Find chunk tags, e.g. "the black cat" = NP = noun phrase.
    relations = True,  # Find relations between chunks.
      lemmata = True,  # Find word lemmata.
        light = False)
pprint(s)


I am working on the file "PuPPy Dec 2015-Parts of Speech in Python.ipynb".
The first task is:
TextBlob
Can you write Python code for it?

from textblob import TextBlob
blob = TextBlob(sent)
blob.parse()


I am working on the file "PuPPy Dec 2015-Parts of Speech in Python.ipynb".
The first task is:
pyStatParser
Can you write Python code for it?

from stat_parser import Parser
parser = Parser()
parser.parse(sent)
tree = parser.parse(sent) # returns nltk Tree instance
tree


I am working on the file "PuPPy Dec 2015-Parts of Speech in Python.ipynb".
The first task is:
spaCy
Can you write Python code for it?

from spacy.en import English
parser = English()
parsedData = parser(unicode(sent))

for i, token in enumerate(parsedData):
    print("original:", token.orth, token.orth_)
    print("lowercased:", token.lower, token.lower_)
    print("lemma:", token.lemma, token.lemma_)
    print("shape:", token.shape, token.shape_)
    print("prefix:", token.prefix, token.prefix_)
    print("suffix:", token.suffix, token.suffix_)
    print("log probability:", token.prob)
    print("Brown cluster id:", token.cluster)
    print("----------------------------------------")
    if i > 1:
        break


I am working on the file "Public Key Cryptography.ipynb".
The first task is:
42 is not a very exciting message to be sending to Alice however, plus our N is ridiculously small, very easy to factor.  What makes RSA hard to crack is we need those two prime factors of N to compute both its totient (as counting totatives won't do) and to thereby compute e's inverse i.e. d, the power we need to raise c by (mod N) to get our m back (the original message).
If you recall Chapter 4, our method for finding the inverse of an M-number involved using brute force.  We simply go through all the totatives until we find the right one.
Where huge Ns are involved, this technique is impractical. Is our whole cryptosystem just a pipe dream then? Assembling all the puzzle pieces to make RSA a reality was not easy.  Much wine was consumed in the process.
Extended Euclidean Algorithm
What comes to the rescue is called the Extended Euclidean Algorithm (EEA).  Lets look at that below.  It's purpose is to find two numbers, m and n, such that ma + nb gives the gcd of a and b.  This is always possible, even if the gcd is 1.
Can you write Python code for it?


I am working on the file "Public Key Cryptography.ipynb".
The first task is:
If our totative was some secret, raising it to a power modulo some public key N would encrypt it. To get our totative back, we'll need to raise it to an even higher power, effectively one higher than the totient power.
$(a^{\varphi(n)})^k \cdot a \equiv 1^ka \equiv a \mod{n}$
RSA in a Nutshell
The basic idea of RSA is raising a message m to some power e, modulo N, turns it into an encrypted message c.
You might think we would have a way of going backward.  If e is public, say 3 or 7, why not just take the 3rd or 7th root of c, modulo whatever N?  So far, no one has published a way to do that once N is large enough.  Raising m to the eth power is what we call a trap door function meaning an inverse function has yet to be discovered, despite best efforts.
Some special preparation is needed first, to randomly pad message m, a kind of seeding, otherwise some clever hacks may crack into the Bob-Alice transmission, allowing Eve to listen in.
Think of the message going part way around a circle when we raise it to the eth power.  We want it to go the rest of the way around the circle so that it pops out again, intact.  Given Euler's Theorem, one more power beyond the totient of N power will do the trick.
We need the inverse of c modulo that totient, in other words.  We've looked at code for that already, but will soon need a different method, given the large size of N.
In other words, raising anything to the 1st power is equivalent to the original.  A zeroth power gives the multiplicative identity, typically. A**0 == One, whatever One means in that namespace.  Raising to the totient power, modulo N, is like raising to the 0th power.
A**1 == A, in contrast.
$A^0 A = A^1 = A$.
Any power equivalent to 1, modulo totient(N), is our goal (thanks to Euler's Theorem), meaning that to decrypt c, we need some d such that (e * d) mod totient(N) == 1.  That will be like raising our message to the first power, not changing it at all.
$(m^e)^d = m^{ed} = m^{k \varphi(n) + 1}\equiv m \mod{n}$
One notch beyond the totient...
Can you write Python code for it?

from groups import M
N = 3 * 47                  # generate a new N from scratch, per web browser session
m = M(42, N)                # 42 is original message, N a product of 2 primes
e = 7                       # raise to power, which others take as a signal how far to go
c = m ** e                  # encrypted
d = ~M(e, totient(N))       # inverse of e mod totient(3 * 47), kept private
pow(c, d.val)               # getting our message, using secret d


I am working on the file "Public Key Cryptography.ipynb".
The first task is:
If you run the above code cell over and over, you'll always get the same answer: 1.
That's what Euler's Theorem asserts.
Lets test it for every totative of 100, why not?
Can you write Python code for it?

# convert to str for horizontal display
str([pow(coprime, 40, 100) for coprime in totatives(100)])


I am working on the file "Public Key Cryptography.ipynb".
The first task is:
So much for bare bones RSA.  If you want to develop a trully robust cryptosystem from scratch then do more reading outside the confines of this particular Notebook.  For example, 3 is probably not the best exponent to use at the outset.
You might be thinking all this coding from scratch is too much work and surely there's some 3rd party assets that make RSA a lot easier to work with.  You'd be right.  We did all this coding from scratch to gain deeper insights into the mathematics, however pyCrypto by Dwayne Litzenberger will do the job.
Let's check it out:
Can you write Python code for it?

from Crypto.PublicKey import RSA
key = RSA.generate( 2048 )  # that's 2048 bits
key


I am working on the file "Public Key Cryptography.ipynb".
The first task is:
Spending some quality time with both Euclid's Method (EA) for finding the gcd, and the above more elaborate method (EEA) is always good practice.
Their coding has gotten very clever over the years, as coding languages have evolved and become more expressive.
However these methods have their historical roots in the more distant past, before people programmed in any computer language.
Can you write Python code for it?


I am working on the file "Public Key Cryptography.ipynb".
The first task is:
That's another way of saying M(97, 100) has an inverse of M(33, 100), since once we subtract away the 100s (however many), we're left with 1.
Lets check that with our "brute force" method from the last chapter:
Can you write Python code for it?

from groups import M
def inverse(n : M, elems : set) -> M:
    for m in elems:
        if (n * m).val == 1:
            return m

a = M(97, 100)
elems = {M(x, 100) for x in totatives(100)}  # set comprehension
m = inverse(a, elems)
m


I am working on the file "Public Key Cryptography.ipynb".
The first task is:
The key object has everything you need to run a session.  We expect p * q to equal n.  Lets try:
Can you write Python code for it?


I am working on the file "Public Key Cryptography.ipynb".
The first task is:
To make these computations a little more clear, lets define inverse to take our coprime and modulus, and return the multiplicative inverse of our coprime.  Remember we were using brute force to find the inverse previously, knowing that every coprime has its inverse.  Now we have a way of finding this inverse without trying billions of candidates.
Can you write Python code for it?

def inverse(a, N):
    """
    If gcd(a,b)=1, then (inverse(a, b) * a) mod b = 1,
    otherwise, if gcd(a,b)!=1, return 0

    Useful in RSA encryption, for finding d such that
    e*d mod totient(n) == 1
    """
    m, n, gcd = xgcd(a, N) # is inverse of a mod N
    return (gcd==1) * m

print(inverse(7, 141))


I am working on the file "Public Key Cryptography.ipynb".
The first task is:
Why is this true?  47 has 46 totatives, 1 through 46, and 19 has 18 totatives.  Because they're prime numbers. Nothing less divides into them evenly (with no remainder) by definition, otherwise we would say they have factors, are composite.
Their product will have like a multiplication table of these two sets, imagine 1-46 and 1-18 across the top and down the side, like a pandas Dataframe (see Chapter 1). All those products make up the newer, bigger list of totatives, of the newer bigger product number of only two factors.
Below is a published RSA-number from Wikipedia, already factored for us.  If N were that easy to crack open, with a quick lookup in Wikipedia, RSA would not still serve as a first line of defense against unauthorized Eves.
The RSA company held contests to encourage people to try cracking them, and some of them did crack, leading to the practice of yet longer numbers, which take exponentially longer to crack according to current theories.  The discovery of some algorithm to factor giant numbers in little time, would revolutionize our vista, as far as cryptosystems go.
An Example
Here's that RSA number, named RSA-210:
Can you write Python code for it?


I am working on the file "Public Key Cryptography.ipynb".
The first task is:
Yep, we get 1 every time!  Thank you Euler.
Fermat's Little Theorem is in the same ballpark.  Check it out.  If N is a prime number, then totient(N) = N - 1.
$a^{p-1} \equiv 1 \mod(p)$ were gcd(a, p) == 1  (Fermat's Little Theorem)
Euler's Theorem will prove useful to us, as if we go one power higher, we effectively raise our message to the first power, which is to effectively leave m unchanged.
Can you write Python code for it?

from random import choice
coprime = choice(totatives(100)) # choose any one of 100's totatives (always coprime)
result = pow(coprime, 40 + 1, 100)  # one power higher than before
print(coprime, result)  # should always be the same number


I am working on the file "Pulsed_NMR/Analysis/20171114_echo_verification_and_time_uncertainty.ipynb".
The first task is:
The echo is still in the correct space!! Whoot whoot!
Time Uncertainty
Can you write Python code for it?

signal_1 = np.loadtxt(data_dir+"ch4.isf", delimiter=",")
signal_2 = np.loadtxt("../Data/20171114_NMR_data_2_ch4.isf", delimiter=",")
signal_3 = np.loadtxt("../Data/20171114_NMR_data_3_ch4.isf", delimiter=",")

endcap = 5000
plt.plot(signal_1[endcap:,0], signal_1[endcap:,1])
plt.plot(signal_2[endcap:,0], signal_2[endcap:,1])
plt.plot(signal_3[endcap:,0], signal_3[endcap:,1])

sig_new = signal_1[5000:,]
max_inds = arg(sig_new[:,1], np.greater, order=600)
plt.plot(sig_new[max_inds,0], sig_new[max_inds, 1], 'rp')
plt.plot(sig_new[:,0],sig_new[:,1], label = "signal")
echo_t = sig_new[max_inds[0][2],0]
echo_t -t0

sig_new = signal_2[5000:,]
max_inds = arg(sig_new[:,1], np.greater, order=600)
plt.plot(sig_new[max_inds,0], sig_new[max_inds, 1], 'rp')
plt.plot(sig_new[:,0],sig_new[:,1], label = "signal")
echo_t = sig_new[max_inds[0][2],0]
echo_t -t0

sig_new = signal_3[5000:,]
max_inds = arg(sig_new[:,1], np.greater, order=600)
plt.plot(sig_new[max_inds,0], sig_new[max_inds, 1], 'rp')
plt.plot(sig_new[:,0],sig_new[:,1], label = "signal")
echo_t = sig_new[max_inds[0][2],0]
echo_t -t0

peaks = [0.0014928000000000001, 0.0014851999999999999, 0.0014863999999999999]

##Uncertainty is half the difference between min and max
(0.0014928000000000001 - 0.0014851999999999999)/2


I am working on the file "Pulsed_NMR/Analysis/20171114_echo_verification_and_time_uncertainty.ipynb".
The first task is:
The maximum amplitude of the echo occurs at 1,493 $\mu$ s.
Can you write Python code for it?


I am working on the file "Pulsed_NMR/Analysis/20171114_echo_verification_and_time_uncertainty.ipynb".
The first task is:
The separation between the two peaks is about 704 $\mu$s. We set this in SRS to be 700 $\mu$ s so this is within the uncertainty that we set.
Can you write Python code for it?

sig_new = signal[5000:,]
max_inds = arg(sig_new[:,1], np.greater, order=600)
plt.plot(sig_new[max_inds,0], sig_new[max_inds, 1], 'rp')
plt.plot(sig_new[:,0],sig_new[:,1], label = "signal");

echo_t = sig_new[max_inds[0][2],0]
echo_t -t0


I am working on the file "Punto2.ipynb".
The first task is:
Podemos ver que el método no converge con el numero de clusters. Se estabiliza ligeramente con alrededor de 10 clusters, por lo que ese puede ser un numero util de clusters, sin embargo el desempeño sigue sin ser muy bueno y no hay ningun numero que sea evidentemente optimo.
Can you write Python code for it?

fig,ax=subplots(3,3,figsize=(10, 10))

n=1
for i in range(3):
    for j in range(3):
        ax[i,j].scatter(X[:,0],X[:,n],c=Y)
        n+=1

Xnorm=sklearn.preprocessing.normalize(X)
pca=sklearn.decomposition.PCA()
pca.fit(Xnorm)

fig,ax=subplots(1,3,figsize=(16, 4))
ax[0].scatter(pca.transform(X)[:,0],Y,c=Y)
ax[0].set_xlabel('Componente principal')
ax[0].set_ylabel('Tipo de vino')
ax[1].scatter(pca.transform(X)[:,0],pca.transform(X)[:,1],c=Y)
ax[1].set_xlabel('Componente principal')
ax[1].set_ylabel('segunda componente principal')

ax[2].plot(pca.explained_variance_ratio_)
xlabel('numero componentes')
ylabel('Varianza explicada')


I am working on the file "Putfile Tests.ipynb".
The first task is:
Attempt to put a file to a non-existing collection
Client should fail immediately
Can you write Python code for it?

%bitmag put-file -c integrationtest3 -i {TESTFILE1}


I am working on the file "Putfile Tests.ipynb".
The first task is:
Attempt to put a file to a non-existing pillar
Client should fail immediately
Can you write Python code for it?

%bitmag put-file -c integrationtest1 -i {TESTFILE1} -p non-existing-pillar


I am working on the file "Putfile Tests.ipynb".
The first task is:
Attempt to put a file which does not exist
Client should fail immediately
Can you write Python code for it?

%bitmag put-file -c integrationtest2 -f ThisFileDoesNotExist


I am working on the file "Putfile Tests.ipynb".
The first task is:
Idempotent test
Attempt to put a file with an fileID that is already present in the collection suppling the same checksum
The putFile request should succeed.
Can you write Python code for it?

%bitmag put-file -c integrationtest2 -f {TESTFILE1}


I am working on the file "Putfile Tests.ipynb".
The first task is:
Note, this will return the stored MD5 hashes, if the file is already put'et
Can you write Python code for it?

hash=!curl -s {URL} | md5sum - | cut -d' ' -f1
print("md5: {}".format(hash))
%bitmag put-file -c integrationtest2 -u {URL} -i {TESTFILE4} -C {hash.s} -S 'abcd' -R HMAC_SHA1


I am working on the file "Putfile Tests.ipynb".
The first task is:
Put a file by using an URL
Use the URL and Checksum to put a file, already on the webdav server.
Can you write Python code for it?

TESTFILE3=''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10))
URL='http://sandkasse-01.kb.dk/dav/test.txt'

hash=!curl -s {URL} | md5sum - | cut -d' ' -f1
print("md5: {}".format(hash))
%bitmag put-file -c integrationtest2 -u {URL} -i {TESTFILE3} -C {hash.s}


I am working on the file "Putfile Tests.ipynb".
The first task is:
Put a file including a request for checksum, but using both a salt and a non-salt algorithm.
Use the request-checksum-algorithm and request-checksum-salt arguments (-R and -S):
Can you write Python code for it?

TESTFILE5=''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10))

hash=!curl -s {URL} | md5sum - | cut -d' ' -f1
print("md5: {}".format(hash))

%bitmag put-file -c integrationtest2 -u {URL} -i {TESTFILE5} -C {hash.s} -R SHA1 -S 'abcd'


I am working on the file "Putfile Tests.ipynb".
The first task is:
Put a file with a different file-id to all pillars
Run put command on a file and use the file-id parameter (-i)
Can you write Python code for it?

TESTFILE2=''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10))

%bitmag put-file -c integrationtest2 -f {TESTFILE1} -i {TESTFILE2}


I am working on the file "Putfile Tests.ipynb".
The first task is:
The response should be
Count:  Size:   FileID:

Indicating that no pillar had the file.
If the file is found, delete it with the following command
See https://www.safaribooksonline.com/blog/2014/02/12/using-shell-commands-effectively-ipython/ for how this works
Can you write Python code for it?

hash=!cat {TESTFILE1} | md5sum - | cut -d' ' -f1
print("md5: {}".format(hash))

%bitmag delete -c integrationtest2 -i {TESTFILE1} -C {hash.s} -p "sbtape2"
%bitmag delete -c integrationtest2 -i {TESTFILE1} -C {hash.s}  -p "reference2"
%bitmag delete -c integrationtest2 -i {TESTFILE1} -C {hash.s}  -p "checksum2"
%bitmag delete -c integrationtest2 -i {TESTFILE1} -C {hash.s}  -p "sbdisk1"
%bitmag delete -c integrationtest2 -i {TESTFILE1} -C {hash.s}  -p "kbpillar2"

%bitmag put-file -c integrationtest2 -f {TESTFILE1}


I am working on the file "Putfile Tests.ipynb".
The first task is:
Verify that it is now present at al pillars, with the requested checksum
Can you write Python code for it?

%bitmag get-file-ids -c integrationtest2 -i {TESTFILE3}

%bitmag get-checksums -c integrationtest2 -i {TESTFILE3}


I am working on the file "Putfile Tests.ipynb".
The first task is:
Verify that it is now present at all pillars, and that it has the same checksum at all pillars
Can you write Python code for it?

%bitmag get-file-ids -c integrationtest2 -i {TESTFILE1}

%bitmag get-checksums -c integrationtest2 -i {TESTFILE1}


%bitmag get-file-ids -c integrationtest2 -i {TESTFILE2}

%bitmag get-checksums -c integrationtest2 -i {TESTFILE2}


I am working on the file "Putfile Tests.ipynb".
The first task is:
Verify that the checksumpillar and all the other pillars have the file
Can you write Python code for it?

%bitmag get-file-ids -c integrationtest2 -i {TESTFILE4}


I am working on the file "Putfile Tests.ipynb".
The first task is:
Verify that the checksumpillar does not reply with a checksum.
Can you write Python code for it?

%bitmag get-checksums -c integrationtest2 -i {TESTFILE4} -R HMAC_SHA1 -S 'abcd'


I am working on the file "Putting your model in production.ipynb".
The first task is:
Deamon mode
Can you write Python code for it?

gunicorn -w4 -b 0.0.0.0:5000 app:app --deamon --access-logfile logs/access.log --error-logfile logs/error.log -k gevent


I am working on the file "Putting your model in production.ipynb".
The first task is:
File: weibo_classification_resource.py
Can you write Python code for it?

from flask_restful import Resource
from flask import jsonify, request
from flask import Response

class WeiboClassificationResource(Resource):
    def get(self):
        return jsonify(hello='world!')

    def post(self):
        json_data = request.get_json(force=True)
        posts = json_data['user_posts']

        if posts[0] == 'fake':
            res_text = 'fake user!'
        else:
            res_text = 'real_user'

        return jsonify(classification_result=res_text)

# 1.
# GET http://localhost:5000/classify_user

# 2.
# POST http://localhost:5000/classify_user
# JSON:
#{
#    "user_posts": ["user post 1", "user post 2"]
#}


I am working on the file "Putting your model in production.ipynb".
The first task is:
Gunicorn

installing - pip install gunicorn
invoking

File: run.sh
Can you write Python code for it?

#!/usr/bin/env bash

source activate py36 # or virtualenv ....
gunicorn -b 0.0.0.0:5000 app:app

# in file: app.py
# app = Flask(__name__)


I am working on the file "Putting your model in production.ipynb".
The first task is:
Gunicorn
Multiple workers
Can you write Python code for it?

gunicorn -w4 -b 0.0.0.0:5000 app:app -k gevent


I am working on the file "Putting your model in production.ipynb".
The first task is:
Logging
Can you write Python code for it?

from flask import Flask
from flask_restful import Api
from resource.weibo_classification_resource import WeiboClassificationResource
from utils import set_logging

app = Flask(__name__)
api = Api(app)

# LOGGING
logging_config_file = 'config/logging_config.yml'
set_logging(logging_config_file)

api.add_resource(WeiboClassificationResource, '/classify_user',)

if __name__ == '__main__':
    app.run(port=5000)


I am working on the file "Putting your model in production.ipynb".
The first task is:
Nginx
very basic config
Can you write Python code for it?

http {
    upstream my_app {
        server localhost:5000;
    }

    server {
        listen 5001;

        location / {
            proxy_pass http://my_app1;
        }
    }
}


I am working on the file "Putting your model in production.ipynb".
The first task is:
Remember to set your Request / Response header to application/json
(Example POST request - in Postman)
Flask Application

Base flask "template"
installing flask related modules - pip install flask flask_restful ...  (all in pip)
virtual env / conda env
running with default python development server

$ python app.py
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [22/Sep/2017 15:31:49] "GET /classify_user HTTP/1.0" 200 -
File: app.py
Can you write Python code for it?

from flask import Flask
from flask_restful import Api
from resource.weibo_classification_resource import WeiboClassificationResource
from utils import set_logging

app = Flask(__name__)
api = Api(app)

# Handle our GETs and POSTs
api.add_resource(WeiboClassificationResource, '/classify_user',)

if __name__ == '__main__':
    app.run(port=5000)


I am working on the file "Putting your model in production.ipynb".
The first task is:
Scaling
nginx
Can you write Python code for it?

http {
    upstream my_app {
        server 127.0.0.1:5000 weight=3;
        server 123.456.789.002:5000;
        server 123.456.789.003:5000;
    }

    server {
        listen 5001;

        location / {
            proxy_pass http://myapp;
        }
    }
}


I am working on the file "Putting your model in production.ipynb".
The first task is:
Security
Header tokens
Can you write Python code for it?

from flask import jsonify, request
from flask import Response

class WeiboClassificationResource(Resource):

    def post(self):
        auth = request.headers.get('Authorization')
        if auth != 'super-secret-password':
            return Response("", status=401) # HTTP ERROR CODE UNAUTHORIZED


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:

Declaring variables
A central feature of programming is the declaration of variables.  When you declare a variable, you are storing data in the computer's memory and you are assigning a name to that data.  Both storage and name-assignment are carried out with the single equality symbol =.
Can you write Python code for it?


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
A more complicated two-input form of the range command produces a list of integers starting at a given number, and terminating before another given number.
Can you write Python code for it?


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
After you declare a variable, its value remains the same until it is changed.  You can change the value of a variable with a simple assignment.  After the above lines, the value of my_number is 17.
Can you write Python code for it?


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
All data in Python has a type, but a common complaint about Python is that types are a bit concealed "under the hood".  But they are not far under the hood.  Anyone can find out the type of some data with a single command.
Can you write Python code for it?

type(3)

type(3.0)

type('Hello')

type([1,2,3])


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
Booleans can be operated on by the standard logical operations and, or, not.  In ordinary English usage, "and" and "or" are conjunctions, while here in Boolean algebra, "and" and "or" are operations with Boolean inputs and Boolean output.  The precise meanings of "and" and "or" are given by the following truth tables.
| and || True | False |
|-----||------|-------|
|||||
| True || True | False |
| False || False | False|
| or || True | False |
|-----||------|-------|
|||||
| True || True | True |
| False || True | False|
Can you write Python code for it?

True and False

True or False

True or True

not True


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
Can you create a string with 100 A's (like AAA...)?  Use an appropriate operation in the cell below.
Can you write Python code for it?


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
Exercises

If a and b are integers, what is the length of the list range(a,b)?

Use a range command to produce the list [1,2,3,4,5,6,7,8,9,10].

Create the list [1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5] with a single range command and another operation.

How many multiples of 3 are there between 300 and 3000?
Can you write Python code for it?

#  Use this space to work on the exercises.


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
Exponents in Python are given by the ** operator.  The following lines compute 2 to the 1000th power, in two different ways.
Can you write Python code for it?


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
For number theory, division with remainder is an operation of central importance.  Integer division provides the quotient, and the operation % provides the remainder.  It's a bit strange that the percent symbol is used for the remainder, but this dates at least to the early 1970s and has become standard across computer languages.
Can you write Python code for it?

23 // 5  # Integer division

23 % 5  # The remainder after division


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
Here we have used the print command with strings and numbers together.  In Python 2.x, you can print multiple things on the same line by separating them by commas.  The "things" can be strings (enclosed by single or double-quotes) and numbers (int, float, etc.).
Can you write Python code for it?


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
How many multiples of 7 are between 10 and 100?  We can find out pretty quickly with the range command and the len command (to count).
Can you write Python code for it?

range(10,100,7)  # What list will this create?  It won't answer the question...

range(14,100,7)  # Starting at 14 gives the multiples of 7.

len(range(14,100,7))  # Gives the length of the list, and answers the question!


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
If we change the indentation, it changes the scope of the for loop.  Predict what the following loop will do, by unraveling, before evaluating it.
Can you write Python code for it?

P = 1
for n in range(1,6):
    P = P * n
print "n is",n,"and P is",P
print P


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
If we unravel the loop above, the linear sequence of commands interpreted by Python is the following.
Can you write Python code for it?

P = 1
n = 1
P = P * n
print "n is",n,"and P is",P
n = 2
P = P * n
print "n is",n,"and P is",P
n = 3
P = P * n
print "n is",n,"and P is",P
n = 4
P = P * n
print "n is",n,"and P is",P
n = 5
P = P * n
print "n is",n,"and P is",P
print P


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
Let's analyze the loop syntax in more detail.
python
P = 1
for n in range(1,6):
    P = P * n  # this command is in the scope of the loop.
    print "n is",n,"and P is",P  # this command is in the scope of the loop too!
print P
The "for" command ends with a colon :, and the next two lines are indented.  The colon and indentation are indicators of scope.  The scope of the for loop begins after the colon, and includes all indented lines.  The scope of the for loop is what is repeated in every step of the loop (in addition to the reassignment of n).
Can you write Python code for it?


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
Note in the code above, there are little "comments".  To place a short comment on a line of code, just put a hashtag # at the end of the line of code, followed by your comment.
Python gives a single command for division with remainder.  Its output is a tuple.
Can you write Python code for it?


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
Now is a good time for reflection.  Double-click in the cell below to answer the given questions.  Cells like this one are used for text rather than Python code.  Text is entered using markdown, but you can typically just enter text as you would in any text editor without problems.  Press shift-Enter after editing a markdown cell to complete the editing process.
Note that a dropdown menu in the toolbar above the notebook allows you to choose whether a cell is Markdown or Code (or a few other things), if you want to add or remove markdown/code cells.
Exercises

What data types have you seen, and what kinds of data are they used for?  Can you remember them without looking back?

How is division / interpreted differently for different types of data?

How is multiplication * interpreted differently for different types of data?

Why does Python have different types for short and long integers?

Double-click this markdown cell to edit it, and answer the exercises.

Calculating with booleans
A boolean (type bool) is the smallest possible piece of data.  While an int can be any integer, positive or negative, a boolean can only be one of two things:  True or False.  In this way, booleans are useful for storing the answers to yes/no questions.
Questions about (in)equality of numbers are answered in Python by operations with numerical input and boolean output.  Here are some examples.  A more complete reference is in the official Python documentation.
Can you write Python code for it?


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
Now is a good time to try a few computations of your own, in the empty cell below.  You can type any Python commands you want in the empty cell.  If you want to insert a new cell into this notebook, it takes two steps:
1.  Click to the left of any existing cell.  This should make a blue bar appear to the left of the cell.
2.  Use the keyboard shortcut a to insert a new cell above the blue-selected cell or b to insert a new cell below the blue-selected cell.
You can also use the keyboard shortcut x do delete a blue-selected cell... be careful!
Can you write Python code for it?


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
Python does give an interpretation to arithmetic operations with booleans and numbers.  Try to guess this interpretation with the following examples.  Change the examples to experiment!
Can you write Python code for it?


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
Python is an interpreted language, which means (roughly) that Python carries out commands line-by-line from top to bottom.  So consider the three lines
python
my_number = 17
new_number = my_number + 1
my_number = 3.14
Line 1 sets the value of my_number to 17.  Line 2 sets the value of new_number to 18.  Line 3 sets the value of my_number to 3.14.  But Line 3 does not change the value of new_number at all.
(This will become confusing and complicated later, as we study mutable and immutable types.)
Exercises

What is the difference between = and == in the Python language?

If the variable x has value 3, and you then evaluate the Python command x = x * x, what will be the value of x after evaluation?

Imagine you have two variables a and b, and you want to switch their values.  How could you do this in Python?
Can you write Python code for it?


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
Scopes can be nested by nesting indentation.  What do you think the following loop will do?  Can you unravel it?
Can you write Python code for it?

for x in [1,2,3]:
    for y in ['a', 'b']:
        print x,y


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
That seems better.  So what is going on?  Python interprets the input number 5 as an int (integer) and 5.0 as a float.  "Float" stands for "floating point number," which are decimal approximations to real numbers.  The word "float" refers to the fact that the decimal (or binary, for computers) point can float around (as in 1.2345 or 12.345 or 123.45 or 1234.5 or 0.00012345).  There are deep computational issues related to how computers handle decimal approximations, and you can read about the IEEE standards if you're interested.
The distinct results of 5/11 and 5.0/11.0 show that Python operates on different types in different ways.  The creators of Python decided that the result of integer division should be an integer:  specifically, the integer obtained by rounding down the quotient.
You might disagree with this decision... it is confusing at first!  In fact the designers of Python changed their mind.  If you're using Python 3.x, then the result of integer division 5/11 will be a float 0.4545...  But in versions 2.x, the result of integer division will be an int.  We assume that you are using Version 2.x throughout the tutorial!
To be safe, however, you can use the modified operation // for integer division.  This yields the same result in Python 2.x and 3.x.
Can you write Python code for it?


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
The final variant of the range command (for now) is the three-parameter command of the form range(a,b,s).  This produces a list like range(a,b), but with a "step size" of s.  In other words, it produces a list of integers, beginning at a, increasing by s from one entry to the next, and going up to (but not including) b.  It is best to experiment a bit to get the feel for it!
Can you write Python code for it?


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
The first line S = 0 is the initial declaration:  the value 0 is stored in memory, and the name S is assigned to this value.
The next line S = S + 1 looks like nonsense, as an algebraic sentence.  But reading = as assignment rather than equality, you should read the line S = S + 1 as assigning the value S + 1 to the name S.  When Python interprets S = S + 1, it carries out the following steps.

Compute the value of the right side, S+1.  (The value is 1, since S was assigned the value 0 in the previous line.)
Assign this value to the left side, S.  (Now S has the value 1.)

Well, this is a slight lie.  Python probably does something more efficient, when given the command S = S + 1, since such operations are hard-wired in the computer and the Python interpreter is smart enough to take the most efficient route.  But at this level, it is most useful to think of a self-referential assignment of the form X = expression(X) as a two step process as above.

Compute the value of expression(X).
Assign this value to X.

Now consider the following three commands.
Can you write Python code for it?

my_number = 17
new_number = my_number + 1
my_number = 3.14


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
The key to careful computation in Python is always being aware of the type of your data, and knowing how Python operates differently on data of different types.
Can you write Python code for it?


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
The range command is a flexible and powerful way to produce lists of integers.  The simplest single-input form of the range command produces a list starting at zero of a given length.
Can you write Python code for it?


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
The results are probably not surprising, except for the last one.  Try the following for contrast.
Can you write Python code for it?


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
There is another type called a tuple that we will rarely use.  Tuples use parentheses for enclosure instead of brackets.
Can you write Python code for it?


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
This ability of Python to interpret operations based on context is a mixed blessing.  On one hand, it leads to handy shortcuts -- quick ways of writing complicated programs.  On the other hand, it can lead to code that is harder to read, especially for a Python novice.  Good programmers aim for code that is easy to read, not just short!
The Zen of Python is a series of 20 aphorisms for Python programmers.  The first seven are below.

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.

Exercises

Did you look at the truth tables closely?  Can you remember, from memory, what True or False equals, or what True and False equals?

How might you easily remember the truth tables?  How do they resemble the standard English usage of the words "and" and "or"?

If you wanted to know whether a number, like 2349872348723, is a multiple of 7 but not a multiple of 11, how might you write this in one line of Python code?

You can chain together and commands, e.g., with an expression like True and True and True (which would evaluate to True).  You can also group booleans, e.g., with True and (True or False).

Experiment to figure out the order of operations (and, or, not) for booleans.

The operation xor means "exclusive or".  Its truth table is: True xor True = False and False xor False = False and True xor False = True and False xor True = True.  How might you implement xor in terms of the usual and, or, and not?
Can you write Python code for it?

#  Use this space to work on the exercises


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
This can be used for descending lists too, and observe that the final number b in range(a,b,s) is not included.
Can you write Python code for it?


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
This command reassigns the value of my_number to 3.14.  Note that it changes the type too!  It effectively overrides the previous value and replaces it with the new value.
Often it is useful to change the value of a variable incrementally or recursively.  Python, like many programming languages, allows one to assign variables in a self-referential way.  What do you think the value of S will be after the following four lines?
Can you write Python code for it?

S = 0
S = S + 1
S = S + 2
S = S + 3
print S


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
This might have been difficult!  So what if you want to trace through the loop, as it goes?  Sometimes, especially when debugging, it's useful to inspect every step of the loop to see what Python is doing.  We can inspect the loop above, by inserting a print command within the scope of the loop.
Can you write Python code for it?

P = 1
for n in range(1,6):
    P = P * n
    print "n is",n,"and P is",P
print P


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
Use the truth tables to predict the result (True or False) of each of the following, before evaluating the code.
Can you write Python code for it?

(2 > 3) and (3 > 2)

(1 + 1 == 2) or (1 + 1 == 3)

not (-1 + 1 >= 0)

2 + 2 == 4

2 + 2 != 4  # For "not equal", Python uses the operation `!=`.

2 + 2 != 5  # Is 2+2 *not* equal to 5?

not (2 + 2 == 5)  # The same as above, but a bit longer to write.


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
Which number is bigger:  $23^{32}$ or $32^{23}$?  Use the cell below to answer the question!
Can you write Python code for it?


I am working on the file "PwNT Notebook 1.ipynb".
The first task is:
With this command, the float 2.71828 is stored somewhere inside your computer, and Python can access this stored number by the name "e" thereafter.  So if you want to compute "e squared", a single command will do.
Can you write Python code for it?


I am working on the file "PwNT Notebook 2.ipynb".
The first task is:

Control statements
It is important for a computer program to behave differently under different circumstances.  The simplest control statements, if and its relative else, can be used to tell Python to carry out different actions depending on the value of a boolean variable.  The following function exhibits the syntax.
Can you write Python code for it?

def is_even(n):
    if n%2 == 0:
        print "%d is even."%(n)
        return True
    else:
        print "%d is odd."%(n)
        return False

is_even(17)

is_even(1000)


I am working on the file "PwNT Notebook 2.ipynb".
The first task is:

While loops and implementation of the Eucidean algorithm
We almost have all the tools we need to implement the Euclidean algorithm.  The last tool we will need is the while loop.  We have seen the for loop already, for iterating over a range of numbers.  The Euclidean algorithm involves repetition, but there is no way to know in advance how many steps it will take.  The while loop allows us to repeat a process as long as a boolean value (sometimes called a flag) is True.  The following countdown example illustrates the structure of a while loop.
Can you write Python code for it?

def countdown(n):
    current_value = n
    while current_value > 0:  # The condition (current_value > 0) is checked before every instance of the scope!
        print current_value
        current_value = current_value - 1

countdown(10)


I am working on the file "PwNT Notebook 2.ipynb".
The first task is:
Let's break down the syntax of the function declaration, line by line.
python
def square(x):
    answer = x * x
    return answer
The first line begins with the Python reserved word def.  (So don't use def as a variable name!).  The word def stands for "define" and it defines a function called square.  After the function name square comes parentheses (x) containing the argument x.  The arguments or parameters of a function refer to the input data.  Even if your function has no arguments, you need parentheses.  The argument x is used to name whatever number is input into the square function.
At the end of the function declaration line is a colon : and the following two lines are indented.  As in the case of for loops, the colon and indentation are signals of scope.  Everything on the indented lines is considered the scope of the function and is carried out when the function is used later.
The second line answer = x * x is the beginning of the scope of the function.  It declares a variable answer and sets the value to be x * x.  So if the argument x is 12, then answer will be set to 144.  The variable answer, being declared within the scope of the function, will not be accessible outside the scope of the function.  It is called a local variable.
The last line return answer contains the Python reserved word return, which terminates the function and outputs the value of the variable answer.  So when you apply the function with the command square(1.5), the number 1.5 is the argument x, and answer is 2.25, and that number 2.25 becomes the output.
A function does not have to return a value.  Some functions might just provide some information.  Here is a function which displays the result of division with remainder as a sentence with addition and multiplication.
Can you write Python code for it?

def display_divmod(a,b):
    quotient = a // b # Integer division
    remainder = a % b #
    print "%d = %d (%d) + %d"%(a,quotient,b,remainder)

display_divmod(23,5)


I am working on the file "PwNT Notebook 2.ipynb".
The first task is:
Note that the return dividend statement occurs after the scope of the while loop.  So as soon as the divisor variable equals zero, the funtion GCD returns the dividend variable and terminates.
Can you write Python code for it?