I am working on the file ".ipynb_checkpoints/Strings-checkpoint.ipynb".
The first task is:
Printing Strings
Recall that in the Numbers notebook, we decided that printing was preferred over using the REPL. This is becuase in all code you write, you will never use the REPL alone, as printing is necessary. We print strings by calling the print function.
Can you write Python code for it?

# Printing a string
print("Hey, there!")

# Printing another string in the same cell, not possible with just the REPL!
print("Hey, there... again!")


I am working on the file ".ipynb_checkpoints/Strings-checkpoint.ipynb".
The first task is:
String Methods
String methods are those functions that are associated with a specific string instance, where are as string functions are concerned with strings as a whole. Note: the word instance refers to the objectifying of anything in Python or another language that has objects in it.
Can you write Python code for it?

# Creating a string instance is just the same thing as assigning a variable a value
c = "Hello"


I am working on the file ".ipynb_checkpoints/Strings-checkpoint.ipynb".
The first task is:
String Properties
What properties to strings have? Well, we said earlier strings are indexable. We already talked about that. We also said that strings were "immutable". To be immutable means to not be able to be changed. A string literal within a variable cannot be altered by indexing. Take a look:
Can you write Python code for it?


I am working on the file ".ipynb_checkpoints/Strings-checkpoint.ipynb".
The first task is:
The "c" variable is now a string instance of the string literal "Hello". The word "string literal" literally means a word or words inside quotes, so the actual string itself.
Now that we have a string object/instance, let's call methods on it specifically. Methods are called as follows:
object.method(arguments)

The string has a few methods. Methods that we'll look at will be the upper(), lower(), split(), and format().
Can you write Python code for it?

my_str = "python is great"

# uppercase a string with upper() method
print(my_str.upper())

# lowercase string with lower() method
print(my_str.lower())


I am working on the file ".ipynb_checkpoints/Strings-checkpoint.ipynb".
The first task is:
There are a few more escape sequences. Note that there is no need to memorize them, as you can just look them up and they will become second nature over time as you need them. View them on Microsoft's Developer Network article: https://msdn.microsoft.com/en-us/library/h21280bw.aspx
String Functions and Methods
You might be wondering, what's difference between a function and a method? What is a function? What is a method? Technically, all methods ARE functions. Methods are functions that belong to objects (remember, everything is essentially an object in Python). Functions in Python can either be defined by you, the programmer or are already defined by the Python developers, which are called "Built-in Functions". These are functions you can call impromptu and they'll just work. Here are some examples of built-in functions.
Can you write Python code for it?

# The built-in float function takes in an integer and returns a float -> float(integer_here)
my_integer = 4
my_float = float(my_integer)
print(my_float)


I am working on the file ".ipynb_checkpoints/Strings-checkpoint.ipynb".
The first task is:
This is called using the the "str %" construct. This construct allows us to substitute the %letter with any variable want. For integers, it's %d, %f for float, and %s for string.
Can you write Python code for it?

my_str = "hello"
print("%s, it's me" % my_str)

pi_val = 3.140000
print("I think pi is around %f" % pi_val)

# You can add multiple arguments
x = 5
y = "hello"
print("%s it's me, I was wondering how you've been these past %d months" % (y, x))


I am working on the file ".ipynb_checkpoints/Strings-checkpoint.ipynb".
The first task is:
We can also concatenate, which means we can add append (add to the end) characters.
Can you write Python code for it?

# adding to our parrots string
my_str = my_str + " that can talk"
print(my_str)


I am working on the file ".ipynb_checkpoints/Strings-checkpoint.ipynb".
The first task is:
We did it! But, imagine you were working with user inputted strings that you did not know of. How would you get the length of a string whose actual contents you don't know? Simple! Python indexing works backwards, too! You can specify the my_str[-1] index to get the string before the first (0th), which would be the last. It works in circularly.
Can you write Python code for it?

print(my_str[-1])

# For fun, let's print each character
print(my_str[0])
print(my_str[1])
print(my_str[2])
print(my_str[3])
print(my_str[4])


I am working on the file ".ipynb_checkpoints/Strings-checkpoint.ipynb".
The first task is:
We just printed a multi-line message with one print function. Here, we used the newline escape sequence, called with "\n". The \n tells the interpreter to skip a line (aka create a new line). This allowed us to make our text look like an email. We can use two \n in a row, as we did above. This applies to all other escape sequences. There are a few others you should know, including the tab escape sequence, which namely allows you to use tabs in your print functions.
Can you write Python code for it?

# Tab escape sequence
print("\t<-- Look at that space!")


I am working on the file ".ipynb_checkpoints/Strings-checkpoint.ipynb".
The first task is:
What just happened? Remember that we said that slicing works like so:
my_str[start(inclusive):end(exclusive)]

Thus, if we want the very first string, we let start be 0. For the end, we put in the index of the character we don't want included up until. Meaning that if we don't want the last character, we put the indice of the character as end. That indice that represents the last character is n-1, where n represents length of the string. We reduce to this formula:
my_str[0:lengthofstring - 1]

How do we get the length? Recall: the len() built-in function. We call the function to get the length, and subtract by 1 to get
my_str[0:len(my_str)-1]

which yields a satisfying "crab" instead of "crabs".
There's a shortcut for getting from any start to the definite end of a string (ex: get all characters starting from 3rd to the very end, get all characters from the 2nd character to then very end). Simply leave out the end argument and fill in the start with an indice you want to start and include.
my_str[start:]
Can you write Python code for it?

# go from the 2nd character to the end
print(my_str[2:])


I am working on the file ".ipynb_checkpoints/Strings-checkpoint.ipynb".
The first task is:
What we saw above is an example of a Built-in FUNCTION. These come with Python. We can also define our own functions/methods. Below, we are defining a function that tells you if a number is even or odd.
Can you write Python code for it?

def even_or_odd(some_number):
    if some_number % 2 == 0:
        return True


I am working on the file ".ipynb_checkpoints/Strings-checkpoint.ipynb".
The first task is:
You should be familiar with this syntax because many programmers still use it. However, in the modern age, many people have started using the format() method. It does the same thing, but in a more organized fashion. The syntax is the following:
my_str.format(var1, var2)
Can you write Python code for it?

x =5
# Format an integer into a string
print("Hey there you have {} apples".format(x))


I am working on the file ".ipynb_checkpoints/TPE-checkpoint.ipynb".
The first task is:
Cinquième étape : calcul du chemin le plus court
Nous calculerons ici le chemin le plus court entre Charlotte et Sophia. Ce résultat est la durée du parcours le plus cours.
Can you write Python code for it?

res = Graph.shortest_paths(graphe, source='Charlotte',target='Sophia', mode='OUT', weights = graphe.es['weight'])
print res[0][0]


I am working on the file ".ipynb_checkpoints/TPE-checkpoint.ipynb".
The first task is:
Quatrième étape : affichage du graphe
Dessinons le graphe pour le rendre plus intelligible. La verison actuelle d'igraph ne supporte pas encore l'affichage des poids des arcs. Pour compenser, les arcs seront plus épais selon leur poids.
Can you write Python code for it?

layout = graphe.layout('kk')

visual_style = {}
visual_style["vertex_size"] = 20
visual_style["vertex_label"] = graphe.vs["name"]
visual_style["layout"] = layout
visual_style["vertex_color"] = 'blue'
visual_style["margin"] = 20
visual_style["vertex_label_size"] = 12
visual_style["vertex_label_dist"] = 1
visual_style["margin"] = 75

plot(graphe, **visual_style)


I am working on the file ".ipynb_checkpoints/Testing Simulation_AM-checkpoint.ipynb".
The first task is:
Attempting to run Simulation_AM
Can you write Python code for it?

xe = dmdd.Simulation_AM('AM_xenon', xe, SI_model,
                        {'mass':50.,'sigma_si':75.5}, Qmin = np.asarray([5.]),
                        Qmax = np.asarray([150.]),
                        Tmin = 0, Tmax = 365, sigma_si = 75.5,
                        element = 'xenon', force_sim = True)

xe = dmdd.Simulation_AM('AM_xenon', xe, anapole_model,
                        {'mass':50.,'sigma_anapole':44.25}, Qmin = np.asarray([5.]),
                        Qmax = np.asarray([150.]),
                        Tmin = 0, Tmax = 365, sigma_anapole = 44.25,
                        element = 'xenon', force_sim = True)


I am working on the file ".ipynb_checkpoints/Testing Simulation_AM-checkpoint.ipynb".
The first task is:
Testing integral function for a known integral with an exact value of 1/6 .
Can you write Python code for it?

def funct1(Q,time, sigma_si, sigma_anapole, mass, element, v_amplitude):
    return (Q**2)*(time)
#have to define like this due to the way integral is defined, but still returns correct answer

dmdd.integral(0, 1, 0, 1, function = funct1)


I am working on the file ".ipynb_checkpoints/Testing Simulation_AM-checkpoint.ipynb".
The first task is:
This cell demonstates the rate_UV.dRdQ can recieve multiple Qs and return them as an array.
The following cell shows the same thing, but for dRdQ_AM.
Can you write Python code for it?

dmdd.rate_UV.dRdQ(Q = np.array([50., 60., 70., 80., 90., 100.]), sigma_si = 75.5)

# demonstrats that rate_UV.dRdQ can take multiple Q's

dmdd.dRdQ_AM(Q = [10., 30., 50., 100.], sigma_si = 75.5, time = 0)
# demonstrates that dRdQ_AM can take multiple Q's as a list


I am working on the file ".ipynb_checkpoints/Testing Simulation_AM-checkpoint.ipynb".
The first task is:
This cell is showing the relative progression of rate over various times for fixed parameters.
Can you write Python code for it?

print dmdd.dRdQ_AM(sigma_si = 75.5, time = 50)
print dmdd.dRdQ_AM(sigma_si = 75.5, time = 100)
print dmdd.dRdQ_AM(sigma_si = 75.5, time = 150)
print dmdd.dRdQ_AM(sigma_si = 75.5, time = 200)
print dmdd.dRdQ_AM(sigma_si = 75.5, time = 250)
print dmdd.dRdQ_AM(sigma_si = 75.5, time = 300)
print dmdd.dRdQ_AM(sigma_si = 75.5, time = 350)


I am working on the file ".ipynb_checkpoints/TextModels-checkpoint.ipynb".
The first task is:
Appendix: Our own data download and preparation
We'll use the Large Movie Review Dataset v1.0 for our corpus.  While Keras has its own data samples you can import for modeling (including this one), I think it's very important to get and process your own data.  Otherwise, the results appear to materialize out of thin air and it's more difficult to get on with your own research.
Can you write Python code for it?

%matplotlib inline
import pandas as pd

import glob

datapath = "/Users/pfigliozzi/aclImdb/train/unsup"
files = glob.glob(datapath+"/*.txt")[:1000] #first 1000 (there are 50k)

df = pd.concat([pd.read_table(filename, header=None, names=['raw']) for filename in files], ignore_index=True)

df.raw.map(lambda x: len(x)).plot.hist()

50000. * 2000. / 10**6


I am working on the file ".ipynb_checkpoints/TextModels-checkpoint.ipynb".
The first task is:
Assign id's to the new features
Can you write Python code for it?

ngram_set.pop()

start_index = MAX_FEATURES + 1
token_indice = {v: k + start_index for k, v in enumerate(ngram_set)}
indice_token = {token_indice[k]: k for k in token_indice}


I am working on the file ".ipynb_checkpoints/TextModels-checkpoint.ipynb".
The first task is:
Create a dense vector for each word in the pair.  The output of Embedding has shape (batch_size, sequence_length, output_dim) which in our case is (batch_size, 1, DENSEVEC_DIM).  We'll use Flatten to get rid of that pesky middle dimension (1), so going into the dot product we'll have shape (batch_size, DENSEVEC_DIM).
Can you write Python code for it?

word1 = Input(shape=(1,), dtype='int64', name='word1')
word2 = Input(shape=(1,), dtype='int64', name='word2')

shared_embedding = Embedding(
    input_dim=VOCAB_SIZE+1,
    output_dim=DENSEVEC_DIM,
    input_length=1,
    embeddings_constraint = unit_norm(),
    name='shared_embedding')

embedded_w1 = shared_embedding(word1)
embedded_w2 = shared_embedding(word2)

w1 = Flatten()(embedded_w1)
w2 = Flatten()(embedded_w2)

dotted = Dot(axes=1, name='dot_product')([w1, w2])

prediction = Dense(1, activation='sigmoid', name='output_layer')(dotted)

sg_model = Model(inputs=[word1, word2], outputs=prediction)

sg_model.compile(optimizer='adam', loss='mean_squared_error')


I am working on the file ".ipynb_checkpoints/TextModels-checkpoint.ipynb".
The first task is:
FastText Model
Can you write Python code for it?

  Image('diagrams/fasttext.png')

from keras.models import Sequential
from keras.layers.embeddings import Embedding
from keras.layers.pooling import GlobalAveragePooling1D
from keras.layers import Dense

ft_model = Sequential()

ft_model.add(Embedding(
    input_dim = MAX_FEATURES,
    output_dim = EMBEDDING_DIMS,
    input_length= MAXLEN))

ft_model.add(GlobalAveragePooling1D())

ft_model.add(Dense(1, activation='sigmoid'))

ft_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

ft_model.layers

print_layer(ft_model, 0)

