I am working on the file "Pytorch Gradient reversal.ipynb".
The first task is:
Modify the gradients
Keep forward pass the same.
The trick is to add $g(x)$, such that $g'(x)$ is the gradient modifier, during the forward pass and substract it as well. But stop gradients from flowing through the substraction part.
$f(x) + g(x) - g(x)$ will lead to gradients $f'(x) + g'(x) -g'(x)$. Since gradients don't flow through $-g'(x)$, hence we get new gradients as $f'(x) + g'(x)$
Can you write Python code for it?

gradient_modifier_op = g(tf_X)

sess.run(gradient_modifier_op)

modified_forward_op = (f(tf_X) + g(tf_X) - tf.stop_gradient(g(tf_X)))
modified_backward_op = tf.gradients(modified_forward_op, tf_X)

sess.run(modified_forward_op)

sess.run(modified_backward_op)

2*X + 3*(X**2) # This should match the gradients above


I am working on the file "Pytorch Gradient reversal.ipynb".
The first task is:
Pytoch case
Can you write Python code for it?

def zero_grad(X):
    if X.grad is not None:
        X.grad.data.zero_()

torch_X = Variable(torch.FloatTensor(X), requires_grad=True)

torch_X.data.numpy()

f(torch_X).data.numpy()

g(torch_X).data.numpy()

zero_grad(torch_X)
f_X = f(torch_X)
f_X.backward(torch.ones(f_X.size()))
torch_X.grad.data.numpy()

2*X


I am working on the file "Pytorch Gradient reversal.ipynb".
The first task is:
Pytorch backward hooks
Can you write Python code for it?

# Gradient reversal
zero_grad(torch_X)
f_X = f(torch_X)
f_X.register_hook(lambda grad: -grad)
f_X.backward(torch.ones(f_X.size()))
torch_X.grad.data.numpy()

-2*X

# Modified grad example
zero_grad(torch_X)
h = torch_X.register_hook(lambda grad: grad + 3*(torch_X*torch_X))
f_X = f(torch_X)
f_X.backward(torch.ones(f_X.size()))
h.remove()
torch_X.grad.data.numpy()

2*X + 3*(X*X) # It should be same as above


I am working on the file "PytorchModels/tutorials/advanced_tutorial.ipynb".
The first task is:
Create model
Can you write Python code for it?

