I am working on the file "20_Natural_Language_Processing.ipynb".
The first task is:
We can then print the words that are near and far from the word 'great' in terms of their vector-embeddings. Note that these may change each time you train the model.
Can you write Python code for it?


I am working on the file "20_Natural_Language_Processing.ipynb".
The first task is:
We can then use the tokenizer to convert all texts in the training-set to lists of these tokens.
Can you write Python code for it?


I am working on the file "20_Natural_Language_Processing.ipynb".
The first task is:
We first convert these texts to arrays of integer-tokens because that is needed by the model.
Can you write Python code for it?


I am working on the file "20_Natural_Language_Processing.ipynb".
The first task is:
When padding or truncating the sequences that have a different length, we need to determine if we want to do this padding or truncating 'pre' or 'post'. If a sequence is truncated, it means that a part of the sequence is simply thrown away. If a sequence is padded, it means that zeros are added to the sequence.
So the choice of 'pre' or 'post' can be important because it determines whether we throw away the first or last part of a sequence when truncating, and it determines whether we add zeros to the beginning or end of the sequence when padding. This may confuse the Recurrent Neural Network.
Can you write Python code for it?

pad = 'pre'

x_train_pad = pad_sequences(x_train_tokens, maxlen=max_tokens,
                            padding=pad, truncating=pad)

x_test_pad = pad_sequences(x_test_tokens, maxlen=max_tokens,
                           padding=pad, truncating=pad)


I am working on the file "20_newsgroup/lda_benchmark_20ng.ipynb".
The first task is:
Additional parameters
Now to simulate a more useful use-case scenario when performing topic modelling, I will now start putting more parameters and tinkering with them, and see how exection time and memory consumption would vary.
Iterations
Can you write Python code for it?

%timeit lda = models.LdaModel(corpus=corpus, id2word=dictionary, num_topics=10, iterations = 100)

%memit lda = models.LdaModel(corpus=corpus, id2word=dictionary, num_topics=10, iterations = 100)

%timeit lda = models.LdaModel(corpus=corpus, id2word=dictionary, num_topics=10, iterations = 300)

%memit lda = models.LdaModel(corpus=corpus, id2word=dictionary, num_topics=10, iterations = 300)

%timeit lda = models.LdaModel(corpus=corpus, id2word=dictionary, num_topics=10, iterations = 1000)

%memit lda = models.LdaModel(corpus=corpus, id2word=dictionary, num_topics=10, iterations = 1000)


I am working on the file "20_newsgroup/lda_benchmark_20ng.ipynb".
The first task is:
Alpha
Can you write Python code for it?

%timeit lda = models.LdaModel(corpus=corpus, id2word=dictionary, num_topics=5, alpha='symmetric', iterations=1000)

%memit lda = models.LdaModel(corpus=corpus, id2word=dictionary, num_topics=5, alpha='symmetric', iterations=1000)

%timeit lda = models.LdaModel(corpus=corpus, id2word=dictionary, num_topics=5, alpha='asymmetric', iterations=1000)

%memit lda = models.LdaModel(corpus=corpus, id2word=dictionary, num_topics=5, alpha='asymmetric', iterations=1000)

%timeit lda = models.LdaModel(corpus=corpus, id2word=dictionary, num_topics=5, alpha='auto', iterations=1000)

%memit lda = models.LdaModel(corpus=corpus, id2word=dictionary, num_topics=5, alpha='auto', iterations=1000)


I am working on the file "20_newsgroup/lda_benchmark_20ng.ipynb".
The first task is:
Default settings
First off I will start by testing with default settings
Can you write Python code for it?

%timeit lda = models.LdaModel(corpus=corpus, id2word=dictionary, num_topics=num_topics)

%memit lda = models.LdaModel(corpus=corpus, id2word=dictionary, num_topics=num_topics)

%timeit lda = models.ldamulticore.LdaMulticore(corpus=corpus, id2word=dictionary, num_topics=num_topics, workers = 1)

%timeit lda = models.ldamulticore.LdaMulticore(corpus=corpus, id2word=dictionary, num_topics=num_topics, workers = 2)

%timeit lda = models.ldamulticore.LdaMulticore(corpus=corpus, id2word=dictionary, num_topics=num_topics, workers = 3)

%timeit lda = models.ldamulticore.LdaMulticore(corpus=corpus, id2word=dictionary, num_topics=num_topics, workers = 4)

%memit lda = models.ldamulticore.LdaMulticore(corpus=corpus, id2word=dictionary, num_topics=num_topics, workers = 1)

%memit lda = models.ldamulticore.LdaMulticore(corpus=corpus, id2word=dictionary, num_topics=num_topics, workers = 2)

%memit lda = models.ldamulticore.LdaMulticore(corpus=corpus, id2word=dictionary, num_topics=num_topics, workers = 3)

%memit lda = models.ldamulticore.LdaMulticore(corpus=corpus, id2word=dictionary, num_topics=num_topics, workers = 4)


I am working on the file "20_newsgroup/lda_benchmark_20ng.ipynb".
The first task is:
Number of passes
Can you write Python code for it?

#default number of passes is 1
%timeit lda = models.LdaModel(corpus=corpus, id2word=dictionary, num_topics=5, passes=1)

%memit lda = models.LdaModel(corpus=corpus, id2word=dictionary, num_topics=5, passes=1)

%timeit lda = models.LdaModel(corpus=corpus, id2word=dictionary, num_topics=5, passes=2)

%memit lda = models.LdaModel(corpus=corpus, id2word=dictionary, num_topics=5, passes=2)


I am working on the file "20_newsgroup/lda_benchmark_20ng.ipynb".
The first task is:
Visualization
Can you write Python code for it?

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
import pandas as pd
%matplotlib inline
sns.set(style="white")

f, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 4))
d = { 'time' : pd.Series([74, 63, 53.2, 51.7, 50.8], index=xrange(5)),
    'peak_memory' : pd.Series([310.88, 462.82, 416.68, 465.73, 502.21], index=xrange(5)),
     'memory_increment' : pd.Series([65.73, 198.18, 127.80, 195.93, 92.29], index=xrange(5)),
     'algo' : pd.Series(['lda', 'lda-m(1)', 'lda-m(2)', 'lda-m(3)', 'lda-m(4)'], index=xrange(5))}
data = pd.DataFrame(d)
plt.suptitle('Default settings; # of topics 10', fontsize=20)
ax1.set_title('algo vs execution time')
ax1.set_xlabel('Algo')
ax1.set_ylabel('Time in sec')
data = pd.DataFrame(d)
sns.barplot(x='algo', y='time', data=data, palette='PuBu', ax=ax1)
ax2.set_title('algo vs memory usage')
ax2.set_xlabel('Algo')
ax2.set_ylabel('Memory in MB')
sns.barplot(x='algo', y='peak_memory', data=data, palette='Greens', ax=ax2)

f1, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 4))
d1 = { 'time' : pd.Series([84, 91, 96, 63, 91, 127], index=xrange(6)),
    'memory' : pd.Series([469.88, 330.81, 331.52, 488.07, 595.02, 542.75], index=xrange(6)),
     'iterations' : pd.Series([100, 300, 1000, 100, 300, 1000], index=xrange(6)),
     'algo' : pd.Series(['lda', 'lda', 'lda', 'lda-m(3)', 'lda-m(3)', 'lda-m(3)'], index=xrange(6))}
data1 = pd.DataFrame(d1)
plt.suptitle('Number of iterations', fontsize=20)
ax1.set_title('algo vs execution time')
ax1.set_xlabel('Algo')
ax1.set_ylabel('Time in sec')
sns.barplot(x='algo', y='time', hue='iterations', data=data1, palette='Purples', ax=ax1)
ax2.set_title('algo vs memory usage')
ax2.set_xlabel('Algo')
ax2.set_ylabel('Memory in MB')
sns.barplot(x='algo', y='memory', hue='iterations', data=data1, palette='pastel', ax=ax2)

f, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 4))
d = { 'time' : pd.Series([68, 113, 172, 226, 273], index=xrange(5)),
    'memory' : pd.Series([448.07, 491.55, 563.61, 706.84, 889.00], index=xrange(5)),
     'topics' :...


I am working on the file "20newsgroup/20newsgroup.ipynb".
The first task is:
Aplicar las stopwords:
Can you write Python code for it?

count_vect = CountVectorizer(decode_error = 'ignore', lowercase=True, strip_accents="unicode", stop_words = stopwords)
X_train_counts = count_vect.fit_transform(data_train.data)
X_train_counts.shape


I am working on the file "20newsgroup/20newsgroup.ipynb".
The first task is:
Entrenamiento y creación de un modelo
Can you write Python code for it?

from sklearn.naive_bayes import MultinomialNB
clf = MultinomialNB().fit(X_train_tfidf, data_train.target)

docs_new = ['God is love', 'OpenGL on the GPU is fast', 'I want to buy a new motorcycle']
X_new_counts = count_vect.transform(docs_new)
X_new_tfidf = tfidf_transformer.transform(X_new_counts)

prediction = clf.predict(X_new_tfidf)

for doc, category in zip(docs_new, prediction):
    print('%r => %s' % (doc, data_train.target_names[category]))

X_new_counts = count_vect.transform(data_test.data)
X_new_tfidf = tfidf_transformer.transform(X_new_counts)

prediction = clf.predict(X_new_tfidf)

#for doc, category in zip(docs_new, predicted):
#    print('%r => %s' % (doc, data_train.target_names[category]))


I am working on the file "20newsgroup/20newsgroup.ipynb".
The first task is:
Matriz de confusión
Can you write Python code for it?

from sklearn.metrics import confusion_matrix
cm = confusion_matrix(data_test.target, prediction)

def plot_confusion_matrix(cm, labels, title='Confusion matrix', cmap=plt.cm.Blues):
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(labels))
    plt.xticks(tick_marks, labels, rotation=45)
    plt.yticks(tick_marks, labels)
    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')

cm_normalized = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print('Normalized confusion matrix')
##print(cm_normalized)
plt.figure(figsize=(18,12))
plot_confusion_matrix(cm_normalized, data_train.target_names, title='Normalized confusion matrix')

plt.show()


I am working on the file "20newsgroup/20newsgroup.ipynb".
The first task is:
Una vez definida la función, se cargan los datos de entrenamiento y test.
Can you write Python code for it?

print "Loading data train..."
data_train = loadDataset("data/20news-bydate-train/")

print "Loading data test...."
data_test = loadDataset("data/20news-bydate-test/")


I am working on the file "20newsgroup/20newsgroup.ipynb".
The first task is:
¿Cómo se podrían obtener los nombres de las categorías de los 10 primeros archivos?
Can you write Python code for it?


I am working on the file "21--lsq-linreg-1-errs.ipynb".
The first task is:
By the way, in Numpy you can determine machine epsilon in a more portable way:
Can you write Python code for it?

EPS_S = np.finfo (np.float32).eps
EPS_D = np.finfo (float).eps

print_float_bin (float (EPS_S), prefix="eps_s")
print_float_bin (float (EPS_D), prefix="eps_d")


I am working on the file "21--lsq-linreg-1-errs.ipynb".
The first task is:
Each row of this dataset is a US State. The columns are described here: http://www.statsci.org/data/general/uscrime.html
Let's take a quick peek at the dataset.
Can you write Python code for it?

df.describe ()

import seaborn as sns
%matplotlib inline

# Look at a few relationships
sns.pairplot (df[['Crime', 'Wealth', 'Ed', 'U1']])


I am working on the file "21--lsq-linreg-1-errs.ipynb".
The first task is:
Exercise. Recall the two program fragments from above:
Program 1:
s = a - b
t = s + b