print_layer(ft_model, 1)

print_layer(ft_model, 2)

ft_model.fit(x_train, y_train, batch_size=100, epochs=3, validation_data=(x_test, y_test))


I am working on the file ".ipynb_checkpoints/TextModels-checkpoint.ipynb".
The first task is:
Let's try training it with our toy data set!
Can you write Python code for it?

import numpy as np

pairs = np.array(sg[0])
targets = np.array(sg[1])

targets

pairs

w1_list = np.reshape(pairs[:, 0], (len(pairs), 1))
w1_list

w2_list = np.reshape(pairs[:, 1], (len(pairs), 1))
w2_list

w2_list.shape

w2_list.dtype

sg_model.fit(x=[w1_list, w2_list], y=targets,  epochs=10)

sg_model.layers[2].weights


I am working on the file ".ipynb_checkpoints/TextModels-checkpoint.ipynb".
The first task is:
Make all input sequences the same length by padding with zeros
Can you write Python code for it?

from keras.preprocessing import sequence

sequence.pad_sequences([[1,2,3,4,5], [6,7,8]], maxlen=10)

x_train = sequence.pad_sequences(x_train, maxlen=MAXLEN)
x_test = sequence.pad_sequences(x_test, maxlen=MAXLEN)

x_train.shape

x_test.shape


I am working on the file ".ipynb_checkpoints/TextModels-checkpoint.ipynb".
The first task is:
Model build
Can you write Python code for it?

from keras.models import Model
from keras.layers import Input
from keras.layers import Embedding
from keras.layers import Dropout
from keras.layers import Conv1D
from keras.layers import MaxPooling1D
from keras.layers import Flatten
from keras.layers import Dense
from keras.layers.merge import Concatenate

# Input, embedding, and dropout layers
input_shape = (sequence_length,)
model_input = Input(shape=input_shape)
z = Embedding(
        input_dim=len(vocabulary) + 1,
        output_dim=embedding_dim,
        input_length=sequence_length,
        name="embedding")(model_input)
z = Dropout(dropout_prob[0])(z)

# Convolutional block
# parallel set of n convolutions; output of all n are
# concatenated into one vector
conv_blocks = []
for sz in filter_sizes:
    conv = Conv1D(filters=num_filters, kernel_size=sz, activation="relu" )(z)
    conv = MaxPooling1D(pool_size=2)(conv)
    conv = Flatten()(conv)
    conv_blocks.append(conv)

z = Concatenate()(conv_blocks) if len(conv_blocks) > 1 else conv_blocks[0]
z = Dropout(dropout_prob[1])(z)

# Hidden dense layer and output layer
z = Dense(hidden_dims, activation="relu")(z)
model_output = Dense(1, activation="sigmoid")(z)

cnn_model = Model(model_input, model_output)
cnn_model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])

cnn_model.layers

print_layer(cnn_model, 12)

print_layer(cnn_model, 12)

cnn_model.fit(x_train, y_train, batch_size=64, epochs=3, validation_data=(x_test, y_test))

cnn_model.layers[1].weights

cnn_model.layers[1].get_weights()

cnn_model.layers[3].weights


I am working on the file ".ipynb_checkpoints/TextModels-checkpoint.ipynb".
The first task is:
Some data prep functions lifted from the example
Can you write Python code for it?

def create_ngram_set(input_list, ngram_value=2):
    """
    Extract a set of n-grams from a list of integers.
    """
    return set(zip(*[input_list[i:] for i in range(ngram_value)]))

create_ngram_set([1, 2, 3, 4, 5], ngram_value=2)

create_ngram_set([1, 2, 3, 4, 5], ngram_value=3)

def add_ngram(sequences, token_indice, ngram_range=2):
    """
    Augment the input list of list (sequences) by appending n-grams values.
    """
    new_sequences = []
    for input_list in sequences:
        new_list = input_list[:]
        for i in range(len(new_list) - ngram_range + 1):
            for ngram_value in range(2, ngram_range + 1):
                ngram = tuple(new_list[i:i + ngram_value])
                if ngram in token_indice:
                    new_list.append(token_indice[ngram])
        new_sequences.append(new_list)

    return new_sequences

sequences = [[1,2,3,4,5, 6], [6,7,8]]
token_indice = {(1,2): 20000, (4,5): 20001, (6,7,8): 20002}

add_ngram(sequences, token_indice, ngram_range=2)

add_ngram(sequences, token_indice, ngram_range=3)


I am working on the file ".ipynb_checkpoints/TextModels-checkpoint.ipynb".
The first task is:
```python
word1 = Input(shape=(1,), dtype='int64', name='word1')
word2 = Input(shape=(1,), dtype='int64', name='word2')
shared_embedding = Embedding(
    input_dim=VOCAB_SIZE+1,
    output_dim=DENSEVEC_DIM,
    input_length=1,
    embeddings_constraint = unit_norm(),
    name='shared_embedding')
embedded_w1 = shared_embedding(word1)
embedded_w2 = shared_embedding(word2)
w1 = Flatten()(embedded_w1)
w2 = Flatten()(embedded_w2)
dotted = Dot(axes=1, name='dot_product')([w1, w2])
prediction = Dense(1, activation='sigmoid', name='output_layer')(dotted)
sg_model = Model(inputs=[word1, word2], outputs=prediction)
```
fastText
```python
ft_model = Sequential()
ft_model.add(Embedding(
    input_dim = MAX_FEATURES,
    output_dim = EMBEDDING_DIMS,
    input_length= MAXLEN))
ft_model.add(GlobalAveragePooling1D())
ft_model.add(Dense(1, activation='sigmoid'))
```
Models and Training data construction
The first step for CBOW and skip-gram
Our training corpus is a collection of sentences, Tweets, emails, comments, or even longer documents.  It is something composed of words.  Each word takes is turn being the "target" word, and we collect the n words behind it and n words which follow it.  This n is referred to as window size.  If our example document is the sentence "I love deep learning" and the window size is 1, we'd get:
  * I, love
  * I, love, deep
  * love, deep, learning
  * deep, learning
The target word is bold.
Skip-gram model training data
Skip-gram means form word pairs with a target word and all words in the window.  These become the "positive" (1) samples for the skip-gram algorithm.  In our "I love deep learning" example we'd get (eliminating repeated pairs):

(I, love) = 1
(love, deep) = 1
(deep, learning) = 1

To create negative samples (0), we pair random vocabulary words with the target word.  Yes, it's possible to unluckily pick a negative sample that usually appears around the target word.
For our prediction task, we'll take the dot product of the words in each pair (a small step away from the cosine...
Can you write Python code for it?

from keras.preprocessing.sequence import skipgrams
from keras.preprocessing.text import Tokenizer, text_to_word_sequence

text1 = "I love deep learning."
text2 = "Read Douglas Adams as much as possible."

tokenizer = Tokenizer()
tokenizer.fit_on_texts([text1, text2])

word2id = tokenizer.word_index
word2id.items()


I am working on the file ".ipynb_checkpoints/TextModels-checkpoint.ipynb".
The first task is:
fastText classifier vs. convolutional neural network (CNN) vs. long short-term memory (LSTM) classifier: Fight!
A CNN takes the dot product of various "filters" (some new vector) with each word window down the sentence.  For each convolutional layer in your model, you can choose the size of the filter (for example, 3 word vectors long) and the number of filters in the layer (for example, ten 3-word filters, or five 3-word filters).
Add a bias to each dot product of the filter and word window, and run it through an activation function.  This produces a number.
Running a single filter down a sentence produces a series of numbers.  Generally the maximum value is taken to represent the alignment of the sentence with that particular filter.  All of this is just another way of extracting features from a sentence.  In fastText, we extracted features in a human-readable way (n-grams) and tacked them onto the input data.  With a CNN we take a different approach, letting the algorithm figure out what makes good features for the dataset.
insert filter operating on sentence image here
Can you write Python code for it?


I am working on the file ".ipynb_checkpoints/TextModels-checkpoint.ipynb".
The first task is:
skip-gram
Can you write Python code for it?


I am working on the file ".ipynb_checkpoints/Training-Neural-Networks-Theano-checkpoint.ipynb".
The first task is:
Let's look at the first three images in the training set, then set about building a machine learning model that has the ability to recognise digits itself!
We've defined a function plot_mnist_digit exactly for printing out the training images - it's just for prettiness.
Can you write Python code for it?

for i in range(3):
    plot_mnist_digit(training_x.get_value()[i])             # training_x is a theano object containing *images*
    print ('Image class: '+ str(training_y.get_value()[i])) # training_y is a theano object containing *labels*


I am working on the file ".ipynb_checkpoints/Training-Neural-Networks-Theano-checkpoint.ipynb".
The first task is:
Note that we had to specify axis=1, that is, we want to get the argmax for each row, as each row represents the distribution for one datapoint.
Create a Theano function that classifies a dataset
Similarly to the training function, the classification function will use givens to pass in the test dataset, and output y_hat which we just defined
Can you write Python code for it?

classification_function = theano.function(
    inputs = [],
    outputs = y_hat,
    givens = {X:test_x} # don't need the true labels test_y here
    )


I am working on the file ".ipynb_checkpoints/Training-Neural-Networks-Theano-checkpoint.ipynb".
The first task is:
Now let's add the gradient updates:
Can you write Python code for it?

LL = T.mean(T.log(P)[T.arange(P.shape[0]), y])  # the log-likelihood (LL)

updates = [
        [W_xh, W_xh + learning_rate * T.grad(LL, W_xh)],
        [W_hy, W_hy + learning_rate * T.grad(LL, W_hy)],
        [b_xh, b_xh + learning_rate * T.grad(LL, b_xh)],
        [b_hy, b_hy + learning_rate * T.grad(LL, b_hy)],
    ]


I am working on the file ".ipynb_checkpoints/Training-Neural-Networks-Theano-checkpoint.ipynb".
The first task is:
OK, let's run ten iterations of our code and see what the log-likelihood does
Can you write Python code for it?

for i in range(10):
    current_LL = training_function()
    print("Log-likelihood = " + str(current_LL) + "\t\t" +
          "Average probability of the correct class = " + str(np.exp(current_LL))
         )


I am working on the file ".ipynb_checkpoints/Training-Neural-Networks-Theano-checkpoint.ipynb".
The first task is:
Redefining Log-loss
The one extremely important thing we did here was redefined LL.
This is a crucial point about how Theano works:
Whenever we define a theano variable, like we did with P, we create a new object.
When we define a new theano variable in terms of another Theano variable, like we did with LL, using
<pre>
LL = T.mean(T.log(P)[T.arange(P.shape[0]), y])
</pre>
we implicitly create a new object for LL that has a reference to our variable P we just defined.
Now the crucial part: say we redefine P. Then our variable LL still has a reference to the old variable P, and we need to update the reference to LL by re-running the definition for LL for everything to work correctly.
Bugs in Theano are very commonly produced by exactly this. It is a good reason to always use Theano in scripts rather than in a notebook like we are here.
Phew, we are now ready to train!
Can you write Python code for it?

training_function = theano.function(
    inputs = [],
    outputs = LL,
    updates = updates,
    givens = {X: training_x[:5000],  # use only 10% of the data so model not too complicated
              y: training_y[:5000]}  # to train on a personal computer
    )
for i in range(60):                  # train more than for logistic regression as this model is more complex
    current_LL = training_function()
    print(
        "Log-likelihood = " + str(current_LL) + "\t\t" +
        "Average probability of the correct class = " + str(np.exp(current_LL))
    )


I am working on the file ".ipynb_checkpoints/Training-Neural-Networks-Theano-checkpoint.ipynb".
The first task is:
Remember our model? Let's write it using matrices and vector for an entire dataset:
$$\boldsymbol{\textrm{H}} = \tanh \left( \boldsymbol{\textrm{X}}  \boldsymbol{\textrm{W}}\boldsymbol{\textrm{xh}}  + \boldsymbol{\textrm{1b}}^\boldsymbol{\textrm{T}}\boldsymbol{\textrm{xh}}\right), \
P(Y \ |\ \boldsymbol{\textrm{x}} ; \theta) =  \textrm{SoftMax} \left( \boldsymbol{\textrm{H}} \boldsymbol{\textrm{W}}\boldsymbol{\textrm{hy}} + \boldsymbol{\textrm{1b}}^\boldsymbol{\textrm{T}} \boldsymbol{\textrm{hy}}\right)$$
Let's code it up:
Can you write Python code for it?

H =         T.tanh( T.dot(X, W_xh) + b_xh )
P = T.nnet.softmax( T.dot(H, W_hy) + b_hy )


I am working on the file ".ipynb_checkpoints/Training-Neural-Networks-Theano-checkpoint.ipynb".
The first task is:
Test the model
Can you write Python code for it?

y_hat = T.argmax(P, axis=1)
test_y_hat = classification_function()

print ("Classification error: " + str(100 * (1 - np.mean(test_y_hat == test_y.get_value()))) + "%")

for i in range(10):
    plot_mnist_digit(
        test_x.get_value()[i]                                # test_y is a theano object of *images*
    )
    print ('Image class: \t\t' + str(test_y.get_value()[i])) # test_y is a theano object of *true labels*
    print ('Model-assigned class: \t' + str(test_y_hat[i]))  # test_y_hat is a theano object of *predicted labels*