class BiLSTM_CRF(nn.Module):

    def __init__(self, vocab_size, tag_to_ix, embedding_dim, hidden_dim):
        super(BiLSTM_CRF, self).__init__()
        self.embedding_dim = embedding_dim
        self.hidden_dim = hidden_dim
        self.vocab_size = vocab_size
        self.tag_to_ix = tag_to_ix
        self.tagset_size = len(tag_to_ix)

        self.word_embeds = nn.Embedding(vocab_size, embedding_dim)
        self.lstm = nn.LSTM(embedding_dim, hidden_dim // 2,
                            num_layers=1, bidirectional=True)

        # Maps the output of the LSTM into tag space.
        self.hidden2tag = nn.Linear(hidden_dim, self.tagset_size)

        # Matrix of transition parameters.  Entry i,j is the score of
        # transitioning *to* i *from* j.
        self.transitions = nn.Parameter(
            torch.randn(self.tagset_size, self.tagset_size))

        # These two statements enforce the constraint that we never transfer
        # to the start tag and we never transfer from the stop tag
        self.transitions.data[tag_to_ix[START_TAG], :] = -10000
        self.transitions.data[:, tag_to_ix[STOP_TAG]] = -10000

        self.hidden = self.init_hidden()

    def init_hidden(self):
        return (autograd.Variable(torch.randn(2, 1, self.hidden_dim // 2)),
                autograd.Variable(torch.randn(2, 1, self.hidden_dim // 2)))

    def _forward_alg(self, feats):
        # Do the forward algorithm to compute the partition function
        init_alphas = torch.Tensor(1, self.tagset_size).fill_(-10000.)
        # START_TAG has all of the score.
        init_alphas[0][self.tag_to_ix[START_TAG]] = 0.

        # Wrap in a variable so that we will get automatic backprop
        forward_var = autograd.Variable(init_alphas)

        # Iterate through the sentence
        for feat in feats:
            alphas_t = []  # The forward variables at this timestep
            for next_tag in range(self.tagset_size):
                # broadcast the emission score: it is the same regardless of
                # the...


I am working on the file "PytorchModels/tutorials/cifar10_tutorial.ipynb".
The first task is:
Define a Convolution Neural Network
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Copy the neural network from the Neural Networks section before and modify it to
take 3-channel images (instead of 1-channel images as it was defined).
Can you write Python code for it?

from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 16, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(16, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

net = Net()


I am working on the file "PytorchModels/tutorials/cifar10_tutorial.ipynb".
The first task is:
Define a Loss function and optimizer
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Let's use a Classification Cross-Entropy loss and SGD with momentum
Can you write Python code for it?

import torch.optim as optim

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)


I am working on the file "PytorchModels/tutorials/cifar10_tutorial.ipynb".
The first task is:
Let us show some of the training images, for fun.
Can you write Python code for it?

import matplotlib.pyplot as plt
import numpy as np

# functions to show an image

def imshow(img):
    img = img / 2 + 0.5     # unnormalize
    npimg = img.numpy()
    plt.imshow(np.transpose(npimg, (1, 2, 0)))

# get some random training images
dataiter = iter(trainloader)
images, labels = dataiter.next()

# show images
imshow(torchvision.utils.make_grid(images))
# print labels
print(' '.join('%5s' % classes[labels[j]] for j in range(4)))


I am working on the file "PytorchModels/tutorials/cifar10_tutorial.ipynb".
The first task is:
The output of torchvision datasets are PILImage images of range [0, 1].
We transform them to Tensors of normalized range [-1, 1]
Can you write Python code for it?

transform = transforms.Compose(
    [transforms.ToTensor(),
     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
                                        download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
                                          shuffle=True, num_workers=2)

testset = torchvision.datasets.CIFAR10(root='./data', train=False,
                                       download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=4,
                                         shuffle=False, num_workers=2)

classes = ('plane', 'car', 'bird', 'cat',
           'deer', 'dog', 'frog', 'horse', 'ship', 'truck')


I am working on the file "PytorchModels/tutorials/neural_networks_tutorial.ipynb".
The first task is:
Now, we have seen how to use loss functions.
Read Later:
The neural network package contains various modules and loss functions
  that form the building blocks of deep neural networks. A full list with
  documentation is here <http://pytorch.org/docs/nn>_
The only thing left to learn is:

updating the weights of the network

Update the weights
The simplest update rule used in practice is the Stochastic Gradient
Descent (SGD):
 ``weight = weight - learning_rate * gradient``

We can implement this using simple python code:
.. code:: python
learning_rate = 0.01
for f in net.parameters():
    f.data.sub_(f.grad.data * learning_rate)

However, as you use neural networks, you want to use various different
update rules such as SGD, Nesterov-SGD, Adam, RMSProp, etc.
To enable this, we built a small package: torch.optim that
implements all these methods. Using it is very simple:
Can you write Python code for it?

import torch.optim as optim

# create your optimizer
optimizer = optim.SGD(net.parameters(), lr=0.01)

# in your training loop:
optimizer.zero_grad()   # zero the gradient buffers
output = net(input)
loss = criterion(output, target)
loss.backward()
optimizer.step()    # Does the update


I am working on the file "PytorchModels/tutorials/neural_networks_tutorial.ipynb".
The first task is:
The input to the forward is an autograd.Variable, and so is the output.
Can you write Python code for it?

input = Variable(torch.randn(1, 1, 32, 32))
out = net(input)
print(out)


I am working on the file "PytorchModels/tutorials/neural_networks_tutorial.ipynb".
The first task is:
Zero the gradient buffers of all parameters and backprops with random
gradients:
Can you write Python code for it?


I am working on the file "Pytorch_Seq2Seq_Practice.ipynb".
The first task is:
Evaluate and predict model
구현한 모델의 training loss값들을 그래프로 확인하고, 번역성능을 확인해보도록 합니다.
Can you write Python code for it?

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np
%matplotlib inline

def showPlot(points):
    plt.figure()
    fig, ax = plt.subplots()
    # this locator puts ticks at regular intervals
    loc = ticker.MultipleLocator(base=0.2)
    ax.yaxis.set_major_locator(loc)
    plt.plot(points)

showPlot(plot_losses)

from predict import ModelPredictor

predictor = ModelPredictor(encoder1, attn_decoder1, input_lang, output_lang, MAX_LENGTH)
predictor.evaluate_randomly(train_pairs[:10])
predictor.predict_sentence("je comprends il est essentiel .")


I am working on the file "Pytorch_Seq2Seq_Practice.ipynb".
The first task is:
Implement Encoder Decoder Model
Encoder와 Decoder를 구현해봅니다.
구현하고자 하는 Encoder는 다음과 같은 구조로 구성되어 있습니다.

위에 그래프를 보면 input vector에 대해서 embedding을 하고 hidden vector와 GRU function을 통해 한 feed forward step을 하게 됩니다. 마지막으로, GRU를 통해서 output과 hidden vector를 각각 얻게 됩니다.
다음, 구현하고자 하는 Decoder의 구조는 아래와 같습니다.

Decoder구현은 Encoder보다 복잡합니다. Decoder를 보다 잘 이해하기 위해서 그래프 구조 설명 이전에 Decoder에 중요한 부분 중 하나인 Attention에 대한 설명을 하겠습니다.

Attention은 예측을 하고자 할 때 Input data에 대해 어디에 집중을 해야할 지 Encoder context에 가중치를 주는 역할을 합니다. 여기 Translator 모델에서는 매 스텝마다 들어오는 Decoder Input과 Hidden vector를 통해 Encoder context에 대한 가중치를 부여하여 Input에 대한 Output을 예측할 수 있도록 합니다.
모델의 전체과정 중 Attention 부분은 다음과 같습니다.
Input에 들어온 데이터는 embedding layer을 통해 이전 스텝의 hidden_vector와 결합을 합니다. 이후 softmax function을 거쳐 attn linear function을 두어 encoder_outputs와 matrix multiplication을 할 수 있도록 해줍니다.
Attention이 적용된 context vector는 input vector와 결합이 되어 hidden vector와 같이 GRU function에 들어갑니다. GRU에서 나온 output은 softmax를 처리하여 return 처리를 합니다.
이제 위 내용을 바탕으로 model을 구현해 보겠습니다.
models.py에 NotImplementedError라 표시된 영역에 구현해보겠습니다.
각 구현에 대한 순서는 다음과 같습니다.

Encoder 모델 __init__에 embedding과 gru 함수를 구현합니다.
Encoder 모델 forward 부분을 구현합니다. 방법은 아래와 같습니다.
Embedding function을 통해 word embedding layer를 구현합니다.
GRU function을 이용하여 multi layer RNN을 구현합니다.

Decoder 모델 forward 부분을 구현합니다. 방법은 아래와 같습니다.
Embedding functiondㅡㄹ 통해 word embedding layer를 구현합니다.
Attention Module을 구현합니다. (구현에 관한 내용은 위 그래프 Image를 참조하여 구현합니다.)
GRU function을 이용하여 multi layer RNN을 구현합니다.
Fully Connected Layer을 구현하고 Softmax를 통해 output data를 보입니다.
Can you write Python code for it?

from models import EncoderRNN, AttnDecoderRNN

hidden_size = 256
encoder1 = EncoderRNN(input_lang.n_words, hidden_size)
attn_decoder1 = AttnDecoderRNN(hidden_size, output_lang.n_words,
                               MAX_LENGTH, dropout_p=0.1)


I am working on the file "Q-Exploration.ipynb".
The first task is:
Load the environment
Can you write Python code for it?


I am working on the file "Q-Exploration.ipynb".
The first task is:
Some statistics on network performance
Can you write Python code for it?


I am working on the file "Q-Exploration.ipynb".
The first task is:
The Deep Q-Network
Helper functions
Can you write Python code for it?

class experience_buffer():
    def __init__(self, buffer_size = 10000):
        self.buffer = []
        self.buffer_size = buffer_size

    def add(self,experience):
        if len(self.buffer) + len(experience) >= self.buffer_size:
            self.buffer[0:(len(experience)+len(self.buffer))-self.buffer_size] = []
        self.buffer.extend(experience)

    def sample(self,size):
        return np.reshape(np.array(random.sample(self.buffer,size)),[size,5])

def updateTargetGraph(tfVars,tau):
    total_vars = len(tfVars)
    op_holder = []
    for idx,var in enumerate(tfVars[0:total_vars//2]):
        op_holder.append(tfVars[idx+total_vars//2].assign((var.value()*tau) + ((1-tau)*tfVars[idx+total_vars//2].value())))
    return op_holder

def updateTarget(op_holder,sess):
    for op in op_holder:
        sess.run(op)


I am working on the file "Q-Exploration.ipynb".
The first task is:
Training the network
Can you write Python code for it?

# Set learning parameters
exploration = "e-greedy" #Exploration method. Choose between: greedy, random, e-greedy, boltzmann, bayesian.
y = .99 #Discount factor.
num_episodes = 20000 #Total number of episodes to train network for.
tau = 0.001 #Amount to update target network at each step.
batch_size = 32 #Size of training batch
startE = 1 #Starting chance of random action
endE = 0.1 #Final chance of random action
anneling_steps = 200000 #How many steps of training to reduce startE to endE.
pre_train_steps = 50000 #Number of steps used before training updates begin.

tf.reset_default_graph()

q_net = Q_Network()
target_net = Q_Network()

init = tf.initialize_all_variables()
trainables = tf.trainable_variables()
targetOps = updateTargetGraph(trainables,tau)
myBuffer = experience_buffer()

#create lists to contain total rewards and steps per episode
jList = []
jMeans = []
rList = []
rMeans = []
with tf.Session() as sess:
    sess.run(init)
    updateTarget(targetOps,sess)
    e = startE
    stepDrop = (startE - endE)/anneling_steps
    total_steps = 0

    for i in range(num_episodes):
        s = env.reset()
        rAll = 0
        d = False
        j = 0
        while j < 999:
            j+=1
            if exploration == "greedy":
                #Choose an action with the maximum expected value.
                a,allQ = sess.run([q_net.predict,q_net.Q_out],feed_dict={q_net.inputs:[s],q_net.keep_per:1.0})
                a = a[0]
            if exploration == "random":
                #Choose an action randomly.
                a = env.action_space.sample()
            if exploration == "e-greedy":
                #Choose an action by greedily (with e chance of random action) from the Q-network
                if np.random.rand(1) < e or total_steps < pre_train_steps:
                    a = env.action_space.sample()
                else:
                    a,allQ = sess.run([q_net.predict,q_net.Q_out],feed_dict={q_net.inputs:[s],q_net.keep_per:1.0})
                    a = a[0]
            if...


I am working on the file "Q-Network.ipynb".
The first task is:
It also begins to progress through the environment for longer than chance aroudn the 750 mark as well.
Can you write Python code for it?


I am working on the file "Q-Network.ipynb".
The first task is:
Load the environment
Can you write Python code for it?


I am working on the file "Q-Network.ipynb".
The first task is:
Some statistics on network performance
We can see that the network beings to consistly reach the goal around the 750 episode mark.
Can you write Python code for it?


I am working on the file "Q-Network.ipynb".
The first task is:
The Q-Network Approach
Implementing the network itself
Can you write Python code for it?

tf.reset_default_graph()

#These lines establish the feed-forward part of the network used to choose actions
inputs1 = tf.placeholder(shape=[1,16],dtype=tf.float32)
W = tf.Variable(tf.random_uniform([16,4],0,0.01))
Qout = tf.matmul(inputs1,W)
predict = tf.argmax(Qout,1)

#Below we obtain the loss by taking the sum of squares difference between the target and prediction Q values.
nextQ = tf.placeholder(shape=[1,4],dtype=tf.float32)
loss = tf.reduce_sum(tf.square(nextQ - Qout))
trainer = tf.train.GradientDescentOptimizer(learning_rate=0.1)
updateModel = trainer.minimize(loss)


I am working on the file "Q-Table.ipynb".
The first task is:
Implement Q-Table learning algorithm
Can you write Python code for it?

#Initialize table with all zeros
Q = np.zeros([env.observation_space.n,env.action_space.n])
# Set learning parameters
lr = .8
y = .95
num_episodes = 2000
#create lists to contain total rewards and steps per episode
#jList = []
rList = []
for i in range(num_episodes):
    #Reset environment and get first new observation
    s = env.reset()
    rAll = 0
    d = False
    j = 0
    #The Q-Table learning algorithm
    while j < 99:
        j+=1
        #Choose an action by greedily (with noise) picking from Q table
        a = np.argmax(Q[s,:] + np.random.randn(1,env.action_space.n)*(1./(i+1)))
        #Get new state and reward from environment
        s1,r,d,_ = env.step(a)
        #Update Q-Table with new knowledge
        Q[s,a] = Q[s,a] + lr*(r + y*np.max(Q[s1,:]) - Q[s,a])
        rAll += r
        s = s1
        if d == True:
            break
    #jList.append(j)
    rList.append(rAll)

print("Score over time: " +  str(sum(rList)/num_episodes))

print("Final Q-Table Values")
print(Q)


I am working on the file "Q001-Q010.ipynb".
The first task is:
Q002
Even Fibonacci numbers
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
Can you write Python code for it?

s=0
a=1
b=1
while b < 4E6:
    if b % 2 ==0: s=s+b;
    c=a+b
    a=b
    b=c
print s


I am working on the file "Q001-Q010.ipynb".
The first task is:
Q003
Largest prime factor
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
Can you write Python code for it?


I am working on the file "Q001-Q010.ipynb".
The first task is:
Q004
Largest palindrome product
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
Can you write Python code for it?


I am working on the file "Q001-Q010.ipynb".
The first task is:
Q007
10001st prime
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10 001st prime number?
Can you write Python code for it?

def is_prime(n):
    if n < 2:
        return False
    elif n < 4:
        return True
    elif (n % 2 == 0) or (n % 3 == 0):
        return False
    i = 5
    while i*i < n + 1:
        if (n % i == 0):
            return False
        i=i+1
    return True

i=1
count_prime=0
while count_prime<10001:
    i+=1
    if is_prime(i):
        count_prime+=1
print i,"#",count_prime


I am working on the file "Q001-Q010.ipynb".
The first task is:
Q009
Special Pythagorean triplet
A Pythagorean triplet is a set of three natural numbers, $a < b < c$, for which,
$a^2 + b^2 = c^2$
For example, $3^2 + 4^2 = 9 + 16 = 25 = 5^2$.
There exists exactly one Pythagorean triplet for which $a + b + c = 1000$.
Find the product abc.
Can you write Python code for it?

# By substituting c=1000-(a+b) into Pythagorean equality, we get
# n^2 = 2n(a+b) - 2ab, n=1000 here

def f(a,b,n):
    return 2*n*(a+b) - 2*a*b - n*n

import numpy as np
n=1000
X,Y=np.meshgrid(range(1,n),range(1,n))
X=np.tril(X,-1).ravel()
Y=np.tril(Y,-1).ravel()
for p in zip(X[X>0],Y[Y>0]):
    if f(p[0],p[1],n) == 0:
        print p[0],p[1], n-p[0]-p[1]
        print p[0]*p[1]*(n-p[0]-p[1])


I am working on the file "Q011-Q020.ipynb".
The first task is:
Q012
Highly divisible triangular number
The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
Let us list the factors of the first seven triangle numbers:
1: 1
 3: 1,3
 6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28
We can see that 28 is the first triangle number to have over five divisors.
What is the value of the first triangle number to have over five hundred divisors?
Can you write Python code for it?

import sympy.ntheory as ntheory
i=1
T=0
n_divisors=0
while n_divisors <= 500:
    T=T+i
    n_divisors=ntheory.divisor_count(T)
    i=i+1
print T


I am working on the file "Q011-Q020.ipynb".
The first task is:
Q015
Lattice paths

Starting in the top left corner of a 2×2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner.
How many such routes are there through a 20×20 grid?
for 2x2 case, we find out the cominations of these 4 operations:

RRDD
RDRD
RDDR
DRRD
DRDR
DDRR

Thus for 20x20 case, we find out the cominations of these 40 operations::
RRRRRRRRRRRRRRRRRRRRDDDDDDDDDDDDDDDDDDDD
or in binary:
1111111111111111111100000000000000000000
which is a problem of combinations
Can you write Python code for it?

from math import factorial as f
n=20
print f(2*n)/(f(n)*f(n))


I am working on the file "Q011-Q020.ipynb".
The first task is:
Q016
Power digit sum
$2^{15}$ = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
What is the sum of the digits of the number $2^{1000}$?
Can you write Python code for it?

n=1000
a=long(2**n)
s=0
while a > 0:
    b=a%10
    s+=b
    a=a/10
print s


I am working on the file "Q011-Q020.ipynb".
The first task is:
Q017
Number letter counts
If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.
Can you write Python code for it?

a=['','one','two','three','four','five','six','seven','eight','nine']
a1=['ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eightine','ninetine']
b=['','ten','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety']
c=[i+' hundred' for i in a]
print c

def words(n):
    s=str(n)
    word=[]
    if len(s) == 1:
        word.append(a[n])
    elif len(s) == 2:
        if n % 100 < 10:
            word.append(a[n%10])
        elif n % 100 < 20:
            word.append(a1[n%10])
        else:
            word.append(a[n%10])
            p=n/10 % 10
            word.append(b[p])
    elif len(s) == 3:
        if n % 100 == 0:
            word.append(c[n / 100])
        elif n % 100 < 10:
            word.append(a[n%10])
            word.append('and')
            word.append(c[n / 100])
        elif n % 100 < 20:
            word.append(a1[n%10])
            word.append('and')
            word.append(c[n / 100])
        else:
            word.append(a[n%10])
            p=n/10 % 10
            word.append(b[p])
            word.append('and')
            word.append(c[n / 100])
    return word[::-1]

total=[]
for i in range(1,1000):
    total=total+words(i)
#print total
total=total+['one thousand']
print len(''.join(total).replace(' ',''))


I am working on the file "Q011-Q020.ipynb".
The first task is:
Q018
Maximum path sum I
By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.
3
7 4
2 4 6
8 5 9 3
That is, 3 + 7 + 4 + 9 = 23.
Find the maximum total from top to bottom of the triangle below:
75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)
Can you write Python code for it?


I am working on the file "Q011-Q020.ipynb".
The first task is:
Q020
Factorial digit sum
n! means n × (n − 1) × ... × 3 × 2 × 1
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
Find the sum of the digits in the number 100!
Can you write Python code for it?

def fac(n):
    if n == 1:
        return 1
    return n*fac(n-1)
a=fac(100)
s=0
while a > 0:
    s=s+a%10
    a=a//10
print s


I am working on the file "Q021-Q030.ipynb".
The first task is:
Q0223
Non-abundant sums
A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.
A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.
As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.
Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
Can you write Python code for it?

n=28123
abundant=[]
for a in xrange(2,n):
    if d(a) > a:
        abundant.append(a)
abundant=set(abundant)
print len(abundant)

abundant_sum=0
for i in xrange(2,n):
    for x in abundant:
        if i-x in abundant:
            abundant_sum+=i
            break
print sum(xrange(2,n))-abundant_sum+1 # we missout the 1


I am working on the file "Q021-Q030.ipynb".
The first task is:
Q024
Lexicographic permutations
A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:
012   021   102   120   201   210
What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
Can you write Python code for it?

def next_permutation(arr):
    # Find non-increasing suffix
    i = len(arr) - 1
    while i > 0 and arr[i - 1] >= arr[i]:
        i -= 1
    if i <= 0:
        return False

    # Find successor to pivot
    j = len(arr) - 1
    while arr[j] <= arr[i - 1]:
        j -= 1
    arr[i - 1], arr[j] = arr[j], arr[i - 1]

    # Reverse suffix
    arr[i : ] = arr[len(arr) - 1 : i - 1 : -1]
    return True

# https://www.nayuki.io/page/next-lexicographical-permutation-algorithm
s=[0,1,2,3,4,5,6,7,8,9]
#s=[0,1,2,3]
n=1000000
def next_lexicographical(s):
    # Find non-increasing suffix
    i = len(s) - 1
    while i > 0 and s[i-1] >= s[i]:
        i -= 1
    if i <= 0:
        return False

    # Find successor to pivot
    j = len(s) - 1
    while s[j] <= s[i - 1]:
        j -= 1

    s[i-1],s[j]=s[j],s[i-1]
    s[i:]=s[len(s)-1:i-1:-1]
    return s
i=1
while i < n:
    s=next_lexicographical(s)
    #print s
    i+=1
for i in s:
    print i,


I am working on the file "Q021-Q030.ipynb".
The first task is:
Q027
Quadratic primes
Euler discovered the remarkable quadratic formula:
$n^2 + n + 41$
It turns out that the formula will produce 40 primes for the consecutive integer values $0 \le n \le 39$. However, when $n = 40, 40^2 + 40 + 41 = 40(40 + 1) + 41$ is divisible by 41, and certainly when $n = 41, 41^2 + 41 + 41$ is clearly divisible by 41.
The incredible formula $n^2 - 79n + 1601$ was discovered, which produces 80 primes for the consecutive values $0 \le n \le 79$. The product of the coefficients, −79 and 1601, is −126479.
Considering quadratics of the form:
$n^2 + an + b$, where $|a| < 1000$ and $|b| \le 1000$
where $|n|$ is the modulus/absolute value of $n$
e.g. $|11| = 11$ and $|-4| = 4$
Find the product of the coefficients, $a$ and $b$, for the quadratic expression that produces the maximum number of primes for consecutive values of $n$, starting with $n = 0$.
Can you write Python code for it?

import numpy as np
def quadratic_prime(n,a,b):
    return n*n+a*n+b
def if_prime(n):
    if n < 1:
        return False
    elif n < 4:
        return True
    elif (n % 2 == 0) or (n % 3 == 0):
        return False
    i = 5
    while i*i < n + 1:
        if (n % i == 0) or (n % (i + 2) == 0):
            return False
        i = i + 6
    return True
A,B = np.meshgrid(range(-1000,1000),range(-1000,1000))
longest=[[],0]
for i in zip(A.ravel(),B.ravel()):
    n=0
    while if_prime(quadratic_prime(n,i[0],i[1])):
        n=n+1
    if n > longest[1]:
        longest=[i,n]
print longest
print longest[0][0]*longest[0][1]


I am working on the file "Q021-Q030.ipynb".
The first task is:
Q028
Number spiral diagonals
Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:
21 22 23 24 25
20  7  8  9 10
19  6  1  2 11
18  5  4  3 12
17 16 15 14 13
It can be verified that the sum of the numbers on the diagonals is 101.
What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?
Can you write Python code for it?

print 1+3+5+7+9+13+17+21+25
def gen_spiral(n):
    result=[1]
    i=1
    number=1
    while i<n:
        j=1
        while j <= 4:
            number=number+i*2
            result.append(number)
            j+=1
        i+=1
    return result
print gen_spiral(3)


I am working on the file "Q021-Q030.ipynb".
The first task is:
Q029
Distinct powers
Consider all integer combinations of $a^b$ for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5:
$2^2=4, 2^3=8, 2^4=16, 2^5=32$
$3^2=9, 3^3=27, 3^4=81, 3^5=243$
$4^2=16, 4^3=64, 4^4=256, 4^5=1024$
$5^2=25, 5^3=125, 5^4=625, 5^5=3125$
If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:
4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125
How many distinct terms are in the sequence generated by $a^b$ for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?
Can you write Python code for it?

print 2*np.log(4)
print 4*np.log(2)

import numpy as np
uniq=[]
for a in range(2,101):
    for b in range(2,101):
        uniq.append(a**b)
len(set(uniq))


I am working on the file "Q021-Q030.ipynb".
The first task is:
Q030
Digit fifth powers
Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:
$1634 = 1^4 + 6^4 + 3^4 + 4^4$
$8208 = 8^4 + 2^4 + 0^4 + 8^4$
$9474 = 9^4 + 4^4 + 7^4 + 4^4$
As 1 = $1^4$ is not a sum it is not included.
The sum of these numbers is 1634 + 8208 + 9474 = 19316.
Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.
Can you write Python code for it?

print "maximum number of digit="
n=1
while 10**(n+1)-1 < n*9**5:
    n+=1
print n

result=[]
for n in range(2,10**6):
    d = [int(i) for i in str(n)]
    e = sum([j**5 for j in d])
    if n == e:
        result.append(n)
sum(result)


I am working on the file "QEP-InteractiveDemo.ipynb".
The first task is:
As a final assessment on the dataset, we pick $m = n/4$ (25% of the number of features) and simulate with a typical instance of QRP what is the smallest margin $$\tau := \min_{j,j'} {\min_{\boldsymbol z \in \mathbb{R}^n} \|\boldsymbol\Phi \boldsymbol z \|\infty: \boldsymbol z \in \mathcal C- := \mathcal C_j - \mathcal C_{j'}}, \ \tau > 0.$$ The rationale is simple: any vector $\boldsymbol z \in \mathbb R^n$ so that $\|\boldsymbol\Phi \boldsymbol z \|\infty < \delta$ for some $\delta > 0$ is surely consistent, i.e., so if $\boldsymbol z = \boldsymbol x - \boldsymbol x'$ verifies this condition $\boldsymbol x$ and $\boldsymbol x'$ will be mapped to the same quantiser cell, and hence they will not be distinguishable when belonging to different classes. For this reason, we take $\boldsymbol z \in \mathcal C-$ in the appropriate difference set. Taking the smallest of such $\tau$ among all classes returns the smallest $\ell_\infty$-sense margin between classes in the projected domain.
Can you write Python code for it?

# Test the $\ell_\infty margin$ between classes in projected domain
combis = combinations(range(nclass),2)
coms = np.asarray([p for p in combis])
tau = np.zeros(len(coms),dtype=float)
m = n//4

np.random.seed(rseed)
Phi = np.random.standard_normal([m,n])

for i in range(len(coms)):
    c = np.subtract(centre[:,coms[i,0]],centre[:,coms[i,1]])
    z = Variable(n)
    obj = Minimize(norm(Phi*z, "inf"))
    con = [norm(z-c) <= radius[coms[i,0]]+coms[i,1]]
    prob = Problem(obj,  con)

    try:
        prob.solve(solver=ECOS,verbose=False)
    except SolverError:
        prob.solve(solver=SCS,verbose=False)

    tau_star = obj.value
    tau[i] = tau_star

print("The current tau are: %s" % str(tau))


I am working on the file "QEP-InteractiveDemo.ipynb".
The first task is:
As shown from the output above, a call to QRP is $\approx 8$ to $10$ times faster than scikit-learn's PCA fitting to the $m$ principal components estimated from the data alone. Clearly, this will come at a loss in terms of probability of error, but the perk of using QRP with a sufficiently small $\delta$ will be two-fold. On one hand, it will allow for storage of the signatures in a few bits; on the other hand, the computation can be achieved in $O(m n)$ with $m$-dim. signatures and $n$-dim. features, or simply $O(n \log n)$ when using random convolution to define $\boldsymbol\Phi$ as it involves a fast transform.
A plot of the probability of error $P_e$ measured after 5-NN classification closes this section, and allows us to see that, as $m/n$ increases, $P_e (\rm QRP)$ approaches $P_e (\rm PCA)$ (the latter naturally overfits the dataset for large $m$).
Can you write Python code for it?

# Plot results (M)
plt.figure(figsize=(8,6))
plt.plot(np.log2(M/n),qrp_perr,label="$QRP_{m,\delta}$", marker='o')
plt.plot(np.log2(M/n),pca_perr,label="$PCA_{m}$", marker='o')
plt.title("Probability of error at $\delta = " + str(delta) + "$ (" + str(bitrate) + " bits)")
plt.xlabel("$\\log_2 m/n$")
plt.ylabel("$P_e$")
plt.legend()
plt.show()


I am working on the file "QEP-InteractiveDemo.ipynb".
The first task is:
Dataset Generation and Analysis
For the scope of this analysis, let us consider a labelled dataset $\mathcal K := \cup^C_{j=1} \mathcal C_j$, where the class $\mathcal C_j$ is identified by a label $j$ and comprised of the datapoints ${\boldsymbol x_i \in \mathcal C_j}$ (note: in the theoretical analysis of [1] the classes are not necessarily finite). Our purpose will be to model the dataset as disjoint convex sets (e.g., convex hulls, or minimum volume spheres enclosing each class), and to understand how QRP interact with these sets - how the quantiser resolution $\delta$ should be chosen, how $m$ should scale with the complexity of the underlying set. As this is only a demo, a thorough explanation of these concepts can be found in [1],[2].
Can you write Python code for it?

# Prepare train/test data
nclass = 5 # Number of classes, $C$
rseed = 42 # Seed of random number generator
dset = "real" # Digits dataset (real data)
# dset = "" # Blobs dataset (randomly generated)

if dset == "real":
    # The Digits dataset (real)
    # This is the well-known handwritten digits dataset, loaded with nclass labels.
    # The classes will tend to be closer and overlap, thus departing from our
    # theoretical analysis that assumes linear separability.
    n = 64
    digits = load_digits(nclass)
    nsamples = digits.data.shape[0]
    idx_train = np.random.choice(nsamples, nsamples//2, replace=False)
    idx_test = np.setxor1d(np.arange(nsamples),idx_train)
    train_data = digits.data[idx_train]
    test_data = digits.data[idx_test]
    ctrain = digits.target[idx_train]
    ctest = digits.target[idx_test]
else:
    # The Blob dataset (synthetic)
    # This is a randomly generated dataset of blobs that tend to be sufficiently far apart.
    # The classes will tend to be separated, thus verifying our theoretical analysis.
    n = 2**9
    nsamples = 2**7
    data,target = make_blobs(n_samples=nsamples,
                             n_features=n, centers=nclass,
                             cluster_std=0.5)
    idx_train = np.random.choice(nsamples, 2*nsamples//3, replace=False)
    idx_test = np.setxor1d(np.arange(nsamples),idx_train)
    train_data = data[idx_train]
    test_data = data[idx_test]
    ctrain = target[idx_train]
    ctest = target[idx_test]


I am working on the file "QEP-InteractiveDemo.ipynb".
The first task is:
Ideally, choosing any $0<\delta \leq \tau$ when $\tau > 0$ will avoid that two datapoints belonging to different classes fall in the same quantiser cell after projection. If $\tau = 0$, then it is clear that the $\text{Null}(\boldsymbol \Phi)$ intersects at least one $\mathcal C_-$, and that will cause at least two points in the datasets above to overlap.
However, this is an excessively strict approach for two reasons: firstly, the dither $\boldsymbol\xi$ introduces a shift in the tiling of the projected domain so that, even if $0 < \delta \leq \tau$, there are cases cases in which the classes could be separable by virtue of dithering; hence, the probability that the classes "eclipse", i.e., $$P_f := \mathbb P[\exists \boldsymbol x \in \mathcal C_j, \boldsymbol x' \in \mathcal C_{j'} : \mathsf{A}(\boldsymbol x) = \mathsf{A}(\boldsymbol x')]$$ is actually smaller in the presence of dithering. Secondly, the empirical probability of error $P_e$ of a classifier on a dataset will be typically $P_e \ll P_f$, as the latter really is a worst-case analysis considering the most critical pair of points between two potentially continuous classes $\mathcal C_j, \mathcal C_{j'}$.
Clearly, this outline of the analysis in [1] is only meant to address separable classes. In natural datasets, it is almost often the case that an overlap between the classes will exist. This explains why, when considering the "real" and low-dimensional dataset above (handwritten digits) $\tau$ is almost $0$ (up to machine precision, as output by the optimisation problem).
Nevertheless, analysing the above margin can be seen as a way to probe how a quasi-isometric, distance-preserving embedding such as $\mathsf{A}$ alters disjoint and easily classified, but high-dimensional datasets.
An "Unfair" Comparison between Quantised Random Projections (${\rm QRP}_{m,\delta}$) and Principal Components Analysis (${\rm PCA}_m$)
In order to compare the signatures produced by QRP (a non-adaptive technique, involving no training and merely leveraging...
Can you write Python code for it?

# With respect to $m$
M = np.unique(np.ceil(n*2**np.arange(-np.log2(n),-1,1))).astype(int)
msteps = len(M)
# delta = min(tau) # Safe estimate
delta = sigma # Rough estimate (relies on isometry)

qrp_perr = []
pca_perr = []

for ss in range(msteps):
    # Apply QRP with dimension m and resolution delta
    print("QRP_" + str(M[ss]))
    %time red_train,_ = quant_rand_embed(train_data.T,m=M[ss],delta=delta,rngseed=rseed)
    red_test,_ = quant_rand_embed(test_data.T,m=M[ss],delta=delta,rngseed=rseed)
    red_train = red_train.T
    red_test = red_test.T

    # Apply PCA with the same dimension m, and train it
    print("PCA_" + str(M[ss]))
    PC = PCA(n_components=M[ss])
    %time PC.fit(train_data)
    pca_train = PC.transform(train_data)
    pca_test = PC.transform(test_data)

    # Run k-NN on reduced-dim. dataset
    KNN = KNeighborsClassifier(n_neighbors=5, p=2, metric='minkowski')
    KNN.fit(red_train,ctrain)
    qrp_cpred = KNN.predict(red_test)
    qrp_perr.append(float(sum(qrp_cpred != ctest))/ctest.size)
    KNN.fit(pca_train,ctrain)
    pca_cpred = KNN.predict(pca_test)
    pca_perr.append(float(sum(pca_cpred != ctest))/ctest.size)
    bitrate = np.floor(np.log2((np.unique(red_train[:])).size))+1


I am working on the file "QEP-InteractiveDemo.ipynb".
The first task is:
Quantised Random Projections and Quasi-Isometric Embeddings
Non-linear dimensionality reduction techniques are important components in machine learning frameworks for very large-scale datasets. Among such techniques, we focus on quantised random embeddings.
First off, let us introduce their definition when using quantised Gaussian random projections (in the following, they are addressed as QRPs), i.e., as obtained by a non-linear map $\mathsf{A}$ applied to $\boldsymbol x \in \mathcal K \subset \mathbb R^n$ (with $\mathcal K$ the dataset), that is
$$
\boldsymbol y = {\mathsf A} (\boldsymbol x) := {\mathcal Q}{\delta}(\boldsymbol \Phi \boldsymbol x + \boldsymbol \xi)
$$
with $\boldsymbol \Phi \in \mathbb R^{m\times n}$ a random sensing matrix, ${\mathcal Q}{\delta} (\cdot) := \delta \lfloor \frac{\cdot}{\delta} \rfloor$ a uniform scalar quantiser of resolution $\delta>0$ (applied component-wise), and the signature $\boldsymbol y \in \delta \mathbb Z^m$. The dithering $\boldsymbol\xi \sim {\mathcal U}^{m}([0,{\delta}])$ is a well-known means to stabilise the action of the quantiser. One-bit random embeddings are a particular case of this arrangement, when $\delta$ is sufficiently large before the range of the components of $\boldsymbol x \in \mathcal K$.
The non-linear map $\mathsf A$ is a non-adaptive dimensionality reduction that yields compact signatures for storage and transmission, while retaining a notion of quasi-isometry [3] (distance preservation, e.g., as an approximate isometry between $(\mathbb R^n, \ell_2)$ and $(\delta \mathbb Z^m, \ell_1)$ up to some bounded, controlled additive and multiplicative distortions of the distances) that enables the approximation of $\boldsymbol x$.
The code defining such an embedding is very simple and given below. As shown in the findings of [2] $\boldsymbol\Phi$ could also be constructed as a fast transform, and would still have quasi-isometric behaviour between $(\mathbb R^n, \ell_2)$ and $(\delta \mathbb Z^m, \ell_2)$, albeit with a worse distortion of the...
Can you write Python code for it?

def quant_rand_embed(x, m=2, delta=1, rngseed=42, mode="randn"):
    '''
    Quantised Random Embedding using Random Projections
    and Uniform Scalar Quantisation with Dithering

    See also:
    Laurent Jacques and Valerio Cambareri.
    "Time for dithering: fast and quantized random embeddings via the restricted isometry property."
    arXiv preprint arXiv:1607.00816 (2016).
    In Information and Inference: A Journal of the IMA, accepted and in press, Mar. 2017.

    Inputs:
    x         input data             ndarray, shape (nvariables, nsamples)
    m         output feature size    int
    delta     quantiser bin width    float (>0)
    rngseed   generator state        int
    mode      type of random proj.   string

    Outputs:
    w         output features        float (quantised)
    q         output features        np.int16 (quantised)
    '''

    # Initialise the quant. rand. proj. setup
    dims = np.shape(x)
    np.random.seed(rngseed)
    if delta <= 0:
        raise Exception('Quantiser resolution parameter must be positive!')

    # Apply the sensing operator
    # The random projection is applied to axis 0 of the input data, which is
    # assumed as a matrix of size $n \times p$
    if mode=="randn":
        # Gaussian Random Projections
        phi = np.random.standard_normal([m,dims[0]])
        q = np.dot(phi, x)

    elif mode=="randc":
        # Random Convolution using Antipodal Kernel (via FFT)
        ker = 2*(np.random.randint(0,1,dims[0])-0.5)
        idx = np.random.choice(dims[0],size=m,replace=False)
        q = np.fft.irfft(np.dot(np.diag(np.fft.rfft(ker)),np.fft.rfft(x,axis=0)),axis=0)[idx]
        # # Alternative w. convolve
        # q = (np.apply_along_axis(lambda x: np.convolve(x,ker,mode='fft'), 0, x))[idx]

    else:
        raise Exception('Mode not recognised. Only "randn" and "randc" are implemented so far.')

    # Generate dithering
    xi = np.random.uniform(-delta*0.5,delta*0.5,m)

    # Apply dithering and quantise with...


I am working on the file "QEP-InteractiveDemo.ipynb".
The first task is:
The error probability of 5-NN is reported below, illustrating how for large $\delta$ the accuracy rapidly degrades.
Can you write Python code for it?

# Plot results (\delta)
plt.figure(figsize=(16,6))
plt.subplot(1,2,1)
plt.plot(np.log2(D),qrp_perr_bit,label="$QRP_{m,\delta}$",marker='o')
plt.title("Probability of error at $m = " + str(m) + "$")
plt.xlabel("$\\log_2 \delta$")
plt.ylabel("$P_e$")
plt.legend()

plt.subplot(1,2,2)
plt.plot(np.log2(D),bitrates,label="$QRP_{m,\delta}$",marker='o')
plt.title("Bits to represent signatures at $m = " + str(m) + "$")
plt.xlabel("$\\log_2 \delta$")
plt.ylabel("$B$")
plt.legend()
plt.show()


I am working on the file "QEP-InteractiveDemo.ipynb".
The first task is:
We can also obtain the intra-class distance $\sigma_{j,j'} = \min_{i,i'} \|\boldsymbol x_i - \boldsymbol x_{i'}\|: \boldsymbol x_i \in \mathcal C_j, \boldsymbol x_{i'} \in \mathcal C_{j'}, j \neq j'$, and actually let $\sigma := \min \sigma_{j,j'}$. If this is relatively small, it will be harder to preserve the separability of each class after projection.
Can you write Python code for it?

# Minimum intra-class distance (closest point problem)
mind = np.inf*np.ones([nclass,nclass])
for i in range(nclass):
    for j in range(i+1,nclass):
        dists = pairwise_distances(train_data[ctrain == i], train_data[ctrain == j], metric='euclidean')
        mind[i,j] = np.min(dists)

sigma = np.min(mind)
print("The separation between classes is " + str(sigma))


I am working on the file "QEP-InteractiveDemo.ipynb".
The first task is:
We can now similarly assess the effect of $\delta$ on the same $P_e$, which we vary in a range obtained from the previous simulation.
Can you write Python code for it?

# With respect to $\delta$
D = 2.0**np.arange(-12,12,1)
dsteps = len(D)
m = min(M[np.asarray(qrp_perr) <=0.1])

bitrates = []
qrp_perr_bit = []

for dd in range(dsteps):
    # Apply QRP with dimension m and resolution delta
    red_train,_ = quant_rand_embed(train_data.T,m,delta=D[dd],rngseed=rseed,mode="randn")
    red_test,_ = quant_rand_embed(test_data.T,m,delta=D[dd],rngseed=rseed,mode="randn")
    red_train = red_train.T
    red_test = red_test.T

    # Run k-NN on reduced-dim. dataset
    KNN = KNeighborsClassifier(n_neighbors=5, p=2, metric='minkowski')
    KNN.fit(red_train,ctrain)
    qrp_cpred = KNN.predict(red_test)
    qrp_perr_bit.append(float(sum(qrp_cpred != ctest))/ctest.size)
    bitrates.append(np.floor(np.log2((np.unique(red_train[:])).size))+1)


I am working on the file "QM_analytical solutions.ipynb".
The first task is:
Pöschl-Teller potential
The Pösch-Teller potential is given by
$$ V(x) = -\frac{\lambda(\lambda + 1)}{2} \operatorname{sech}^2(x)$$
Can you write Python code for it?

def posch_teller_psi_n(n, x, lam):
    psi = P(lam, n, tanh(x))
    return psi

posch_teller_psi_n(n, x, lam)

def morse_E_n(n, lam):
    if n <= lam:
        return -n**2/ 2
    else:
        raise ValueError("Lambda should not be greater than n.")

morse_E_n(5, 6)


I am working on the file "QPGP_notes.ipynb".
The first task is:
As we move through the time domain, we rotate around the circle in $\mathbf{u}$-space, and observe the value of the function at the corresponding location. This offers (for me) fresh insight as to how modelling the light curve of a rotating star with this particular kind of GP relates to the actual surface features on the star. Note that one full rotation of the star corresponds to half a rotation in $\mathbf{u}$-space. The observed function value at any given time is the stellar flux integrated over the stellar disk, i.e. the "intrisic" surface contrast multiplied with a visibility function and integrated over the visible hemisphere.
Consider a point on the stellar surface with latitude $\chi$ and longitude $\phi_0$, where the meridian ($\phi_0=0$) is taken to face the observer at $t=0$. If we ignore limb-darkening, the visibility function is simply the cosine of the angle $\beta$ between the line joining the centre of the star to the observer, and that joining the centre of the star to the point under consideration:
$$\cos \beta = \cos i \sin \chi + \sin i \cos \chi \cos \phi,$$
where $\phi = \phi_0 + 2 \pi t / P$ (here $P$ is the stellar rotation period). So, if the "intrinsic" stellar surface contrast is $C(\chi,\phi_0)$, the observed flux at time $t$ is
$$f(t) = \int_{-\pi/2}^{\pi/2} \mathrm{d}\chi \int_{0}^{2 \pi} \mathrm{d}\phi_0 V(\chi,\phi_0) C(\chi,\phi_0),$$
where $V(\chi,\phi_0) = \max(\cos \beta,0)$, ensuring the integral covers the visible hemisphere only.
 Now relate this to $x$ and $y$ in $\mathbf{u}$-space.
Other periodic kernels
This isn't by any means the only way to create a periodic kernel. To start with, one could use, presumably, any other valid kernel in $\mathbf{u}$-space, or construct a different periodic warping of $t$. This may be worth investigating a little more...
Furthermore, another commonly used periodic kernel is the cosine kernel:
$$ k(r) = \cos \left(\frac{2 \pi r}{P} \right).$$
This is simpler, in that it doesn't have a scale...
Can you write Python code for it?

dt = np.r_[0:10:501j]
T = dt[-1]-dt[0]
P = 1.0
n = 20
nsim = 50
logV = np.random.uniform(0,5,size=n)
logGamma = np.random.uniform(-1,1,size=n)
V = 10.0**logV
Gamma = 10.0**logGamma
ran = V * (1-np.exp(-Gamma))
l = np.sqrt(2/Gamma)
var = np.zeros((n,nsim))
msq = np.zeros((n,nsim))
for i in range(n):
    gp = GP(V[i] * kernels.ExpSine2Kernel(Gamma[i],P))
    gp.compute(dt,yerr=V[i]*1e-3)
    sam = gp.sample(dt, size = nsim)
    for j in range(nsim):
        tmp = sam[j,:].flatten()
        var[i,j] = tmp.var()
        msq[i,j] = (tmp.mean()**2)
var_mean = np.mean(var, axis=1)
pl.figure(figsize=(14,5))
ax1 = pl.subplot(121)
for i in range(n):
    pl.plot(np.ones(nsim)*ran[i], var[i,:].flatten(), 'k.')
pl.plot(ran, var_mean, 'ro')
pl.plot(ran, 10.0**(-0.25+np.log10(ran)), 'r-')
pl.title('variance')
pl.xlabel(r"$V$ $\exp(-\Gamma)$")
pl.loglog()
ax2 = pl.subplot(122, sharex = ax1, sharey = ax1)
for i in range(n):
    pl.plot(np.ones(nsim)*ran[i], msq[i,:].flatten(), 'k.')
pl.title('mean squared')
pl.xlabel(r"$V$ $\exp(-\Gamma)$")
pl.loglog()


I am working on the file "QPGP_notes.ipynb".
The first task is:
So as we'd expect, $V$ controls the amplitude and $l$ the "wigglyness" of the time-series. But can we be a bit more specific?
What does $l$ mean exactly?
It would be useful to understand the relationship between $l$ and the typical number of turning points, or zero crossings, per unit time. I don't have a good mathematical insight into this, but I can try to get at it empirically... Warning: the next cell takes a while to run.
Can you write Python code for it?

dt = np.r_[0:20:501j]
T = dt[-1]-dt[0]
logl = np.r_[-1:0.5:15j]
l = 10.0**logl
n = len(l)
nsim = 100
ntp = np.zeros((n,nsim))
def turningPoints(lst):
    dx = np.diff(lst)
    return (dx[1:] * dx[:-1] < 0)
nzc = np.zeros((n,nsim))
def zeroCrossings(lst):
    z = 0
    si = np.sign(lst)
    return (si[1:] * si[:-1] < 0)
for i in range(n):
    gp = GP(kernels.ExpSquaredKernel(l[i]**2))
    gp.compute(dt,yerr=1e-3)
    sam = gp.sample(dt, size = nsim)
    for j in range(nsim):
        tmp = sam[j,:].flatten()
        tp = turningPoints(tmp)
        ntp[i,j] = tp.sum()/float(T)
        zc = zeroCrossings(tmp)
        nzc[i,j] = zc.sum()/float(T)
ntp_mean = np.mean(ntp, axis=1)
nzc_mean = np.mean(nzc, axis=1)
for i in range(n):
    pl.plot(np.ones(nsim)*l[i], ntp[i,:].flatten(), 'k.')
    pl.plot(np.ones(nsim)*l[i], nzc[i,:].flatten(), 'r.')
pl.plot(l, ntp_mean, 'ko')
pl.plot(l, 10.0**(-0.25-np.log10(l)), 'k-', label = 'turning points')
pl.plot(l, nzc_mean, 'ro')
pl.plot(l, 10.0**(-0.5-np.log10(l)), 'r-', label = 'zero crossings')
pl.xlabel(r"$l$")
pl.legend(loc=0)
pl.loglog()
pl.xlim(0.08,4);


I am working on the file "QPGP_notes.ipynb".
The first task is:
This seems to give reasonable results for a wide range of $\Gamma$: the solid and dashed lines have similar amplitudes and length scales. Good!
Quasi-periodic kernel
We can construct a quasi-periodic kernel by multiplying the periodic (ExpSine2) kernel by a squared exponential (or any other kernel, but let's stick to squared exponential for now). This is defined as:
$k_{\mathrm{QP}}(t,t') = \exp \left[ - \Gamma \sin^2 \left(\frac{\pi \Delta t}{P} \right) - \frac{(\Delta t)^2}{2L^2}\right]$. Let us take a look at the covariance function.
Can you write Python code for it?

def expsine2_SE(Gamma, P, L, dt):
    return np.exp(-Gamma*(np.sin(np.pi*dt/P))**2 - dt**2/(2*L**2))
dt = np.r_[0:5:301j]
Gammas = [0.5,5]
Ls = [0.3,3,30]
lss = ['-', '--']
cols = ['b','g','r']
for i in range(len(Gammas)):
    Gamma = Gammas[i]
    ls = lss[i]
    for j in range(len(Ls)):
        L = Ls[j]
        col = cols[j]
        pl.plot(dt,expsine2_SE(Gamma,1,L,dt), \
                label = r"$\Gamma=$%s, $L=$%s" % (repr(Gamma), repr(L)), \
                ls = ls, c = col)
#pl.legend(loc='lower right')
pl.title(r"ExpSine2_SE with $P=1$")
pl.xlabel(r"$\Delta t/P$")
pl.ylabel(r"$k_{\mathrm{Per}}(t,t')$")
pl.xlim(0,5)
pl.ylim(0,1.1)
print "solid: Gamma=%s, dashed: Gamma=%s" % (Gammas[0], Gammas[1])
print "blue: L=%s, green: L=%s, red: L=%s" % (Ls[0], Ls[1], Ls[2])


I am working on the file "QPGP_notes.ipynb".
The first task is:
We can immediately see things are going to be a little more complicated than before. Obviously, $P$ controls the period, but the shape and range of the covariance function, and hence the "wigglyness", variance and even mean of the resulting time-series, depend on both $V$ and $l$ in an interconnected way.
Before we delve into that any deeper, though, a short aside...
2-D interpretation
According to RW, this type of kernel was introduced by Mackay (1998), by mapping the one-dimensional variable $t$ onto a 2-D space $\mathbf{u}(t)=(\cos(t),\sin(t))$, and using the squared exponential kernel in $\mathbf{u}$-space. Noting that $(\cos(t)-\cos(t'))^2+(\sin(t)-\sin(t'))^2=4 \sin^2 (r/2)$, one obtains:
$$k(r) = \exp{\left( -\frac{2 \sin^2 (r/2)}{l^2} \right)}.$$
(As $\sin^2(x)$ has period $\pi$, the above expression produces random periodic functions with period $2\pi$, the modified expression I used enables me to generate random periodic functions with arbitrary period $P$.)
This provides some insight into the "meaning" of $l$: it is still a length scale, but in $\mathbf{u}$ space, not in the time domain itself. In other words, this type of GP sets up a probability distribution over smooth, 2-D random functions in $\mathbf{u}$-space with characteristic length scale $l$. Note however that those functions are defined only over a unit circle in $\mathbf{u}$-space, and must symmetric under 180 degree rotation. The same functions are also smooth, 1-D random periodic functions in $t$-space. Let's see if we can illustrate that.
Can you write Python code for it?

t = np.r_[0:2*np.pi:401j]
x = np.cos(t)
y = np.sin(t)
l = 1.0
Gamma = 2/l**2
gp = GP(kernels.ExpSine2Kernel(Gamma, np.pi))
gp.compute(t,yerr=1e-3)
f = gp.sample(t, size = 1)
pl.figure(figsize=(10,10))
ax1 = pl.subplot(221)
pl.scatter(x,y,s=10,c=t,edgecolors='none')
pl.xlabel(r"$\cos(t)$")
pl.ylabel(r"$\sin(t)$")
pl.title(r"$t$")
ax2 = pl.subplot(222, sharex = ax1, sharey = ax1)
pl.scatter(x,y,s=10,c=f,edgecolors='none')
pl.xlim(-1.1,1.1)
pl.ylim(-1.1,1.1)
pl.title(r"$f(t)$")
ax3 = pl.subplot(212)
pl.plot(t,f,'r-')
pl.ylabel(r"$f(t)$")
pl.xlabel(r"$t$")
pl.xlim(0,2*np.pi);


I am working on the file "QPGP_notes.ipynb".
The first task is:
We can see the variance does scale like $V \exp(-\Gamma)$, as expected, though there is a proportionality factor of 0.56, the origin of which I don't understand. The mean (or rather, the range of the squared means) doesn't quite scale with $V \exp(-\Gamma)$; the dependence on $V$ and $\Gamma$ must be a bit different.
 DONE TILL HERE
What does $l$ mean now?
In the squared exponential kernel, the covariance ranges from 1 to 0 and falls to $e^{-0.5}$ when $\Delta t=l$. The range sets the variance of the variations and $l$ their length scale. How the lenght scale relates to the number of turning points per unit time is something I'll want to investigate later.
In the periodic kernel we're considering, the covariance ranges from 1 to $e^{-\Gamma}$. The variance of the variations therefore scales as $1-e^{-\Gamma}$.
The covariance falls to $e^{-0.5\Gamma}$ times its full range when $\Gamma \sin^2 \left(\frac{\pi \Delta t}{P} \right) = 0.5 \left( 1-e^{-\Gamma} \right)$, i.e. $\frac{\Delta t}{P} = \frac{1}{\pi} \arcsin\left( \sqrt{\frac{1-e^{-\Gamma}}{2\Gamma}}\right)$. This gives an estimate of the length scale of the variations within a period. </s>
NB1: For small $\Gamma$, the covariance never approaches zero, and this leads to functions which have non-zero mean, even over an infinite interval. Something similar happens with the squared exponential kernel for large $l$, but over a finite time interval $\Delta t \sim < l$.
NB2: for very small $\Gamma$, the variations look close to sinusoidal with period $P$.
Let's see if all this holds out in practice, by drawing samples from periodic and squared exponential GP priors with the appropriate parameters.
Can you write Python code for it?

from george import GP, kernels
import time
def compare_per_se(P, Gamma, dt):
    gp_per = GP(kernels.ExpSine2Kernel(Gamma,P))
    gp_per.compute(dt,yerr=1e-3)
    seed = int(time.time())
    np.random.seed(seed)
    sam_per = gp_per.sample(dt,size=1)
    pl.plot(dt,sam_per.T,'k-', label = 'Per')
    pl.title(r"$P=$%s, $\Gamma=%s$" % (repr(P), repr(Gamma)))
    pl.xlabel(r"$t$")
    pl.ylabel(r"$f$")
    a_effective = np.sqrt(1 - np.exp(-Gamma))
    l_effective = (P/np.pi) * np.arcsin(np.sqrt((a_effective**2/2/Gamma)))
    print "amplitude=%.3f, length scale=%.3f" % (a_effective, l_effective)
    gp_se = GP(a_effective**2 * kernels.ExpSquaredKernel(l_effective**2))
    gp_se.compute(dt,yerr=1e-3)
    np.random.seed(seed)
    sam_se = gp_se.sample(dt,size=1)
    pl.plot(dt,sam_se.T,'k--', label = 'SE')
    pl.legend(loc=0)

compare_per_se(1.0,10.0,dt)

compare_per_se(1.0,0.1,dt)

compare_per_se(1.0,1.0,dt)


I am working on the file "QUTpy Tweepy Introduction.ipynb".
The first task is:
And here's a script for CSVs (with errorhandling etc.)
Can you write Python code for it?

# Usage:
# insert consumer and app keys below
# run (eg in Terminal): python3 scriptname.py inputfile.csv outputfile.csv
# inputfile is a list of userids

import time
import csv
import io
import tweepy
from os import stat
from sys import argv

#ckey = ''
#csecret = ''
#atoken = ''
#asecret = ''
#uncomment if necessary

auth = tweepy.OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)

api = tweepy.API(auth, wait_on_rate_limit = True, wait_on_rate_limit_notify = True)

# userlistpath = argv[1]
# friendlistpath = argv[2]
# uncomment two lines above for script use

# change the following two lines
userlistpath = ''
friendlistpath = ''

def get_friend_ids(userid):

    userid = int(userid)

    uids = tweepy.Cursor(api.friends_ids, user_id = userid).items()

    idlist = []
    idcount = 0

    try:
        for uid in uids:
            idlist.append(uid)
            idcount += 1
    except tweepy.TweepError as e:
        idlist.append(-int(e.reason[-3:]))
        print(e.reason + ' for %d'  %(userid))

    print('%d friend ids collected for %d.' %(idcount,userid))

    return idlist

def get_screen_name_and_created_at(uids):

    friend_count = len(uids)

    i = 0

    results = []

    while i < friend_count:

        if uids[0] < 0:
            results.append([uids[i]]*3)
            break
        else:
            j = i + 100
            if j > friend_count:
                j = friend_count
            try:
                users = api.lookup_users(user_ids=uids[i:j])
                for user in users:
                    results.append([uids[i],user.screen_name,int(time.mktime(user.created_at.timetuple()))])
                    i += 1
            except tweepy.TweepError as e:
                code = int(e.reason[-3:])
                for uid in uids[i:j]:
                    results.append([uid, -code, -code])
                    i += 1

    print('%d friend details collected.' %(i))

    return results

# opens csv,...


I am working on the file "QUTpy Tweepy Introduction.ipynb".
The first task is:
Example: Gather following network of a list of users
Can you write Python code for it?

import time

user = 'flxvctr' # the screenname of the user you want the friends of

def get_friend_ids(username):

    uids = tweepy.Cursor(api.friends_ids, screen_name=username).items()

    idlist = []

    for uid in uids:
        idlist.append(uid)

    return idlist

def get_screen_name_and_created_at(uids):

    friend_count = len(uids)

    i = 0

    results = []

    while i < friend_count:
        j = i + 100
        users = api.lookup_users(user_ids=uids[i:j])
        for user in users:
            results.append([uids[i],user.screen_name,int(time.mktime(user.created_at.timetuple()))])
            i += 1

    return results

friendlist = get_screen_name_and_created_at(get_friend_ids(user))

print(len(friendlist),friendlist)


I am working on the file "QWZ.ipynb".
The first task is:
Bulk spectrum
The bulk spectrum of the QWZ model is simply given by the magnitude of the $\mathbf{d}(\mathbf{k})$ vector as it goes around the Brillouin zone. We have two bands. At $$E_{\pm}=\pm\sqrt{\sin(k_x)^2+\sin(k_y)^2+(\cos(k_x)+\cos(k_y)+u)^2} .$$
Can you write Python code for it?

def QWZbulk(u,ee=13,aa=-30):
    '''
    Simple function to generate spectrum
    '''
    xx, yy = meshgrid(linspace(-pi,pi,25),linspace(-pi,pi,25))
    z1 = sqrt(sin(xx)**2+sin(yy)**2+(cos(xx)+cos(yy)+(u))**2)
    # plot the data..
    ax0=subplot(111, projection='3d')

    props={'linewidth':0,'cmap':'nipy_spectral','antialiased':False,
       'rstride':1, 'cstride':1,'vmin':-z1.max(), 'vmax':z1.max()}

    ax0.plot_surface(xx,yy,z1,**props)
    ax0.plot_surface(xx,yy,-z1,**props)

    # everything belo is only for the look
    ax0.view_init(elev=ee, azim=aa)
    ax0.set_aspect(4/pi)

    ax0.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
    ax0.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
    ax0.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
    ax0.set_zlim3d(-4, 4)
    ax0.set_ylim3d(-pi, pi);ax0.set_xlim3d(-pi, pi);
    ax0.grid(False)

    lprop={'fontsize':20}
    ax0.set_xlabel(r'$k_x$',**lprop);ax0.set_ylabel(r'$k_y$',**lprop)

    ax0.set_xticks(linspace(-pi,pi,3))
    ax0.set_xticklabels([r'-$\pi$',r'$0$',r'$\pi$'],
                    va='baseline',ha='center',**lprop)
    ax0.set_yticks(linspace(-pi,pi,3))
    ax0.set_yticklabels([r'-$\pi$',r'$0$',r'$\pi$'],
                    va='baseline',ha='center',**lprop)
    ax0.set_zticks(linspace(-4,4,5))
    ax0.set_zticklabels([r'$-4$',r'$-2$',r'  $0$',r'  $2$',r'  $4$',],
                    va='baseline',ha='left',**lprop)
    ax0.text(pi+0.6, pi+0.6, 1,r'$E(\mathbf{k})$' ,**lprop);


I am working on the file "QWZ.ipynb".
The first task is:
Depending on the value of $u$ the spectrum can be gapless $u=\pm2$ and $u=0$ or in general gaped
Can you write Python code for it?


I am working on the file "QWZ.ipynb".
The first task is:
Finite sample - the potato argument
Finally we define a finite rectangular shaped lattice and than cut out a potato shaped region out of it and inspect the spectrum and the wavefunctions of this finite irregular sample.
Can you write Python code for it?

class finite_lattice:
    '''
    Class for a generic 2D lattice.
    '''
    def __init__(self,xL=4,yL=3,**kwargs):
        '''
        Initializatin
        '''

        self.xL=xL
        self.yL=yL
        self.xran=arange(xL)
        self.yran=arange(yL)
        self.ym,self.xm=meshgrid(self.yran,self.xran)
        self.y,self.x=(self.ym.flatten(),self.xm.flatten())

    def make_ham(self,Tx,Ty,U,**kwargs):
        '''
        Make Hamiltonian for a square shape
        '''
        ODX=ssp.diags([ones(self.xL-1)],[1])
        DX =ssp.diags([ones(self.xL)],[0])
        ODY=ssp.diags([ones(self.yL-1)],[1])
        DY =ssp.diags([ones(self.yL)],[0])
        # internal degrees of freedom
        self.idf=shape(Tx)[0]
        self.H=(ssp.kron(DX,ssp.kron(ODY,Ty))+
                ssp.kron(DX,ssp.kron(ODY,Ty)).H +
                ssp.kron(ODX,ssp.kron(DY,Tx))+
                ssp.kron(ODX,ssp.kron(DY,Tx)).H +
                ssp.kron(DX,ssp.kron(DY,U)) )

    def plot_fullwf(self,psi,**kwargs):
        '''
        Plotter for wavefunctions of the whole lattice.
        '''
        pcolor(self.xm,self.ym,
               reshape(sum(
                    reshape(abs(psi)**2,(self.xL*self.yL,self.idf)),
                    axis=1),(self.xL,self.yL)
                      ),**kwargs)

    def cut_potato(self,potx,poty,inverted=False,**kwargs):
        '''
        Make Hamiltonian for an arbitrary shape.
        We carve out the shape from the rectangle
        of the original lattice.
        '''
        potato=matplotlib.path.Path(array([potx,poty]).T)
        self.potato_cindx=(potato.contains_points(
                           array([self.x,self.y]).T))
        self.potato_hindx=(potato.contains_points(
                           array([kron(self.x,ones(self.idf)),
                                  kron(self.y,ones(self.idf))]).T))
        if inverted:
            self.potato_cindx=logical_not(self.potato_cindx)
            self.potato_hindx=logical_not(self.potato_hindx)
       ...


I am working on the file "QWZ.ipynb".
The first task is:
Now we define cutting and calculate the spectrum of the "potato".
Can you write Python code for it?

th=linspace(0,2*pi,100)
krx,kry=(60*(cos(th)+0.1*cos(3*th-0.3))+80,40*(sin(th)-0.1*sin(6*th+0.5))+50)
QWZ.cut_potato(krx,kry)
Vdis=ssp.diags(kron(randn(int(QWZ.Hpotato.shape[0]/QWZ.idf)),ones(QWZ.idf)),0)

vap,vep=sla.eigsh(QWZ.Hpotato+0.01*Vdis,100,sigma=0.01)
vep=vep[:,argsort(vap)]
vap=sort(vap)
dummy=zeros((QWZ.H.shape[0],len(vap)),dtype=complex)
dummy[QWZ.potato_hindx,:]=vep

figsize(10,7)
@interact(i=(0,len(vap)-1))
def play(i=40):
    subplot(121,aspect=50)
    plot(arange(len(vap)),vap,'r.')
    plot(arange(len(vap))[i],vap[i],'wo',ms=10)
    ylim(-0.4,0.4)
    yticks(linspace(-0.4,0.4,5))
    xticks(linspace(0,len(vap),3))
    grid()
    subplot(122,aspect='equal')
    QWZ.plot_fullwf(dummy[:,i],cmap='CMRmap_r')
    plot(krx+0.5,kry+0.5,'-',color='darkcyan',lw=2)
    axis('off');
    tight_layout()


I am working on the file "QWZ.ipynb".
The first task is:
Now we generate the rectangule shape
Can you write Python code for it?


I am working on the file "QWZ.ipynb".
The first task is:
Ribbons
Since the Chern number of the QWZ model is nonzero for $|u|<2$ we expect edge states to show up in this intervall.
We write a simple class below that generates data and figures for a translationally invariant ribbon with arbitrary hoppings and onsite terms. We than look at the specific case of the QWZ model at $u=-1.0$.
Can you write Python code for it?

class ribbon:
    '''
    A class that holds information about a generic translationally inveriant ribbon.
    '''
    def __init__(self,Tx,Ty,U,L=10,kran=linspace(-pi,pi,314),**kwargs):
        '''
        Initialization
        '''
        self.L=L
        self.Tx=Tx
        self.Ty=Ty
        self.U=U
        self.kran=kran
        self.idf=Tx.shape[0]

    def build_kham(self,k,**kwargs):
        '''
        Build kspace Hamiltonian
        '''
        idL=matrix(eye(self.L))            # identity matrix of dimension L
        odL=matrix(diag(ones(self.L-1),1)) # upper off diagonal matrix with ones of size L

        H=(kron(idL,self.U)+
           kron(odL,self.Tx)+
           kron(odL,self.Tx).H+
           kron(idL,self.Ty)*exp(1.0j*k)+
           kron(idL,self.Ty).H*exp(-1.0j*k))

        return H

    def spectrdata(self,numLoc=2,norm_treshold=0.6,**kwargs):
        '''
        Get data to be plotted.
        '''
        self.dat=[]             # We will collect the data to be
        self.vecdat=[]          # plotted in these arrays.
        self.lefty=[]
        self.righty=[]
        for k in self.kran:
            H=self.build_kham(k)
            eigdat=eigh(H);       # for a given ky here we calculate the eigensystem (values and vectors)
            # finding and classifying edgestates
            for i in range(self.idf*self.L):
                    if sum(reshape(abs(array(eigdat[1][:,i]))**2,(self.L,self.idf))[:numLoc,:])>norm_treshold:
                        self.lefty=append(self.lefty,[[k,eigdat[0][i]]]);
                    if sum(reshape(abs(array(eigdat[1][:,i]))**2,(self.L,self.idf))[-numLoc:,:])>norm_treshold:
                        self.righty=append(self.righty,[[k,eigdat[0][i]]]);

            self.dat=append(self.dat,eigdat[0]);
            self.vecdat=append(self.vecdat,eigdat[1]);

        self.dat=reshape(self.dat,[len(self.kran),2*self.L]);          # rewraping the data
       ...


I am working on the file "QWZ.ipynb".
The first task is:
The two commented codeblocks below calculate the spectrum and vizualize the wavefunction of the rectangle.
Can you write Python code for it?

#va,ve=sla.eigsh(QWZ.H,100,sigma=0.02)

#figsize(10,5)
#@interact(i=(0,len(va)-1))
#def play(i=0):
#    subplot(121)
#    plot(arange(len(va)),sort(va),'r+')
#    plot(arange(len(va))[i],va[argsort(va)][i],'wo',ms=10)
#    subplot(122,aspect='equal')
#    QWZ.plot_fullwf(ve[:,argsort(va)[i]],cmap='magma')
#    axis('off');


I am working on the file "QWZ.ipynb".
The first task is:
inspect spectrum and wavefunctions:
Can you write Python code for it?


I am working on the file "Qhue playground.ipynb".
The first task is:
Details of a particular scene from the list:
Can you write Python code for it?


I am working on the file "Qhue playground.ipynb".
The first task is:
Groups and rooms
Let's look at groups:
Can you write Python code for it?


I am working on the file "Qhue playground.ipynb".
The first task is:
Here's a more complete list:
Can you write Python code for it?


I am working on the file "Qhue playground.ipynb".
The first task is:
Let's get the numbers and names of the lights:
Can you write Python code for it?

bridge = Bridge(BRIDGE_IP, username)
lights = bridge.lights()
for num, info in lights.items():
    print("{:16} {}".format(info['name'], num))


I am working on the file "Qhue playground.ipynb".
The first task is:
Let's list scenes with IDs, last updated time, and the lights affected:
Can you write Python code for it?

for sid, info in scenes.items():
    print("\n{:16} {:20} {}".format( sid, info['name'], info['lastupdated']))
    for li in info['lights']:
        print("{:40}- {}".format('', lights[li]['name']))


I am working on the file "Qhue playground.ipynb".
The first task is:
Scenes
Let's look at the scenes defined in the bridge, and their IDs.  Some of these may be created manually, and others by the Hue app or other software.
Version 1-type scenes just refer to the lights - each light is told: "Set the value you have stored for this scene".
Version 2 scenes have more details stored in the hub, which is generally more useful.
Can you write Python code for it?

scenes = bridge.scenes()
print("{} scenes:\n".format(len(scenes)))
print(yaml.safe_dump(scenes, indent=4))


I am working on the file "Qhue playground.ipynb".
The first task is:
Sensors
Sensors are mostly switches, but a few other things come under the same category in the bridge.  There's a 'daylight' sensor, implemented in software, for example, and various bits of state can also be stored here so they can be used in rule conditions later.
Can you write Python code for it?

sensors = bridge.sensors()
summary = [(info['name'], i, info['type']) for i,info in sensors.items()]
# Sort by name
# Python 2: summary.sort(lambda a,b: cmp(a[0], b[0]))
# Python 3:
summary.sort(key = lambda a: a[0])
for n,i,t in summary:
    print("{:30} {:>3} {}".format(n,i,t))
    #print(bridge.sensors[i]())


I am working on the file "Qhue playground.ipynb".
The first task is:
Show the details of the scenes that affect a particular light:
Can you write Python code for it?

lightname = 'Sitting room 1'
# How's this for a nice use of python iterators?
light_id = next(i for i,info in lights.items() if info['name'] == lightname)
print("Light {} - {}".format(light_id, lightname))
for line in ["{} : {:20} {}".format(sid, info['name'], info['lastupdated']) for sid, info in scenes.items() if light_id in info['lights']]:
    print(line)


I am working on the file "Qhue playground.ipynb".
The first task is:
Show the rules triggered by the Sitting Room switch.
For Tap switches, buttons 1,2,3,4 are represented by the values 34,16,17,18 respectively.
Can you write Python code for it?

switch = '10'  # sitting room
print("Switch {} -- {}\n".format(switch, sensors[switch]['name']))

# State changes on the switch will look like this:
state_string = "/sensors/{}/state/".format(switch)

# Look through the rules for once which contain this
# string in their conditions:
for rid, info in rules.items():
    this_switch = False
    matching_conditions = [c for c in info['conditions'] if state_string in c['address']]
    if len(matching_conditions) > 0:
        print("{:3} {:20}".format(rid, info['name']))
        for c in info['conditions']:
            print("   ? condition {}".format(c))
        for a in info['actions']:

            # If the action involves applying a scene, get its name
            scene_name = ""
            if 'scene' in a['body']:
                scene_name = scenes[a['body']['scene']]['name']

            print("   - action address {} body {!s:29s} {} ".format( a['address'], a['body'], scene_name))


I am working on the file "Qhue playground.ipynb".
The first task is:
The current Hue software creates 'rooms', which are groups with a type value set to Room:
Can you write Python code for it?

groups = bridge.groups()
rooms = [(gid, info['name']) for gid, info in groups.items() if info.get('type') == 'Room' ]
for room_id, info in rooms:
    print("{:3} : {}".format(room_id, info))


I am working on the file "Qhue playground.ipynb".
The first task is:
Tidying things up; let's delete a scene:
Can you write Python code for it?

# Uncomment and edit this if you actually want to run it!
# print(bridge.scenes['cd06c70f7-on-0'](http_method='delete'))


I am working on the file "Qingdao.ipynb".
The first task is:
Section
List

One
Two

Link: See Madagascar
Explanation for the modeling part
We create a model in time-velocity space $(t_0,v)$, then we model data by spreading in time-diatance space $(t,x)$ over hyperbolas $t(x) = \sqrt{t_0^2 + \frac{x^2}{v^2}}$.
Can you write Python code for it?


I am working on the file "QuadraysJN.ipynb".
The first task is:
At the end is a method for outputting in quadray coordinates.
Some design decisions were taken, conventions followed, in how the XYZ and IVM systems were overlaid.
Lets not worry about that for now and just imagine a cube with edges sqrt(2)/2, one corner in each octant.  The face diagonals will have length 1 in this case.
For example, in the all positive octant (+ + +) we would have a point at (sqrt(2)/4, sqrt(2)/4, sqrt(2)/4).
Can you write Python code for it?

octant0 = Vector((root2/4, root2/4, root2/4))
print(octant0.xyz)
q0 = octant0.quadray()
print(q0)

q0.length()


I am working on the file "QuadraysJN.ipynb".
The first task is:
Converting to xyz will not work yet, as the Vector class is not yet defined.  That's what's coming.
Can you write Python code for it?

class Vector:

    def __init__(self, arg):
        """Initialize a vector at an (x,y,z)"""
        self.xyz = XYZ(*map(float,arg))

    def __repr__(self):
        return repr(self.xyz)

    @property
    def x(self):
        return self.xyz.x

    @property
    def y(self):
        return self.xyz.y

    @property
    def z(self):
        return self.xyz.z

    def __mul__(self, scalar):
        """Return vector (self) * scalar."""
        newcoords = [scalar * dim for dim in self.xyz]
        return type(self)(newcoords)

    __rmul__ = __mul__ # allow scalar * vector

    def __truediv__(self,scalar):
        """Return vector (self) * 1/scalar"""
        return self.__mul__(1.0/scalar)

    def __add__(self,v1):
        """Add a vector to this vector, return a vector"""
        newcoords = map(add, v1.xyz, self.xyz)
        return type(self)(newcoords)

    def __sub__(self,v1):
        """Subtract vector from this vector, return a vector"""
        return self.__add__(-v1)

    def __neg__(self):
        """Return a vector, the negative of this one."""
        return type(self)(tuple(map(neg, self.xyz)))

    def unit(self):
        return self.__mul__(1.0/self.length())

    def dot(self,v1):
        """Return scalar dot product of this with another vector."""
        return sum(map(mul , v1.xyz, self.xyz))

    def cross(self,v1):
        """Return the vector cross product of this with another vector"""
        newcoords = (self.y * v1.z - self.z * v1.y,
                     self.z * v1.x - self.x * v1.z,
                     self.x * v1.y - self.y * v1.x )
        return type(self)(newcoords)

    def length(self):
        """Return this vector's length"""
        return self.dot(self) ** 0.5

    def quadray(self):
        """return (a, b, c, d) quadray based on current (x, y, z)"""
        x, y, z = self.xyz
        k = 2/root2
        a = k * ((x >= 0)* ( x) + (y >= 0) * ( y) + (z >= 0) * ( z))
        b = k * ((x <  0)* (-x) + (y <  0) * (-y) + (z >= 0)...


I am working on the file "QuadraysJN.ipynb".
The first task is:
Half the cube's vertices will align with the four spokes of the caltrop (in blue). These correspond the the vertexes of an embedded tetrahedron of edges 2 (in red), or edges 1 if measuring in D units.

Note that the Qvector class comes with two ways to express a Qray in canonical lowest terms.  One way preserves the non-negative coordinate address for every point.  The other way assures that the 4-tuple coordinates sum to zero.  I'm using the former for all representations (repr) whereas the latter gets used in various internal computations.
Can you write Python code for it?

# add up three quadrays and negate their sum, to get the other Qray
a = Qvector((1,0,0,0))
c = Qvector((0,0,1,0))
d = Qvector((0,0,0,1))
v_sum = -(a + c + d)
print("Canonical representation:", v_sum)
print("Alternative expression:  ", v_sum.norm0())
print("v_sum length:            ", v_sum.length())


I am working on the file "QuadraysJN.ipynb".
The first task is:
This confirms the cube has the expected edge length.  This is not the length between two quadray tips, which is $1.0$, but between two adjacent corners of our cube, so $\sqrt{2}/2$.
Can you write Python code for it?

diff.quadray().length()

a = Qvector((1,0,0,0))
b = Qvector((0,1,0,0))
(a-b).length()


I am working on the file "QuadraysJN.ipynb".
The first task is:
This might seem strange already. What appears to be a unit vector, has some irrational length. The cube below is flipped over somehow, but gives the idea.  Think of (1,0,0,0) as being in the all positive octant (+, +, +) of XYZ.
Another issue with this cube, in the context of the surrounding computations, is all edges are doubled, with cube face diagonals set at 2.  Divide every linear dimension through by 2 to get the computations here.
The body diagonal of a cube with unit face diagonas is $\sqrt{6}/2$ meaning each quadray, which goes from the cube center to a corner, has a length of $\sqrt(6)/4$. That's the lenght of q0 shown above.

<div style="text-align: center;">
Fig 1:  Divide Through by 2 for Dimensions
</div>
Can you write Python code for it?

octant1 = Vector((-root2/4, root2/4, root2/4))  # neighboring octant
diff = octant0 - octant1
print("diff.quadray()", diff.quadray())
print("Length between adjacent corners: ", diff.length())


I am working on the file "Quakes.ipynb".
The first task is:
Choose your data
In the box below, uncomment one of the data sets by deleting the # at the beginning of the line. Here's the full listing of the different data sets you can grab.
Can you write Python code for it?

# Just earthquakes from the past hour
#data = pd.read_csv("http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.csv")

# All earthquake events in the last month
data = pd.read_csv("http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.csv")

# Only large earthquakes (magnitude 4.5 or greater) in teh past month
#data = pd.read_csv("http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_month.csv")

data.head(4)

# Set variables for scatter plot
x = data.longitude
y = data.latitude

plt.scatter(x,y)
plt.title('Earthquakes around the world (last month)')
plt.xlabel('Longitude')
plt.ylabel('Latitude')

# This actually shows the plot
plt.show()


I am working on the file "Qualitative analysis and Bifurcation diagram Tutorial.ipynb".
The first task is:
And, looking again at the phase space plot, we now see that the flux (the arrows) inside circles outwards, towards a limit cycle, and the arrows outside points inwards. The limit cycle corresponds to the periodic solution we just saw.
Can you write Python code for it?

plot(y_osc[:,0], y_osc[:,1])
R, C = meshgrid(arange(0, 6., .4), arange(0, 2.1, 0.2))
dy = RM(array([R, C]), 0, *pars)
quiver(R, C, dy[0,:], dy[1,:], scale_units='xy', angles='xy')
xlabel('R')
ylabel('C')


I am working on the file "Qualitative analysis and Bifurcation diagram Tutorial.ipynb".
The first task is:
For the parameters chosen above, the long-term (asymptotic) solution is a fixed point. Let's see this in the phase space, that is, the space of Predators ($P$) vs. Prey ($V$). We note that the arrows are "circulating", but always point inwards, and so the trajectory moves toward the middle, to the fixed point.
Can you write Python code for it?

# plot the solution in the phase space
plot(y[:,0], y[:,1])
# defines a grid of points
R, C = meshgrid(arange(0.95, 1.25, .05), arange(0.95, 1.04, 0.01))
# calculates the value of the derivative at the point in the grid
dy = RM(array([R, C]), 0, *pars)
# plots arrows on the points of the grid, with the difection
# and length determined by the derivative dy
# This is a picture of the flow of the solution in the phase space
quiver(R, C, dy[0,:], dy[1,:], scale_units='xy', angles='xy')
xlabel('Resource')
ylabel('Consumer')


I am working on the file "Qualitative analysis and Bifurcation diagram Tutorial.ipynb".
The first task is:
Messing a little with the parameters...
Increasing the carrying capacity $K$ from $10$ to $15$, we now see oscillations...
Can you write Python code for it?

# now K = 15
t = arange(0, 1000, .1)
pars =  (1., 15., 1., 0.1, 0.1, 0.1)
y_osc = odeint(RM, y0, t, pars)
plot(t, y_osc)
xlabel('time')
ylabel('population')
legend(['resource', 'consumer'])


I am working on the file "Qualitative analysis and Bifurcation diagram Tutorial.ipynb".
The first task is:
The bifurcation diagram
We have seen the solutions for two values of $K$, $10$ and $15$, so we want to plot those as a function of $K$. In the second case, there are oscilations, so instead of taking all of the solution, we just pick the minimum and maximum of the solution (after a long time). When the solution is a fixed point, the minimum and maximum should coincide.
Can you write Python code for it?


I am working on the file "Qualitative analysis and Bifurcation diagram Tutorial.ipynb".
The first task is:
This is a very poor bifurcation diagram: it has only two points in $K$! Let's try with many values of $K$.
What happens when we change the carrying capacity $K$ from very small values up to very large values? For very small values, the resource is not going to sustain the consumer population, but for larger values ok $K$, both species should be benefited... right?
Can you write Python code for it?

## this block calculates solutions for many K's, it should take some time
# empty lists to append the values later
ymin = []
ymax = []
KK = arange(.5, 25, .5)
t = arange(0, 6000, 1.)
# loop over the values of K (KK)
for K in KK:
    # redefine the parameters using the new K
    pars =  (1., K, 1., 0.1, 0.1, 0.1)
    # integrate again the equation, with new parameters
    y = odeint(RM, y0, t, pars)
    # calculate the minimum and maximum of the populations, but
    # only for the last 1000 steps (the long-term solution),
    # appending the result to the list
    # question: is 1000 enough? When it wouldn't be?
    ymin.append(y[-1000:,:].min(axis=0))
    ymax.append(y[-1000:,:].max(axis=0))
# convert the lists into arrays
ymin = array(ymin)
ymax = array(ymax)

# and now, we plot the bifurcation diagram
plot(KK, ymin[:,0], 'g', label='resource')
plot(KK, ymax[:,0], 'g')
plot(KK, ymin[:,1], 'b', label='consumer')
plot(KK, ymax[:,1], 'b')
xlabel('$K$')
ylabel('min/max populations')
legend(loc='best')
# use a log scale in the y-axis
yscale('log')


I am working on the file "Qualitative analysis and Bifurcation diagram Tutorial.ipynb".
The first task is:
Well, the first prediction was OK (notice that the plot above uses a log scale), but for high $K$, the minima of the oscillation go to very low values, so that the populations have a high risk of extinction. This phenomenon is the so-called paradox of enrichment.
Consumer-resource dynamics in a seasonal environment
A special type of bifurcation diagram can be used when we have parameters that oscilate with time, and we want to see how this interacts with the system. Let's consider the Rosenzweig-MacArthur equations again, but now we make $r$, the growth rate of the prey, oscilate sinusoidally in time:
$$ \begin{aligned}
\frac{dR}{dt} &= r(t) R \left( 1 - \frac{R}{K} \right) - \frac{a R C}{1+ahR} \
\frac{dC}{dt} &= \frac{e a R C}{1+ahR} - d C \
r(t) &= r_0 (1+\alpha \sin(2\pi t/T))
\end{aligned} $$
We integrate this in the usual manner:
Can you write Python code for it?

def RM_season(y, t, r, alpha, T, K, a, h, e, d):
    # in this function, `t` appears explicitly
    return array([ y[0] * ( r * (1+alpha*sin(2*pi*t/T)) *
                           (1-y[0]/K) - a*y[1]/(1+a*h*y[0]) ),
    y[1] * (e*a*y[0]/(1+a*h*y[0]) - d) ])

t = arange(0, 2000, 1.)
y0 = [1., 1.]
pars =  (1., 0.1, 80., 10., 1., 0.1, 0.1, 0.1)
y = odeint(RM_season, y0, t, pars)
plot(t, y)
xlabel('time')
ylabel('population')
legend(['resource', 'consumer'])


I am working on the file "Quality Of Features.ipynb".
The first task is:
Dependency Parsing Using Stanford Parser
Can you write Python code for it?

train.head(2)

from nltk.parse.stanford import StanfordDependencyParser
import string

path_to_jar = '../../../Downloads/stanford-parser-full-2014-08-27/stanford-parser.jar'
path_to_models_jar = '../../../Downloads/stanford-parser-full-2014-08-27/stanford-parser-3.4.1-models.jar'

dependency_parser = StanfordDependencyParser(path_to_jar=path_to_jar, path_to_models_jar=path_to_models_jar)

def dep_parse(phrase):
    words = [word for word in set(word_tokenize(phrase)) if word not in string.punctuation]
    result = dependency_parser.raw_parse(phrase)
    dep = result.next()
    if dep == None:
        return ''
    triplet = list(dep.triples())
    if triplet == None:
        return ''
    parse = []
    for i in triplet:
        try:
            parse.append("{}({}-{}, {}-{})".format(i[1],i[0][0],words.index(i[0][0])+1,i[2][0],words.index(i[2][0])+1))
        except:
            pass
    return parse

from nltk.tokenize import word_tokenize

import nltk.data
tokenizer = nltk.data.load('nltk:tokenizers/punkt/english.pickle')

## Warning : TAKES A LONG TIME TO RUN
## DON'T RUN UNTIL REQUIRED
text_documents,i = [],2501
for text in train:
    i += 1
    parsed_sents = {}
    try:
        parsed_sents['text'] = text
        parsed_sents['sentences'] = [sents for sents in np.asarray(tokenizer.tokenize(text)) if len(sents.split()) > 1]
        temp = []
        for sentence in parsed_sents['sentences']:
            try:
                temp.append(dep_parse(sentence))
            except:
                pass
        parsed_sents['parses'] = temp
    except:
        print text
        break
    text_documents.append(parsed_sents)
    print '{} requests parsed...'.format(i)
    if i%100 == 0:
        %store text_documents >> test_documents_new.py

filename = str(raw_input('Enter the filename: '))
%store text_documents >> filename


I am working on the file "Quality Of Features.ipynb".
The first task is:
Finding 'Evidentiality' in each of the request
Can you write Python code for it?

import re
regex = 'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'

evidentiality,i = np.ones((len(train['request_text_edit_aware']))),0
for request in train["request_text_edit_aware"]:
    url = re.findall(regex,request)
    if len(url) <= 0:
        evidentiality[i] = 0
    i += 1

train['evidentiality'] = evidentiality
train.head(2)

## How evidentiality affects the success rate
total = train[train['evidentiality'] == 1].received
success = len(total[total == True])
print 'Percentage of successful requests with evidence: {}%'.format(round(float(success)*100/len(total),3))

total = train[train['evidentiality'] == 0].received
success = len(total[total == True])
print 'Percentage of successful requests without evidence: {}%'.format(round(float(success)*100/len(total),3))


I am working on the file "Quality Of Features.ipynb".
The first task is:
Including the politeness feature
Can you write Python code for it?

politeness_data = pd.read_csv('../../Dataset/Random Acts Of Pizza/politeness_one.csv',index_col=0)
part_two = pd.read_csv('../../Dataset/Random Acts Of Pizza/politeness_two.csv',index_col=0)

## One data is missing... So appending missing data
politeness_data = politeness_data.append({'text':data_train.iloc[2500,2],'polite':0.5,'impolite':0.5},ignore_index=True)

## Sanity Check for the size of dataset
print politeness_data.shape[0] + part_two.shape[0] == train.shape[0]

## Adding the politeness data into 'master' dataframe
politeness_data = politeness_data.append(part_two,ignore_index=True)
train['polite'] = politeness_data['polite']
train['impolite'] = politeness_data['impolite']


I am working on the file "Quality Of Features.ipynb".
The first task is:
Length and Topic relation in view of the successful requests
Can you write Python code for it?

i = 1
for points in topic_points:
    plt.subplot(3,4,i)
    plt.plot([point[0] for point in points],[point[1] for point in points])
    plt.ylabel('Probability of Success')
    plt.title('Topic {}'.format(imp_topics[i-1][0]))
    i += 1
    if i > 10:
        i = 1
plt.show()


I am working on the file "Quality Of Features.ipynb".
The first task is:
Relation of Evidentiality with Success of a request
Can you write Python code for it?

evidence_relation = pd.Series({'Success with evidence':35.192,'Success without evidence':23.794})
evidence_relation.to_csv('evidenceRelation.csv',sep=',')
evidence_relation.plot(kind='bar',rot=0)
plt.ylabel('Percentage of Successful request')
plt.title('How evidence effects a successful request')
plt.show()


I am working on the file "Quality Of Features.ipynb".
The first task is:
Relation of Reciprocity with Successful request
Can you write Python code for it?

reciprocity_relation = pd.Series({'Success with reciprocity':30.058,'Success without reciprocity':23.8})
reciprocity_relation.to_csv('reciprocity_relation.csv',sep=',')
reciprocity_relation.plot(kind='bar',rot=0)
plt.ylabel('Percentage of Successful request')
plt.title('How reciprocity effects a successful request')
plt.show()


I am working on the file "Quality Of Features.ipynb".
The first task is:
Success correlation with the length of request
Can you write Python code for it?

train.dropna(inplace=True,axis=0)
train.tail(1)

train['request_length'] = [len(x.split()) for x in train['request_text_edit_aware']]
train.head(2)

length = []
def length_success(topic):
    max_length = train[train['topics'] == topic]['request_length'].max()
    min_length = train[train['topics'] == topic]['request_length'].min()
    bin_size = (max_length - min_length)/20
    df = train[train['topics'] == topic]
    for i in range(10):
        df_one = df[(df['request_length'] >= min_length) & (df['request_length'] < min_length+bin_size)]
        df_new = df_one[df_one['received'] == True]
        if(len(df_one) == 0):
            df_one = ['a']
        length.append([topic,min_length,min_length+bin_size,float(len(df_new))/len(df_one)])
        min_length = min_length + bin_size

for topic in imp_topics:
    print 'Calculating length probabilities for {} topic..'.format(topic[0])
    length_success(topic[0])

df_length = pd.DataFrame(length,columns=['Topic','Lower Bound','Upper Bound','Probability Success'])
df_length.head(5)

df_length.to_csv('LengthCorrelation.csv',sep=',',columns=df_length.columns)

topic_points = []
for topic in imp_topics:
    points = []
    df_new = df_length[df_length['Topic'] == topic[0]]
    for i in range(8):
        points.append(((df_new.iloc[i,1] + df_new.iloc[i,2]/2),df_new.iloc[i,3]))
    topic_points.append(points)


I am working on the file "Quality Of Features.ipynb".
The first task is:
Success of Different Topics
Can you write Python code for it?

topic_df.plot(kind='bar',y='Success Rate',x='Topic')
plt.xlabel('Topic')
plt.ylabel('Success Rate')
plt.title('Success Rate vs Topics')
plt.show()


I am working on the file "Quandl_and_csv.ipynb".
The first task is:
http://www.edwardsaquifer.org/scientific-research-and-data/aquifer-data-and-maps/historical-data/historic-data-downloads
Can you write Python code for it?

! curl http://data.edwardsaquifer.org/csv_j17.php > csv_j17
! curl http://data.edwardsaquifer.org/csv_j27.php > csv_j27
! curl http://data.edwardsaquifer.org/csv_comal.php > csv_comal
! curl http://data.edwardsaquifer.org/csv_san.php > csv_san_marcos
! curl http://data.edwardsaquifer.org/csv_hondo.php > csv_hondd

import csv
with open('csv_j17','rb') as infile:
    reader = csv.reader(infile)
    j17_list = list(reader0
len(j17_list)
print j17_list[0]
print j17_list[1]
print j17_list[-1]


I am working on the file "Quandl_and_csv.ipynb".
The first task is:
https://www.quandl.com/data/SOCSEC/RETIRED-Social-Security-Beneficiary-Data-Retired-Workers-and-Dependants
Can you write Python code for it?

import quandl
print quandl.get("SOCSEC/RETIRED")

! apt-get install curl


I am working on the file "QuantTrading/creating_trading_strategy_01-sma_crossover.ipynb".
The first task is:
Rychlý přehled základních běžných typů strategií
Pro automatické obchodování, které využívá analýzu dat, se používá termín Quantitative trading. Běžně a nejčastěji se v obchodování se používají dva typy strategií:
Strategie založené na momentu (momentum strategy)
Zde spadají jak trendové strategie, tak i strategie na principu divergencí. V případě obchodování těchto typů strategií spoléháme, že aktuální pohyb trhu bude pokračovat ve stejném směru i v budoucnosti. Zároveň věříme, že tyto trendy dokážeme detekovat a následně využít. Jako příklad takové strategií jsou strategie na bázi křížení klouzavých průměrů (moving average crossover).
Reverzní strategie (reversion strategy)
Někdy se jim říká také strategie konvergence (covergence) nebo strategie burzovních cyklů (cycle trading). Tyto strategie zakládají na myšlence, že aktuální pohyb může eventuálně začít reversovat. Jako příklad může posloužit strategie založená na principu návratu k průměrné ceně (mean reversion strategy).
Strategie na principu křížení klouzavých průměrů
Ta nejjednodušší strategie, kterou lze vytvořit, je křížení klouzavých průměrů.
Využiji dva jednoduché klouzavé průměry (SMA - simple moving average) s rozdílnými periodami, např. 30 a 90 dní. Pro pozici long chci vidět překřížení SMA s kratší periodou směrem nahoru přes SMA s delší periodou. Pro pozici short chci vidět překřížení SMA s kratší periodou směrem dolů přes SMA s delší periodou.
Postup
1. Získám data trhu, kterého chci obchodovat a ty si vložím do proměnné ohlc_data.
+ Definuji si periody pro kratší a delší SMA do proměnných short_period a long_period.
+ Vytvořím si signální DataFrame, který bude mít sloupec signal. Jen musím zajistit, aby indexy v signals odpovídaly s index v ohlc_data.
+ Pomocí rolling() a mean() funkcí vypočítám SMA pro kratší a delší periodu.
+ Do signals['signal'] vložím 1.0 pro dny, kde SMA s kratší periodou je nad SMA s delší periodou. Tento signál musím počítat od pozice, kdy je platná vypočítaná hodnota pro SMA s kratší periodou, tj. od řádku s...
Can you write Python code for it?

# Zíkám data ETF trhu SPY, který kopíruje trh S&P 500
start_date = datetime.datetime(2008, 1, 1)
end_date = datetime.datetime.now()
ohlc_data = pdr_web.DataReader("NYSEARCA:SPY", 'google', start=start_date, end=end_date)

# Příprava period pro SMA
short_period = 30
long_period = 90

# Připrava signálního DataFrame s vynulovaným sloupcem 'signal', 0=bez signálu
signals = pd.DataFrame(index=ohlc_data.index)
signals['signal'] = 0.0

# SMA pro kratší a delší periodu.
signals['short_sma'] = ohlc_data['Close'].rolling(window=short_period, min_periods=1, center=False).mean()
signals['long_sma'] = ohlc_data['Close'].rolling(window=long_period, min_periods=1, center=False).mean()

# Získání signálu pro situace, kde SMA s menší periodou je nad SMA s větší periodou.
signals['signal'][short_period:] = np.where(signals['short_sma'][short_period:]
                                            > signals['long_sma'][short_period:], 1.0, 0.0)

# Vygenerování obchodních příkazů
signals['positions'] = signals['signal'].diff()


I am working on the file "QuantTrading/creating_trading_strategy_01-sma_crossover.ipynb".
The first task is:
Vypočítané short pozice
Can you write Python code for it?


I am working on the file "QuantTrading/creating_trading_strategy_01-sma_crossover.ipynb".
The first task is:
Zobrazení výsledků
Can you write Python code for it?

import matplotlib.pyplot as plt

# Příprava oblasti grafu
fig = plt.figure(figsize=(15,7))

# Přidání grafu s pojmenovanou osou y do oblasti grafu
ax1 = fig.add_subplot(111,  ylabel='Cena v $')

# Přidání vývoje Close ceny do grafu.
ohlc_data['Close'].plot(ax=ax1, color='r', lw=2.)

# Přidání vývoje klouzavých průměrů do grafu.
signals[['short_sma', 'long_sma']].plot(ax=ax1, lw=2.)

# Přidání vstupních signálů "buy"
ax1.plot(signals.loc[signals.positions == 1.0].index,
         signals.short_sma[signals.positions == 1.0],
         '^', markersize=10, color='b')

# Přidání vstupních signálů "sell"
ax1.plot(signals.loc[signals.positions == -1.0].index,
         signals.short_sma[signals.positions == -1.0],
         'v', markersize=10, color='k')

# Zobrazení grafu
plt.show()