Program 2:
s = a + b
t = s - b

Let $a=1.0$ and $b=\epsilon_d / 2 \approx 1.11 \times 10^{-16}$. Write some Python code to evaluate these two programs and compare their outputs. (To look closely at their outputs, use the print_float_bin() function from above.)
Can you write Python code for it?

a = 1.0
b = 2.**(-53) # What value goes here?

s1 =  a - b
t1 = s1 + b

s2 =  a + b
t2 = s2 - b

print_float_bin (s1, prefix="s1")
print_float_bin (t1, prefix="t1")
print ("\n")
print_float_bin (s2, prefix="s2")
print_float_bin (t2, prefix="t2")

print "\n", t1, t2
print ("\n(t1 == t2) == %s" % (t1 == t2))


I am working on the file "21--lsq-linreg-1-errs.ipynb".
The first task is:
For today's lesson, it will be helpful to occasionally peek at floating-point values "in the raw." For this purpose, we've provided you with a handy routine called float_to_bin(x), which given a floating-point value x will return its IEEE representation as a binary string. (It is defined in the cse6040utils module.)
The following code uses float_to_bin() to define another function that dumps a floating-point number's complete Decimal form along with its binary form.
Can you write Python code for it?

def print_float_bin (x, prefix=""):
    print ("%s: %s\n%s  %s" % (prefix,
                                Decimal (x),
                                ' ' * len (prefix),
                                cse6040.float_to_bin (x)))

a = -1.0
b = 2.**(-52)  # Recall: \epsilon_d
c = b / 2.

print_float_bin (a, prefix="a")
print_float_bin (b, prefix="b")
print_float_bin (c, prefix="c")


I am working on the file "21. Zipcode Visualization.ipynb".
The first task is:
Based on the example in
http://www.christianpeccei.com/zipmap/
ZIP area data downloaded from
ftp://ftp.cs.brown.edu/u/spr/zipdata
The mapping from states to numbers can be seen here:
https://github.com/ssoper/zip-code-boundaries/blob/master/raw.html
Can you write Python code for it?

import os.path
if not os.path.exists('zipdata/zt06_d00_ascii.zip'):
    !wget -P zipdata ftp://ftp.cs.brown.edu/u/spr/zipdata/zt06_d00_ascii.zip
    !unzip -d zipdata zipdata/zt06_d00_ascii.zip

if not os.path.exists('zipdata/zt48_d00_ascii.zip'):
    !wget -P zipdata ftp://ftp.cs.brown.edu/u/spr/zipdata/zt48_d00_ascii.zip
    !unzip -d zipdata zipdata/zt48_d00_ascii.zip

def read_ascii_boundary(filestem):
    '''
    Reads polygon data from an ASCII boundary file.
    Returns a dictionary with polygon IDs for keys. The value for each
    key is another dictionary with three keys:
    'name' - the name of the polygon
    'polygon' - list of (longitude, latitude) pairs defining the main
    polygon boundary
    'exclusions' - list of lists of (lon, lat) pairs for any exclusions in
    the main polygon
    '''
    metadata_file = filestem + 'a.dat'
    data_file = filestem + '.dat'
    # Read metadata
    lines = [line.strip().strip('"') for line in open(metadata_file)]
    polygon_ids = lines[::6]
    polygon_names = lines[2::6]
    polygon_data = {}
    for polygon_id, polygon_name in zip(polygon_ids, polygon_names):
        # Initialize entry with name of polygon.
        # In this case the polygon_name will be the 5-digit ZIP code.
        polygon_data[polygon_id] = {'name': polygon_name}
    del polygon_data['0']
    # Read lon and lat.
    f = open(data_file)
    for line in f:
        fields = line.split()
        if len(fields) == 3:
            # Initialize new polygon
            polygon_id = fields[0]
            polygon_data[polygon_id]['polygon'] = []
            polygon_data[polygon_id]['exclusions'] = []
        elif len(fields) == 1:
            # -99999 denotes the start of a new sub-polygon
            if fields[0] == '-99999':
                polygon_data[polygon_id]['exclusions'].append([])
        else:
            # Add lon/lat pair to main polygon or exclusion
            lon = float(fields[0])
            lat = float(fields[1])
            if polygon_data[polygon_id]['exclusions']:
    ...


I am working on the file "21. Zipcode Visualization.ipynb".
The first task is:
From:
http://mpld3.github.io/
Can you write Python code for it?

import mpld3
if True:
    mpld3.enable_notebook()

reduced = data[['Pat_ZIP', 'Total_Charges']]
chargesbyzip = reduced.groupby('Pat_ZIP').mean()

countbyzip = reduced.groupby('Pat_ZIP').count()

#def makezipfigure(series, zipstem = 'zipdata/zt48_d00'):
series = chargesbyzip
series = countbyzip
zipstem = 'zipdata/zt48_d00'

maxvalue = series.max().values[0]
valuename = series.keys()[0]

# Read in ZIP code boundaries for Te
d = read_ascii_boundary(zipstem)

# Create figure and two axes: one to hold the map and one to hold
# the colorbar
figure(figsize=(5, 5), dpi=100)
map_axis = axes([0.0, 0.0, 0.8, 0.9])
cb_axis = axes([0.83, 0.1, 0.03, 0.8])
#map_axis = axes([0.0, 0.0, 4.0, 4.5])
#cb_axis = axes([4.15, 0.5, 0.15, 3.0])

#map_axis = axes([0.0, 0.0, 1.6, 1.8])
#cb_axis = axes([1.66, 0.2, 0.06, 1.2])

# Define colormap to color the ZIP codes.
# You can try changing this to cm.Blues or any other colormap
# to get a different effect
cmap = cm.PuRd

# Create the map axis
axes(map_axis)
gca().set_axis_off()

# Loop over the ZIP codes in the boundary file
for polygon_id in d:
    polygon_data = array(d[polygon_id]['polygon'])
    zipcode = d[polygon_id]['name']
    try:
        value = series.xs(zipcode).values[0]
        # Define the color for the ZIP code
        fc = cmap(float(value) / maxvalue)
    except:
        fc = (1.0, 1.0, 1.0, 1.0)

    edgecolor = [ square(min(fc[:3])) ]*3 + [0.5]
    # Draw the ZIP code
    patch = Polygon(array(polygon_data), facecolor=fc,
        edgecolor=edgecolor, linewidth=.1)
#    patch = Polygon(array(polygon_data), facecolor=fc,
#        edgecolor=(.5, .5, .5, 1), linewidth=.2)
    gca().add_patch(patch)

gca().autoscale()
title(valuename + " per ZIP Code in Texas")

# Draw colorbar
cb = mpl.colorbar.ColorbarBase(cb_axis, cmap=cmap,
    norm = mpl.colors.Normalize(vmin=0, vmax=maxvalue))
cb.set_label(valuename)

savefig('texas.pdf', dpi=100)

# Change all fonts to Arial
#for o in gcf().findobj(matplotlib.text.Text):
#    o.set_fontname('Arial')


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
Callback Functions
During training we want to save checkpoints and log the progress to TensorBoard so we create the appropriate callbacks for Keras.
This is the callback for writing checkpoints during training.
Can you write Python code for it?

path_checkpoint = '21_checkpoint.keras'
callback_checkpoint = ModelCheckpoint(filepath=path_checkpoint,
                                      monitor='val_loss',
                                      verbose=1,
                                      save_weights_only=True,
                                      save_best_only=True)


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
Compare this to the original text from the data-set, which is almost identical except for punctuation marks and a few words such as "dreaded millennium bug". This is because we only use a vocabulary of the 10000 most frequent words in the data-set and those 3 words were apparently not used frequently enough to be included in the vocabulary, so they are merely skipped.
Can you write Python code for it?


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
Connect and Create the Models
We can now connect the encoder and decoder in different ways.
First we connect the encoder directly to the decoder so it is one whole model that can be trained end-to-end. This means the initial-state of the decoder's GRU units are set to the output of the encoder.
Can you write Python code for it?

decoder_output = connect_decoder(initial_state=encoder_output)