I am working on the file ".ipynb_checkpoints/Training-Neural-Networks-Theano-checkpoint.ipynb".
The first task is:
The neural network model
The neural network model that we will build defines a probability distribution
$$P(Y = y \ |\ X = \boldsymbol{\textrm{x}} ; \theta),$$
where $Y$ represents the image class, which means it is a random variable that can take the values 0-9, $X$ represents the image pixels and is a vector-valued random variable (we collapse the image matrix into a vector), and $\theta$ is the set of model parameters that we are going to learn.
In this tutorial we build two models: first implementing a logistic regression model, then extending it to a neural network model.
Multi-class logistic regression
The equation for our first model is give by
$$P(Y = y \ |\ \boldsymbol{\textrm{x}} ; \theta) \propto \left[\sigma \left( \boldsymbol{\textrm{x}}^\boldsymbol{\textrm{T}} \boldsymbol{\textrm{W}}  + \boldsymbol{\textrm{b}}^\boldsymbol{\textrm{T}}\right)\right]_y,$$
where $[\boldsymbol{\textrm{x}}]_i$ is the $i$th entry of vector $\boldsymbol{\textrm{x}}$, and the use of the proportionality symbol $\propto$ means that the probability is equal to the expression on the right hand side times a constant chosen such that $\sum_y{P(Y = y \ |\ \boldsymbol{\textrm{x}} ; \theta)} = 1$
The parameter set $\theta$ for this model is $\theta = {\boldsymbol{\textrm{W}}, \boldsymbol{\textrm{b}}}$, where $\boldsymbol{\textrm{W}}$ is a matrix and $\boldsymbol{\textrm{b}}$ is a vector. We also use the non-linearity $\sigma$ given by
$$\sigma(t) = \frac{1}{1+e^{-t}}$$
<img src=https://upload.wikimedia.org/wikipedia/commons/thumb/8/88/Logistic-curve.svg/200px-Logistic-curve.svg.png />
When applied to a vector or matrix, the sigmoid function $\sigma(t)$ is applied entrywise.
Understanding the logistic regression model
One way to think about the logistic regression model is that it takes the input ($\boldsymbol{\textrm{x}}$), puts it through a linear combination ($\boldsymbol{\textrm{x}}^\boldsymbol{\textrm{T}} \boldsymbol{\textrm{W}}  + \boldsymbol{\textrm{b}}^\boldsymbol{\textrm{T}}$) and then finally through a non-linearity:...
Can you write Python code for it?

n_classes = 10  # each digit is one of 0-9
dims = 28 * 28  # our input data is flattened 28x28 matrices of image pixels

X = T.dmatrix() # Theano double matrix
y = T.ivector() # Theano integer vector
W = theano.shared(np.zeros([dims,n_classes])) # Theano shared double matrix
b = theano.shared(np.zeros(n_classes))        # Theano shared double vector


I am working on the file ".ipynb_checkpoints/Training-Neural-Networks-Theano-checkpoint.ipynb".
The first task is:
Theano functions
OK, we're done coding the model. What do we do next?
When working with Theano, the next step is to create a Theano function. A Theano function is the basic unit of Theano code that we call to do something. In our case, this something will be performing a single gradient ascent iteration.
We create a Theano function by calling the function theano.function (yes, we create a function by calling a function). theano.function has four important parameters that we provide in order to get a Theano function:

inputs  -- the list of input variables of the Theano function, similar to the inputs of a Python function
outputs -- the list of output variables of the Theano function, similar to the inputs of a Python function
updates -- a list of updates for shared variables, in the format we used above when we defined the variable updates
givens -- a dictionary that allows substituting some variables from the model with other variables

In our case, we want the input to be the training dataset, the updates to be the gradient ascent updates, and while we don't really need an output, it will be helpful to get the log-likelihood as an output to see that we are doing the right things.
However, we will use the givens instead of the input to provide the data to the function.
Doing it this way is more efficient, as we've already loaded up the training dataset into memory as a shared Theano function, when we first loaded the data.
Our Theano function will look like this:
Can you write Python code for it?

training_function = theano.function(
    inputs = [],             # use givens instead of the inputs as it's more efficient
    outputs = LL,            # output log-likelihood just to check that it is improving
    updates = updates,       # these are the gradient updates, the one part that's really important
    givens = {X: training_x, # we indicate that the model variables X and y defined in the abstract
              y: training_y} # should take the values in the shared variables training_x and training_y
    )


I am working on the file ".ipynb_checkpoints/Training-Neural-Networks-Theano-checkpoint.ipynb".
The first task is:
Theano provides us with a T.nnet.softmax function to compute SoftMax, correctly normalizing the probabilities so that each row of the matrix P is a proper probability distribution that sums to $1$.
Note that we didn't need to multiply b by the 1 vector, Theano will do the correct addition for us automatically, just like numpy would do it. Here is a simple numpy example illustrating this:
Can you write Python code for it?

amatrix = np.zeros((3,2)) # 3x2 matrix of all zeros
avector = np.array((1,2)) # the vector [1,2]
amatrix + avector


I am working on the file ".ipynb_checkpoints/Training-Neural-Networks-Theano-checkpoint.ipynb".
The first task is:
Training a neural network
So far we have trained a logistic regression model. The neural network model is so similar that we can imlepement it with just a few changes to the code.
We need
- to declare the hidden layer variable
- to decide on the size of the hidden layer (we'll keep it small so it will run on your personal computer)
- new parameters
Can you write Python code for it?

H = T.dmatrix()  # Theano double matrix
hidden_layer_size = 20
W_xh = theano.shared(0.01 * np.random.randn(dims, hidden_layer_size))
W_hy = theano.shared(np.zeros([hidden_layer_size, n_classes]))
b_xh = theano.shared(np.zeros(hidden_layer_size))
b_hy = theano.shared(np.zeros(n_classes))


I am working on the file ".ipynb_checkpoints/Training-Neural-Networks-Theano-checkpoint.ipynb".
The first task is:
Using the model for classification
Great, it appears we're improving the log-likelihood of the model on the training data. Now to use it to classify. Recall our equation that expresses how to use the model to get the class:
$$\hat{\boldsymbol{\textrm{y}}} = {\arg\max} \ P(Y \ |\ \boldsymbol{\textrm{X}} ; \theta) $$
Let's put that in code:
Can you write Python code for it?


I am working on the file ".ipynb_checkpoints/Training-Neural-Networks-Theano-checkpoint.ipynb".
The first task is:
We're done with the model. There's one more thing we need to do, and that is specify the gradient updates
\begin{align}
\boldsymbol{\textrm{W}} &\leftarrow \boldsymbol{\textrm{W}} + \lambda \frac{\partial \log P( Y = \boldsymbol{\textrm{y}}\ | \ X = \boldsymbol{\textrm{X}} ; \theta)}{\partial \boldsymbol{\textrm{W}}} \
\boldsymbol{\textrm{b}} &\leftarrow \boldsymbol{\textrm{b}} + \lambda \frac{\partial \log P( Y = \boldsymbol{\textrm{y}}\ | \ X = \boldsymbol{\textrm{X}} ; \theta)}{\partial \boldsymbol{\textrm{b}}} \
\end{align}
Can you write Python code for it?

learning_rate = 0.5 # we tuned this parameter by hand
updates = [
        [W, W + learning_rate * T.grad(LL, W)],
        [b, b + learning_rate * T.grad(LL, b)]
        ]


I am working on the file ".ipynb_checkpoints/Training-Neural-Networks-Theano-checkpoint.ipynb".
The first task is:
With the data loaded into a variable mnist_dataset, we split it into three part: a training set, used to teach the network to recognize digits, a validation set that could be used to tune and compare models, and finally a test set, used to see how well it has learned.
We then prepare the datasets, splitting each set into the images and their labels, and store them in Theano shared variables, a bit of theano magic that's explained later. Understanding this massaging of the data isn't crucial.
Can you write Python code for it?

import os
import gzip
import pickle

myPath = '' # mnist.pkl.gz is in this directory
f = gzip.open(os.path.join(myPath, 'mnist.pkl.gz'), 'rb')

try:        # for cross-platform, cross-version reasons, try two different pickle.load statements
    mnist_dataset = pickle.load(f, encoding='latin1')
except:
    mnist_dataset = pickle.load(f)
f.close()

train_set, valid_set, test_set = mnist_dataset
prepare_data = lambda x: (theano.shared(x[0].astype('float64')),
                          theano.shared(x[1].astype('int32')))
(training_x, training_y), (valid_x, valid_y), (test_x, test_y) = map(prepare_data, (train_set, valid_set, test_set))

import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline

def plot_mnist_digit(image):
    '''Plot a single digit from the mnsist dataset'''
    image = np.reshape(image, [28,28])
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.matshow(image, cmap=matplotlib.cm.binary)
    plt.xticks(np.array([]))
    plt.yticks(np.array([]))
    plt.show()


I am working on the file ".ipynb_checkpoints/Tutorial 7 - Data Visualization and Plotting-checkpoint.ipynb".
The first task is:
7.4 3D Plotting
A 3D plot can be created by importing the Axes3D class and passing a projection='3d' argument.
Can you write Python code for it?

from mpl_toolkits.mplot3d.axes3d import Axes3D

# 2D Gaussian Plot (3D image)

import matplotlib.cm as cm
import matplotlib.ticker as ticker

a = 12.0
b = 5.0
c = 0.3

fig = plt.figure(figsize=(6,4))
ax = fig.gca(projection='3d')                  # Passing a projection='3d' argument
X = np.linspace(4, 6, 100)
Y = np.linspace(4, 6, 100)
X, Y = np.meshgrid(X, Y)
R = (X-b)**2/(2*c**2) + (Y-b)**2/(2*c**2)      # Use the declared a,b,c values in 1D-Gaussian
Z = a*np.exp(-R)

# Applying the plot features to variable surf
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.hsv,
                       linewidth=0, antialiased=True, alpha=0.3)

ax.set_zlim(-1.01, 14)
ax.zaxis.set_major_locator(ticker.LinearLocator(8))
ax.zaxis.set_major_formatter(ticker.FormatStrFormatter('%.1f'))

fig.colorbar(surf, shrink=0.4, aspect=10)
ax.view_init(30, 40)                          # Angles of viewing

plt.tight_layout()


I am working on the file ".ipynb_checkpoints/Tutorial 7 - Data Visualization and Plotting-checkpoint.ipynb".
The first task is:
7.5 Animated Plotting
$Matplotlib$ has the ability to generate a movie file from sequences of figures. Let see an example of the syntax needed to capture a Brownian motion in a video (it is necessary to explicitly import the animation module from $Matplotlib$).
Can you write Python code for it?

# Brownian motion of particle suspended in liquid

nu = 0.7978E-3            # Pa s
kB = 1.3806488E-23        # m^2 kg s^-2 K^-1
d = 0.001                 # 1 micron
T = 30+273.15             # Kelvin

D = kB*T/(3*np.pi*nu*d)

dt = 0.00001

dl = np.sqrt(2*D*dt)

xp = np.random.uniform(0,0.0001,20)
yp = np.random.uniform(-0.00000005,0.000000005,20)

for value in range(0,xp.size,1):

    angle1 = np.random.normal(0,np.pi,500)
    xb = xp[value]+np.cumsum(dl*np.cos(angle1))
    yb = yp[value]+np.cumsum(dl*np.sin(angle1))

plt.figure(figsize=(6,4))
plt.plot(xb,yb)

from matplotlib import animation       # Importing the animation module

fig, ax = plt.subplots(figsize=(6,4))

xr = np.sqrt((xb.max()-xb.min())**2)/20.0
yr = np.sqrt((yb.max()-yb.min())**2)/20.0

ax.set_xlim([(xb.min()-xr), (xb.max()+xr)])
ax.set_ylim([(yb.min()-yr), (yb.max()+yr)])

line1, = ax.plot([], [], 'ro-')
line2, = ax.plot([], [], color="blue", lw=1)

x2 = np.array([])
y2 = np.array([])

def init():
    line1.set_data([], [])
    line2.set_data([], [])

def update(n):
    # n = frame counter

    global x2, y2
    x2 = np.hstack((x2,xb[n]))
    y2 = np.hstack((y2,yb[n]))
    line1.set_data([xb[n],xb[n+1]],[yb[n],yb[n+1]])
    line2.set_data(x2, y2)

# Creating the animation
# anim = animation.FuncAnimation(fig, update, init_func=init, frames=len(xb)-1, blit=True)
anim = animation.FuncAnimation(fig, update, init_func=init, frames=len(xb)-1)

# Saving the animation film in .mp4 format
anim.save('Tutorial7/Brownian.mp4', fps=10, writer="avconv", codec="libx264")

plt.close(fig)


I am working on the file ".ipynb_checkpoints/Tutorial 7 - Data Visualization and Plotting-checkpoint.ipynb".
The first task is:
Here the line (%matplotlib inline) means that the plot when created will be displayed on the working document. The second line import the plotting function of $Matplotlib$ library.
Can you write Python code for it?

# Data in lists
x = [1,2,3,4,5]
y = [21,34,78,12,9]

# Plot x against y
plt.plot(x, y)


I am working on the file ".ipynb_checkpoints/Tutorial 7 - Data Visualization and Plotting-checkpoint.ipynb".
The first task is:
Other subplot functions like $subplot2grid()$ and $gridspec()$ allow more control on the produced multi-plots. In the case of $subplot2grid()$, the location of a plot in a 2-dimensional graphical grid can be specified using coordinate (i,j) with both i and j starting at $0$.
Can you write Python code for it?

# A canvas of 2 x 3 graph

plot1 = plt.subplot2grid((2,3), (0,0), rowspan=2)   # a(i,j) = starting at (0,0) and extend to (1,0)
plot2 = plt.subplot2grid((2,3), (0,1), colspan=2)   # a(i,j) = starting at (0,1) and extend to (0,2)
plot3 = plt.subplot2grid((2,3), (1,1))              # a(i,j) = starting at (1,1)
plot4 = plt.subplot2grid((2,3), (1,2))              # a(i,j) = starting at (1,2)


I am working on the file ".ipynb_checkpoints/Tutorial 7 - Data Visualization and Plotting-checkpoint.ipynb".
The first task is:
Relative size of row and column can be specified using $GridSpec()$:
Can you write Python code for it?

# a 2 x 2 grid plot with specified relative width and height

plt.figure(figsize=(8,4))

gs = plt.GridSpec(2, 2, width_ratios=[1,2],height_ratios=[2.5,1.5])   # a 2 x 2 grid plot

gp1 = plt.subplot(gs[0])
gp2 = plt.subplot(gs[1])
gp3 = plt.subplot(gs[2])
gp4 = plt.subplot(gs[3])
plt.tight_layout()

gp1.plot(x_data,np.exp(x_data),'go-')
gp2.plot(x_data,2.0*np.exp(-((x_data-1.5)**2/(2*0.05))),'ms-')
gp3.plot(x_data,x_data,'bs-')
gp4.plot(x_data,x_data**(-1),'r--')
gp4.set_yticks(np.linspace(0.4,2.0,5))          # setting the y ticks for plot 4


I am working on the file ".ipynb_checkpoints/Tutorial 7 - Data Visualization and Plotting-checkpoint.ipynb".
The first task is:
Replacing the first line with %matplotlib qt, an interactive plotting graphical user interface (GUI) will be displayed instead in a separate window (similar to MATLAB).
Can you write Python code for it?


I am working on the file ".ipynb_checkpoints/Tutorial 7 - Data Visualization and Plotting-checkpoint.ipynb".
The first task is:
Similarly, let add some data:
Can you write Python code for it?

plt.figure(figsize=(12,4))

gs1 = plt.GridSpec(3, 3, width_ratios=[1.5,1,1.5],height_ratios=[1.5,1,1.5])
gs1.update(left=0.05, right=0.48, wspace=0.3, hspace=0.4)        # size on canvas
fp1 = plt.subplot(gs1[:2, :2])
fp2 = plt.subplot(gs1[0, 2])
fp3 = plt.subplot(gs1[2, :2])
fp4 = plt.subplot(gs1[1:, 2])

gs2 = plt.GridSpec(3, 3, width_ratios=[1.6,1,1.4],height_ratios=[1.3,1,1.7])
gs2.update(left=0.55, right=0.98, wspace=0.3, hspace=0.4)       # size on canvas
afp1 = plt.subplot(gs2[:2,0])
afp2 = plt.subplot(gs2[:2, 1:])
afp3 = plt.subplot(gs2[2, :-1])
afp4 = plt.subplot(gs2[2, -1])

fp1.plot(x_data,2.0*np.exp(-((x_data-1.5)**2/(2*0.05))),'ms-')
fp2.plot(x_data,np.exp(x_data),'go-')
fp2.set_yticks(np.linspace(0,14,5))
fp3.plot(x_data,x_data,'bs-')
fp4.plot(x_data,x_data**(-1),'r--')

afp1.plot(x_data,x_data**(-1),'r--')
afp2.plot(x_data,2.0*np.exp(-((x_data-1.5)**2/(2*0.05))),'ms-')
afp3.plot(x_data,np.exp(x_data),'go-')
afp4.plot(x_data,x_data,'bs-')
afp4.set_yticks(np.linspace(0,3,6))


I am working on the file ".ipynb_checkpoints/Tutorial 7 - Data Visualization and Plotting-checkpoint.ipynb".
The first task is:
The $r\${text}\$$ notation allows the writing of mathematical equations using $LaTeX$ syntax.
Let us now see some variety of plot lines that can be created.
Can you write Python code for it?

xv = np.linspace(0,10,11)
yv = 5*xv

# Setting the canvas size and also resolution
plt.figure(figsize=(14,4), dpi=100)

plt.xlim(0,50)
plt.ylim(0,50)

Lp = plt.subplot()

# Plot without top and right borders
Lp.spines['top'].set_visible(False)
Lp.spines['right'].set_visible(False)

# Show ticks only on left and bottom spines
Lp.yaxis.set_ticks_position('left')
Lp.xaxis.set_ticks_position('bottom')

# Title and axes labels
Lp.set_title('Variety of Plot')
Lp.set_xlabel(r'$x$')
Lp.set_ylabel(r'$y$')

# Plotting
## Line widths and colors
Lp.plot(xv+2, yv, color="blue", linewidth=0.25)
Lp.plot(xv+4, yv, color="red", linewidth=0.50)
Lp.plot(xv+6, yv, color="m", linewidth=1.00)
Lp.plot(xv+8, yv, color="blue", linewidth=2.00)

## Linestype options: ‘-‘, ‘–’, ‘-.’, ‘:’, ‘steps’
Lp.plot(xv+12, yv, color="red", lw=2, linestyle='-')
Lp.plot(xv+14, yv, color="#08088A", lw=2, ls='-.')
Lp.plot(xv+16, yv, color="red", lw=2, ls=':')

## Dash line can be cusotomized
line, = Lp.plot(xv+20, yv, color="blue", lw=2)
line.set_dashes([5, 10, 15, 10]) # format: line length, space length, ...

## Possible marker symbols: marker = '+', 'o', '*', 's', ',', '.', '1', '2', '3', '4', ...
Lp.plot(xv+24, yv, color="#0B3B0B", lw=2, ls='', marker='+', markersize = 12)
Lp.errorbar(xv+26, yv, color="green", lw=2, ls='', marker='o', yerr=5)
Lp.plot(xv+28, yv, color="green", lw=2, ls='', marker='s')
Lp.plot(xv+30, yv, color="#0B3B0B", lw=2, ls='', marker='1', ms = 12)

# Marker sizes and colorsplt.errorbar(x, y, xerr=0.2, yerr=0.4)

Lp.plot(xv+34, yv, color="r", lw=1, ls='-', marker='o', markersize=3)
Lp.plot(xv+36, yv, color="g", lw=1, ls='-', marker='o', markersize=5)
Lp.plot(xv+38, yv, color="purple", lw=1, ls='-', marker='o', markersize=8, markerfacecolor="red")
Lp.plot(xv+40, yv, color="purple", lw=1, ls='-', marker='s', markersize=8,
        markerfacecolor="yellow", markeredgewidth=2, markeredgecolor="blue");


I am working on the file ".ipynb_checkpoints/Tutorial 7 - Data Visualization and Plotting-checkpoint.ipynb".
The first task is:
The axes numerics are quite messy becuase of overlapping. User can use the $tight{_}layout()$ function to automatically tidy up the whole graph.
Can you write Python code for it?

# A canvas of 2 x 3 graph

p1 = plt.subplot2grid((2,3), (0,0), rowspan=2)   # a(i,j) = starting at (0,0) and extend to (1,0)
p2 = plt.subplot2grid((2,3), (0,1), colspan=2)   # a(i,j) = starting at (0,1) and extend to (0,2)
p3 = plt.subplot2grid((2,3), (1,1))              # a# it return best fit values for parameters
# pass the function name, x array, y array, initial value of parameters (in list)(i,j) = starting at (1,1)
p4 = plt.subplot2grid((2,3), (1,2))              # a(i,j) = starting at (1,2)
plt.tight_layout()


I am working on the file ".ipynb_checkpoints/Tutorial 7 - Data Visualization and Plotting-checkpoint.ipynb".
The first task is:
The input cell above shows the user how to embed a plot within a plot by specifying the initial coordinates and sizes of the plots. A slightly different syntax is used here with the sub plot properties assigned to variables axes1 and axes2. From here on it is easy to add features to each plot by operating on the each plot variable.
It is possibly much better to use sub plotting functions (depends on users taste) like $subplot(i,j,k)$ function where $i$ represents number of rows, $j$ represents number of columns and $k$ is the position of the plot (moving horizontally to the left from above to below) as shown for $i$ = 2 and $j$ = 2 below.

Can you write Python code for it?

# y data are calculated inside plot() function

plt.subplot(1,2,1)                        # graph with one row, two columns at position 1
plt.plot(x_data,np.exp(x_data),'go-')     # green with square object and line
plt.subplot(1,2,2)                        # graph with one row, two columns at position 2
plt.plot(x_data,x_data**(-1),'r--')       # just a red dashed line

plt.subplot(2,2,1)
plt.plot(x_data,np.exp(x_data),'go-')
plt.subplot(2,2,2)
plt.plot(x_data,x_data**(-1),'r--')
plt.subplot(2,2,4)
plt.plot(x_data,x_data,'bs-')


I am working on the file ".ipynb_checkpoints/Tutorial 7 - Data Visualization and Plotting-checkpoint.ipynb".
The first task is:
Using the data X, Y and Z in the computer memory from the above cell, we create a wireframe plot and a surface plot with contour projection.
Can you write Python code for it?

fig_wire = plt.figure(figsize=(12,6))

# Wireframe plot
ax1 = fig_wire.add_subplot(1,2,1, projection='3d')
ax1.plot_wireframe(X, Y, Z, rstride=4, cstride=4, alpha=0.75)
ax1.view_init(55, 30)
ax1.set_zlim(-2, 14)
ax1.set_title('Wireframe')

# Surface plot
ax2 = fig_wire.add_subplot(1,2,2, projection='3d')
ax2.plot_surface(X, Y, Z, rstride=6, cstride=6, alpha=0.45)
ax2.view_init(30, 30)
ax2.set_title('Surface and Contour')

# Different color maps used for each contours
# the offset argument refers to the position of contour
ax2.contour(X, Y, Z, zdir='x', offset=3, cmap=cm.hsv)
ax2.contour(X, Y, Z, zdir='y', offset=3, cmap=cm.prism)
ax2.contour(X, Y, Z, zdir='z', offset=-8, cmap=cm.coolwarm)

# Axes range for contours
ax2.set_xlim3d(3, 7)
ax2.set_ylim3d(3, 7)
ax2.set_zlim3d(-8, 20)

fig_wire.tight_layout()


I am working on the file ".ipynb_checkpoints/Tutorial 7 - Data Visualization and Plotting-checkpoint.ipynb".
The first task is:
We can now add some data inside these plots using data in x_data (and also increase the canvas size).
Can you write Python code for it?

# A canvas of 2 x 3 graph

plt.figure(figsize=(8,4))

p1 = plt.subplot2grid((2,3), (0,0), rowspan=2)   # a(i,j) = starting at (0,0) and extend to (1,0)
p2 = plt.subplot2grid((2,3), (0,1), colspan=2)   # a(i,j) = starting at (0,1) and extend to (0,2)
p3 = plt.subplot2grid((2,3), (1,1))              # a(i,j) = starting at (1,1)
p4 = plt.subplot2grid((2,3), (1,2))              # a(i,j) = starting at (1,2)
plt.tight_layout()

p1.plot(x_data,np.exp(x_data),'go-')
p2.plot(x_data,2.0*np.exp(-((x_data-1.5)**2/(2*0.05))),'ms-')   # Gaussian function for y value
p3.plot(x_data,x_data,'bs-')
p4.plot(x_data,x_data**(-1),'r--')


I am working on the file ".ipynb_checkpoints/TutorialAndExample-checkpoint.ipynb".
The first task is:
Compare the results of scenarios 1 and 2a:
Can you write Python code for it?


I am working on the file ".ipynb_checkpoints/TutorialAndExample-checkpoint.ipynb".
The first task is:
Compare the results of scenarios 1 and 2b:
Can you write Python code for it?


I am working on the file ".ipynb_checkpoints/TutorialAndExample-checkpoint.ipynb".
The first task is:
Compare the results of scenarios 1a and 2a2:
Can you write Python code for it?


I am working on the file ".ipynb_checkpoints/TutorialAndExample-checkpoint.ipynb".
The first task is:
Compare the results of scenarios 1b and 2b:
Can you write Python code for it?


I am working on the file ".ipynb_checkpoints/TutorialAndExample-checkpoint.ipynb".
The first task is:
Conclusions: Again the cookie-cutter deployment strategy was able to absorb the change in load mix, while the individualized strategy was not, with visibly lower throughput and higher mean and 95th percentile response times.  Notice that due to the changed load mix, the total load was lower than before and, with the same number of servers, the cookie-cutter configuration had excess capacity while the individualized configuration had excess capacity for svc_1 and insufficient capacity for svc_2.
Simulation 4
We now continue with the weights used in Simulation 3, but adjust server capacity to account for the lower aggregate load and different load mix.
Below we have three scenarios:
- Scenario 1 (cookie-cutter) removes one server
- Scenario 2 (individualized) removes one server from the pool allocated to svc_1
- Scenario 3 (individualized) removes one server and reassigns one server from the svc_1 pool to the svc_2 pool.
Run the three scenarios:
Can you write Python code for it?