model_train = Model(inputs=[encoder_input, decoder_input],
                    outputs=[decoder_output])


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
Error in Data
The data-set contains about 2 million sentence-pairs. Some of the data is incorrect. This example appears to be French (or some other weird language I don't understand), although the Danish text is also included.
Can you write Python code for it?

idx = 8002

data_src[idx]

data_dest[idx]


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
Example of Token Sequences
This is the output of the tokenizer. Note how it is padded with zeros at the beginning (pre-padding).
Can you write Python code for it?


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
Examples
Translate a text from the training-data. This translation is quite good. Note how it is not identical to the translation from the training-data, but the actual meaning is similar.
Can you write Python code for it?

idx = 3
translate(input_text=data_src[idx],
          true_output_text=data_dest[idx])


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
Here is another example which is also a reasonable translation, although it has incorrectly translated the natural disasters. Note "countries of the European Union" has instead been translated as "member states" which are synonyms in this context.
Can you write Python code for it?

idx = 4
translate(input_text=data_src[idx],
          true_output_text=data_dest[idx])


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
If we reverse the order of these two texts then the meaning is not quite so clear for the latter text.
Can you write Python code for it?

idx = 3
translate(input_text=data_src[idx+1] + data_src[idx],
          true_output_text=data_dest[idx+1] + data_dest[idx])


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
In order for the decoder to know when to begin and end a sentence, we need to mark the start and end of each sentence with words that most likely don't occur in the data-set.
Can you write Python code for it?


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
In this example we join two texts from the training-set. The model first sends this combined text through the encoder, which produces a "thought-vector" that seems to summarize both texts reasonably well so the decoder can produce a reasonable translation.
Can you write Python code for it?

idx = 3
translate(input_text=data_src[idx] + data_src[idx+1],
          true_output_text=data_dest[idx] + data_dest[idx+1])


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
In this tutorial I have used the English-Danish data-set which contains about 2 million sentence-pairs. You can use another language by changing this language-code, see europarl.py for a list of available language-codes.
Can you write Python code for it?


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
Load Checkpoint
You can reload the last saved checkpoint so you don't have to train the model every time you want to use it.
Can you write Python code for it?

try:
    model_train.load_weights(path_checkpoint)
except Exception as error:
    print("Error trying to load checkpoint.")
    print(error)


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
Load Data
We will use the Europarl data-set which has sentence-pairs in most European languages. The data was created by the European Union which translates a lot of their communications to the languages of the member-countries of the European Union.
http://www.statmt.org/europarl/
Can you write Python code for it?


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
Load the texts for the source-language, here we use Danish.
Can you write Python code for it?

data_src = europarl.load_data(english=False,
                              language_code=language_code)


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
Note how the encoder uses the normal output from its last GRU-layer as the "thought vector". Research papers often use the internal state of the encoder's last recurrent layer as the "thought vector". But this makes the implementation more complicated and is not necessary when using the GRU. But if you were using the LSTM instead then it is necessary to use the LSTM's internal states as the "thought vector" because it actually has two internal vectors, which we would need to initialize the two internal states of the decoder's LSTM units.
We can now use this function to connect all the layers in the encoder so it can be connected to the decoder further below.
Can you write Python code for it?


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
Note that all these models use the same weights and variables of the encoder and decoder. We are merely changing how they are connected. So once the entire model has been trained, we can run the encoder and decoder models separately with the trained weights.
Compile the Model
The output of the decoder is a sequence of one-hot encoded arrays. In order to train the decoder we need to supply the one-hot encoded arrays that we desire to see on the decoder's output, and then use a loss-function like cross-entropy to train the decoder to produce this desired output.
However, our data-set contains integer-tokens instead of one-hot encoded arrays. Each one-hot encoded array has 10000 elements so it would be extremely wasteful to convert the entire data-set to one-hot encoded arrays.
A better way is to use a so-called sparse cross-entropy loss-function, which does the conversion internally from integers to one-hot encoded arrays.
We have used the Adam optimizer in many of the previous tutorials, but it seems to diverge in some of these experiments with Recurrent Neural Networks. RMSprop seems to work much better for these.
Can you write Python code for it?

model_train.compile(optimizer=RMSprop(lr=1e-3),
                    loss='sparse_categorical_crossentropy')


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
Now create the tokenizer for the destination language. We need a tokenizer for both the source- and destination-languages because their vocabularies are different. Note that this tokenizer does not reverse the sequences and it pads zeros at the end ('post') of the arrays.
Can you write Python code for it?

%%time
tokenizer_dest = TokenizerWrap(texts=data_dest,
                               padding='post',
                               reverse=False,
                               num_words=num_words)


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
Now we can train the model. One epoch of training took about 1 hour on a GTX 1070 GPU. You probably need to run 10 epochs or more during training. After 10 epochs the loss was about 1.10 on the training-set and about 1.15 on the validation-set.
The batch-size was chosen to keep the GPU running at nearly 100% while being within the memory limits of 8GB for this GPU.
Can you write Python code for it?

model_train.fit(x=x_data,
                y=y_data,
                batch_size=384,
                epochs=10,
                validation_split=validation_split,
                callbacks=callbacks)


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
The decoder is built using the functional API of Keras, which allows more flexibility in connecting the layers e.g. to route different inputs to the decoder. This is useful because we have to connect the decoder directly to the encoder, but we will also connect the decoder to another input so we can run it separately.
This function connects all the layers of the decoder to some input of the initial-state values for the GRU layers.
Can you write Python code for it?

def connect_decoder(initial_state):
    # Start the decoder-network with its input-layer.
    net = decoder_input

    # Connect the embedding-layer.
    net = decoder_embedding(net)

    # Connect all the GRU-layers.
    net = decoder_gru1(net, initial_state=initial_state)
    net = decoder_gru2(net, initial_state=initial_state)
    net = decoder_gru3(net, initial_state=initial_state)

    # Connect the final dense layer that converts to
    # one-hot encoded arrays.
    decoder_output = decoder_dense(net)

    return decoder_output


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
The input and output data for the decoder is identical, except shifted one time-step. We can use the same numpy array to save memory by slicing it, which merely creates different 'views' of the same data in memory.
Can you write Python code for it?

decoder_input_data = tokens_dest[:, :-1]
decoder_input_data.shape

decoder_output_data = tokens_dest[:, 1:]
decoder_output_data.shape


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
Then we create a model for just the encoder alone. This is useful for mapping a sequence of integer-tokens to a "thought-vector" summarizing its contents.
Can you write Python code for it?

model_encoder = Model(inputs=[encoder_input],
                      outputs=[encoder_output])


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
This creates the 3 GRU layers of the decoder. Note that they all return sequences because we ultimately want to output a sequence of integer-tokens that can be converted into a text-sequence.
Can you write Python code for it?

decoder_gru1 = GRU(state_size, name='decoder_gru1',
                   return_sequences=True)
decoder_gru2 = GRU(state_size, name='decoder_gru2',
                   return_sequences=True)
decoder_gru3 = GRU(state_size, name='decoder_gru3',
                   return_sequences=True)


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
This creates the 3 GRU layers that will map from a sequence of embedding-vectors to a single "thought vector" which summarizes the contents of the input-text. Note that the last GRU-layer does not return a sequence.
Can you write Python code for it?

encoder_gru1 = GRU(state_size, name='encoder_gru1',
                   return_sequences=True)
encoder_gru2 = GRU(state_size, name='encoder_gru2',
                   return_sequences=True)
encoder_gru3 = GRU(state_size, name='encoder_gru3',
                   return_sequences=False)


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
This helper-function connects all the layers of the encoder.
Can you write Python code for it?

def connect_encoder():
    # Start the neural network with its input-layer.
    net = encoder_input

    # Connect the embedding-layer.
    net = encoder_embedding(net)

    # Connect all the GRU-layers.
    net = encoder_gru1(net)
    net = encoder_gru2(net)
    net = encoder_gru3(net)

    # This is the output of the encoder.
    encoder_output = net

    return encoder_output


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
This is a text from a Danish song. It doesn't even make much sense in Danish. However the translation is probably so broken because several of the words are not in the vocabulary.
Can you write Python code for it?

translate(input_text="Hvem spæner ud af en butik og tygger de stærkeste bolcher?",
          true_output_text="Who runs out of a shop and chews the strongest bon-bons?")


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
This is an example I made up. It is a quite broken translation.
Can you write Python code for it?

translate(input_text="der var engang et land der hed Danmark",
          true_output_text='Once there was a country named Denmark')


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
This is the callback for stopping the optimization when performance worsens on the validation-set.
Can you write Python code for it?

callback_early_stopping = EarlyStopping(monitor='val_loss',
                                        patience=3, verbose=1)


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
This is the embedding-layer which converts integer-tokens to vectors of real-valued numbers roughly between -1 and 1. Note that we have different embedding-layers for the encoder and decoder because we have two different vocabularies and two different tokenizers for the source and destination languages.
Can you write Python code for it?

decoder_embedding = Embedding(input_dim=num_words,
                              output_dim=embedding_size,
                              name='decoder_embedding')


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
This is the length of the vectors output by the embedding-layer, which maps integer-tokens to vectors of values roughly between -1 and 1, so that words that have similar semantic meanings are mapped to vectors that are similar. See Tutorial #20 for a more detailed explanation of this.
Can you write Python code for it?


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
This is the sequence of integer-tokens for the corresponding text in the destination-language. Note how it is padded with zeros at the end (post-padding).
Can you write Python code for it?


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
This text is actually reversed, as can be seen when compared to the original text from the data-set:
Can you write Python code for it?


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
This was developed using Python 3.6 (Anaconda) and package versions:
Can you write Python code for it?


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
Tokenizer
Neural Networks cannot work directly on text-data. We use a two-step process to convert text into numbers that can be used in a neural network. The first step is to convert text-words into so-called integer-tokens. The second step is to convert integer-tokens into vectors of floating-point numbers using a so-called embedding-layer. See Tutorial #20 for a more detailed explanation.
Set the maximum number of words in our vocabulary. This means that we will only use e.g. the 10000 most frequent words in the data-set. We use the same number for both the source and destination languages, but these could be different.
Can you write Python code for it?


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
Train the Model
We wrap the data in named dicts so we are sure the data is assigned correctly to the inputs and outputs of the model.
Can you write Python code for it?

x_data = \
{
    'encoder_input': encoder_input_data,
    'decoder_input': decoder_input_data
}

y_data = \
{
    'decoder_output': decoder_output_data
}


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
Training Data
Now that the data-set has been converted to sequences of integer-tokens that are padded and truncated and saved in numpy arrays, we can easily prepare the data for use in training the neural network.
The input to the encoder is merely the numpy array for the padded and truncated sequences of integer-tokens produced by the tokenizer:
Can you write Python code for it?


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
Translate Texts
This function translates a text from the source-language to the destination-language and optionally prints a true translation.
Can you write Python code for it?

def translate(input_text, true_output_text=None):
    """Translate a single text-string."""

    # Convert the input-text to integer-tokens.
    # Note the sequence of tokens has to be reversed.
    # Padding is probably not necessary.
    input_tokens = tokenizer_src.text_to_tokens(text=input_text,
                                                reverse=True,
                                                padding=True)

    # Get the output of the encoder's GRU which will be
    # used as the initial state in the decoder's GRU.
    # This could also have been the encoder's final state
    # but that is really only necessary if the encoder
    # and decoder use the LSTM instead of GRU because
    # the LSTM has two internal states.
    initial_state = model_encoder.predict(input_tokens)

    # Max number of tokens / words in the output sequence.
    max_tokens = tokenizer_dest.max_tokens

    # Pre-allocate the 2-dim array used as input to the decoder.
    # This holds just a single sequence of integer-tokens,
    # but the decoder-model expects a batch of sequences.
    shape = (1, max_tokens)
    decoder_input_data = np.zeros(shape=shape, dtype=np.int)

    # The first input-token is the special start-token for 'ssss '.
    token_int = token_start

    # Initialize an empty output-text.
    output_text = ''

    # Initialize the number of tokens we have processed.
    count_tokens = 0

    # While we haven't sampled the special end-token for ' eeee'
    # and we haven't processed the max number of tokens.
    while token_int != token_end and count_tokens < max_tokens:
        # Update the input-sequence to the decoder
        # with the last token that was sampled.
        # In the first iteration this will set the
        # first element to the start-token.
        decoder_input_data[0, count_tokens] = token_int

        # Wrap the input-data in a dict for clarity and safety,
        # so we are sure we input the data in the right order.
        x_data = \
        {
           ...


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
We can reconstruct the original text by converting each integer-token back to its corresponding word:
Can you write Python code for it?


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
We need a few more functions than provided by Keras' Tokenizer-class so we wrap it.
Can you write Python code for it?

class TokenizerWrap(Tokenizer):
    """Wrap the Tokenizer-class from Keras with more functionality."""

    def __init__(self, texts, padding,
                 reverse=False, num_words=None):
        """
        :param texts: List of strings. This is the data-set.
        :param padding: Either 'post' or 'pre' padding.
        :param reverse: Boolean whether to reverse token-lists.
        :param num_words: Max number of words to use.
        """

        Tokenizer.__init__(self, num_words=num_words)

        # Create the vocabulary from the texts.
        self.fit_on_texts(texts)

        # Create inverse lookup from integer-tokens to words.
        self.index_to_word = dict(zip(self.word_index.values(),
                                      self.word_index.keys()))

        # Convert all texts to lists of integer-tokens.
        # Note that the sequences may have different lengths.
        self.tokens = self.texts_to_sequences(texts)

        if reverse:
            # Reverse the token-sequences.
            self.tokens = [list(reversed(x)) for x in self.tokens]

            # Sequences that are too long should now be truncated
            # at the beginning, which corresponds to the end of
            # the original sequences.
            truncating = 'pre'
        else:
            # Sequences that are too long should be truncated
            # at the end.
            truncating = 'post'

        # The number of integer-tokens in each sequence.
        self.num_tokens = [len(x) for x in self.tokens]

        # Max number of tokens to use in all sequences.
        # We will pad / truncate all sequences to this length.
        # This is a compromise so we save a lot of memory and
        # only have to truncate maybe 5% of all the sequences.
        self.max_tokens = np.mean(self.num_tokens) \
                          + 2 * np.std(self.num_tokens)
        self.max_tokens = int(self.max_tokens)

        # Pad / truncate all token-sequences to the given length.
        # This creates a 2-dim...


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
We need to import several things from Keras.
Can you write Python code for it?

# from tf.keras.models import Model  # This does not work!
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense, GRU, Embedding
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, TensorBoard
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences


I am working on the file "21_Machine_Translation.ipynb".
The first task is:
We will build a model to translate from the source language (Danish) to the destination language (English). If you want to make the inverse translation you can merely exchange the source and destination data.
Example Data
The data is just a list of texts that is ordered so the source and destination texts match. I can confirm that this example is an accurate translation.
Can you write Python code for it?


I am working on the file "21_Transforms_and_Resampling.ipynb".
The first task is:
A number of other transforms exist to represent non-affine deformations, well-behaved rotation in 3D, etc. See the Transforms tutorial for more information.
Applying Transforms to Images
Create a function to display the images that is aware of image spacing.
Can you write Python code for it?

def myshow(img, title=None, margin=0.05, dpi=80):
    nda = sitk.GetArrayFromImage(img)
    spacing = img.GetSpacing()

    ysize = nda.shape[0]
    xsize = nda.shape[1]

    figsize = (1 + margin) * ysize / dpi, (1 + margin) * xsize / dpi

    fig = plt.figure(title, figsize=figsize, dpi=dpi)
    ax = fig.add_axes([margin, margin, 1 - 2*margin, 1 - 2*margin])

    extent = (0, xsize*spacing[1], 0, ysize*spacing[0])

    t = ax.imshow(nda,
            extent=extent,
            interpolation='hamming',
            cmap='gray',
            origin='lower')

    if(title):
        plt.title(title)


I am working on the file "21_Transforms_and_Resampling.ipynb".
The first task is:
Composite Transform
It is possible to compose multiple transform together into a single transform object.  With a composite transform, multiple resampling operations are prevented, so interpolation errors are not accumulated.  For example, an affine transformation that consists of a translation and rotation,
Can you write Python code for it?

translate = (8.0, 16.0)
rotate = 20.0

affine = sitk.AffineTransform(dimension)
affine = affine_translate(affine, translate[0], translate[1])
affine = affine_rotate(affine, rotate)

resampled = resample(grid, affine)
myshow(resampled, 'Single Transform')


I am working on the file "21_Transforms_and_Resampling.ipynb".
The first task is:
Creating and Manipulating Transforms
A number of different spatial transforms are available in SimpleITK.
The simplest is the Identity Transform.  This transform simply returns input points unaltered.
Can you write Python code for it?

dimension = 2

print('*Identity Transform*')
identity = sitk.Transform(dimension, sitk.sitkIdentity)
print('Dimension: ' + str(identity.GetDimension()))

# Points are always defined in physical space
point = (1.0, 1.0)
def transform_point(transform, point):
    transformed_point = transform.TransformPoint(point)
    print('Point ' + str(point) + ' transformed is ' + str(transformed_point))

transform_point(identity, point)


I am working on the file "21_Transforms_and_Resampling.ipynb".
The first task is:
To apply the transform, a resampling operation is required.
Can you write Python code for it?

def resample(image, transform):
    # Output image Origin, Spacing, Size, Direction are taken from the reference
    # image in this call to Resample
    reference_image = image
    interpolator = sitk.sitkCosineWindowedSinc
    default_value = 100.0
    return sitk.Resample(image, reference_image, transform,
                         interpolator, default_value)

translation.SetOffset((3.1, 4.6))
transform_point(translation, point)
resampled = resample(grid, translation)
myshow(resampled, 'Resampled Translation')


I am working on the file "21_Transforms_and_Resampling.ipynb".
The first task is:
Transform are defined by two sets of parameters, the Parameters and FixedParameters.  FixedParameters are not changed during the optimization process when performing registration.  For the TranslationTransform, the Parameters are the values of the translation Offset.
Can you write Python code for it?

print('*Translation Transform*')
translation = sitk.TranslationTransform(dimension)

print('Parameters: ' + str(translation.GetParameters()))
print('Offset:     ' + str(translation.GetOffset()))
print('FixedParameters: ' + str(translation.GetFixedParameters()))
transform_point(translation, point)

print('')
translation.SetParameters((3.1, 4.4))
print('Parameters: ' + str(translation.GetParameters()))
transform_point(translation, point)


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

def affine_rotate(transform, degrees=15.0):
    parameters = np.array(transform.GetParameters())
    new_transform = sitk.AffineTransform(transform)
    matrix = np.array(transform.GetMatrix()).reshape((dimension,dimension))
    radians = -np.pi * degrees / 180.
    rotation = np.array([[np.cos(radians), -np.sin(radians)],[np.sin(radians), np.cos(radians)]])
    new_matrix = np.dot(rotation, matrix)
    new_transform.SetMatrix(new_matrix.ravel())
    resampled = resample(grid, new_transform)
    print(new_matrix)
    myshow(resampled, 'Rotated')
    return new_transform

affine = sitk.AffineTransform(dimension)

interact(affine_rotate, transform=fixed(affine), degrees=(-90.0, 90.0))


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

def affine_scale(transform, x_scale=3.0, y_scale=0.7):
    new_transform = sitk.AffineTransform(transform)
    matrix = np.array(transform.GetMatrix()).reshape((dimension,dimension))
    matrix[0,0] = x_scale
    matrix[1,1] = y_scale
    new_transform.SetMatrix(matrix.ravel())
    resampled = resample(grid, new_transform)
    myshow(resampled, 'Scaled')
    print(matrix)
    return new_transform

affine = sitk.AffineTransform(dimension)

interact(affine_scale, transform=fixed(affine), x_scale=(0.2, 5.0), y_scale=(0.2, 5.0))


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

def affine_shear(transform, x_shear=0.3, y_shear=0.1):
    new_transform = sitk.AffineTransform(transform)
    matrix = np.array(transform.GetMatrix()).reshape((dimension,dimension))
    matrix[0,1] = -x_shear
    matrix[1,0] = -y_shear
    new_transform.SetMatrix(matrix.ravel())
    resampled = resample(grid, new_transform)
    myshow(resampled, 'Sheared')
    print(matrix)
    return new_transform

affine = sitk.AffineTransform(dimension)

interact(affine_shear, transform=fixed(affine), x_shear=(0.1, 2.0), y_shear=(0.1, 2.0))


I am working on the file "21_gae_kelly/bulkloader/Analysis.ipynb".
The first task is:
Count by step
Goal:
- to establish probability of ruin
Can you write Python code for it?

vals = df['step'].value_counts()
print vals
#print vals.index.values
#print vals.values
coeffs = np.polyfit(vals.index.values, vals.values, 2)
poly = np.poly1d(coeffs)
print poly
vals.plot(kind='bar')
#plt.plot(vals.index.values, [0.5**(x+1) for x in vals.index.values])

df_last = df[df['is_win'] == True]
vals = df_last['step'].value_counts()
print vals
vals.plot(kind='bar')


I am working on the file "21_gae_kelly/bulkloader/Analysis.ipynb".
The first task is:
Load Data
Can you write Python code for it?

df = pd.read_csv('runs.csv')
print df.columns


I am working on the file "21_gae_kelly/bulkloader/Analysis.ipynb".
The first task is:
Profitability
Win/loss ratio
Can you write Python code for it?


I am working on the file "21_gae_kelly/bulkloader/Analysis.ipynb".
The first task is:
Stake by step
Goal:
- show the volatility per step
- used in bankroll management
Can you write Python code for it?

grouped = df.groupby(['step'])
grouped['stake'].mean().plot(kind='bar', yerr=grouped['stake'].std())


I am working on the file "21_gae_kelly/bulkloader/Analysis.ipynb".
The first task is:
Step
ROI by step
Goal:
- confirm visualisation should be useless
Can you write Python code for it?

grouped = df.groupby(['step'])
grouped['roi'].mean().plot(kind='bar', grid=False, yerr=grouped['roi'].std())


I am working on the file "21_gae_kelly/bulkloader/Analysis.ipynb".
The first task is:
Time Frames
Can you write Python code for it?

tf_roi = df[['roi']].groupby(df['time_frame']) # .sort('roi', ascending=False)
#tf_roi
#tf_roi.plot()
#tf_roi.plot(subplots=True)
# tf_roi.plot(secondary_y=('roi', 'count'))
tf_roi.mean().plot(kind='bar', subplots=True, grid=False, yerr=tf_roi.std())
#tf_roi.plot(kind='kde', subplots=True, grid=False)
#tf_roi.boxplot()
tf_roi.count().plot(kind='bar', subplots=True, grid=False)


I am working on the file "21_gae_kelly/bulkloader/Analysis.ipynb".
The first task is:
Trade Base & Aim
Goal:

Want to see the ROI per selection
Want to see the avg steps per selection (reduce risk)
Can you write Python code for it?

grouped = df.groupby(['trade_base', 'trade_aim'])
#for name, group in grouped:
#    print(name)
#    print(group['roi'].tail())
#print grouped['roi'].mean()
#print grouped['roi'].std()
grouped['roi'].mean().plot(kind='bar', yerr=grouped['roi'].std())

grouped['step'].mean().plot(kind='bar', yerr=grouped['step'].std())


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

outpins = [25, 26]
for pin in outpins:
    GPIO.setup(pin, GPIO.OUT)

for pin in outpins:
    GPIO.output(pin, True) # hello world?
    time.sleep(0.2)        # a bit of delay between lighting each LED

for pin in outpins:
    GPIO.output(pin, False) # goodbye world?
    time.sleep(0.2)

for i in range(20):
    for pin in outpins:
        GPIO.output(pin, True) # hello world?
        time.sleep(0.2)        # a bit of delay between lighting each LED
    for pin in outpins:
        GPIO.output(pin, False) # goodbye world?
        time.sleep(0.2)

GPIO.cleanup()


I am working on the file "21_gpio_intro.ipynb".
The first task is:
The following code will set the mode to a Broadcom pin layout (which seems to be what we want), and set up our GPIO pin 25 as an output pin.
Can you write Python code for it?


I am working on the file "22_Transforms.ipynb".
The first task is:
BSpline
Using a sparse set of control points to control a free form deformation.
Can you write Python code for it?

# Create the transformation (when working with images it is easier to use the BSplineTransformInitializer function
# or its object oriented counterpart BSplineTransformInitializerFilter).
dimension = 2
spline_order = 3
direction_matrix_row_major = [1.0,0.0,0.0,1.0] # identity, mesh is axis aligned
origin = [-1.0,-1.0]
domain_physical_dimensions = [2,2]

bspline = sitk.BSplineTransform(dimension, spline_order)
bspline.SetTransformDomainOrigin(origin)
bspline.SetTransformDomainDirection(direction_matrix_row_major)
bspline.SetTransformDomainPhysicalDimensions(domain_physical_dimensions)
bspline.SetTransformDomainMeshSize((4,3))

# Random displacement of the control points.
originalControlPointDisplacements = np.random.random(len(bspline.GetParameters()))
bspline.SetParameters(originalControlPointDisplacements)

# Apply the bspline transformation to a grid of points
# starting the point set exactly at the origin of the bspline mesh is problematic as
# these points are considered outside the transformation's domain,
# remove epsilon below and see what happens.
numSamplesX = 10
numSamplesY = 20

coordsX = np.linspace(origin[0]+np.finfo(float).eps, origin[0] + domain_physical_dimensions[0], numSamplesX)
coordsY = np.linspace(origin[1]+np.finfo(float).eps, origin[1] + domain_physical_dimensions[1], numSamplesY)
XX, YY = np.meshgrid(coordsX, coordsY)

interact(display_displacement_scaling_effect, s= (-1.5,1.5), original_x_mat = fixed(XX), original_y_mat = fixed(YY),
         tx = fixed(bspline), original_control_point_displacements = fixed(originalControlPointDisplacements));


I am working on the file "22_Transforms.ipynb".
The first task is:
Composite transform (Transform)
The generic SimpleITK transform class. This class can represent both a single transformation (global, local), or a composite transformation (multiple transformations applied one after the other). This is the output typed returned by the SimpleITK registration framework.
The choice of whether to use a composite transformation or compose transformations on your own has subtle differences in the registration framework.
Below we represent the composite transformation $T_{affine}(T_{rigid}(x))$ in two ways: (1) use a composite transformation to contain the two; (2) combine the two into a single affine transformation. We can use both as initial transforms (SetInitialTransform) for the registration framework (ImageRegistrationMethod). The difference is that in the former case the optimized parameters belong to the rigid transformation and in the later they belong to the combined-affine transformation.
Can you write Python code for it?

# Create a composite transformation: T_affine(T_rigid(x)).
rigid_center = (100,100,100)
theta_x = 0.0
theta_y = 0.0
theta_z = np.pi/2.0
rigid_translation = (1,2,3)
rigid_euler = sitk.Euler3DTransform(rigid_center, theta_x, theta_y, theta_z, rigid_translation)

affine_center = (20, 20, 20)
affine_translation = (5,6,7)

# Matrix is represented as a vector-like data in row major order.
affine_matrix = np.random.random(9)
affine = sitk.AffineTransform(affine_matrix, affine_translation, affine_center)

# Using the composite transformation we just add them in (stack based, first in - last applied).
composite_transform = sitk.Transform(affine)
composite_transform.AddTransform(rigid_euler)

# Create a single transform manually. this is a recipe for compositing any two global transformations
# into an affine transformation, T_0(T_1(x)):
# A = A=A0*A1
# c = c1
# t = A0*[t1+c1-c0] + t0+c0-c1
A0 = np.asarray(affine.GetMatrix()).reshape(3,3)
c0 = np.asarray(affine.GetCenter())
t0 = np.asarray(affine.GetTranslation())

A1 = np.asarray(rigid_euler.GetMatrix()).reshape(3,3)
c1 = np.asarray(rigid_euler.GetCenter())
t1 = np.asarray(rigid_euler.GetTranslation())

combined_mat = np.dot(A0,A1)
combined_center = c1
combined_translation = np.dot(A0, t1+c1-c0) + t0+c0-c1
combined_affine = sitk.AffineTransform(combined_mat.flatten(), combined_translation, combined_center)

# Check if the two transformations are equivalent.
print('Apply the two transformations to the same point cloud:')
print('\t', end='')
print_transformation_differences(composite_transform, combined_affine)

print('Transform parameters:')
print('\tComposite transform: ' + point2str(composite_transform.GetParameters(),2))
print('\tCombined affine: ' + point2str(combined_affine.GetParameters(),2))

print('Fixed parameters:')
print('\tComposite transform: ' + point2str(composite_transform.GetFixedParameters(),2))
print('\tCombined affine: ' + point2str(combined_affine.GetFixedParameters(),2))


I am working on the file "22_Transforms.ipynb".
The first task is:
Displacement field transform created from an image. Remember that SimpleITK will clear the image you provide, as shown in the cell below.
Can you write Python code for it?

displacement_image = sitk.Image([64,64], sitk.sitkVectorFloat64)
# The only point that has any displacement is (0,0)
displacement = (0.5,0.5)
displacement_image[0,0] = displacement

print('Original displacement image size: ' + point2str(displacement_image.GetSize()))

displacement_field_transform = sitk.DisplacementFieldTransform(displacement_image)

print('After using the image to create a transform, displacement image size: ' + point2str(displacement_image.GetSize()))

# Check that the displacement field transform does what we expect.
print('Expected result: {0}\nActual result:{1}'.format(str(displacement), displacement_field_transform.TransformPoint((0,0))))


I am working on the file "22_Transforms.ipynb".
The first task is:
DisplacementField
A dense set of vectors representing the displacment inside the given domain. The most generic representation of a transformation.
Can you write Python code for it?

# Create the displacment field.

# When working with images the safer thing to do is use the image based constructor,
# sitk.DisplacementFieldTransform(my_image), all the fixed parameters will be set correctly and the displacement
# field is initialized using the vectors stored in the image. SimpleITK requires that the image's pixel type be
# sitk.sitkVectorFloat64.
displacement = sitk.DisplacementFieldTransform(2)
field_size = [10,20]
field_origin = [-1.0,-1.0]
field_spacing = [2.0/9.0,2.0/19.0]
field_direction = [1,0,0,1] # direction cosine matrix (row major order)

# Concatenate all the information into a single list
displacement.SetFixedParameters(field_size+field_origin+field_spacing+field_direction)
# Set the interpolater, either sitkLinear which is default or nearest neighbor
displacement.SetInterpolator(sitk.sitkNearestNeighbor)

originalDisplacements = np.random.random(len(displacement.GetParameters()))
displacement.SetParameters(originalDisplacements)

coordsX = np.linspace(field_origin[0], field_origin[0]+(field_size[0]-1)*field_spacing[0], field_size[0])
coordsY = np.linspace(field_origin[1], field_origin[1]+(field_size[1]-1)*field_spacing[1], field_size[1])
XX, YY = np.meshgrid(coordsX, coordsY)

interact(display_displacement_scaling_effect, s= (-1.5,1.5), original_x_mat = fixed(XX), original_y_mat = fixed(YY),
         tx = fixed(displacement), original_control_point_displacements = fixed(originalDisplacements));


I am working on the file "22_Transforms.ipynb".
The first task is:
Points in SimpleITK
Utility functions
A number of functions that deal with point data in a uniform manner.
Can you write Python code for it?

import numpy as np

def point2str(point, precision=1):
    """
    Format a point for printing, based on specified precision with trailing zeros. Uniform printing for vector-like data
    (tuple, numpy array, list).

    Args:
        point (vector-like): nD point with floating point coordinates.
        precision (int): Number of digits after the decimal point.
    Return:
        String represntation of the given point "xx.xxx yy.yyy zz.zzz...".
    """
    return ' '.join(format(c, '.{0}f'.format(precision)) for c in point)

def uniform_random_points(bounds, num_points):
    """
    Generate random (uniform withing bounds) nD point cloud. Dimension is based on the number of pairs in the bounds input.

    Args:
        bounds (list(tuple-like)): list where each tuple defines the coordinate bounds.
        num_points (int): number of points to generate.

    Returns:
        list containing num_points numpy arrays whose coordinates are within the given bounds.
    """
    internal_bounds = [sorted(b) for b in bounds]
         # Generate rows for each of the coordinates according to the given bounds, stack into an array,
         # and split into a list of points.
    mat = np.vstack([np.random.uniform(b[0], b[1], num_points) for b in internal_bounds])
    return list(mat[:len(bounds)].T)

def target_registration_errors(tx, point_list, reference_point_list):
    """
    Distances between points transformed by the given transformation and their
    location in another coordinate system. When the points are only used to evaluate
    registration accuracy (not used in the registration) this is the target registration
    error (TRE).
    """
    return [np.linalg.norm(np.array(tx.TransformPoint(p)) -  np.array(p_ref))
          for p,p_ref in zip(point_list, reference_point_list)]

def print_transformation_differences(tx1, tx2):
    """
    Check whether two transformations are "equivalent" in an arbitrary spatial region
    either 3D or 2D, [x=(-10,10), y=(-100,100), z=(-1000,1000)]. This is...


I am working on the file "22_Transforms.ipynb".
The first task is:
Rigid to Similarity [3D]
Copy the translation, center, and matrix or versor.
Can you write Python code for it?

rotation_center = (100, 100, 100)
theta_x = 0.0
theta_y = 0.0
theta_z = np.pi/2.0
translation = (1,2,3)

rigid_euler = sitk.Euler3DTransform(rotation_center, theta_x, theta_y, theta_z, translation)

similarity = sitk.Similarity3DTransform()
similarity.SetMatrix(rigid_euler.GetMatrix())
similarity.SetTranslation(rigid_euler.GetTranslation())
similarity.SetCenter(rigid_euler.GetCenter())

# Apply the transformations to the same set of random points and compare the results
# (see utility functions at top of notebook).
print_transformation_differences(rigid_euler, similarity)


I am working on the file "22_Transforms.ipynb".
The first task is:
Scale Skew Versor
Again, not what you expect based on the name, this is not a composition of transformations. This is:
$$T(x) = (R+S+K)(\mathbf{x}-\mathbf{c}) + \mathbf{t} + \mathbf{c},\;\; \textrm{where } S = \left[\begin{array}{ccc} s_0-1 & 0 & 0 \ 0 & s_1-1 & 0 \ 0 & 0 & s_2-1 \end{array}\right]\;\; \textrm{and } K = \left[\begin{array}{ccc} 0 & k_0 & k_1 \ k_2 & 0 & k_3 \ k_4 & k_5 & 0 \end{array}\right]$$
In practice this is an over-parametrized version of the affine transform, 15 (scale, skew, versor, translation) vs. 12 parameters (matrix, translation).
Can you write Python code for it?

scale = (2,2.1,3)
skew = np.linspace(start=0.0, stop=1.0, num=6) #six eqaully spaced values in[0,1], an arbitrary choice
translation = (1,2,3)
versor = (0,0,0,1.0)
scale_skew_versor = sitk.ScaleSkewVersor3DTransform(scale, skew, versor, translation)
print(scale_skew_versor)


I am working on the file "22_Transforms.ipynb".
The first task is:
Similarity to Affine [3D]
Copy the translation, center and matrix.
Can you write Python code for it?

rotation_center = (100, 100, 100)
axis = (0,0,1)
angle = np.pi/2.0
translation = (1,2,3)
scale_factor = 2.0
similarity = sitk.Similarity3DTransform(scale_factor, axis, angle, translation, rotation_center)

affine = sitk.AffineTransform(3)
affine.SetMatrix(similarity.GetMatrix())
affine.SetTranslation(similarity.GetTranslation())
affine.SetCenter(similarity.GetCenter())

# Apply the transformations to the same set of random points and compare the results
# (see utility functions at top of notebook).
print_transformation_differences(similarity, affine)


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

# Rotation only, parametrized by Versor (vector part of unit quaternion),
# quaternion defined by rotation of theta around axis n:
#  q = [n*sin(theta/2), cos(theta/2)]

# 180 degree rotation around z axis

# Use a versor:
rotation1 = sitk.VersorTransform([0,0,1,0])

# Use axis-angle:
rotation2 = sitk.VersorTransform((0,0,1), np.pi)

# Use a matrix:
rotation3 = sitk.VersorTransform()
rotation3.SetMatrix([-1, 0, 0, 0, -1, 0, 0, 0, 1]);

point = (10, 100, 1000)

p1 = rotation1.TransformPoint(point)
p2 = rotation2.TransformPoint(point)
p3 = rotation3.TransformPoint(point)

print('Points after transformation:\np1=' + str(p1) +
      '\np2='+ str(p2) + '\np3='+ str(p3))


I am working on the file "22_Transforms.ipynb".
The first task is:
Writing and Reading
The SimpleITK.ReadTransform() returns a SimpleITK.Transform . The content of the file can be any of the SimpleITK transformations or a composite (set of tranformations).
Can you write Python code for it?

import os

# Create a 2D rigid transformation, write it to disk and read it back.
basic_transform = sitk.Euler2DTransform()
basic_transform.SetTranslation((1,2))
basic_transform.SetAngle(np.pi/2)

full_file_name = os.path.join(OUTPUT_DIR, 'euler2D.tfm')

sitk.WriteTransform(basic_transform, full_file_name)

# The ReadTransform function returns an sitk.Transform no matter the type of the transform
# found in the file (global, bounded, composite).
read_result = sitk.ReadTransform(full_file_name)

print('Different types: '+ str(type(read_result) != type(basic_transform)))
print_transformation_differences(basic_transform, read_result)

# Create a composite transform then write and read.
displacement = sitk.DisplacementFieldTransform(2)
field_size = [10,20]
field_origin = [-10.0,-100.0]
field_spacing = [20.0/(field_size[0]-1),200.0/(field_size[1]-1)]
field_direction = [1,0,0,1] #direction cosine matrix (row major order)

# Concatenate all the information into a single list.
displacement.SetFixedParameters(field_size+field_origin+field_spacing+field_direction)
displacement.SetParameters(np.random.random(len(displacement.GetParameters())))

composite_transform = sitk.Transform(basic_transform)
composite_transform.AddTransform(displacement)

full_file_name = os.path.join(OUTPUT_DIR, 'composite.tfm')

sitk.WriteTransform(composite_transform, full_file_name)
read_result = sitk.ReadTransform(full_file_name)

print_transformation_differences(composite_transform, read_result)


I am working on the file "22_gpio_leds.ipynb".
The first task is:
Lighting an LED (to test our connection)
Can you write Python code for it?

GPIO.output(outpin, True)  # Let there be light

GPIO.output(outpin, False)  # Turn it off


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

timeunit  = 0.1
dashtime  = timeunit*3.0
dottime   = timeunit
dotgap    = timeunit
lettergap = timeunit*2.0
wordgap   = timeunit*7.0

GPIO.output(outpin, False) # start with the LED off
time.sleep(1.0)

for i in range(0,2):  # SOS, the hard way...
    GPIO.output(outpin, True)
    time.sleep(dottime)
    GPIO.output(outpin, False)
    time.sleep(dotgap)
    GPIO.output(outpin, True)
    time.sleep(dottime)
    GPIO.output(outpin, False)
    time.sleep(dotgap)
    GPIO.output(outpin, True)
    time.sleep(dottime)
    GPIO.output(outpin, False)
    time.sleep(lettergap)
    GPIO.output(outpin, True)
    time.sleep(dashtime)
    GPIO.output(outpin, False)
    time.sleep(dotgap)
    GPIO.output(outpin, True)
    time.sleep(dashtime)
    GPIO.output(outpin, False)
    time.sleep(dotgap)
    GPIO.output(outpin, True)
    time.sleep(dashtime)
    GPIO.output(outpin, False)
    time.sleep(lettergap)
    GPIO.output(outpin, True)
    time.sleep(dottime)
    GPIO.output(outpin, False)
    time.sleep(dotgap)
    GPIO.output(outpin, True)
    time.sleep(dottime)
    GPIO.output(outpin, False)
    time.sleep(dotgap)
    GPIO.output(outpin, True)
    time.sleep(dottime)
    GPIO.output(outpin, False)
    time.sleep(wordgap)

morsedict = {
        'A': '.-',
        'B': '-...',
        'C': '-.-.',
        'D': '-..',
        'E': '.',
        'F': '..-.',
        'G': '--.',
        'H': '....',
        'I': '..',
        'J': '.---',
        'K': '-.-',
        'L': '.-..',
        'M': '--',
        'N': '-.',
        'O': '---',
        'P': '.--.',
        'Q': '--.-',
        'R': '.-.',
        'S': '...',
        'T': '-',
        'U': '..-',
        'V': '...-',
        'W': '.--',
        'X': '-..-',
        'Y': '-.--',
        'Z': '--..',
        '0': '-----',
        '1': '.----',
        '2': '..---',
        '3': '...--',
        '4': '....-',
        '5': '.....',
        '6': '-....',
        '7': '--...',
        '8': '---..',
        '9': '----.',
        ' ': ' ',
        ',': '--..--',
   ...


I am working on the file "22_gpio_leds.ipynb".
The first task is:
The following code will set the mode to a Broadcom pin layout (which seems to be what we want), and set up our GPIO pin 25 as an output pin.
Can you write Python code for it?

GPIO.setmode(GPIO.BCM)
outpin = 25
GPIO.setup(outpin, GPIO.OUT)


I am working on the file "23.how_to_build_a_first_model_SDK/Build your first model.ipynb".
The first task is:
Build a model from the default schema
Can you write Python code for it?

mdl = prj.default_schema().fit('My first model')

mdl.auc()


I am working on the file "23.how_to_build_a_first_model_SDK/Build your first model.ipynb".
The first task is:
Getting insights
Retrieve your project thanks to its name.
Build your first model, on the central table, from the default schema.
Can you write Python code for it?

prj = PredicSis.project('Outbound Mail Campaign')


I am working on the file "23_Prelab_Python-I/Python-I-Prelab.ipynb".
The first task is:
A variable takes on the "type" of whatever data it is currently storing. So a variable holding a string has the string type, and a variable holding an integer has the integer type. Thus, we can apply the type conversion functions to variables as well:
Can you write Python code for it?

age = "16"
print float(age)

age = "16"
print int(age) + 1


I am working on the file "23_Prelab_Python-I/Python-I-Prelab.ipynb".
The first task is:
Another example of overwriting a variable. Note that the value of animals is unchanged.
Can you write Python code for it?

name = "Joe Shmo"
age = 20
print name,"will be",(age + 1),"next year"


I am working on the file "23_Prelab_Python-I/Python-I-Prelab.ipynb".
The first task is:
Another version-related note
This print statement is an example of how the syntax changed between Python 2 and 3. In Python 3, print statements are enclosed in parentheses, so this command would look like print("1 + 2 = ", 1 + 2, "!"), but if you try to run that in Python 2, your output will include the parentheses. This isn't so important to remember for this class since we are always using Python 2, but if you ever use Python 3, this is worth remembering!
[ Check yourself! ] Print practice
Think you got it? Using the code block below, write code to print your name.
5. Variables and data types

What is a variable?
You can think of variables as little boxes that we put data in. You name each box so that you can refer to it and use it in your code. This gives your code flexibility, for reasons you will see soon.
Creating a variable is sometimes called "declaring" or "defining" a variable. This basically just requires giving the variable a name and assigning it an initial value. A variable usually needs to be defined in this way before you can use it elsewhere in your code (although there are a few exceptions we'll go over later).
Here's an example of a variable definition:
geneID = "Fmr1"
Here, geneID is the variable name and "Fmr1" is the piece of data that is being stored in the variable. The = is what we call an assignment operator. In English, we might read this line of code as "store the string 'Fmr1' in the variable 'geneID'".

Important to note:
You should try not to think of = in Python as the same kind of equals sign you use in math. In math, a = implies equality of the information on either side. In Python, = simply means "assign the value on the right to the variable on the left". It may help to think of it more like an arrow pointing to the left, e.g. geneID <- "Fmr1".

What should I name my variables?
You can name your variables almost anything, but there are a few important rules and conventions to keep in mind.
Rules (if you break these, you will get errors):
   - only letters, numbers, and...
Can you write Python code for it?


I am working on the file "23_Prelab_Python-I/Python-I-Prelab.ipynb".
The first task is:
As above, we can mix and match different data types in our print statements when we use a comma.
Can you write Python code for it?

people = 3
people = people + 1
print people


I am working on the file "23_Prelab_Python-I/Python-I-Prelab.ipynb".
The first task is:
As we saw before, the comma is basically allows us to list multiple things we would like to print at once, without trying to add or concatenate them. A space is automatically inserted between each item.
Can you write Python code for it?

apples = 5
oranges = 10
print "I have", apples, "apples"


I am working on the file "23_Prelab_Python-I/Python-I-Prelab.ipynb".
The first task is:
As you can see, only 6 / 2 gave the correct answer -- all the other answers appear as if they've been rounded down. Why is this? Basically, it is a somewhat odd rule in Python that whenever you divide two integers, Python always returns an integer answer. If the answer should have a decimal component, the decimal is simply truncated (e.g. 2.5 is truncated to 2).
To get a proper answer, at least one of the numbers being divided must be a float:
Can you write Python code for it?

print 5.0 / 2

print float(5) / 2

print 5 / float(2)


I am working on the file "23_Prelab_Python-I/Python-I-Prelab.ipynb".
The first task is:
Here we just did some simple math within the print statement.
Can you write Python code for it?

yourAge = "16"
print "You will be", (yourAge + 1), "next year"


I am working on the file "23_Prelab_Python-I/Python-I-Prelab.ipynb".
The first task is:
Here's where it becomes very important to be aware if you are using a string or a number. Let's see what happens when we try to add two numbers enclosed in quotes:
Can you write Python code for it?


I am working on the file "23_Prelab_Python-I/Python-I-Prelab.ipynb".
The first task is:
However, the most important difference to watch out for is integer division. Run the following examples:
Can you write Python code for it?

print 6 / 2

print 5 / 2

print 5 / 3

print 5 / 4


I am working on the file "23_Prelab_Python-I/Python-I-Prelab.ipynb".
The first task is:
Many of the code blocks in this notebook will start off in the "un-run" state, so make sure you run them to see what the output is!
You can also edit these code blocks yourself and then re-run them to see what changes. This is a great way to learn more about programming, so feel free to do this to any of the code blocks in the lesson.
As you go through these exercises, you might run into issues where pressing "Shift+Enter" doesn't work correctly and you can't get your code to run. This can happen with the platform we are using, the IPython notebook. The solution is to go to the menu bar underneath the 'Jupyter' logo, select 'Kernel', and then select 'Restart.' In our experience, this typically will do the trick to get the notebook running again!
2. Introduction
Why learn Python?

It's a particularly simple and easy to learn for beginners
It's widely used by the scientific community

Languages such as Perl and R are also quite popular among scientists, and are worth learning. Luckily, once you learn your first programming language, it's usually much easier to pick up additional languages!
3. Writing and running Python code

A note about Python versions
For all of the Python exercises in this course, we are using Python 2, which is the second version of the Python language. Python 3 has been released, but many of the same principles apply. It is important to know which version of a programming language you are using, especially in the case of Python, as the syntax has changed slightly between these two versions!

There are several different ways you can write and run Python code. Here are the two ways we'll cover in this course:
Programs and scripts: This is the most common method. Code is written in a .py file and then executed on the command line. We'll talk more about this later.
Jupyter / IPython notebooks: That's what you're using right now! These notebooks allow you to write small (or large) blocks of code and immediately see the result. They also let you intersperse text and images with your code, which...
Can you write Python code for it?

print "I am a string. I am enclosed in quotes."

print 123

print 2934.454


I am working on the file "23_Prelab_Python-I/Python-I-Prelab.ipynb".
The first task is:
Remainder (aka modulus or "mod"):
Can you write Python code for it?


I am working on the file "23_Prelab_Python-I/Python-I-Prelab.ipynb".
The first task is:
Sometimes, like in the last example above, we'd actually like to convert one data type into another. Fortunately, Python provides simple built-in functions that allow us to do this in certain cases.
Here's a partial list of the most useful conversion function:

str() - converts a variable or piece of data to a string. Works on integers, floats, and booleans (and others).
int() - converts a variable or piece of data to an integer. Works strings made up only of numbers (e.g. "123"), floats (decimal part will be truncated), and booleans (True converts to 1, False to 0).
float() - converts a variable or piece of data to a float (decimal). Works on strings made up only of numbers (e.g. "123.45"), integers (a .0 will be added), and booleans (True converts to 1.0, False to 0.0)

Let's look at a few example:
Can you write Python code for it?

print 1 + int("1")

print "1" + str(1)

print float("1")


I am working on the file "23_Prelab_Python-I/Python-I-Prelab.ipynb".
The first task is:
There are also a few notations that you might not be familiar with.
Exponents:
Can you write Python code for it?


I am working on the file "23_Prelab_Python-I/Python-I-Prelab.ipynb".
The first task is:
This is a very common source of errors, so always keep it in mind when you divide! When in doubt, convert one number with float().
7. Commenting code

Our final topic for the day is a simple but important one: commenting your code.
A "comment" is a line of code that Python ignores when it executes your code. We mark these lines by starting them with a hash/pound sign (#). Here's an example:
Can you write Python code for it?

# This is a comment line! It won't be printed!
print "Hello, friend"

# Use comments to leave notes on what your code is doing
print "Nice day we're having, isn't it?"

# ...or to temporarily prevent certain lines of code from executing
# print 1 + "2" + holy illegal operations, batman! + 42.8g$

# You can put them almost anywhere!
print "Well, goodbye then" # even here!


I am working on the file "23_Prelab_Python-I/Python-I-Prelab.ipynb".
The first task is:
This is an important one to understand. What we did here was overwrite the value of people with the value of people + 1. The important thing to remember is that the right side of the = sign is evaluated first. So first Python figures out what people + 1 is, which is 3 + 1 or 4. Once that is completely done, it takes that result and stores it in the variable on the left. In this case, this overwrites the value that was already in people.
Later on, this is how we will create counters, i.e. variables that we increment by 1 every time something happens.
Can you write Python code for it?

people = 3
animals = 4
people = animals
print people
print animals


I am working on the file "23_Prelab_Python-I/Python-I-Prelab.ipynb".
The first task is:
This is not allowed! Remember, Python doesn't consider "5" a number when it's enclosed in quotes -- it's just any old string. Python doesn't know how to combine a string and a number, so it gives an error message.
If we want to print multiple types of data in the same line, we can use a comma instead of a plus sign. This tells Python, "don't try to combine these data -- just print them all separately to the screen!". For example:
Can you write Python code for it?


I am working on the file "23_Prelab_Python-I/Python-I-Prelab.ipynb".
The first task is:
When we print a variable, Python knows that what we're really interested in is what's stored in the variable, not the variable itself. Therefore, Python automatically prints the contents of the variable when we do this kind of print statement.
Can you write Python code for it?

apples = 5
oranges = 10
fruit = apples + oranges
print fruit


I am working on the file "23_Prelab_Python-I/Python-I-Prelab.ipynb".
The first task is:
With numbers, we can also do basic math operations. For example:
Can you write Python code for it?


I am working on the file "23_Prelab_Python-I/Python-I-Prelab.ipynb".
The first task is:
You can also make comments that span multiple lines, using triple quotes (""") like so:
Can you write Python code for it?

"""
This here is a multi-line comment.
Make sure to end it with matching quotes!
"""
print "Aye aye!"


I am working on the file "23_gpio_buttons.ipynb".
The first task is:
Now for the infinite button loop to listen for a press:
Can you write Python code for it?

prev_input = 1 # looking for a 'False' as the button press signal, so we start with a "True"
counter = 1
while True:
    #take a reading
    input = GPIO.input(inpin)
    #if the last reading was low and this one high, print
    if ((not prev_input) and input):
        print("Taking photo%s" % counter)
        blink_and_shoot(counter)
        counter += 1
    #update previous input
    prev_input = input
    #slight pause to debounce
    time.sleep(0.05)

GPIO.cleanup()


I am working on the file "23_gpio_buttons.ipynb".
The first task is:
We'll wait to instantiate our camera object, just so we don't crash anything with a bunch of button presses.
Can you write Python code for it?

def blink_and_shoot(idx):
    """ idx: an index number for photos to be stored at /img/snapshot-[idx] """

    # set up camera
    with PiCamera() as camera:
        #camera.resolution = (480, 320)  # uncomment this line if you want smaller photos
        camera.vflip = True
        camera.hflip = True
        camera.start_preview()

        for i in range(3):
            GPIO.output(outpin, True)
            time.sleep(0.2)
            GPIO.output(outpin, False)

        #camera.image_effect = 'oilpaint' # just to make things interesting
        sleep(0.5)
        camera.capture('img/snapshot-%s.jpg' % idx)
        camera.stop_preview()


I am working on the file "24--online-linreg.ipynb".
The first task is:
Benchmark varying $m$
Let's benchmark the time to compute $x$ when the dimension $n$ of each point is fixed but the number $m$ of points varies.
Can you write Python code for it?

# Benchmark, as 'm' varies:

n = 32 # dimension
M = [100, 1000, 10000, 100000, 1000000]
times = [0.] * len (M)
for (i, m) in enumerate (M):
    x_true = generate_model (n)
    (A, b) = generate_data (m, x_true, sigma=0.1)
    t = %timeit -o estimate_coeffs (A, b)
    times[i] = t.best


I am working on the file "24--online-linreg.ipynb".
The first task is:
Exercise. How does the running time scale with $m$?
Can you write Python code for it?

t_linear = [times[0]/M[0]*m for m in M]

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.loglog (M, times, 'bo')
ax1.loglog (M, t_linear, 'r--')


I am working on the file "24--online-linreg.ipynb".
The first task is:
Exercise. Implement the online LMS algorithm in the code cell below where indicated. It should produce a final parameter estimate, x_lms, as a column vector.
In addition, the skeleton code below uses rel_diff() to record the relative difference between the estimate and the true vector, storing the $k$-th relative difference in rel_diffs[k]. Doing so will allow you to see the convergence behavior of the method.
Lastly, to help you out, we've defined a constant in terms of $\lambda_{\mathrm{max}}(A^TA)$ that you can use for $\phi$.

In practice, you would only maintain the current estimate, or maybe just a few recent estimates, rather than all of them. Since we want to inspect these vectors later, let's just store them all.
Can you write Python code for it?

PHI = 1.99 / LAMBDA_MAX # Fudge factor
rel_diffs = np.zeros ((m+1, 1))

x_k = np.zeros ((d+1))
for k in range (m):
    rel_diffs[k] = rel_diff (x_k, x_true)

    # @YOUSE: Implement the online LMS algorithm.
    # Use (b[k], A[k, :]) as the k-th observation.
    pass

x_lms = x_k
rel_diffs[m] = rel_diff (x_lms, x_true)


I am working on the file "24--online-linreg.ipynb".
The first task is:
Exercise. Now fix the number $m$ of observations but vary the dimension $n$. How does time scale with $n$? Complete the benchmark code below to find out. In particular, given the array N[:], compute an array, times[:], such that times[i] is the running time for a problem of size m$\times$(N[i]+1).

Hint: You can basically copy and modify the preceding benchmark. Also, note that the code cell following the one immediately below will plot your results against $\mathcal{O}(n)$ and $\mathcal{O}(n^2)$.
Can you write Python code for it?

N = [2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 3072]
m = 5000
times = [0.] * len (N)

# @YOUSE: Implement a benchmark to compute the time,
# `times[i]`, to execute a problem of size `N[i]`.
for (i, n) in enumerate (N):
    pass

t_linear = [times[0]/N[0]*n for n in N]
t_quadratic = [times[0]/N[0]/N[0]*n*n for n in N]

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.loglog (N, times, 'bo')
ax1.loglog (N, t_linear, 'r--')
ax1.loglog (N, t_quadratic, 'g--')


I am working on the file "24--online-linreg.ipynb".
The first task is:
Let's also compute the relative differences between each estimate X[:, k] and the true coefficients x_true, measured in the two-norm, to see if the estimate is converging to the truth.
Can you write Python code for it?

plt.plot (range (len (rel_diffs)), rel_diffs)


I am working on the file "24. PCA/01. 고유분해와 특이값 분해.ipynb".
The first task is:
특이값 분해
정방 행렬이 아닌 행렬 $M$에 대해서도 고유 분해와 유사한 분해가 가능하다. 이를 특이값 분해(singular value decomposition)이라고 한다.

$M \in \mathbf{R}^{m \times n}$

$$M = U \Sigma V^T$$
여기에서
* $U \in \mathbf{R}^{m \times m}$
* $\Sigma  \in \mathbf{R}^{m \times n}$
* $V \in \mathbf{R}^{n \times n}$
이고 행렬 $U$와 $V$는 다음 관계를 만족한다.
$$ U^T U = UU^T = I $$
$$ V^T V = VV^T = I $$
예를 들어
$$\mathbf{M} = \begin{bmatrix}
                      1 & 0 & 0 & 0 & 2 \
                      0 & 0 & 3 & 0 & 0 \
                      0 & 0 & 0 & 0 & 0 \
                      0 & 2 & 0 & 0 & 0
                    \end{bmatrix}
$$
에 대한 특이값 분해 결과는 다음과 같다.
$$
\begin{align}
\mathbf{U} &= \begin{bmatrix}
               0 & 0 & 1 &  0 \
               1 & 0 & 0 &  0 \
               0 & 0 & 0 & -1 \
               0 & 1 & 0 &  0 \
             \end{bmatrix} \
\boldsymbol{\Sigma} &= \begin{bmatrix}
                        \sqrt{5} & 0 & 0 & 0 & 0 \
                        0 & 2 & 0 & 0 & 0 \
                        0 & 0 & 1 & 0 & 0 \
                        0 & 0 & 0 & 0 & 0
                      \end{bmatrix} \
\mathbf{V}^T &= \begin{bmatrix}
                           0 & 0 & \sqrt{0.2} & 0 & \sqrt{0.8} \
                           0 & 1 & 0 & 0 & 0 \
                           1 & 0 & 0 & 0 & 0 \
                           0 & 0 & -\sqrt{0.8} & 0 & \sqrt{0.2} \
                           0 & 0 & 0 & 1 & 0 \
               \end{bmatrix}
\end{align}$$
이는 다음과 같이 확인 할 수 있다.
$$\begin{align}
\mathbf{U} \mathbf{U^T} &=
   \begin{bmatrix}
               0 & 0 & 1 &  0 \
               1 & 0 & 0 &  0 \
               0 & 0 & 0 & -1 \
               0 & 1 & 0 &  0 \
             \end{bmatrix}
\cdot
  \begin{bmatrix}
               0 & 1 & 0 & 0 \
               0 & 0 & 0 & 1 \
               1 & 0 & 0 & 0 \
               0 & 0 & -1 & 0 \
             \end{bmatrix}
   =
  \begin{bmatrix}
    1 & 0 & 0 & 0 \
    0 & 1 & 0 & 0 \
    0 & 0 & 1 & 0 \
    0 & 0 & 0 & 1
  \end{bmatrix}
   = \mathbf{I}_4 \
\mathbf{V} \mathbf{V^T} &=
  \begin{bmatrix}
                          ...
Can you write Python code for it?

from pprint import pprint
M = np.array([[1,0,0,0,0],[0,0,2,0,3],[0,0,0,0,0],[0,2,0,0,0]])
print("\nM:"); pprint(M)
U, S0, V0 = np.linalg.svd(M, full_matrices=True)
print("\nU:"); pprint(U)
S = np.hstack([np.diag(S0), np.zeros(M.shape[0])[:, np.newaxis]])
print("\nS:"); pprint(S)
print("\nV:"); pprint(V)
V = V0.T
print("\nU.dot(U.T):"); pprint(U.dot(U.T))
print("\nV.dot(V.T):"); pprint(V.dot(V.T))
print("\nU.dot(S).dot(V.T):"); pprint(U.dot(S).dot(V.T))


I am working on the file "24.how_to_produce_scores_from_trained_model/Predictive Scoring with several tables.ipynb".
The first task is:
Retrieve the predictive model
Can you write Python code for it?

prj = PredicSis.project('Outbound Mail Campaign')
mdl = prj.schema('My first model')


I am working on the file "24.how_to_produce_scores_from_trained_model/Predictive Scoring with several tables.ipynb".
The first task is:
Scoring the uploaded dataset
Asking to add the user information such as email, first_name and last_name
Can you write Python code for it?

score = mdl.compute_scores('datasets_January', output_features=['email', 'first_name' ,'last_name'])

score.download_scorefile('./Outbound Mail Campaign/January_scores.csv')


I am working on the file "24.how_to_produce_scores_from_trained_model/Predictive Scoring with several tables.ipynb".
The first task is:
Upload dataset to be scored
Can you write Python code for it?

mdl.upload_files_to_score('datasets_January',
 [
     {
       'name': 'Customers',
       'file': './Outbound Mail Campaign/tobedeployed/Master.csv'
     },
     {
       'name': 'Orders',
       'file': './Outbound Mail Campaign/tobedeployed/Orders.csv'
     },
     {
       'name': 'VisitedPages',
       'file': './Outbound Mail Campaign/tobedeployed/VisitedPages.csv'
     },
     {
       'name': 'Email',
       'file': './Outbound Mail Campaign/tobedeployed/Email.csv'
     },

 ])


I am working on the file "24.how_to_produce_scores_from_trained_model/Predictive Scoring.ipynb".
The first task is:
Scoring the uploaded dataset
Asking to add the user information such as email, first_name and last_name
Can you write Python code for it?


I am working on the file "24.how_to_produce_scores_from_trained_model/Predictive Scoring.ipynb".
The first task is:
Upload dataset to be scored
Can you write Python code for it?

mdl.upload_files_to_score('datasets_January',
 [
     {
       'name': 'Customers',
       'file': './Outbound Mail Campaign/tobedeployed/Master.csv'
     },
 ])


I am working on the file "24_Prelab_Python-II/Lesson2.ipynb".
The first task is:
"not" only applies to the variable directly in front of it (in this case, a). So here, a becomes False, so the conditional as a whole becomes False.
Can you write Python code for it?

a = True
b = False
if not (a and b):
    print "Apple"
else:
    print "Banana"


I am working on the file "24_Prelab_Python-II/Lesson2.ipynb".
The first task is:
4. Test your understanding: practice set 2

For the following blocks of code, first try to guess what the output will be, and then run the code yourself. These examples may introduce some ideas and common pitfalls that were not explicitly covered in the text above, so be sure to complete this section.
The first block below holds the variables that will be used in the problems. Since variables are shared across blocks in Jupyter notebooks, you just need to run this block once and then those variables can be used in any other code block.
Can you write Python code for it?

# RUN THIS BLOCK FIRST TO SET UP VARIABLES!
a = True
b = False
x = 2
y = -2
cat = "Mittens"

print a

print (not a)

print (a == b)

print (a != b)

print (x == y)

print (x > y)

print (x = 2)

print (a and b)

print (a and not b)

print (a or b)

print (not b or a)

print not (b or a)

print (not b) or a

print (not b and a)

print not (b and a)

print (not b) and a

print (x == abs(y))

print len(cat)

print cat + x

print cat + str(x)

print float(x)

print ("i" in cat)

print ("g" in cat)

print ("Mit" in cat)

if (x % 2) == 0:
    print "x is even"
else:
    print "x is odd"

if (x - 4*y) < 0:
    print "Invalid!"
else:
    print "Banana"

if "Mit" in cat:
    print "Hey Mits!"
else:
    print "Where's Mits?"

x = "C"
if x == "A" or "B":
    print "yes"
else:
    print "no"

x = "C"
if (x == "A") or (x == "B"):
    print "yes"
else:
    print "no"


I am working on the file "24_Prelab_Python-II/Lesson2.ipynb".
The first task is:
As you would probably expect, when we use "or", we only need a or b to be True in order for the whole conditional to be True.
Can you write Python code for it?

cat = "Mittens"
if cat == "Mittens":
    print "Awwww"
else:
    print "Get lost, cat"

a = 5
b = 10
if (a == 5) and (b > 0):
    print "Apple"
else:
    print "Banana"

a = 5
b = 10
if ((a == 1) and (b > 0)) or (b == (2 * a)):
    print "Apple"
else:
    print "Banana"


I am working on the file "24_Prelab_Python-II/Lesson2.ipynb".
The first task is:
By using "not" before b, we negate its current value (False), making b True. Thus the entire conditional as a whole becomes True, and we execute the if-block.
Can you write Python code for it?

a = True
b = False
if not a and b:
    print "Apple"
else:
    print "Banana"


I am working on the file "24_Prelab_Python-II/Lesson2.ipynb".
The first task is:
If you want to learn more built in functions, go here: https://docs.python.org/2/library/functions.html
3. Modules

Modules are groups of additional functions that come with Python, but unlike the built-in functions we just saw, these functions aren't accessible until you import them. Why aren’t all functions just built-in? Basically, it improves speed and memory usage to only import what is needed (there are some other considerations, too, but we won't get into it here).
The functions in a module are usually all related to a certain kind of task or subject area. For example, there are modules for doing advanced math, generating random numbers, running code in parallel, accessing your computer's file system, and so on. We’ll go over just two modules today: math and random. See the full list here: https://docs.python.org/2.7/py-modindex.html
How to use a module
Using a module is very simple. First you import the module. Add this to the top of your script:
import <moduleName>