rand_state = random.getstate()
sc1 = simulate_deployment_scenario(num_users=720, weight1=1, weight2=1,
                                   server_range1=range(0, 9), server_range2=range(0, 9))
random.setstate(rand_state)
sc2a = simulate_deployment_scenario(num_users=720, weight1=1, weight2=1,
                                    server_range1=range(0, 7), server_range2=range(7, 9))
random.setstate(rand_state)
sc2b = simulate_deployment_scenario(num_users=720, weight1=1, weight2=1,
                                    server_range1=range(0, 6), server_range2=range(6, 9))


I am working on the file ".ipynb_checkpoints/TutorialAndExample-checkpoint.ipynb".
The first task is:
Conclusions: The results of the two deployment strategies are similar in terms of throughput, mean response times, and 95th percentile response times.  This is as would be expected, since the capacities allocated under the individualized deployment strategy are proportional to the respective service loads.
Simulation 2
Now, we change the weights of the different services, significantly increasing the weight of svc_1 from 2 to 5.
Can you write Python code for it?

rand_state = random.getstate()
sc1 = simulate_deployment_scenario(num_users=720, weight1=5, weight2=1,
                                   server_range1=range(0, 10), server_range2=range(0, 10))
random.setstate(rand_state)
sc2 = simulate_deployment_scenario(num_users=720, weight1=5, weight2=1,
                                   server_range1=range(0, 8), server_range2=range(8, 10))
compare_scenarios(sc1, sc2)


I am working on the file ".ipynb_checkpoints/TutorialAndExample-checkpoint.ipynb".
The first task is:
Mini-batching, plotting, and comparison of results
The following three functions handle mini-batching, plotting, and comparison of results.

minibatch_resp_times -- This function takes the user group from the results of the deployment_example function, scans the service request log of the user group, and produces mini-batch statistics for every time_resolution time units.  For example, with a simulation of 200 time units and a time_resolution of 5 time units, we end up with 40 mini-batches.  The statistics produced are the x values corresponding to each mini-batch, and the counts, means, medians, 95th percentile, and 99th percentile in each mini-batch.
plot_counts_means_q95 -- Plots superimposed counts, means, and 95th percentiles for two mini-batch sets coming from two simulations.
compare_scenarios -- Combines the above two functions to produce comparison plots from two simulations.
Can you write Python code for it?

# %load report_resp_times.py
from typing import TYPE_CHECKING, Sequence, Tuple
import functools as ft
from collections import OrderedDict

import matplotlib.pyplot as plt
from livestats import livestats

if TYPE_CHECKING:
    from serversim import UserGroup

def minibatch_resp_times(time_resolution, grp):
    # type: (float, UserGroup) -> Tuple[Sequence[float], Sequence[float], Sequence[float], Sequence[float], Sequence[float], Sequence[float]]
    quantiles = [0.5, 0.95, 0.99]

    xys = [(int(svc_req.time_dict["submitted"]/time_resolution),
            svc_req.time_dict["completed"] - svc_req.time_dict["submitted"])
           for (_, svc_req) in grp.svc_req_log
           if svc_req.is_completed]

    def ffold(map_, p):
        x, y = p
        if x not in map_:
            map_[x] = livestats.LiveStats(quantiles)
        map_[x].add(y)
        return map_

    xlvs = ft.reduce(ffold, xys, dict())

    xs = xlvs.keys()
    xs.sort()

    counts = [xlvs[x].count for x in xs]
    means = [xlvs[x].average for x in xs]
    q_50 = [xlvs[x].quantiles()[0] for x in xs]
    q_95 = [xlvs[x].quantiles()[1] for x in xs]
    q_99 = [xlvs[x].quantiles()[2] for x in xs]

    return xs, counts, means, q_50, q_95, q_99

def plot_counts_means_q95(quantiles1, quantiles2):

    x = quantiles1[0]  # should be same as quantiles2[0]

    counts1 = quantiles1[1]
    counts2 = quantiles2[1]

    means1 = quantiles1[2]
    means2 = quantiles2[2]

    q1_95 = quantiles1[4]
    q2_95 = quantiles2[4]

    # Plot counts
    plt.plot(x, counts1, color='b', label="Counts 1")
    plt.plot(x, counts2, color='r', label="Counts 2")
    plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
    plt.xlabel("Time buckets")
    plt.ylabel("Throughput")
    plt.show()

    # Plot averages and 95th percentiles

    plt.plot(x, means1, color='b', label="Means 1")
    plt.plot(x, q1_95, color='c', label="95th Percentile 1")

    plt.plot(x, means2, color='r', label="Means 2")
    plt.plot(x, q2_95, color='m', label="95th Percentile 2")

 ...


I am working on the file ".ipynb_checkpoints/TutorialAndExample-checkpoint.ipynb".
The first task is:
Random number generator seed
We set the random number generator seed to a known value to produce repeatable simulations.  Comment-out this line to have a different system-generated seed every time the simulations are executed.
Can you write Python code for it?


I am working on the file ".ipynb_checkpoints/TutorialAndExample-checkpoint.ipynb".
The first task is:
Repeating above comparison to illustrate variability of results.
Can you write Python code for it?

rand_state = random.getstate()
sc1 = simulate_deployment_scenario(num_users=720, weight1=2, weight2=1,
                                   server_range1=range(0, 10), server_range2=range(0, 10))
random.setstate(rand_state)
sc2 = simulate_deployment_scenario(num_users=720, weight1=2, weight2=1,
                                   server_range1=range(0, 8), server_range2=range(8, 10))
compare_scenarios(sc1, sc2)


I am working on the file ".ipynb_checkpoints/TutorialAndExample-checkpoint.ipynb".
The first task is:
Simulation 1
In the first scenario, there are 10 servers which are shared by both services.  In the second scenario, there are 10 servers, of which 8 are allocated to the first service and 2 are allocated to the second service.  This allocation is proportional to their respective loads.
Can you write Python code for it?


I am working on the file ".ipynb_checkpoints/Tyrosine Phosphorylation Example-checkpoint.ipynb".
The first task is:
Next we are going to load our data and generate random negative data aka gibberish data. The clean data files has negatives created from the data sets pulled from phosphoELM and dbptm.
In generate_random_data the amino acid parameter represents the amino acid being modified aka the target amino acid modification, the float being passed through is multiplier. For example we use .5 here, that means that .5 * number of data points = random negatives generated.
Can you write Python code for it?

y = Predictor()
y.load_data(file="Data/Training/clean_Y.csv")


I am working on the file ".ipynb_checkpoints/Tyrosine Phosphorylation Example-checkpoint.ipynb".
The first task is:
Next we can check against the benchmarks pulled from dbptm.
Can you write Python code for it?


I am working on the file ".ipynb_checkpoints/Tyrosine Phosphorylation Example-checkpoint.ipynb".
The first task is:
Want to explore the data some more, easily generate PCA and TSNE diagrams of the training set.
Can you write Python code for it?


I am working on the file ".ipynb_checkpoints/UCL-adult-vectorizer-checkpoint.ipynb".
The first task is:
Adult dataset
Now we work on the Adult UCL dataset.
This is a popular dataset with the following data about a collection of adults:
Header
age: continuous.
workclass: Private, Self-emp-not-inc, Self-emp-inc, Federal-gov, Local-gov, State-gov, ithout-pay, Never-worked.
fnlwgt: continuous.
education: Bachelors, Some-college, 11th, HS-grad, Prof-school, Assoc-acdm, Assoc-voc, 9th, 7th-8th, 12th, Masters, 1st-4th, 10th, Doctorate, 5th-6th, Preschool.
education-num: continuous.
marital-status: Married-civ-spouse, Divorced, Never-married, Separated, Widowed, Married-spouse-absent, Married-AF-spouse.
occupation: Tech-support, Craft-repair, Other-service, Sales, Exec-managerial, Prof-specialty, Handlers-cleaners, Machine-op-inspct, Adm-clerical, Farming-fishing, Transport-moving, Priv-house-serv, Protective-serv, Armed-Forces.
relationship: Wife, Own-child, Husband, Not-in-family, Other-relative, Unmarried.
race: White, Asian-Pac-Islander, Amer-Indian-Eskimo, Other, Black.
sex: Female, Male.
capital-gain: continuous.
capital-loss: continuous.
hours-per-week: continuous.
native-country: United-States, Cambodia, England, Puerto-Rico, Canada, Germany, Outlying-US(Guam-USVI-etc), India, Japan, Greece, South, China, Cuba, Iran, Honduras, Philippines, Italy, Poland, Jamaica, Vietnam, Mexico, Portugal, Ireland, France, Dominican-Republic, Laos, Ecuador, Taiwan, Haiti, Columbia, Hungary, Guatemala, Nicaragua, Scotland, Thailand, Yugoslavia, El-Salvador, Trinadad&Tobago, Peru, Hong, Holand-Netherlands.
class: >50K, <=50K
Can you write Python code for it?

#Import data from files
import pandas as pd

df = pd.read_csv('https://archive.ics.uci.edu/ml/'
        'machine-learning-databases/adult/adult.data', header=None).head(2000)

df.columns = ["age","workclass","fnlwgt","education","education-num","marital-status","occupation",
              "relationship","race","sex","capital-gain","capital-loss","hours-per-week","native-country","class"]
#nfeature=len(df.columns)
#print("Number of features: "+str(nfeature-1))
#nfeature=14
df.head(5)

df.shape


I am working on the file ".ipynb_checkpoints/UCL-adult-vectorizer-checkpoint.ipynb".
The first task is:
Precision score
Use sklearn to plot the precision score.
This time, I use the test dataset.
The precision is the ratio tp / (tp + fp) where tp is the number of true positives and fp the number of false positives. The precision is intuitively the ability of the classifier not to label as positive a sample that is negative.
The best value is 1 and the worst value is 0.
Can you write Python code for it?

from sklearn.metrics import classification_report

estimator.fit(X_train, y_train)

y_pred = estimator.predict(X_test)

ntest=y_pred.size

#Total of true positives:
from sklearn.metrics import precision_score

#Precision score for test dataset:
print("Precision score for test dataset: \n")
precision_score(y_test, y_pred, average='micro')


I am working on the file ".ipynb_checkpoints/UCL-adult-vectorizer-checkpoint.ipynb".
The first task is:
We find that most features are not correlated with the class:
Lets make a logistic regression using the top ten features above:
Plot learning curves
Use training and cross validation datasets.
Can you write Python code for it?

#Routine to plot learning curve from sklearn
print(__doc__)

import numpy as np
import matplotlib.pyplot as plt
from sklearn.naive_bayes import GaussianNB
from sklearn.datasets import load_digits
from sklearn.model_selection import learning_curve
from sklearn.model_selection import ShuffleSplit
from sklearn import svm, datasets

def plot_learning_curve(estimator, title, X, y, ylim=None, cv=None,
                        n_jobs=1, train_sizes=np.linspace(.1, 1.0, 5)):
    """
    Generate a simple plot of the test and training learning curve.

    Parameters
    ----------
    estimator : object type that implements the "fit" and "predict" methods
        An object of that type which is cloned for each validation.

    title : string
        Title for the chart.

    X : array-like, shape (n_samples, n_features)
        Training vector, where n_samples is the number of samples and
        n_features is the number of features.

    y : array-like, shape (n_samples) or (n_samples, n_features), optional
        Target relative to X for classification or regression;
        None for unsupervised learning.

    ylim : tuple, shape (ymin, ymax), optional
        Defines minimum and maximum yvalues plotted.

    cv : int, cross-validation generator or an iterable, optional
        Determines the cross-validation splitting strategy.
        Possible inputs for cv are:
          - None, to use the default 3-fold cross-validation,
          - integer, to specify the number of folds.
          - An object to be used as a cross-validation generator.
          - An iterable yielding train/test splits.

        For integer/None inputs, if ``y`` is binary or multiclass,
        :class:`StratifiedKFold` used. If the estimator is not a classifier
        or if ``y`` is neither binary nor multiclass, :class:`KFold` is used.

        Refer :ref:`User Guide <cross_validation>` for the various
        cross-validators that can be used here.

    n_jobs : integer, optional
        Number of jobs to run in parallel (default 1).
   ...


I am working on the file ".ipynb_checkpoints/UX_analytics-checkpoint.ipynb".
The first task is:
Settings
Can you write Python code for it?

# Time period 17th Jan - 16th April (arbitrary )

# API credentials
# Email address 705762800217-compute@developer.gserviceaccount.com
# Key IDs 948ee8e2a420ef14a5d5a29bd35104fe2f1e6ed4

# open file. It is requested via API explorer using request parameters:

#Account: TMRW Tech Hub
#Property: TMRW
#View: All Web Site Data
#ids: ga:123303369
#start-date: 2017-01-15
#end-date: yesterday

#metrics
#ga:sessions
#ga:percentNewSessions
#ga:bounceRate
#ga:pageviewsPerSession
#ga:avgSessionDuration
#ga:goal1ConversionRate
#ga:goal1Completions

#dimensions
#ga:city
#ga:userAgeBracket

#sort
#ga:goal1ConversionRate


I am working on the file ".ipynb_checkpoints/UX_analytics-checkpoint.ipynb".
The first task is:
Skipped step:
Check statistical validity
Filter off spam traffic and own dev/marketing IPs
Algorithm of actions:

Determine buckets

Are there lines that can be grouped by each metric:
Bounce Rate
Avg. Session Duration
Goal 1 Completions
Goal 1 Conversion Rate
Pages / Session
Acceptable spread = 10%
Take Key_metrics and check volume of traffic and conversions. Which is the most extreme?
Conversion bucket = new array
TMRW_users_city.max=TMRW_users_city.max()

Define key metrics
Key_metrics = location/age. Can be location/gender or age/gender also. For them other API call needs to be made.

Open file

Visualise
Can you write Python code for it?

# Open file
TMRW_users= pd.read_csv("files/TMRW_geo_loc_API.csv")
#TMRW_users[TMRW_users.Age=='55-64']
TMRW_users

# rename columns
TMRW_users.columns=['city','age', 'new_sessions','sessions','bounce_rate','asd','goal1','goal1CR','PPS']
TMRW_users=TMRW_users.sort_values('goal1CR')

TMRW_users_filter = TMRW_users[TMRW_users.sessions > 80]
TMRW_users_filter

TMRW_users_filter.describe()
# will need to convert dnumbers into tim
#TMRW_users_filter_new =pd.to_datetime(TMRW_users_filter['asd'], format='%H:%M')

# Check if the number of sessions is enough for analysis

if TMRW_users.sessions.sum() < 80:
    print("Error")

#if sessions are too small remove
#if one conversion - remove

# 1. Too small sessions
TMRW_users.describe().loc['mean', 'sessions']

#algo for bucketing into varios secsions:

#buckets by goal1CR

TMRW_users_goal1CR = TMRW_users_filter.nlargest(3, 'goal1CR')
TMRW_users_goal1CR_gCity = TMRW_users_goal1CR.groupby(['city']).mean() #this is not too correct- average of proportion, but ok for now
TMRW_users_goal1CR_gAge = TMRW_users_goal1CR.groupby(['age']).mean()
TMRW_users_goal1CR_gCity

TMRW_users_goal1CR.loc[11,'age']

TMRW_users_goal1CR_gAge.loc[: , 'goal1']

TMRW_users_goal1CR.loc[]
'age'.max

# The most converting audience
print(+" is most converting Demographic category")

#x = TMRW_users_goal1CR_gAge.index

y = TMRW_users_goal1CR.[: , 'goal1CR']

plt.hist(y)

plt.title("Top converting buckets")
plt.xlabel("Conv rate")
plt.ylabel("Frequency")

plt.show()

TMRW_users_bounce_rate = TMRW_users_filter.nlargest(3, 'bounce_rate')
TMRW_users_bounce_rate
#if the_largest traffic source = ('not_set') then output error ''

TMRW_users_PPS = TMRW_users_filter.nlargest(3, 'PPS')
TMRW_users_PPS

TMRW_users_agcities=TMRW_users.groupby(["city"]).sum()
TMRW_users_agcities

selected=TMRW_users_agcities.loc[:,'Goal 1 Completions']
selected

labels = selected.index
sizes = selected
# colours are taken from http://tools.medialab.sciences-po.fr/iwanthue/
colors = ['#1f394d','#2a7585', '#163c45',...


I am working on the file ".ipynb_checkpoints/Untitled-Copy1-checkpoint.ipynb".
The first task is:
Assign driver in group based on degree and distance
Can you write Python code for it?

# Get degree for each customer in the cluster

def getDegree(data):
    # distance
    # center long lat (start of routing)
    center_latitude = #Tiap Kitchen
    center_longitude = #Tiap Kitchen
    degrees = []
    degree = 0
    # For each row in the column,
    for row in data['longitude']:
        degrees = np.rint(np.rad2deg(np.arctan2((data['latitude']-center_latitude),(data['longitude']-center_longitude))))
       #center di pulogadung
    data['degrees'] = degrees
    return data

# Assign driver dari kitchen ke customer berdasarkan degree dan jarak
# Priority utama berdasarkan degree jadi gaada driver yang deket doang
# Tapi belum dipikir gimana bisa optimize waktu harus satu jam max, tapi seenggaknya driver udah agak rata jaraknya
# Kasus khusus apabila yg degree nya kecil jaraknya jauh banget, dia driver baru.

# BELUM SELESAI YANG INI


I am working on the file ".ipynb_checkpoints/Untitled-Copy1-checkpoint.ipynb".
The first task is:
Overview
Kunci utama dari Efisiensi pengiriman adalah customer harus terassign ke kitchen yang terdekat dulu.
Kami melakukannya dengan menSort customer dari jarak yang paling jauh dari titik pusat customer (sum/total lat long).
Solusi tersebut belum optimal,tapi mendekati.
Solusi optimal = Sort dari Outermost customer.
Customer kemudian di assign ke kitchen terdekatnya, apabila sudah full maka diassign ke kitchen kedua terdekat, dst.
Sehingga bisa didapat group berupa customer yang terassign ke suatu kitchen.
Driver kemudian di assign per group berdasarkan degree dan jarak.
Di assign tidak hanya berdasarkan jarak untuk mengoptimalkan waktu pengiriman selama 1 jam.
Grouping customer to the best kitchen
Can you write Python code for it?

# Find center point of customer, buat nyari
# long
long_centroid = sum(customer['long'])/len(customer)
# lat
lat_centroid = sum(customer['lat'])/len(customer)

# Find distance from customer point to central customer point
customer['distSort'] = np.sqrt( (customer.long-long_centroid)**2 + (customer.lat-lat_centroid)**2)

# Sort by longest distance
customer = data.sort_values(['distSort'], ascending=False)

# Data already sorted from outermost customer
# For each row in the column,assign customer to the the nearest kitchen,
# if the kitchen already full, assign customer to the second nearest kitchen and so on.

# BELUM SELESAI YANG INI
clusters = []

for row in customer['distSort']:
    #clusters.append(cluster)

data['cluster'] = clusters

# Data visulization customer assigned to its kitchen

def visualize(data):
    x = data['long']
    y = data['lat']
    Cluster = data['cluster']

    fig = plt.figure()
    ax = fig.add_subplot(111)
    scatter = ax.scatter(x,y,c=Cluster, cmap=plt.cm.Paired, s=10, label='customer')
    ax.scatter(kitchen['long'],kitchen['lat'], s=10, c='r', marker="x", label='second')
    ax.set_xlabel('longitude')
    ax.set_ylabel('latitude')
    plt.colorbar(scatter)

    fig.show()

# Visualization Example customer assigned to kitchen (without following constraint)
# THIS IS ONLY EXAMPLE
y = kitchen['kitchenName']
X = pd.DataFrame(kitchen.drop('kitchenName', axis=1))

clf = NearestCentroid()
clf.fit(X, y)
pred = clf.predict(customer)

customer1['cluster'] = pd.Series(pred, index=customer1.index)
customer['cluster'] = pd.Series(pred, index=customer.index)

visualize(customer)

# Count customer order assigned to Kitchen
dapurMiji = (customer1.where(customer1['cluster'] == 0))['qtyOrdered'].sum()
dapurNusantara = (customer1.where(customer1['cluster'] == 1))['qtyOrdered'].sum()
familiaCatering = (customer1.where(customer1['cluster'] == 2))['qtyOrdered'].sum()
pondokRawon = (customer1.where(customer1['cluster'] == 3))['qtyOrdered'].sum()
roseCatering =...


I am working on the file ".ipynb_checkpoints/Untitled-checkpoint.ipynb".
The first task is:
Finally, convert the response data into a dataframe:
Can you write Python code for it?

out_df=pd.DataFrame(index=docFreq_dict.keys())
for resp_dict in responses_ls:
    out_df[resp_dict['idx']]=pd.Series(resp_dict['tfidf'], index=out_df.index)

out_df=out_df.fillna(0).T
out_df.head()

accuracy_ss=pd.Series({r['idx']:r['accuracy'] for r in responses_ls})
accuracy_ss.head()


I am working on the file ".ipynb_checkpoints/Untitled-checkpoint.ipynb".
The first task is:
Next, we need to carry out whatever analysis we want on the answers. In this case, we'll split on whitespace, convert to lower case, and strip punctuation. Feel free to redefine the to_tokens function to do whatever analysis you prefer.
Can you write Python code for it?

from string import punctuation

def to_tokens(textIn):
    '''Convert the input textIn to a list of tokens'''
    tokens_ls=[t.lower().strip(punctuation) for t in textIn.split()]
    # remove any empty tokens
    return [t for t in tokens_ls if t]

str='"Help!" yelped the banana, who was obviously scared out of his skin.'
print(str)
print(to_tokens(str))


I am working on the file ".ipynb_checkpoints/Untitled-checkpoint.ipynb".
The first task is:
Now add a tf.idf dict to each of the responses:
Can you write Python code for it?

for resp_dict in responses_ls:
    resp_dict['tfidf']={t:resp_dict['tokens'].count(t)/docFreq_dict[t] for t in resp_dict['tokens']}

responses_ls[6]


I am working on the file ".ipynb_checkpoints/Untitled-checkpoint.ipynb".
The first task is:
Now we can set up a document frequency dict:
Can you write Python code for it?

docFreq_dict={}

for t in vocab_set:
    docFreq_dict[t]=len([resp_dict for resp_dict in responses_ls if t in resp_dict['tokens']])

docFreq_dict


I am working on the file ".ipynb_checkpoints/Untitled-checkpoint.ipynb".
The first task is:
The reference answers are the third daughter node of the tree:
Can you write Python code for it?


I am working on the file ".ipynb_checkpoints/VPEqualZero-checkpoint.ipynb".
The first task is:
$f_{2}(c,p) = \dfrac{1}{2}r_{c}c^{2}+\dfrac{1}{4}u_{c}c^{4}+\dfrac{1}{6}v_{c}c^{6}+\dfrac{1}{2}r_{p}p^{2}+\dfrac{1}{4}u_{p}p^{4}-\gamma cp-\dfrac{1}{2}ec^{2}p^{2}-Ep$
Can you write Python code for it?


I am working on the file ".ipynb_checkpoints/VPEqualZero-checkpoint.ipynb".
The first task is:
Rescaling
Can you write Python code for it?

P, C, w, rho, beta, tau, Epr = symbols('P C w rho beta tau E^{\prime}')

fP = nsimplify(f2.subs(p,w*Pprm))
fP

fP = nsimplify(fP.subs(w,sqrt(r_p/u_p)))
fP

fP = expand(fP/(r_p**2/u_p),r_p)
fP

fP = fP.subs([(E,Epr/sqrt(u_p)*r_p**(3/2)),(c,cprm*sqrt(r_p/e))])
fP

fP = fP.subs([(cprm,thetaprm),(r_c,rtheta*e*r_p/u_p),(u_c,gammaprm*e**2/u_p),(v_c,rhoprm*e**3/(u_p*r_p)),(gamma,betaprm*r_p*sqrt(e/u_p))])
fP

fhelp = nsimplify((1/2)*rtheta*thetaprm**2+(1/4)*gammaprm*thetaprm**4+(1/6)*rhoprm*thetaprm**6+(1/2)*Pprm**2+(1/4)*Pprm**4
        +(1/6)*sigmaprm*Pprm**6-betaprm*thetaprm*Pprm-(1/2)*Pprm**2*thetaprm**2-Eprm*Pprm)
fhelp

Ep = solve(fhelp.diff(Pprm),Eprm)[0]
Ep

fhelp.diff(thetaprm)

P_min = solve(fhelp.diff(thetaprm),Pprm)[0]
P_min

Ep = Ep.subs(Pprm,P_min)
Ep

series(Ep,thetaprm,n=7)

# Series expansion of $E$ out to $\mathcal{O(\theta^{\prime 7})}$:

# $\beta E = \theta^{\prime}\left(-\beta^{\prime 2} + r_{\theta}^{\prime}\right) + \theta^{\prime 3}\left(\gamma^{\prime} - r_{\theta}^{\prime} + \dfrac{r_{\theta}^{\prime 3}}{\beta^{\prime 2}} - \dfrac{r_{\theta}^{\prime 2}}{\beta^{\prime 2}}\right) + \theta^{\prime 5}\left(-\gamma^{\prime} + \rho^{\prime} + \dfrac{3\gamma^{\prime}r_{\theta}^{\prime 2}}{\beta^{\prime 2}} - \dfrac{2\gamma^{\prime}r_{\theta}}{\beta^{\prime 2}} + \dfrac{r_{\theta}^{\prime 2}}{\beta^{\prime 2}} - \dfrac{3r_{\theta}^{\prime 4}}{\beta^{\prime 4}} + \dfrac{2r_{\theta}^{\prime 3}}{\beta^{\prime 4}}\right)$

# The coefficients of $E$ i.t.o. $a$ where $r_{\theta}^{\prime} = \beta^{\prime 2} + a$:

# $B(a) = \dfrac{a^{3}}{\beta^{\prime 2}} + a^{2}\left(3 - \dfrac{1}{\beta^{\prime 2}}\right) + a(3\beta^{\prime 2} - 3) + \beta^{\prime 4} - 2\beta^{\prime 2} + \gamma^{\prime}$

# $C(a) = -\dfrac{3a^{4}}{\beta^{\prime 4}} + a^{3}\left(\dfrac{2}{\beta^{\prime 4}} - \dfrac{12}{\beta^{\prime 2}}\right) + a^{2}\left(\dfrac{3\gamma^{\prime}}{\beta^{\prime 2}} - 18 + \dfrac{7}{\beta^{\prime 2}}\right) + a\left(6\gamma^{\prime} - 12\beta^{\prime 2} - \dfrac{2\gamma^{\prime}}{\beta^{\prime 2}} +...


I am working on the file ".ipynb_checkpoints/WebScrapingnDataAnalysis-checkpoint.ipynb".
The first task is:
Best Movie Run time
Let's plot a graph to see the movie run time trend from 1955 thru 2015
Can you write Python code for it?


I am working on the file ".ipynb_checkpoints/WebScrapingnDataAnalysis-checkpoint.ipynb".
The first task is:
Conclusion :

Movies with Ratings greater than 7
Run time more than 2hrs
Category Drama & Musical are most likely to be selcted for Best Picture
Can you write Python code for it?

import pandas as pd
data = 'Director: Satyen Bose'
data.replace('Director: ','')

headers = ['Movie Name','Run Time','Rating','Actor','Actor1']
pd.DataFrame([data],columns=headers)

df.plot(x=df.Year,y=['RunTime']);


I am working on the file ".ipynb_checkpoints/WebScrapingnDataAnalysis-checkpoint.ipynb".
The first task is:
Data in Pandas Dataframe
<img src="./img/Dataframe.png">
Data is consumed in a Pandas Dataframe, Which is more convenient way to perform data analysis,manipulation or aggregation
Can you write Python code for it?

# create dataframe
df = pd.DataFrame.from_dict(result,orient='index')
df = df[['Year', 'Movie Name', 'Rating', 'Votes','Genre','RunTime','Director']]
df.index = np.arange(1, 62)
df.head(10)


I am working on the file ".ipynb_checkpoints/WebScrapingnDataAnalysis-checkpoint.ipynb".
The first task is:
Getting Data
Function to extract the data from Web using Selenium
Can you write Python code for it?

def ExtractText(Xpath):
    textlist=[]
    if(Xpath=='Movies_Director_Xpath'):
        for item in range(1,123,2):
            textlist.append(driver.find_element_by_xpath(datatbl['data'][Xpath]+'[%d]'%item).text)
    else:
        [textlist.append(item.text) for item in driver.find_elements_by_xpath(datatbl['data'][Xpath])]
    return textlist


I am working on the file ".ipynb_checkpoints/WebScrapingnDataAnalysis-checkpoint.ipynb".
The first task is:
How does data looks now?
Is this Data is in the correct format to perform data manipulation?
The individual movie related data is stored in a Python List, it's hard to corelated the data attributes with the respective Movies
Can you write Python code for it?

# Movie Name List & Ratings
print(datarepo[0][:5])
print("")
print(datarepo[3][:5])


I am working on the file ".ipynb_checkpoints/WebScrapingnDataAnalysis-checkpoint.ipynb".
The first task is:
Initial Setup and Launch the browser to open the URL
Can you write Python code for it?

url = "http://www.imdb.com/list/ls061683439/"
with open('./img/filmfare.json',encoding="utf-8") as f:
    datatbl = json.load(f)
driver = webdriver.Chrome(datatbl['data']['chromedriver'])
driver.get(url)


I am working on the file ".ipynb_checkpoints/WebScrapingnDataAnalysis-checkpoint.ipynb".
The first task is:
Let's extract all the required data like Ratings,Votes,Genre, Year of Release for the Best Movies
Can you write Python code for it?

#Extracting Data from Web
Movies_Votes,Movies_Name,Movies_Ratings,Movies_RunTime=[[] for i in range(4)]
datarepo = [[]]*5
Xpath_list = ['Movies_Name_Xpath','Movies_Rate_Xpath','Movies_Runtime_Xpath','Movies_Votes_Xpath',
              'Movies_Director_Xpath']

for i in range(5):
    if(i==3):
        driver.find_element_by_xpath(datatbl['data']['listview']).click()
    if(i==4):
        driver.find_element_by_xpath(datatbl['data']['detailview']).click()
    datarepo[i] = ExtractText(Xpath_list[i])

driver.quit()


I am working on the file ".ipynb_checkpoints/WebScrapingnDataAnalysis-checkpoint.ipynb".
The first task is:
Let's see now how the data in dictionary looks like?
Can you write Python code for it?

import pprint as pp
pp = pprint.PrettyPrinter(depth=10)
pp.pprint(result[0])

print(json.dumps((result[0]),indent=4))


I am working on the file ".ipynb_checkpoints/WebScrapingnDataAnalysis-checkpoint.ipynb".
The first task is:
Let's use some of the Pandas functions now and start the Analysis
No. of rows with missing values(NaN)
Can you write Python code for it?

nans = df.shape[0] - df.dropna().shape[0]
print('%d rows have missing values' % nans)


I am working on the file ".ipynb_checkpoints/WebScrapingnDataAnalysis-checkpoint.ipynb".
The first task is:
Mean of the Movie Run Time
Can you write Python code for it?


I am working on the file ".ipynb_checkpoints/WebScrapingnDataAnalysis-checkpoint.ipynb".
The first task is:
Movie Ratings Visualization using Bar Graph
Can you write Python code for it?

Rating_Histdic = defaultdict(dict)

Rating_Histdic['Btwn 6&7'] = df[(df['Rating']>=6)&(df['Rating']<7)]['Rating'].count()
Rating_Histdic['GTEQ 8'] = df[(df['Rating']>=8)]['Rating'].count()
Rating_Histdic['Btwn 7 & 8'] = df[(df['Rating']>=7)&(df['Rating']<8)]['Rating'].count()

plt.bar(range(len(Rating_Histdic)), Rating_Histdic.values(), align='center',color='brown',width=0.4)
plt.xticks(range(len(Rating_Histdic)), Rating_Histdic.keys(), rotation=25);


I am working on the file ".ipynb_checkpoints/WebScrapingnDataAnalysis-checkpoint.ipynb".
The first task is:
Movies with Highest Ratings
<img src="./img/Ratings.png">
The top five movies with Maximum Rating since 1955
Can you write Python code for it?

#Highest Rating Movies
df1=df.sort_values('Rating',ascending=[False]).head(5)
df1.index = np.arange(1, 6)
df1


I am working on the file ".ipynb_checkpoints/WebScrapingnDataAnalysis-checkpoint.ipynb".
The first task is:
Movies with Lowest Ratings
Can you write Python code for it?

df1=df.sort_values('Rating',ascending=[True]).head(5)
df1.index = np.arange(1, 6)
df1


I am working on the file ".ipynb_checkpoints/WebScrapingnDataAnalysis-checkpoint.ipynb".
The first task is:
Now let's look at the data and see how it looks like
Can you write Python code for it?


I am working on the file ".ipynb_checkpoints/WebScrapingnDataAnalysis-checkpoint.ipynb".
The first task is:
Rating Trend for Best Movies from last 65 years
Can you write Python code for it?


I am working on the file ".ipynb_checkpoints/WebScrapingnDataAnalysis-checkpoint.ipynb".
The first task is:
Replace NaN with Mean value of that Column
Can you write Python code for it?

df=df.fillna(int(df['RunTime'].mean()))
df[0:5]

df.info()


I am working on the file ".ipynb_checkpoints/WebScrapingnDataAnalysis-checkpoint.ipynb".
The first task is:
Store Data in a  Python Dictionary
<img src="./img/pydic.png">
The data is stored in a python dictionary which is more structured way to store the data here and all the movie attributes are now linked with the respective movie
Can you write Python code for it?

# Result in a Python Dictionary
Years=range(2015,1954,-1)
result = defaultdict(dict)
for i in range(0,len(datarepo[0])):
    result[i]['Movie Name']= datarepo[0][i]
    result[i]['Year']= Years[i]
    result[i]['Rating']= datarepo[1][i]
    result[i]['Votes']= datarepo[3][i]
    result[i]['RunTime']= datarepo[2][i]
    result[i]['Genre']= datatbl['data']['Genre'][i]
    result[i]['Director']= datarepo[4][i]


I am working on the file ".ipynb_checkpoints/Week4_lecture-checkpoint.ipynb".
The first task is:
Now we know where distractors can be placed. But we don't want to put a distractor at each grid position, but draw a number of them (say 10) at random. One way to do this is the 'shuffle' the array, and then select the 10 first elements.
Can you write Python code for it?

import time # Used to animate below
nSelect = 10

# Randomly change the positions of the locations in the array
np.random.shuffle(xy_all)

# Plot the result (looks much better!)
plt.scatter(xy_all[:nSelect, 0], xy_all[:nSelect, 1], s=distractor_size ,c='b',marker='v')
plt.axis([0, image_size[0], 0, image_size[1]])
plt.show()


I am working on the file ".ipynb_checkpoints/all-datasets-together-checkpoint.ipynb".
The first task is:
Build up sensor to pvoutput model
Can you write Python code for it?

from datetime import datetime,timedelta, time
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from data_helper_functions import *
from IPython.display import display
pd.options.display.max_columns = 999
%matplotlib inline

#iterate over datetimes:
mytime = datetime(2014, 4, 1, 13)
times = make_time(mytime)

# Now that we can call data up over any datetime and we have a list of interested datetimes,
# we can finally construct an X matrix and y vector for regression.

sensor_filefolder = 'data/sensor_data/colorado6months/'
pvoutput_filefolder = 'data/pvoutput/pvoutput6months/'

X = []
y = []

for desired_datetime in times:

    try: #something wrong with y on last day
        desired_date = (desired_datetime - timedelta(hours=6)).date() #make sure correct date
        desired_date = datetime.combine(desired_date, time.min) #get into datetime format

        sensor_filename = find_file_from_date(desired_date, sensor_filefolder)
        df_sensor = return_sensor_data(sensor_filename, sensor_filefolder).ix[:,-15:-1]
        df_sensor[df_sensor.index == desired_datetime]

        pvoutput_filename = find_file_from_date(desired_date, pvoutput_filefolder)
        df_pvoutput = return_pvoutput_data(pvoutput_filename, pvoutput_filefolder)

        y.append(df_pvoutput[df_pvoutput.index == desired_datetime].values[0][0])
        X.append(df_sensor[df_sensor.index == desired_datetime].values[0])
    except:
        pass

X = np.array(X)
y = np.array(y)

print X.shape
print y.shape


I am working on the file ".ipynb_checkpoints/all-datasets-together-checkpoint.ipynb".
The first task is:
Extra Trees!
Can you write Python code for it?

from sklearn.ensemble import ExtraTreesRegressor
etr = ExtraTreesRegressor(oob_score=True, bootstrap=True,
                          n_jobs=-1, n_estimators=1000) #nj_obs uses all cores!
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=99)

etr.fit(X_train, y_train)

print etr.score(X_test,y_test)
print etr.oob_score_

y_pred = etr.predict(X_test)

from random import randint
val = randint(0,y_test.shape[0])
print y_pred[val]
print y_test[val]

print X.shape
print y.shape


I am working on the file ".ipynb_checkpoints/all-datasets-together-checkpoint.ipynb".
The first task is:
Linear model
Can you write Python code for it?

#now do a linear model and compare:
from sklearn.linear_model import LinearRegression
lr = LinearRegression()
lr.fit(X_train,y_train)
lr.score(X_test,y_test)

sorted_mask = np.argsort(lr.coef_)

for i in zip(df_sensor.columns.values,lr.coef_[sorted_mask])[::-1]:
    print i

df_sensor.ix[:,-15:-1].head() #selects photometer and AOD,
# useful in next iteration of using sensor data to fit


I am working on the file ".ipynb_checkpoints/all-datasets-together-checkpoint.ipynb".
The first task is:
Save this thing and try it out on the simulated sensors!
Can you write Python code for it?

from sklearn.externals import joblib
joblib.dump(etr, 'data/sensor-to-power-model/sensor-to-power-model.pkl')

np.savez_compressed('data/y.npz',y=y) #save y


I am working on the file ".ipynb_checkpoints/all-datasets-together-checkpoint.ipynb".
The first task is:
When only keeping the photometer data, random forest and linear model do pretty similar.  When I added all of the sensor instruments to the fit, rfr scored 0.87 and lr scored negative!
Also, I threw away the mysterious "Research 2" sensor, that was probably just a solar panel!  I asked NREL what it is, so we'll see.  If it turns out to be a solar panel, then I can do some feature engineering with the sensor data by simulating a solar panel!
Neural Net Exploration
Can you write Python code for it?

import pandas as pd
import numpy as np
from sklearn.preprocessing import scale
from lasagne import layers
from lasagne.nonlinearities import  softmax, rectify, sigmoid, linear, very_leaky_rectify, tanh
from lasagne.updates import nesterov_momentum, adagrad, momentum
from nolearn.lasagne import NeuralNet
import theano
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import StandardScaler

y = y.astype('float32')
x = X.astype('float32')
scaler = StandardScaler()
scaled_x = scaler.fit_transform(x)
x_train, x_test, y_train, y_test = train_test_split(scaled_x, y, test_size = 0.2, random_state = 12)

nn_regression = NeuralNet(layers=[('input', layers.InputLayer),
#                                   ('hidden1', layers.DenseLayer),
#                                    ('hidden2', layers.DenseLayer),
                                  ('output', layers.DenseLayer)
                                ],

               # Input Layer
               input_shape=(None, x.shape[1]),

               # hidden Layer
#                hidden1_num_units=512,
#                hidden1_nonlinearity=softmax,

             # hidden Layer
#                hidden2_num_units=128,
#                hidden2_nonlinearity=linear,

              # Output Layer
              output_num_units=1,
              output_nonlinearity=very_leaky_rectify,

              # Optimization
              update=nesterov_momentum,
              update_learning_rate=0.03,#0.02
              update_momentum=0.8,#0.8
              max_epochs=600, #was 100

              # Others
              #eval_size=0.2,
              regression=True,
              verbose=0,
         )

nn_regression.fit(x_train, y_train)
y_pred = nn_regression.predict(x_test)
nn_regression.score(x_test, y_test)

val = 11
print y_pred[val][0]
print y_test[val]

plt.plot(y_pred,'ro')

plt.plot(y_test,'go')


I am working on the file ".ipynb_checkpoints/astrobao-checkpoint.ipynb".
The first task is:
2. Jeans Length and Jeans mass
Can you write Python code for it?

def Jeanslength_cgs(density, temperature, particlemass):
  """
  Calculate Jeans Length.

  Inputs:
     density: density, in  g cm^-3
     temperature: temperature in Kelvin
     particlemass: in g, to be used for calculating sound speed

  Return:
     Jeans length in cgs unit ( cm )
  """

  # defining physical constants
  kB_cgs      = 1.38064852e-16  # Boltzmann constant
  G_cgs       = 6.674e-8

  inputsize = size(density)
  if (inputsize ==1):

    A = 15.0 * kB_cgs * temperature
    B = 4.0 * math.pi * G_cgs * density * particlemass
    length = math.sqrt( A / B )

    return length

  else:
    out_array = np.arange(0, inputsize) * 0.0
    for id in list(range(0,inputsize)):
      A = 15.0 * kB_cgs * temperature
      B = 4.0 * math.pi * G_cgs * density[id] * particlemass
      length = math.sqrt( A / B )
      out_array[id] = length

    return out_array

def Jeansmass_cgs(density, temperature, particlemass):
  """
  Calculate Jeans mass.

  Inputs:
     density: density, in  g cm^-3
     temperature: temperature in Kelvin
     particlemass: in g, to be used for calculating sound speed

  Return:
     Jeans mass in cgs unit ( g )
  """

  inputsize = size(density)
  if (inputsize ==1):

    mass = (4.0 / 3.0) * math.pi  \
         * ( Jeanslength_cgs(density, temperature, particlemass) **3 ) \
         * density

    return mass

  else:
    out_array = np.arange(0, inputsize) * 0.0
    for id in list(range(0,inputsize)):
      out_array[id] = (4.0 / 3.0) * math.pi  \
         * ( Jeanslength_cgs(density[id], temperature, particlemass) **3 ) \
         * density[id]

    return out_array


I am working on the file ".ipynb_checkpoints/astrobao-checkpoint.ipynb".
The first task is:
3. Toomore Q parameter
Observatory related
Target source availability
Frequently used unit/coordinate transformation
Brightness tempreature
Plotting
Can you write Python code for it?

import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline


I am working on the file ".ipynb_checkpoints/astrobao-checkpoint.ipynb".
The first task is:
Motions
1. free-fall timescale
Can you write Python code for it?

def freefall_cgs(density):
  """
  Calculate free-fall timescale.

  Input:
     density: density, in g cm^-3

  Return:
     Free fall time ( seconds )
  """

  # defining physical constants
  G_cgs       = 6.674e-8

  inputsize = size(density)

  if (inputsize ==1):
    A = 3.0 * math.pi
    B = 32.0 * G_cgs * density
    time = math.sqrt( A / B )

    return time

  else:
    out_array = np.arange(0, inputsize) * 0.0
    for id in list(range(0,inputsize)):
      A = 3.0 * math.pi
      B = 32.0 * G_cgs * density[id]
      out_array[id] = math.sqrt( A / B )

    return out_array


I am working on the file ".ipynb_checkpoints/astrobao-checkpoint.ipynb".
The first task is:
Plot Jeans mass as a function of cloud particle number density
Can you write Python code for it?

# physical constants
mean_mol_weight = 2.76  # mean molecular weight
mole            =  6.02214129e23
parsec_to_cm    = 3.08567758e18
solar_mass_cgs  = 1.9891e33

# initializing arrays
number_density = np.arange(1, 10001, 1) * 1e3
output_array_cgs = np.arange(1, 10001, 1) * 0.0

# initializing plotting
fig = plt.figure(figsize=(9, 14))
plt.subplot(2, 1, 1)
plt.axis([4.0, 6.5, 0.01, 15])

# initial conditions
particlemass = mean_mol_weight / mole
density = number_density * mean_mol_weight / mole

temperature = 10.0
output_array_cgs = Jeansmass_cgs(density, temperature, particlemass) / solar_mass_cgs

line10K = plt.plot(np.log10( number_density ), output_array_cgs, \
          color = (0, 0, 1.0, 0.2), \
          linewidth=8, \
          label = '10 K')

temperature = 20.0
output_array_cgs = Jeansmass_cgs(density, temperature, particlemass) / solar_mass_cgs

line20K = plt.plot(np.log10( number_density ), output_array_cgs, \
          color = (0, 0.5, 0.5, 0.2), \
          linewidth=8, \
          label = '20 K')

temperature = 30.0
output_array_cgs = Jeansmass_cgs(density, temperature, particlemass) / solar_mass_cgs

line30K = plt.plot(np.log10( number_density ), output_array_cgs, \
          color = (0, 0.5, 0.0, 0.2), \
          linewidth=8, \
          label = '30 K')

temperature = 40.0
output_array_cgs = Jeansmass_cgs(density, temperature, particlemass) / solar_mass_cgs

line40K = plt.plot(np.log10( number_density ), output_array_cgs, \
          color = (0.5, 0.5, 0.0, 0.2), \
          linewidth = 8, \
          label = '40 K')

temperature = 50.0
output_array_cgs = Jeansmass_cgs(density, temperature, particlemass) / solar_mass_cgs

line50K = plt.plot(np.log10( number_density ), output_array_cgs, \
          color = (0.5, 0.0, 0.0, 0.2), \
          linewidth = 8, \
          label = '50 K')

plt.title('Jeans mass')
plt.xlabel('Log$_{10}$(Molecular gas number density [cm$^{-3}$])')
plt.ylabel('Jeans mass [$M_{\odot}$]')
plt.legend(loc=1)


I am working on the file ".ipynb_checkpoints/astrobao-checkpoint.ipynb".
The first task is:
Plot Planck function in cgs and mks unit
Can you write Python code for it?

freq_array   = np.arange(1, 2501) * 1e10  # frequency in Hz
output_array_mks = np.arange(1, 2501) * 0.0

# physical constants
cgsflux_to_Jy = 1e23
mksflux_to_Jy = 1e26

str_to_sqdegree    = 3282.80635
str_to_sqarcsecond = 3282.80635 * (3600.0 ** 2.0)

# initializing plotting
fig = plt.figure(figsize=(9, 14))
plt.subplot(2, 1, 1)
plt.axis([0.5, 4.9, -5, 5.2])

# plt.axis([2.0, 3.0, -1, 1])

# evaluate Planck function in CGS unit
temperature  = 15.0                  # Kelvin
output_array_cgs = Planckfunc_cgs(freq_array, temperature) \
                   * cgsflux_to_Jy / str_to_sqarcsecond

plt.plot(np.log10( freq_array / 1e9) , np.log10(output_array_cgs), \
         color = (0, 0, 1.0, 0.2),
         linewidth=8, label = '15 K')

# evaluate Planck function in MKS unit
TCMB  = 2.72548                  # Kelvin

for id in list(range(0, 2500)):

  output_array_mks[id] = Planckfunc_mks(freq_array[id], TCMB) * \
                                  mksflux_to_Jy / str_to_sqarcsecond

plt.plot(np.log10( freq_array / 1e9), np.log10(output_array_mks), \
          linestyle = 'dashed',
          color = (0.2, 0.6, 0, 0.4),
          linewidth =5, label = '$T_{CMB}$')
plt.plot(np.log10( freq_array / 1e9), np.log10(output_array_cgs - output_array_mks), \
          linestyle = 'dashed',
          color = (1.0, 0, 0, 0.4),
          linewidth =5, label = '15 K - $T_{CMB}$')

# evaluate Planck function in CGS unit
temperature  = 30.0                  # Kelvin
output_array_cgs = Planckfunc_cgs(freq_array, temperature) \
                   * cgsflux_to_Jy / str_to_sqarcsecond

plt.plot(np.log10( freq_array / 1e9) , np.log10(output_array_cgs), \
         color = (0, 0, 1.0, 0.4),
         linewidth=8, label = '30 K')

# evaluate Planck function in CGS unit
temperature  = 100.0                  # Kelvin
output_array_cgs = Planckfunc_cgs(freq_array, temperature) \
                   * cgsflux_to_Jy / str_to_sqarcsecond

plt.plot(np.log10( freq_array / 1e9) , np.log10(output_array_cgs), \
        ...


I am working on the file ".ipynb_checkpoints/astrobao-checkpoint.ipynb".
The first task is:
Plot free-fall time as a function of cloud particle number density
Can you write Python code for it?

# physical constants
mean_mol_weight = 2.76  # mean molecular weight
mole            = 6.02214129e23
year_to_s       = 365.0 * 24.0 * 60.0 * 60.0

# initializing arrays
number_density = np.arange(1, 10001, 1) * 1e3
# output_array_cgs = np.arange(1, 10001, 1) * 0.0

# initializing plotting
fig = plt.figure(figsize=(9, 14))
plt.subplot(2, 1, 1)
# plt.axis([4.0, 6.5, 0.01, 0.2])

# evaluate free-fall time
density = number_density * mean_mol_weight / mole
output_array_cgs = freefall_cgs(density) / ( year_to_s * 1e5)

plt.plot(np.log10( number_density ), output_array_cgs, \
         color = (0.5, 0.0, 0.0, 0.2),
         linewidth=8, label = 'Mean molecular weight: 2.76')

plt.title('Free-fall time')
plt.xlabel('Log$_{10}($Molecular gas number density [cm$^{-3}$])')
plt.ylabel('Time [10$^{5}$ year]')
plt.legend(loc=1)


I am working on the file ".ipynb_checkpoints/astrobao-checkpoint.ipynb".
The first task is:
Plotting the measurements for FU Ori as an practical example. Reference see Liu, H. B. et al. (2017) [arXiv:1701.06531]
Can you write Python code for it?

# initializing arrays
freq1   = np.arange(1, 50000) * 1e9  # frequency in Hz  # Hz
freq2   = np.arange(50000, 150000, 1000) * 1e9
freq    = np.concatenate((freq1, freq2), axis=0)

# physical constants
c_mks = 299792458.0 # light speed in m/s
cgsflux_to_Jy = 1e23

# initializing plotting
fig = plt.figure(figsize=(9, 14))
plt.subplot(2, 1, 1)
plt.axis([0.5, 2e5, 3e-5, 1e5])
plt.xscale('log')
plt.yscale('log')

# FU Ori
Te_FUOri  = 16000.0 # Kelvin
EM_FUOri  = 6.98e9
Omega_ff_FUOri = 1.41e-16  # solid angle
FUOri_ff_flux = blackbody_Fnu_cgs(freq, Te_FUOri, tauff_Mezger67(freq, Te_FUOri, EM_FUOri), Omega_ff_FUOri)

plt.plot( ( freq / 1e9 ), (FUOri_ff_flux * cgsflux_to_Jy * 1e3), \
          color = (0.1, 0.0, 1, 0.1),
          linestyle = 'dashed',
          linewidth=8, label = 'Free-free emission')

T_HID_FUOri = 300.0
kappa230Sigma_FUOri = 103.0
Omega_HID_FUOri = 1.38e-14
betaHID_FUOri   = 1.75  # dust opacity spectral index
tauHID_FUOri = dustkappa_cgs(freq, 230.0e9, kappa230Sigma_FUOri, betaHID_FUOri)
FUOri_HID_flux = blackbody_Fnu_cgs(freq, T_HID_FUOri, \
                        tauHID_FUOri, Omega_HID_FUOri)

plt.plot( ( freq / 1e9 ), (FUOri_HID_flux * cgsflux_to_Jy * 1e3), \
          color = (0, 0.7, 0.7, 0.1),
          linestyle = 'dashdot',
          linewidth=8, label = 'Dust emission from HID')

T_disk_FUOri = 60.0
kappa230Sigma_FUOri = 2.06e-2
Omega_disk_FUOri = 3.88e-12
betadisk_FUOri   = 1.75  # dust opacity spectral index
taudisk_FUOri = dustkappa_cgs(freq, 230.0e9, kappa230Sigma_FUOri, betadisk_FUOri)
FUOri_disk_flux = blackbody_Fnu_cgs(freq, T_disk_FUOri, \
                        taudisk_FUOri, Omega_disk_FUOri)

plt.plot( ( freq / 1e9 ), (FUOri_disk_flux * cgsflux_to_Jy * 1e3), \
          color = (0.9, 0.05, 0.05, 0.1),
          linestyle = 'dotted',
          linewidth=8, label = 'Dust emission from extended disk')

# plot summed model
plt.plot( ( freq / 1e9 ), \
          ( (FUOri_disk_flux + FUOri_HID_flux + FUOri_ff_flux) * cgsflux_to_Jy * 1e3), \
          color = (0.1, 0.1, 0.1,...