Then, to use a function of the module, you prefix the function name with the name of the module (using a period between them):
<moduleName>.<functionName>

(Replace <moduleName> with the name of the module you want, and <functionName> with the name of a function in the module.)
The <moduleName>.<functionName> synatx is needed so that Python knows where the function comes from. Sometimes, especially when using user created modules, there can be a function with the same name as a function that's already part of Python. Using this syntax prevents functions from overwriting each other or causing ambiguity.

[ Definition ] The math module
Description: Contains many advanced math-related functions.
See full list of functions here: https://docs.python.org/2/library/math.html
Examples:
Can you write Python code for it?

import math

print math.sqrt(4)
print math.log10(1000)
print math.sin(1)
print math.cos(0)


I am working on the file "24_Prelab_Python-II/Lesson2.ipynb".
The first task is:
Ok, this one is a little bit much! Try to avoid complex conditionals like this if possible, since it can be difficult to tell if they're actually testing what you think they're testing. If you do need to use a complex conditional, use parentheses to make it more obvious which terms will be evaluated first!

Note on indentation

Indentation is very important in Python; it’s how Python tells what code belongs to which control statements
Consecutive lines of code with the same indenting are sometimes called "blocks"
Indenting should only be done in specific circumstances (if statements are one example, and we'll see a few more soon). Indent anywhere else and you'll get an error.
You can indent by however much you want, but you must be consistent. Pick one indentation scheme (e.g. 1 tab per indent level, or 4 spaces) and stick to it.

[ Check yourself! ] if/else practice
Think you got it? In the code block below, write an if/else statement to print a different message depending on whether x is positive or negative.
Can you write Python code for it?


I am working on the file "24_Prelab_Python-II/Lesson2.ipynb".
The first task is:
So what types of conditionals are we allowed to use in an if / else statement? Anything that can be evaluated as True or False! For example, in natural language we might ask the following true/false questions:

is a True?
is a less than b?
is a equal to b?
is a equal to "ATGCTG"?
is (a greater than b) and (b greater than c)?

To ask these questions in our code, we need to use a special set of symbols/words. These are called the logical operators, because they allow us to form logical (true/false) statements. Below is a chart that lists the most common logical operators:

Most of these are pretty intuitive. The big one people tend to mess up on in the beginning is ==. Just remember: a single equals sign means assignment, and a double equals means is the same as/is equal to. You will NEVER use a single equals sign in a conditional statement because assignment is not allowed in a conditional! Only True / False questions are allowed!
if / else statements in action
Below are several examples of code using if / else statements. For each code block, first try to guess what the output will be, and then run the block to see the answer.
Can you write Python code for it?

a = True
if a:
    print "Hooray, a was true!"

a = True
if a:
    print "Hooray, a was true!"
print "Goodbye now!"

a = False
if a:
    print "Hooray, a was true!"
print "Goodbye now!"


I am working on the file "24_Prelab_Python-II/Lesson2.ipynb".
The first task is:
[ Definition ] The random module
Description: contains functions for generating random numbers.
See full list of functions here: https://docs.python.org/2/library/random.html
Examples:
Can you write Python code for it?

import random

print random.random()       # Return a random floating point number in the range [0.0, 1.0)
print random.randint(0, 10) # Return a random integer between the specified range (inclusive)
print random.gauss(5, 2)    # Draw from the normal distribution given a mean and standard deviation

# this code will output something different every time you run it!


I am working on the file "24_Prelab_Python-II/Lesson2.ipynb".
The first task is:
[ Definition ] abs()
Description: Returns the absolute value of a numerical value. Doesn't accept strings.
Syntax:
abs(number)

Examples:
Can you write Python code for it?

print abs(-10)

print abs(int("-10"))

positiveNum = abs(-23423)
print positiveNum


I am working on the file "24_Prelab_Python-II/Lesson2.ipynb".
The first task is:
[ Definition ] len()
Description: Returns the length of a string (also works on certain data structures). Doesn’t work on numerical types.
Syntax:
len(string)

Examples:
Can you write Python code for it?

print len("cat")

print len("hi there")

seqLength = len("ATGGTCGCAT")
print seqLength


I am working on the file "24_Python-II/Lesson2_extra_material.ipynb".
The first task is:
Nested if/else:
You can put pretty much anything inside the code blocks of an if/else statement, including additional if/else statements! Just make sure that all the indentation is correct.
Can you write Python code for it?

test = raw_input("What is 1+1? ") #prompts user to input something on the command line, saves it in a variable. see below!
if (test == "2"):
    print "Correct!"
    test2 = raw_input("What is 2314*32626? ")
    if (test2 == "75496564"):
        print "Correct! You passed all my tests!"
    else:
        print "Sorry, that's wrong."
else:
    print "Sorry, that's wrong.


I am working on the file "25--logreg.ipynb".
The first task is:
An alternative linear discriminant: the logistic or "sigmoid" function
The heaviside function, $H(\theta^T x)$, enforces a sharp boundary between classes around the $\theta^T x=0$ line. The following code produces a contour plot to show this effect.
Can you write Python code for it?

# Use Numpy's handy meshgrid() to create a regularly-spaced grid of values.
# http://docs.scipy.org/doc/numpy/reference/generated/numpy.meshgrid.html

x1 = np.linspace (-2., +2., 100)
x2 = np.linspace (-2., +2., 100)
x1_grid, x2_grid = np.meshgrid (x1, x2)
h_grid = heaviside (theta[0] + theta[1]*x1_grid + theta[2]*x2_grid)

trace_grid = Contour (x=x1, y=x2, z=h_grid)
py.iplot ([trace_grid])


I am working on the file "25--logreg.ipynb".
The first task is:
Exercise. Make a contour plot of the log-likelihood and draw the trajectory taken by the $\theta(t)$ values laid on top of it.
Can you write Python code for it?

def log_likelihood (theta, l, X):
    # @YOUSE: Complete this function to evaluate the log-likelihood
    pass

n1_ll = 100
x1_ll = np.linspace (-20., 0., n1_ll)
n2_ll = 100
x2_ll = np.linspace (-20., 0., n2_ll)
x1_ll_grid, x2_ll_grid = np.meshgrid (x1_ll, x2_ll)

ll_grid = np.zeros ((n1_ll, n2_ll))
# @YOUSE: Write some code to compute ll_grid, which the following code cell visualizes

trace_ll_grid = Contour (x=x1_ll, y=x2_ll, z=ll_grid)
trace_thetas = Scatter (x=thetas[1, :], y=thetas[2, :], mode='markers+lines')
py.iplot ([trace_ll_grid, trace_thetas])