I am working on the file "Introduction to Python for Data Analysis_Outputs.ipynb".
The first task is:
Visually from creating the data frame you can tell the data looks different, yet in the .describe() call, the data shares very similar features. The two primary plotting tools we uses from matplotlib are histograms and scatterplots, which help us understand the shape of data.
Can you write Python code for it?


I am working on the file "Introduction to Python.ipynb".
The first task is:
As these examples show,
glob.glob's result is a list of strings,
which means we can loop over it
to do something with each filename in turn.
In our case,
the "something" we want to do is generate a set of plots for each file in our inflammation dataset.
Let's test it by analyzing the first three files in the list:
Can you write Python code for it?

filenames = glob('data/inflammation*.csv')
filenames = filenames[0:3]

for f in filenames:
    print(f)

    data = numpy.loadtxt(fname=f, delimiter=',')

    fig = plt.figure(figsize=(10.0, 3.0))

    axes1 = fig.add_subplot(1, 3, 1)
    axes2 = fig.add_subplot(1, 3, 2)
    axes3 = fig.add_subplot(1, 3, 3)

    axes1.set_ylabel('average')
    axes1.plot(data.mean(axis=0))

    axes2.set_ylabel('max')
    axes2.plot(data.max(axis=0))

    axes3.set_ylabel('min')
    axes3.plot(data.min(axis=0))

    fig.tight_layout()


I am working on the file "Introduction to Python.ipynb".
The first task is:
But we can also now call it with just one parameter,
in which case desired is automatically assigned the default value of 0.0:
Can you write Python code for it?

more_data = 5 + numpy.zeros((2, 2))
print('data before centering:')
print(more_data)
print('centered data:')
print(center(more_data))


I am working on the file "Introduction to Python.ipynb".
The first task is:
By giving our functions human-readable names,
we can more easily read and understand what is happening in the for loop.
Even better, if at some later date we want to use either of those pieces of code again,
we can do so in a single line.
Testing and Documenting
Once we start putting things in functions so that we can re-use them,
we need to start testing that those functions are working correctly.
To see how to do this,
let's write a function to center a dataset around a particular value:
Can you write Python code for it?

def center(data, desired):
    return (data - data.mean()) + desired


I am working on the file "Introduction to Python.ipynb".
The first task is:
Dictionaries
One of the more flexible built-in data structures is the dictionary. A dictionary maps a collection of values to a set of associated keys. These mappings are mutable, and unlike lists or tuples, are unordered. Hence, rather than using the sequence index to return elements of the collection, the corresponding key must be used. Dictionaries are specified by a comma-separated sequence of keys and values, which are separated in turn by colons. The dictionary is enclosed by curly braces.
For example:
Can you write Python code for it?

my_dict = {'a':16,
           'b':(4,5),
           'foo':'''(noun) a term used as a universal substitute
           for something real, especially when discussing technological ideas and
           problems'''}

my_dict

my_dict['b']


I am working on the file "Introduction to Python.ipynb".
The first task is:
Exercise: Turn a string into a list
Use a for-loop to convert the string "hello" into a list of letters:
Can you write Python code for it?


I am working on the file "Introduction to Python.ipynb".
The first task is:
Finally, the third possibility is that you made a typo when you were writing your code.
Let's say we fixed the error above by adding the line Count = 0 before the for loop.
Frustratingly, this actually does not fix the error.
Remember that variables are case-sensitive,
so the variable count is different from Count. We still get the same error, because we still have not defined count:
Can you write Python code for it?

Count = 0
for number in range(10):
    count = count + number
print("The count is: " + str(count))


I am working on the file "Introduction to Python.ipynb".
The first task is:
Here's a better approach:
Can you write Python code for it?

word = 'lead'
for char in word:
    print(char)


I am working on the file "Introduction to Python.ipynb".
The first task is:
Here,
we have put the average per day across all patients in the variable ave_inflammation,
then asked matplotlib.pyplot to create and display a line graph of those values.
The result is roughly a linear rise and fall,
which is suspicious:
based on other studies,
we expect a sharper rise and slower fall.
Let's have a look at two other statistics:
Can you write Python code for it?


I am working on the file "Introduction to Python.ipynb".
The first task is:
Hint: You can create an empty list like this:
Can you write Python code for it?


I am working on the file "Introduction to Python.ipynb".
The first task is:
If we would rather not get the error, we can use the get method, which returns None if the value is not present.
Can you write Python code for it?


I am working on the file "Introduction to Python.ipynb".
The first task is:
If,
instead of taking an array and doing arithmetic with a single value (as above)
you did the arithmetic operation with another array of the same shape,
the operation will be done on corresponding elements of the two arrays.
Thus:
Can you write Python code for it?


I am working on the file "Introduction to Python.ipynb".
The first task is:
In this way,
we have asked Python to do something different depending on the condition of our data.
Here we printed messages in all cases,
but we could also imagine not using the else catch-all
so that messages are only printed when something is wrong,
freeing us from having to manually examine every plot for features we've seen before.
What is truth?
True and False are special words in Python called booleans which represent true
and false statements. However, they aren't the only values in Python that are true and false.
In fact, any value can be used in an if or elif.
After reading and running the code below,
explain what the rule is for which values are considered true and which are considered false.
Can you write Python code for it?

if '':
    print('empty string is true')
if 'word':
    print('word is true')
if []:
    print('empty list is true')
if [1, 2, 3]:
    print('non-empty list is true')
if 0:
    print('zero is true')
if 1:
    print('one is true')


I am working on the file "Introduction to Python.ipynb".
The first task is:
In-place operators
Python (and most other languages in the C family) provides in-place operators
that work like this:
Can you write Python code for it?

x = 1  # original value
x += 1 # add one to x, assigning result back to x
x *= 3 # multiply x by 3

x


I am working on the file "Introduction to Python.ipynb".
The first task is:
It is easy to remove items from a dictionary.
Can you write Python code for it?

my_dict.popitem()

my_dict

my_dict.clear()

my_dict


I am working on the file "Introduction to Python.ipynb".
The first task is:
It's worth tracing the execution of this little program step by step.
Since there are five characters in 'aeiou',
the statement on line 3 will be executed five times.
The first time around,
length is zero (the value assigned to it on line 1)
and vowel is 'a'.
The statement adds 1 to the old value of length,
producing 1,
and updates length to refer to that new value.
The next time around,
vowel is 'e' and length is 1,
so length is updated to be 2.
After three more updates,
length is 5;
since there is nothing left in 'aeiou' for Python to process,
the loop finishes
and the print statement on line 4 tells us our final answer.
Note that a loop variable is just a variable that's being used to record progress in a loop.
It still exists after the loop is over,
and we can re-use variables previously defined as loop variables as well:
Can you write Python code for it?

letter = 'z'
for letter in 'abc':
    print(letter)
print('after the loop, letter is', letter)


I am working on the file "Introduction to Python.ipynb".
The first task is:
Item Errors
Next up are errors having to do with containers (like lists and dictionaries) and the items within them.
If you try to access an item in a list or a dictionary that does not exist,
then you will get an error.
This makes sense:
if you asked someone what day they would like to get coffee,
and they answered "caturday",
you might be a bit annoyed.
Python gets similarly annoyed if you try to ask it for an item that doesn't exist:
Can you write Python code for it?

letters = ['a', 'b', 'c']
print("Letter #1 is " + letters[0])
print("Letter #2 is " + letters[1])
print("Letter #3 is " + letters[2])
print("Letter #4 is " + letters[3])


I am working on the file "Introduction to Python.ipynb".
The first task is:
Note also that finding the length of a string is such a common operation
that Python actually has a built-in function to do it called len:
Can you write Python code for it?


I am working on the file "Introduction to Python.ipynb".
The first task is:
Notice that a indexes an integer, b a tuple, and foo a string. Hence, a dictionary is a sort of associative array. Some languages refer to such a structure as a hash or key-value store.
As with lists, being mutable, dictionaries have a variety of methods and functions that take dictionary arguments. For example, some dictionary functions include:
Can you write Python code for it?


I am working on the file "Introduction to Python.ipynb".
The first task is:
Often, we want to do more than add, subtract, multiply, and divide values of data.
Arrays also know how to do more complex operations on their values.
If we want to find the average inflammation for all patients on all days,
for example,
we can just ask the array for its mean value
Can you write Python code for it?


I am working on the file "Introduction to Python.ipynb".
The first task is:
Once a variable has a value, we can print it to the screen:
Can you write Python code for it?


I am working on the file "Introduction to Python.ipynb".
The first task is:
Since weight_lb doesn't "remember" where its value came from,
it isn't automatically updated when weight_kg changes.
This is different from the way spreadsheets work.
Just as we can assign a single value to a variable, we can also assign an array of values
to a variable using the same syntax.  Let's re-run numpy.loadtxt and save its result:
Can you write Python code for it?

data = numpy.loadtxt(fname='data/inflammation-01.csv', delimiter=',')


I am working on the file "Introduction to Python.ipynb".
The first task is:
Some useful dictionary methods are:
Can you write Python code for it?

# Returns key/value pairs as list
my_dict.items()

# Returns list of keys
my_dict.keys()

# Returns list of values
my_dict.values()


I am working on the file "Introduction to Python.ipynb".
The first task is:
That seems almost right:
the original mean was about 6.1,
so the lower bound from zero is how about -6.1.
The mean of the centered data isn't quite zero --- we'll explore why not in the challenges --- but it's pretty close.
We can even go further and check that the standard deviation hasn't changed:
Can you write Python code for it?

print('std dev before and after:', data.std(), centered.std())


I am working on the file "Introduction to Python.ipynb".
The first task is:
The expression (40,) tells us we have an N×1 vector,
so this is the average inflammation per day for all patients.
If we average across axis 1 (columns in our 2D example), we get:
Can you write Python code for it?


I am working on the file "Introduction to Python.ipynb".
The first task is:
The expression data[30, 20] may not surprise you,
but data[0, 0] might.
Programming languages like Fortran and MATLAB start counting at 1,
because that's what human beings have done for thousands of years.
Languages in the C family (including C++, Java, Perl, and Python) count from 0
because that's simpler for computers to do.
As a result,
if we have an M×N array in Python,
its indices go from 0 to M-1 on the first axis
and 0 to N-1 on the second.
It takes a bit of getting used to,
but one way to remember the rule is that
the index is how many steps we have to take from the start to get the item we want.
An index like [30, 20] selects a single element of an array,
but we can select whole sections as well.
For example,
we can select the first ten days (columns) of values
for the first four patients (rows) like this:
Can you write Python code for it?


I am working on the file "Introduction to Python.ipynb".
The first task is:
The function definition opens with the word def,
which is followed by the name of the function
and a parenthesized list of parameter names.
The body of the function --- the
statements that are executed when it runs --- is indented below the definition line,
typically by four spaces.
When we call the function,
the values we pass to it are assigned to those variables
so that we can use them inside the function.
Inside the function,
we use a return statement to send a result back to whoever asked for it.
Let's try running our function.
Calling our own function is no different from calling any other function:
Can you write Python code for it?

print('freezing point of water:', fahr_to_kelvin(32))
print('boiling point of water:', fahr_to_kelvin(212))


I am working on the file "Introduction to Python.ipynb".
The first task is:
The glob library contains a single function, also called glob,
that finds files whose names match a pattern.
We provide those patterns as strings:
the character * matches zero or more characters,
while ? matches any one character.
We can use this to get the names of all the HTML files in the current directory:
Can you write Python code for it?


I am working on the file "Introduction to Python.ipynb".
The first task is:
The improved version of print_characters uses a for loop
to repeat an operation---in this case, printing---once for each thing in a collection.
The general form of a loop is:
for variable in collection:
    do things with variable
We can call the loop variable anything we like,
but there must be a colon at the end of the line starting the loop,
and we must indent anything we want to run inside the loop. Unlike many other languages, there is no
command to end a loop (e.g. end for); what is indented after the for statement belongs to the loop.
Here's another loop that repeatedly updates a variable:
Can you write Python code for it?

length = 0
for vowel in 'aeiou':
    length = length + 1
print('There are', length, 'vowels')


I am working on the file "Introduction to Python.ipynb".
The first task is:
The output tells us that data currently refers to an n-dimensional array created by the NumPy library. These data corresponds to arthritis patient's inflammation. The rows are the individual patients and the columns are there daily inflammation measurements.
We can see what its shape is like this:
Can you write Python code for it?


I am working on the file "Introduction to Python.ipynb".
The first task is:
The second is that you just forgot to create the variable before using it.
In the following example,
count should have been defined (e.g., with count = 0) before the for loop:
Can you write Python code for it?

for number in range(10):
    count = count + number
print("The count is: " + str(count))


I am working on the file "Introduction to Python.ipynb".
The first task is:
The second line of this code uses the keyword if to tell Python that we want to make a choice.
If the test that follows the if statement is true,
the body of the if
(i.e., the lines indented underneath it) are executed.
If the test is false,
the body of the else is executed instead.
Only one or the other is ever executed:

Conditional statements don't have to include an else.
If there isn't one,
Python simply does nothing if the test is false:
Can you write Python code for it?

num = 53
print('before conditional...')
if num > 100:
    print('53 is greater than 100')
print('...after conditional')


I am working on the file "Introduction to Python.ipynb".
The first task is:
The tuple function can be used to cast any sequence into a tuple:
Can you write Python code for it?


I am working on the file "Introduction to Python.ipynb".
The first task is:
There's a lot of information here,
but the most important part is the first couple of lines:
loadtxt(fname, dtype=<type 'float'>, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None,
    unpack=False, ndmin=0)

This tells us that loadtxt has one parameter called fname that doesn't have a default value,
and eight others that do.
If we call the function like this:
Can you write Python code for it?


I am working on the file "Introduction to Python.ipynb".
The first task is:
This is because python stores a list in memory, and then can use multiple names to refer to the same list.
If all we want to do is copy a (simple) list, we can index the values into a new list, so we do not modify a list we did not mean to:
Can you write Python code for it?

odds = [1, 3, 5, 7]
# remember what this does!
primes = odds[:]
primes += [2]
print('primes:', primes)
print('odds:', odds)


I am working on the file "Introduction to Python.ipynb".
The first task is:
This is better because we can now ask Python's built-in help system to show us the documentation for the function:
Can you write Python code for it?


I am working on the file "Introduction to Python.ipynb".
The first task is:
This is handy:
if we usually want a function to work one way,
but occasionally need it to do something else,
we can allow people to pass a parameter when they need to
but provide a default to make the normal case easier.
The example below shows how Python matches values to parameters:
Can you write Python code for it?

def display(a=1, b=2, c=3):
    print('a:', a, 'b:', b, 'c:', c)

print('no parameters:')
display()
print('one parameter:')
display(55)
print('two parameters:')
display(55, 66)


I am working on the file "Introduction to Python.ipynb".
The first task is:
This is our first taste of how larger programs are built:
we define basic operations,
then combine them in ever-large chunks to get the effect we want.
Real-life functions will usually be larger than the ones shown here --- typically half a dozen to a few dozen lines --- but
they shouldn't ever be much longer than that,
or the next person who reads it won't be able to understand what's going on.
Tidying up
Now that we know how to wrap bits of code up in functions,
we can make our inflammation analyasis easier to read and easier to reuse.
First, let's make an analyze function that generates our plots:
Can you write Python code for it?

def analyze(filename):

    data = numpy.loadtxt(fname=filename, delimiter=',')

    fig = plt.figure(figsize=(10.0, 3.0))

    axes1 = fig.add_subplot(1, 3, 1)
    axes2 = fig.add_subplot(1, 3, 2)
    axes3 = fig.add_subplot(1, 3, 3)

    axes1.set_ylabel('average')
    axes1.plot(data.mean(axis=0))

    axes2.set_ylabel('max')
    axes2.plot(data.max(axis=0))
    axes2.set_title(filename[:-4])

    axes3.set_ylabel('min')
    axes3.plot(data.min(axis=0))

    fig.tight_layout()


I am working on the file "Introduction to Python.ipynb".
The first task is:
This is shorter---certainly shorter than something that prints every character in a hundred-letter string---and
more robust as well:
Can you write Python code for it?

word = 'oxygen'
for char in word:
    print(char)


I am working on the file "Introduction to Python.ipynb".
The first task is:
This particular traceback has two levels.
You can determine the number of levels by looking for the number of arrows on the left hand side.
In this case:

The first shows code from the cell above,
    with an arrow pointing to Line 2 (which is favorite_ice_cream()).

The second shows some code in another function (favorite_ice_cream, located in the file errors_01.py),
    with an arrow pointing to Line 7 (which is print(ice_creams[3])).

The last level is the actual place where the error occurred.
The other level(s) show what function the program executed to get to the next level down.
So, in this case, the program first performed a function call to the function favorite_ice_cream.
Inside this function,
the program encountered an error on Line 7, when it tried to run the code print(ice_creams[3]).

Long Tracebacks
Sometimes, you might see a traceback that is very long -- sometimes they might even be 20 levels deep!
This can make it seem like something horrible happened,
but really it just means that your program called many functions before it ran into the error.
Most of the time,
you can just pay attention to the bottom-most level,
which is the actual place where the error occurred.

So what error did the program actually encounter?
In the last line of the traceback,
Python helpfully tells us the category or type of error (in this case, it is an IndexError)
and a more detailed error message (in this case, it says "list index out of range").
If you encounter an error and don't know what it means,
it is still important to read the traceback closely.
That way,
if you fix the error,
but encounter a new one,
you can tell that the error changed.
Additionally,
sometimes just knowing where the error occurred is enough to fix it,
even if you don't entirely understand the message.
If you do encounter an error you don't recognize,
try looking at the official documentation on errors.
However,
note that you may not always be able to find the error there,
as it is possible to create custom errors.
In that...
Can you write Python code for it?

def some_function()
    msg = "hello, world!"
    print(msg)
     return msg


I am working on the file "Introduction to Python.ipynb".
The first task is:
This tells us that data has 60 rows and 40 columns. When we created the
variable data to store our arthritis data, we didn't just create the array, we also
created information about the array, called
attributes. This extra information describes data in
the same way an adjective describes a noun.
data.shape is an attribute  of data which described the dimensions of data.
We use the same dotted notation for the attributes of variables
that we use for the functions in libraries
because they have the same part-and-whole relationship.
If we want to get a single number from the array,
we must provide an index in square brackets,
just as we do in math:
Can you write Python code for it?


I am working on the file "Introduction to Python.ipynb".
The first task is:
Tuples
If we wish to create an immutable, ordered sequence of elements, we can use a tuple. These elements may be of arbitrary and mixed types. The tuple is specified by a comma-separated sequence of items, enclosed by parentheses:
Can you write Python code for it?

(34,90,56) # Tuple with three elements

(15,) # Tuple with one element

(12, 'foobar') # Mixed tuple


I am working on the file "Introduction to Python.ipynb".
The first task is:
Variable name errors come with some of the most informative error messages,
which are usually of the form "name 'the_variable_name' is not defined".
Why does this error message occur?
That's harder question to answer,
because it depends on what your code is supposed to do.
However,
there are a few very common reasons why you might have an undefined variable.
The first is that you meant to use a string, but forgot to put quotes around it:
Can you write Python code for it?


I am working on the file "Introduction to Python.ipynb".
The first task is:
We also don't have to include the upper and lower bound on the slice.
If we don't include the lower bound,
Python uses 0 by default;
if we don't include the upper,
the slice runs to the end of the axis,
and if we don't include either
(i.e., if we just use ':' on its own),
the slice includes everything:
Can you write Python code for it?

small = data[:3, 36:]
print('small is:')
small


I am working on the file "Introduction to Python.ipynb".
The first task is:
We can also chain several tests together using elif,
which is short for "else if".
The following Python code uses elif to print the sign of a number.
Can you write Python code for it?

num = -3

if num > 0:
    print(num, "is positive")
elif num == 0:
    print(num, "is zero")
else:
    print(num, "is negative")


I am working on the file "Introduction to Python.ipynb".
The first task is:
We can also check an object for membership in a dictionary using the in expression:
Can you write Python code for it?


I am working on the file "Introduction to Python.ipynb".
The first task is:
We could test this on our actual data,
but since we don't know what the values ought to be,
it will be hard to tell if the result was correct.
Instead,
let's use NumPy to create a matrix of 0's
and then center that around 3:
Can you write Python code for it?

z = numpy.zeros((2,2))
print(center(z, 3))


I am working on the file "Introduction to Python.ipynb".
The first task is:
We don't actually need to store the row in a variable of its own.
Instead, we can combine the selection and the method call:
Can you write Python code for it?

print('maximum inflammation for patient 2:', data[2, :].max())


I am working on the file "Introduction to Python.ipynb".
The first task is:
We've successfully called the function that we defined,
and we have access to the value that we returned.

Integer division
We are using Python 3, where division always returns a floating point number:
$ python3 -c "print(5/9)"
0.5555555555555556
Unfortunately, this wasn't the case in Python 2:
```

5/9

0
```

If you are using Python 2 and want to keep the fractional part of division
you need to convert one or the other number to floating point:
```

5.0/9

0.555555555556

5/9.0

0.555555555556
```

And if you want an integer result from division in Python 3,
use a double-slash:
```

3//2

1
```

Composing Functions
Now that we've seen how to turn Fahrenheit into Kelvin,
it's easy to turn Kelvin into Celsius:
Can you write Python code for it?

def kelvin_to_celsius(temp):
    return temp - 273.15

print('absolute zero in Celsius:', kelvin_to_celsius(0.0))


I am working on the file "Introduction to Python.ipynb".
The first task is:
What about converting Fahrenheit to Celsius?
We could write out the formula,
but we don't need to.
Instead,
we can compose the required function, based on the two functions we have already created:
Can you write Python code for it?

def fahr_to_celsius(temp):
    temp_k = fahr_to_kelvin(temp)
    result = kelvin_to_celsius(temp_k)
    return result

print('freezing point of water in Celsius:', fahr_to_celsius(32.0))


I am working on the file "Introduction to Python.ipynb".
The first task is:
What if we need the maximum inflammation for all patients (as in the
next diagram on the left), or the average for each day (as in the
diagram on the right)? As the diagram below shows, we want to perform the
operation across an axis:

To support this,
most array methods allow us to specify the axis we want to be consumed by the operation.
If we ask for the average across axis 0 (rows in our 2D example),
we get:
Can you write Python code for it?


I am working on the file "Introduction to Python.ipynb".
The first task is:
What is the value of element[:4]?
What about element[4:]?
Or element[:]?
What is element[-1]?
What is element[-2]?
Given those answers,
explain what element[1:-1] does.
Repeating Actions with Loops
Above, we wrote some code that plots some values of interest from our first inflammation dataset,
and reveals some suspicious features in it.
We have a dozen data sets right now, though, and more on the way.
We want to create plots for all of our data sets with a single statement.
To do that, we'll have to teach the computer how to repeat things.
An example task that we might want to repeat is printing each character in a
word on a line of its own. One way to do this would be to use a series of print statements:
Can you write Python code for it?

word = 'lead'
print(word[0])
print(word[1])
print(word[2])
print(word[3])


I am working on the file "Introduction to Python.ipynb".
The first task is:
When we try to index a value that does not exist, it raises a KeyError.
Can you write Python code for it?


I am working on the file "Introduction to Python.ipynb".
The first task is:
While modifying in place, it is useful to remember that python treats lists in a slightly counterintuitive way.
If we make a list and (attempt to) copy it then modify in place, we can cause all sorts of trouble:
Can you write Python code for it?

odds = [1, 3, 5, 7]
primes = odds
primes += [2]
print('primes:', primes)
print('odds:', odds)


I am working on the file "Introduction to Python.ipynb".
The first task is:
With that in hand,
let's look at the help for numpy.loadtxt:
Can you write Python code for it?


I am working on the file "Introduction to Python.ipynb".
The first task is:
Writing Functions
At this point,
we've written code to draw some interesting features in our inflammation data,
loop over all our data files to quickly draw these plots for each of them,
and have Python make decisions based on what it sees in our data.
But, our code is getting pretty long and complicated;
what if we had thousands of datasets,
and didn't want to generate a figure for every single one?
Commenting out the figure-drawing code is a nuisance.
Also, what if we want to use that code again,
on a different dataset or at a different point in our program?
Cutting and pasting it is going to make our code get very long and very repetative,
very quickly.
We'd like a way to package our code so that it is easier to reuse,
and Python provides for this by letting us define things called functions -
a shorthand way of re-executing longer pieces of code.
Let's start by defining a function fahr_to_kelvin that converts temperatures from Fahrenheit to Kelvin:
Can you write Python code for it?

def fahr_to_kelvin(temp):
    return ((temp - 32) * (5/9)) + 273.15


I am working on the file "Introduction to Python.ipynb".
The first task is:
and another function called detect_problems that checks for those systematics
we noticed:
Can you write Python code for it?

def detect_problems(filename):

    data = numpy.loadtxt(fname=filename, delimiter=',')

    if data.max(axis=0)[0] == 0 and data.max(axis=0)[20] == 20:
        print('Suspicious looking maxima!')
    elif data.min(axis=0).sum() == 0:
        print('Minima add up to zero!')
    else:
        print('Seems OK!')


I am working on the file "Introduction to Python.ipynb".
The first task is:
and if we loop over a list,
the loop variable is assigned elements one at a time:
Can you write Python code for it?


I am working on the file "Introduction to Python.ipynb".
The first task is:
and then change weight_kg:
Can you write Python code for it?

weight_kg = 100.0
print('weight in kilograms is now:', weight_kg, 'and weight in pounds is still:', weight_lb)


I am working on the file "Introduction to Python.ipynb".
The first task is:
does not.

Ch-Ch-Ch-Changes
Data which can be modified in place is called mutable,
while data which cannot be modified is called immutable.
Strings and numbers are immutable. This does not mean that variables with string or number values are constants,
but when we want to change the value of a string or number variable, we can only replace the old value
with a completely new value.
Lists and arrays, on the other hand, are mutable: we can modify them after they have been created. We can
change individual elements, append new elements, or reorder the whole list.  For some operations, like
sorting, we can choose whether to use a function that modifies the data in place or a function that returns a
modified copy and leaves the original unchanged.
Be careful when modifying data in place.  If two variables refer to the same list, and you modify the list
value, it will change for both variables! If you want variables with mutable values to be independent, you
must make a copy of the value when you assign it.
Because of pitfalls like this, code which modifies data in place can be more difficult to understand. However,
it is often far more efficient to modify a large data structure in place than to create a modified copy for
every small change. You should consider both of these aspects when writing your code.

There are many ways to change the contents of lists besides assigning new values to
individual elements:
Can you write Python code for it?

odds.append(11)
print('odds after adding a value:', odds)

del odds[0]
print('odds after removing the first element:', odds)

odds.reverse()
print('odds after reversing:', odds)


I am working on the file "Introduction to Python.ipynb".
The first task is:
mean is a method of the array.
A method is simply a function that is an attribute of the array,
in the same way that the member shape does.
If variables are nouns, methods are verbs:
they are what the thing in question knows how to do.
We need empty parentheses for data.mean(),
even when we're not passing in any parameters,
to tell Python to go and do something for us. data.shape doesn't
need () because it is just a description but data.mean() requires the ()
because it is an action.
NumPy arrays have lots of useful methods:
Can you write Python code for it?

print('maximum inflammation:', data.max())
print('minimum inflammation:', data.min())
print('standard deviation:', data.std())


I am working on the file "Introduction to Python.ipynb".
The first task is:
then the filename is assigned to fname (which is what we want),
but the delimiter string ',' is assigned to dtype rather than delimiter,
because dtype is the second parameter in the list. However ',' isn't a known dtype so
our code produced an error message when we tried to run it.
When we call loadtxt we don't have to provide fname= for the filename because it's the
first item in the list, but if we want the ',' to be assigned to the variable delimiter,
we do have to provide delimiter= for the second parameter since delimiter is not
the second parameter in the list.
Exercise: Combining strings
"Adding" two strings produces their concatenation:
'a' + 'b' is 'ab'.
Write a function called fence that takes two parameters called original and wrapper
and returns a new string that has the wrapper character at the beginning and end of the original.
A call to your function should look like this:
print(fence('name', '*'))
*name*
Exercise: Rescaling an array
Write a function rescale that takes an array as input
and returns a corresponding array of values scaled to lie in the range 0.0 to 1.0.
Exercise: Variables inside and outside functions
What does the following piece of code display when run - and why?
Can you write Python code for it?


I am working on the file "Introduction to Python.ipynb".
The first task is:
which is the average inflammation per patient across all days.
Plotting Data
The mathematician Richard Hamming once said,
"The purpose of computing is insight, not numbers,"
and the best way to develop insight is often to visualize data.
Visualization deserves an entire lecture (or course) of its own,
but we can explore a few features of Python's matplotlib library here.
While there is no "official" plotting library,
this package is the de facto standard.
First,
we will import the pyplot module from matplotlib
and use two of its functions to create and display a heat map of our data:
Can you write Python code for it?

%matplotlib inline

import matplotlib.pyplot as plt
plt.imshow(data)


I am working on the file "Introduction to Python.ipynb".
The first task is:
while or is true if at least one part is true:
Can you write Python code for it?

if (1 < 0) or (-1 < 0):
    print('at least one test is true')


I am working on the file "Introduction to Python.ipynb".
The first task is:
will create a new array doubledata
whose elements have the value of two times the value of the corresponding elements in data:
Can you write Python code for it?

print('original:')
data[:3, 36:]

print('doubledata:')
doubledata[:3, 36:]


I am working on the file "Introduction to Python.ipynb".
The first task is:
will give you an array where tripledata[0,0] will equal doubledata[0,0] plus data[0,0],
and so on for all other elements of the arrays.
Can you write Python code for it?


I am working on the file "Introduction to Python.ipynb".
The first task is:
works, but:
Can you write Python code for it?


I am working on the file "Introduction to nixpy.ipynb".
The first task is:
Create a Block element
All other data objects of the NIX data model reside inside a Block element.
A block is used to group data that belongs together.
Each data element in NIX has a property type which is used to provide additional semantic meaning.
We will now create a single block and assign a type which indicates that the block represents a recording session.
Further we print out the block's id. The id is unique and automatically generated.
Can you write Python code for it?

block = f.create_block("Test Session", "nix.session")
print "Name: %s" % block.name
print "Id: %s" % block.id

print_stats(f.blocks)


I am working on the file "Introduction to nixpy.ipynb".
The first task is:
Create and open a File
First we open a new file. The open method gets two parameters: the name of the file to open and and a file mode.
The file mode determines whether the file should be opened for reading or writing.
Choosing the file mode Overwrite opens the file for reading and writing and overwrites any existing content.
Can you write Python code for it?

f = File.open("first_file.demo.h5", FileMode.Overwrite)
print "Format: %s %s" % (f.format, f.version)


I am working on the file "Introduction to nixpy.ipynb".
The first task is:
Plot the result
Since the NIX data model fully describes the stored data semantically, it is possible to implement a generic
plotting tool, that uses this information to generate a nice visualization of the data.
Can you write Python code for it?

plotter = Plotter(width=1000, height=500)
plotter.add(spikes, color="red")
plotter.add(signal)

plotter.plot()

print_stats(f.blocks)
print_stats(f.blocks[0].data_arrays)
print_stats(f.blocks[0].multi_tags)
print_stats(f.blocks[0].sources)


I am working on the file "Introduction to nixpy.ipynb".
The first task is:
Write data to the file
From the above generated data we now create to DataArray elements.
The DataArray is the central element of the NIX data model.
It represents an n-dimensional array, which also provides a dimension descriptor for each dimension of the data.
Can you write Python code for it?

# create a data array for the membrane voltage
signal = block.create_data_array("Membrane Voltage", "nix.regular_sampled.time_series", data=voltage)
signal.label = "voltage"
signal.unit  = "V"

# describe the x-axis of the array
xaxis  = signal.append_sampled_dimension(sampling_interval)
xaxis.label = "time"
xaxis.unit  = "s"

# create a data array for the spiketimes
spikes = block.create_data_array("Spike Times", "nix.events.spike_times", data=spike_times)
spikes.label = "time"
spikes.unit  = "s"
spikes.append_set_dimension()

print_stats(block.data_arrays)


I am working on the file "Introduction to pyzmq.ipynb".
The first task is:
Echo client
Can you write Python code for it?

%%writefile client.py
#!/usr/bin/env python
import zmq

context = zmq.Context()

s = context.socket(zmq.DEALER)
s.connect('tcp://127.0.0.1:11235')
for i in range(10):
    s.send('Hello world')
    print(s.recv())


I am working on the file "Introduction to pyzmq.ipynb".
The first task is:
Exercise
In this session, using what reviewed in those two examples, let's create three different kinds of nodes:

The Broker is the only single instance node of the exercise, holding a simple list of IP addresses and ports where others are listening. All other processes in the system know the address of the Broker previously. It will accept connections at port 9999, in the facilitator's computer, accepting just two different commands:

REGISTER [<ip>:<port>] : When receiving this command, the Broker will put the pair IP-port into the internal list, unless the pair IP-port already exists, then, it will reply HOP, without adding it.

LIST : When receiving this command, the Broker will send the list as a string separating each <ip>:<port> with a single space.

The Hider is the program with more instances running at the same time. When launched, it will connect to the Broker, and will register its current IP and port, then will disconnect, will randomly choose the name of a city from a list, and will accept connections in that IP and port. It will only accept one single command, including the name of the city, and if the city name provided is the one chosen, it will reply the guess was correct, and then will choose another random city name, and will accept connections again.

The Seeker is a program which will connect to Broker, and ask for the list of Hiders, and then, will go connecting to each one of the Hiders, asking a possible city. It will store a counter of how much hiders it found and will print in stdout everytime it tries to guess the city for a hider.

When running the processes, it's quite recommendable to run quite some Hiders, let's say 10, and some less Seekers, let's say 2 or 3.
Except for the communication with the broker, which has been defined to provide the broker already created, the rest of the protocols should be defined by the teams.
Can you write Python code for it?

%%writefile broker.py
#!/usr/bin/env python
import argparse
import zmq
from zmq.eventloop import ioloop, zmqstream

clients = set()

def action_register(message):
    address = message.split()[1].strip()
    if address in clients:
        return 'HOP'
    else:
        clients.add(address)
        return 'OK'

def action_list(message):
    return ' '.join(clients)

def handle(stream, message):
    addr, text = message
    print('BROKER: ' + text)
    action = text.split()[0].lower()
    try:
        reply = globals()['action_' + action](text)
    except KeyError:
        print('BROKER: Unknown action', action)
        reply = 'ERROR'
    stream.send_multipart((addr, reply))

io_loop = ioloop.IOLoop()
context = zmq.Context()
socket = context.socket(zmq.ROUTER)
stream = zmqstream.ZMQStream(socket, io_loop=io_loop)
stream.on_recv_stream(handle)

parser = argparse.ArgumentParser()
parser.add_argument('-b', '--bind-address', default='tcp://0.0.0.0:5555')

if __name__ == '__main__':
    args = parser.parse_args()
    socket.bind(args.bind_address)
    io_loop.start()

%%writefile cities.txt
Barcelona
Berlin
Madrid
New York
Londres
Igualada

%%writefile hider.py
#!/usr/bin/env python
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-p', '--port', default='5556')
parser.add_argument('-b', '--broker', default='tcp://127.0.0.1:5555')
args = parser.parse_args()

%%writefile seeker.py
#!/usr/bin/env python
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-b', '--broker', default='tcp://127.0.0.1:5555')


I am working on the file "Introduction to pyzmq.ipynb".
The first task is:
Fibonacci example
In a very similar way, now, the client generates a list of integers within certain range, and sends each number to the server.
The server reads the integer, and checks if there is any coincidence in the Fibonacci sequence.
Fibo server
Can you write Python code for it?

%%writefile fibo.py
#!/usr/bin/env python
import zmq
from zmq.eventloop import ioloop, zmqstream

def fibonacci(n):
    a, b = 0, 1
    while n >= a:
        yield(a)
        a, b = b, a + b
    return

def fibo(stream, message):
    n = int(message[1])
    reply = [message[0]] + [str(n in [x for x in fibonacci(n)])]
    stream.send_multipart(reply)

io_loop = ioloop.IOLoop()
context = zmq.Context()
socket = context.socket(zmq.ROUTER)
stream = zmqstream.ZMQStream(socket, io_loop=io_loop)
stream.on_recv_stream(fibo)
socket.bind('tcp://0.0.0.0:11235')
io_loop.start()


I am working on the file "Introduction to pyzmq.ipynb".
The first task is:
Usage
In a terminal, run python echo.py, and:
Can you write Python code for it?


I am working on the file "Introduction to pyzmq.ipynb".
The first task is:
Usage
In a terminal, run python fibo.py, and then:
Can you write Python code for it?


I am working on the file "Introduction-to-Pandas.ipynb".
The first task is:
Building up data summaries across categorical variables
Can you write Python code for it?

# 'category' is a categorical variable. Count the number in each category
df['category'].value_counts()

df.category.value_counts() # same as above

# similar counts for location, note that None is treated as missing values and not counted
df['location'].value_counts()

# overall mean of the "prob" variable
df.prob.mean()

# mean of prob grouped by location
df.groupby("location").prob.mean()

# mean of all the continuous variables, group by category
df.groupby("category").mean()

# groupby multiple columns
df.groupby(["location","category"]).mean()


I am working on the file "Introduction-to-Pandas.ipynb".
The first task is:
Creating a data frame from scratch
First let's see how to create a DataFrame from scratch. In this first example, we'll use random numbers generated using functions defined in the np.random module.
Can you write Python code for it?

np.random.seed(482010)  # seed the pseudo-random number generators
x = np.random.random(15)
y = np.random.binomial(10, x)

df = pd.DataFrame()
df['prob'] = x
df['count'] = y
df.head()

# alternate way to create same df by passing a dictionary
df2 = pd.DataFrame({'prob':x, 'count':y})
df2.head()

# column and row names
print("Column names: ", df.columns)
print("Row names (index): ", df.index)

# add a new column to our existing df
# randomly give each observation a categorization
df['category'] = np.random.choice(['A','B','C'], size=15)
df

# create a new column representing locations
# using None to represent missing valuess
df['location'] = np.random.choice(["Raleigh","Durham","Chapel Hill", None], size=15)
df

# find out for which observations location is null
df["location"].isnull()  # notice None objects are consider null


I am working on the file "Introduction-to-Pandas.ipynb".
The first task is:
Indexing DataFrames
Pandas DataFrames support three different indexing mechanisms.

.iloc -- indexes rows and columns by integers.  Indexes go from 0 to $n-1$ for rows, and 0 to $p-1$ for columns, where $n$ and $p$ are the number of rows and columns respectively.  Slicing is like normal python slices.

.loc -- indexes rows and columns by labels.  If you use slices with .loc, note that contrary to standard Python slices, both the start and the stop are included! Note that if the row names (df.index) are integers, than slice

.ix -- allows you to mix integers and labels.  However, it defaults to label based indexing like .loc, and only adopts integer based indexing if row or column labels don't have integers in them.  For the most part, .ix works intuitively, but if you're not careful it can lead to seom unexpected behavior.

For a fuller explanation of the differences between these indexing mechanisms see:
- http://pandas.pydata.org/pandas-docs/stable/indexing.html
- http://stackoverflow.com/questions/31593201/pandas-iloc-vs-ix-vs-loc-explanation
Can you write Python code for it?

df["prob"]  # labels by themselves index column names

df.prob # if your column name is a valid python variable name you can acces it like an attribute

# get specific columns using list of column names
df[["count","location"]].head()  # lookup the head method in the docs

# get specific columns using list of column names
df[["prob","category"]].tail()  # lookup the tail method in the docs

# get first three rows of df using slices
df[:3]

# last three rows of df using slices
df[-3:]

df.iloc[:3,:]  # integer indexing and slicing using iloc

df.iloc[:4,(1,3)]  # integer indexing and slicing

df.loc[:4,("count","location")]  # label indexing and slicing, contrast to above

df.ix[:4,:3]  # indexes rows like .loc (because row names are integers)
              # indexes columns like .iloc (because no integers in column names)


I am working on the file "Introduction-to-Simulation.ipynb".
The first task is:
## Generate a large number of samples of size 25
Every time we take a random sample from our population of interest we'll get a different estimate of the mean and standard deviation (or whatever other statistics we're interested in).  To explore how well random samples of size 25 perform, generally, in terms of estimating the mean and standard deviation of the population of interest we need a large number of such samples.
It's tedious to take one sample at a time, so we'll generate 100 samples of size 25, and calculate the mean and standard deviation for each of those samples (storing the means and standard deviations in lists).
Can you write Python code for it?

means25 = []
std25 = []
for i in range(100):
    s = np.random.choice(popn, size=25)
    means25.append(np.mean(s))
    std25.append(np.std(s,ddof=1))

plt.hist(means25,bins=15)
plt.xlabel("Mean glucocorticoid concentration")
plt.ylabel("Frequency")
plt.title("Distribution of estimates of the\n mean glucocorticoid concentration\n for 100 samples of size 25")
plt.vlines(np.mean(popn), 0, 18, linestyle='dashed', color='red',label="True Mean")
plt.legend(loc="upper right")
pass


I am working on the file "Introduction-to-Simulation.ipynb".
The first task is:
A brief note about pseudo-random numbers
When carrying out simulations, it is typical to use random number generators.  Most computers can not generate true random numbers -- instead we use algorithms that approximate the generation of random numbers (pseudo-random number generators). One important difference between a true random number generator and a pseudo-random number generator is that a series of pseudo-random numbers can be regenerated if you know the "seed" value that initialized the algorithm.  We can specifically set this seed value, so that we can guarantee that two different people evaluating this notebook get the same results, even though we're using (pseudo)random numbers in our simulation.
Can you write Python code for it?

# set the seed for the pseudo-random number generator
# the seed is any 32 bit integer
# different seeds will generate different results for the
# simulations that follow
np.random.seed(20160208)


I am working on the file "Introduction-to-Simulation.ipynb".
The first task is:
Compare the first and second samples
Can you write Python code for it?

plt.hist(sample1)
plt.hist(sample2,alpha=0.5)
plt.xlabel("Glucorticoid concentration (nM)")
plt.ylabel("Frequency")
pass


I am working on the file "Introduction-to-Simulation.ipynb".
The first task is:
Density histogram
If instead of constraining the total height of the bars, we constrain the total area of the bars to sum to one, we call this a density histogram. When comparing histograms based on different numbers of samples, with different bin width, etc. you should usually use the density histogram.
The argument normed=True to the pyplot.hist function will this function calculate a density histogram instead of the default frequency histogram.
Can you write Python code for it?

plt.hist(means25,bins=15,normed=True)
plt.xlabel("Mean glucocorticoid concentration")
plt.ylabel("Density")
plt.vlines(np.mean(popn), 0, 2.5, linestyle='dashed', color='red',label="True Mean")
plt.legend(loc="upper right")
pass


I am working on the file "Introduction-to-Simulation.ipynb".
The first task is:
Relative Frequency Histogram
A relative frequency histogram is like a frequency histogram, except the bin heights are given in fractions of the total sample size (relative frequency) rather than absolute frequency.  This is equivalent to adding the constraint that the total height of all the bars in the histogram will add to 1.0.
Can you write Python code for it?

# Relative Frequency Histogram
plt.hist(means25, bins=15, weights=np.ones_like(means25) * (1.0/len(means25)))
plt.xlabel("mean glucocorticoid concentration")
plt.ylabel("Relative Frequency")
plt.vlines(np.mean(popn), 0, 0.20, linestyle='dashed', color='red',label="True Mean")
plt.legend(loc="upper right")
pass


I am working on the file "Introduction-to-Simulation.ipynb".
The first task is:
Standard Error of the Mean
We see from the graph above that our estimates of the mean cluster more tightly about the true mean as our sample size increases.  Let's quantify that by calculating the standard deviation of our mean estimates as a function of sample size.
The standard deviation of the sampling distribution of a statistic of interest is called the "Standard Error" of that statistic.  Here, through simulation, we are estimating the "Standard Error of the Mean".
Can you write Python code for it?

sm25 = np.std(means25,ddof=1)
sm50 = np.std(means50,ddof=1)
sm100 = np.std(means100,ddof=1)
sm200 = np.std(means200, ddof=1)

x = [25,50,100,200]
y = [sm25,sm50,sm100,sm200]
plt.scatter(x,y)
plt.xlabel("Sample size")
plt.ylabel("Std Dev of Mean Estimates")
pass


I am working on the file "Introduction-to-Simulation.ipynb".
The first task is:
Standard Errors of the Standard Deviation
Above we explored how the spread in our estimates of the mean changed with sample size.  We can similarly explore how our estimates of the  standard deviation of the population change as we vary our sample size.
Can you write Python code for it?

# the label arguments get used when we create a legend
plt.hist(std25,  normed=True, alpha=0.75, histtype="stepfilled", label="n=25")
plt.hist(std50,  normed=True, alpha=0.75, histtype="stepfilled", label="n=50")
plt.hist(std100, normed=True, alpha=0.75, histtype="stepfilled", label="n=100")
plt.hist(std200, normed=True, alpha=0.75, histtype="stepfilled", label="n=200")
plt.xlabel("Standard Deviation of Glucocorticoid Concentration")
plt.ylabel("Density")
plt.vlines(np.std(popn), 0, 9, linestyle='dashed', color='black',label="True Standard Deviation")
#plt.legend()
pass


I am working on the file "Introduction-to-Simulation.ipynb".
The first task is:
You can show mathematically for normally distributed data, that the expected Standard Error of the Mean as a function of sample size is:
$$
\mbox{Standard Error of Mean} = \frac{\sigma}{\sqrt{n}}
$$
where $\sigma$ is the population standard deviation, and $n$ is the sample size.
Let's compare that theoretical expectation to our simulated estimates.
Can you write Python code for it?

x = [25,50,100,200]
y = [sm25,sm50,sm100,sm200]
theory = [np.std(popn)/np.sqrt(i) for i in range(10,250)]
plt.scatter(x,y, label="Simulation estimates")
plt.plot(range(10,250), theory, color='red', label="Theoretical expectation")
plt.xlabel("Sample size")
plt.ylabel("Std Error of Mean")
plt.legend()
plt.xlim(0,300)
pass


I am working on the file "Introduction-to-Simulation.ipynb".
The first task is:
You can show mathematically for normally distributed data, that the expected Standard Error of the Standard Deviation is approximately
$$
\mbox{Standard Error of Standard Deviation} \approx \frac{\sigma}{\sqrt{2(n-1)}}
$$
where $\sigma$ is the population standard deviation, and $n$ is the sample size.
Let's compare that theoretical expectation to our simulated estimates.
Can you write Python code for it?

x = [25,50,100,200]
y = [ss25,ss50,ss100,ss200]
plt.scatter(x,y, label="Simulation estimates")
plt.xlabel("Sample size")
plt.ylabel("Std Error of Std Dev")

theory = [np.std(popn)/(np.sqrt(2.0*(i-1))) for i in range(10,250)]
plt.plot(range(10,250), theory, color='red', label="Theoretical expectation")

plt.xlim(0,300)
plt.legend()
pass


I am working on the file "Introduction/Exercicio01_Titanic.ipynb".
The first task is:
Por fim, geramos nossa base de treino e teste no formato para o ScikitLearn
Can you write Python code for it?

dataset = {
    'data': df.values[0:,2:],
    'target': df.values[0:,0]
}

X = dataset['data']
y = dataset['target']


I am working on the file "Introduction/TensorFlow Basics.ipynb".
The first task is:
Convolution
Loading 'camera' images from sklearn
Can you write Python code for it?

from skimage import data
img = data.camera().astype(np.float32)

plt.imshow(img, cmap='gray')
plt.show()


I am working on the file "Introduction/TensorFlow Basics.ipynb".
The first task is:
Convolution using Placeholders
We can specify parameters that we expect to fit in the graph later on, now, by using placeholders. Convolution using placeholders is presented below.
Can you write Python code for it?

img = tf.placeholder(tf.float32, shape = [None, None], name = 'img')
# Reshaping inbuilt function
img3d = tf.expand_dims(img, 2)
print img3d.get_shape()

img4d = tf.expand_dims(img3d, 0)
print img4d.get_shape()

mean = tf.placeholder(tf.float32, name = 'mean')
sigma = tf.placeholder(tf.float32, name = 'sigma')
ksize = tf.placeholder(tf.int32, name = 'ksize')

# Giving formula for x, gaussian kernel, gabor kernel etc..
x = tf.linspace(-3.0, 3.0, ksize)
z = 1.0/(tf.sqrt(2*3.14)*sigma) * (tf.exp(-1*(tf.pow(x-mean, 2)/(2*tf.pow(sigma, 2)))))
z2d = tf.matmul(tf.reshape(z, [ksize, 1]), tf.reshape(z, [1, ksize]))

xs = tf.linspace(-3.0, 3.0, ksize)
ys = tf.sin(xs)

ys = tf.reshape(ys, [ksize, 1])
ones = tf.ones([1, ksize])

mat = tf.matmul(ys, ones)
gabor = tf.matmul(mat, z2d)

gabor4d = tf.reshape(gabor, [ksize, ksize, 1, 1])

convolved = tf.nn.conv2d(img4d, gabor4d, strides = [1, 1, 1, 1],
                        padding = 'SAME')

# We defined the graph above, now we are going to evaluate it.
result = convolved.eval(feed_dict = {
        img: data.camera(),
        mean: 0.0,
        sigma: 1.0,
        ksize: 5
    })

plt.imshow(result[0, :, :, 0], cmap = 'gray')
plt.title('Gabor filter output')
plt.show()


I am working on the file "Introduction/TensorFlow Basics.ipynb".
The first task is:
Example - Creating a Gaussian Curve
Can you write Python code for it?

mean = 0
sigma = 1.0
z = 1.0/(tf.sqrt(2*3.14)*sigma) * (tf.exp(-1*(tf.pow(x-mean, 2)/(2*tf.pow(sigma, 2)))))

res = z.eval() # Note that x is already defined from above
plt.plot(res)
plt.show()


I am working on the file "Introduction/TensorFlow Basics.ipynb".
The first task is:
Gabor Kernel
We can take a sin wave and modulate it with the gaussian kernel to get a gabor kernel.
Can you write Python code for it?

ksize = 100
xs = tf.linspace(-3.0, 3.0, ksize)
ys = tf.sin(xs+2)

# The following two statements are equivalent to
# plt.plot(xs.eval(), ys.eval())
plt.figure()
plt.plot(ys.eval())

plt.show()


I am working on the file "Introduction/TensorFlow Basics.ipynb".
The first task is:
Interactive Session - This is a way to run session in environments like notebooks where you don't want to pass around a session variable. But it's just like a session. Here's how to create one. Also this behaves more like a normal python program. You have to recompute the formula if you want updates. For example, z is defined below in the gaussian curve example. You have to rerun the formula after changing x to get new z. Just running z.eval() won't do it. However, in a normal session, it will.
Without interactive session, whenever you call x.eval(), you have to pass session as x.eval(session = sess)
Can you write Python code for it?

sess = tf.InteractiveSession()

# print x.eval()

print x.get_shape() # x.shape
print x.get_shape().as_list() # x.shape.tolist()


I am working on the file "Introduction/TensorFlow Basics.ipynb".
The first task is:
LinSpace:0 means output of LinSpace. TensorFlow doesn't compute the values immediately. It only specifies the nature of the output of a TF operation, also called an Op node.
Can you write Python code for it?

x = tf.linspace(-3.0, 3.0, 100) # Doesn't compute immediately
# Note that tf.linspace(-3, 3, 5) gives an error because datatypes are
# mismatched
print (x)


I am working on the file "Introduction/TensorFlow Basics.ipynb".
The first task is:
Session
In order to get run a TF program, we need a session. The session computes the graph we construct. Here's an example.
Can you write Python code for it?

sess = tf.Session()

# We can ask a session to compute the value of a node
computed_x = sess.run(x)
# print (computed_x)

# Or we can ask the node to compute itself using the session
computed_x = x.eval(session = sess)

# print computed_x

# We can close the session by doing this
sess.close()


I am working on the file "Introduction/TensorFlow Basics.ipynb".
The first task is:
We need to convert this sine wave into a matrix and multiply with the gaussian kernel. That will be the gabor filter.
Can you write Python code for it?

ys = tf.reshape(ys, [ksize, 1])
ones = tf.ones([1, ksize])

mat = tf.matmul(ys, ones)
plt.imshow(mat.eval(), cmap = 'gray')
plt.show()

# Multiply with the gaussian kernel
# kernel is 4 dimensional, res2d is the 2d version
gabor = tf.matmul(mat, res2d)
plt.imshow(gabor.eval(), cmap = 'gray')
plt.show()


I am working on the file "Introduction/Tutorial01_HelloWorld.ipynb".
The first task is:
Agora que já temos nosso dataset separado, vamos criar o classificador e treina-lo com os dados de treinamento.
Can you write Python code for it?

clf = tree.DecisionTreeClassifier()
clf.fit(train_data, train_target)


I am working on the file "Introduction/Tutorial01_HelloWorld.ipynb".
The first task is:
Como estamos trabalhando com o aprendizado supervisionado, podemos comparar com o target que já conhecemos da base de teste.
Can you write Python code for it?


I am working on the file "Introduction/Tutorial01_HelloWorld.ipynb".
The first task is:
Construindo e testando um modelo de treinamento
Uma vez que já temos nossa base de dados, o próximo passo é construir nosso modelo de aprendizagem de máquina capaz de utilizar o dataset. No entanto, antes de construirmos nosso modelo é preciso saber qual modelo desenvolver e para isso precisamos definir qual o nosso propósito na tarefa de treinamento.
Existem vários tipos de tarefas dentro da aprendizagem de máquina. Como dito anteriormente, vamos trabalhar com a tarefa de classificação. A classificação consiste em criar um modelo a partir de dados que estejam de alguma forma classificados. O modelo gerado é capaz de determinar qual classe uma instância pertence a partir dos dados que foram dados como entrada.
Na apresentação do dataset da Iris vimos que cada instância é classificada com um tipo (no caso, o tipo da espécie a qual a planta pertence). Sendo assim, vamos tratar esse problema como um problema de classificação. Existem outras tarefas dentro da aprendizagem de máquina, como: clusterização, agrupamento, dentre outras. Mais detalhes de cada uma deles serão apresentados na aula de aprendizagem de máquina.
O passo seguinte é construir o modelo. Para tal, vamos seguir 4 passos:

Passo 1: Importar o classificador que deseja utilizar
Passo 2: Instanciar o modelo
Passo 3: Treinar o modelo
Passo 4: Fazer predições para novos valores

Nessa apresentação, vamos continuar utilizando o modelo de Árvore de Decisão. O fato de usá-la nesta etapa é que é fácil visualizar o que o modelo está fazendo com os dados.
Para nosso exemplo, vamos treinar o modelo com um conjunto de dados e, em seguida, vamos testá-lo com um conjunto de dados que não foram utilizados para treinar. Para isso, vamos retirar algumas instâncias da base de treinamento e usá-las posteriormente para testá-la. Vamos chamar isso de dividir a base em base de treino e base de teste. É fácil perceber que não faz sentido testarmos nosso modelo com um padrão que ele já conhece. Por isso, faz-se necessária essa separação.
Can you write Python code for it?

import numpy as np

# Determinando os índices que serão retirados da base de treino para formar a base de teste

test_idx = [0, 50, 100] # as instâncias 0, 50 e 100 da base de dados

# Criando a base de treino

train_target = np.delete(dataset_iris.target, test_idx)
train_data = np.delete(dataset_iris.data, test_idx, axis=0)

# Criando a base de teste
test_target = dataset_iris.target[test_idx]
test_data = dataset_iris.data[test_idx]

print("Tamanho dos dados originais: ", dataset_iris.data.shape) #np.delete não modifica os dados originais
print("Tamanho do treinamento: ", train_data.shape)
print("Tamanho do teste: ", test_data.shape)


I am working on the file "Introduction/Tutorial01_HelloWorld.ipynb".
The first task is:
Ele classificou essa fruta como sendo uma Laranja.
HelloWorld++
Vamos estender um pouco mais esse HelloWorld. Claro que o exemplo anterior foi só para passar a idéia de funcionamento de um sistema desse tipo. No entanto, o nosso programa não está aprendendo muita coisa já que a quantidade de exemplos passada para ele é muito pequena. Vamos trabalhar com um exemplo um pouco maior.
Para esse exemplo, vamos utilizar o Iris Dataset. Esse é um clássico dataset utilizado na aprendizagem de máquina. Ele tem o propósito mais didático e a tarefa é classificar 3 espécies de um tipo de flor (Iris). A classificação é feita a partir de 4 características da planta: sepal length, sepal width, petal length e petal width.
<img src="http://5047-presscdn.pagely.netdna-cdn.com/wp-content/uploads/2015/04/iris_petal_sepal.png" />
As flores são classificadas em 3 tipos:  Iris Setosa, Iris Versicolor e Iris Virginica.
Vamos para o código ;)
O primeiro passo é carregar a base de dados. Os arquivos desta base estão disponíveis no UCI Machine Learning Repository. No entanto, como é uma base bastante utilizada, o ScikitLearn permite importá-la diretamente da biblioteca.
Can you write Python code for it?

from sklearn.datasets import load_iris

dataset_iris = load_iris()


I am working on the file "Introduction/Tutorial01_HelloWorld.ipynb".
The first task is:
Imprimindo as características:
Can you write Python code for it?


I am working on the file "Introduction/Tutorial01_HelloWorld.ipynb".
The first task is:
O classificador foi treinado, agora vamos utiliza-lo para classificar as instâncias da base de teste.
Can you write Python code for it?


I am working on the file "Introduction/Tutorial01_HelloWorld.ipynb".
The first task is:
Observe que, neste caso, nosso classificador teve uma acurácia de 100% acertando todas as instâncias informadas. Claro que esse é só um exemplo e normalmente trabalhamos com valores de acurácias menores que 100%. No entanto, vale ressaltar que para algumas tarefas, como reconhecimento de imagens, as taxas de acurácias estão bem próximas de 100%.
Visualizando nosso modelo
A vantagem em se trablhar com a árvore de decisão é que podemos visualizar exatamente o que modelo faz. De forma geral, uma árvore de decisão é uma árvore que permite serparar o conjunto de dados. Cada nó da árvore é "uma pergunta" que direciona aquela instância ao longo da árvore. Nos nós folha da árvore se encontram as classes. Esse tipo de modelo será mais detalhado mais a frente no nosso curso.
Para isso, vamos utilizar um código que visualiza a árvore gerada.
Can you write Python code for it?

from IPython.display import Image
import pydotplus

dot_data = tree.export_graphviz(clf, out_file=None,
                         feature_names=dataset_iris.feature_names,
                         class_names=dataset_iris.target_names,
                         filled=True, rounded=True,
                         special_characters=True)
graph = pydotplus.graph_from_dot_data(dot_data)
Image(graph.create_png(), width=800)


I am working on the file "Introduction/Tutorial01_HelloWorld.ipynb".
The first task is:
Observer que o classificador recebe com parâmetro as features e os labels. Esse classificador é um tipo de classificador supervisionado, logo precisa conhecer o "gabarito" das instâncias que estão sendo passadas.
Uma vez que temos o modelo construído, podemos utiliza-lo para classificar uma instância desconhecida.
Can you write Python code for it?

# Peso 160 e Textura Irregular. Observe que esse tipo de fruta não está presente na base de dados.
print(clf.predict([[160, 0]]))


I am working on the file "Introduction/Tutorial01_HelloWorld.ipynb".
The first task is:
Vamos agora criar um modelo baseado nesse conjunto de dados. Vamos utilizar o algoritmo de árvore de decisão para fazer isso.
Can you write Python code for it?

from sklearn import tree
clf = tree.DecisionTreeClassifier()


I am working on the file "IntroductionIOAsync/IntroductionIOAsync.ipynb".
The first task is:
Création d'un Socket Asynchrone
Can you write Python code for it?

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

sock.setblocking(False)
try:
    sock.connect(("etsmtl.ca" , 80))
except socket.error:
    pass # L'exception est toujours lancé!
         # C'est normal, l'OS veut nous avertir que
         # nous ne sommes pas encore connecté


I am working on the file "IntroductionIOAsync/IntroductionIOAsync.ipynb".
The first task is:
Exemple d'un Socket client qui envoi des données
Can you write Python code for it?

msg = b'GET /ETS/media/Prive/logo/ETS-rouge-devise-ecran.jpg HTTP/1.1\r\nHost:etsmtl.ca\r\n\r\n'
sock.sendall(msg)


I am working on the file "IntroductionIOAsync/IntroductionIOAsync.ipynb".
The first task is:
Exemple d'un Socket qui reçoit des données
Can you write Python code for it?

recvd = b''
while True:
    data = sock.recv(1024)
    if not data:
        break
    recvd += data

sock.shutdown(1)
sock.close()

response = recvd.split(b'\r\n\r\n', 1)
Image(data=response[1])


I am working on the file "IntroductionStreamingAdaptatif/Introduction au streaming adaptatif.ipynb".
The first task is:
Exemple réel
Le contenu des segments reçu par HLS est "pipé" dans GStreamer
Même, si GStreamer possède un buffer d'entrée, nous utilisons quand même un buffer interne pour ne pas blocker un téléchargement lorsque le buffer de GStreamer est plein.
L'appel de player.stdin.write étant bloquant, ceci va servir de mécanisme de controlle de flux pour les requêtes HLS.
Can you write Python code for it?

from subprocess import Popen, PIPE, STDOUT

hls = HLS('http://192.168.3.14:8000/LlamaDrama.m3u8')
player = Popen("/usr/local/bin/gst-play-1.0 fd://0".split(), stdout=PIPE, stdin=PIPE)

for segment in hls:
    player.stdin.write(segment.getvalue())


I am working on the file "IntroductionStreamingAdaptatif/Introduction au streaming adaptatif.ipynb".
The first task is:
Llama Drama High (1920x1080)

1024 kbits/secondes
Le Manifeste
HLS utilise le format m3u8
/LlamaDrama.m3u8
```#EXTM3U
EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=256000
http://localhost:8000/low/LlamaDrama.m3u8
EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=512000
http://localhost:8000/medium/LlamaDrama.m3u8
EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1024000
http://localhost:8000/high/LlamaDrama.m3u8```
Can you write Python code for it?

from collections import namedtuple
from io import BytesIO
from requests import get
import m3u8
from time import time
from io import BytesIO
from subprocess import call

Stream = namedtuple('Stream',['bandwidth', 'uri'])


I am working on the file "IntroductionStreamingAdaptatif/Introduction au streaming adaptatif.ipynb".
The first task is:
Playlist
Chaque playlist pointe les segments de la séquence
/high/LlamaDrama.m3u8
```#EXTM3U
EXT-X-VERSION:3
EXT-X-TARGETDURATION:4
EXT-X-MEDIA-SEQUENCE:0
EXTINF:4.000000,
http://localhost:8000/high/LlamaDrama0.ts
EXTINF:4.000000,
http://localhost:8000/high/LlamaDrama1.ts
EXTINF:4.000000,
http://localhost:8000/high/LlamaDrama2.ts
EXTINF:4.000000,
http://localhost:8000/high/LlamaDrama3.ts
...
EXTINF:4.000000,
http://localhost:8000/high/LlamaDrama21.ts
EXTINF:1.875000,
http://localhost:8000/high/LlamaDrama22.ts
EXT-X-ENDLIST```
Client HLS
La classe HLS permet d'itérer a traver les segments.
Le temps est mesuré, lors du téléchargment du segment.
En combinant le temps et la taille du fichier, on obtient la vitesse.
Le StreamEngine choisit le stream approprié en fonction de la vitesse.

@startuml
skinparam style strictuml
skinparam dpi 300
class HLS << iterable >> {
    ByteIO next()
}
class StreamEngine {
    Stream selectStream(speed)
}
HLS -right-> StreamEngine
@enduml
Classe HLS
Le patron Iterator est utilisé pour itérer à travers l'ensemble des segments de la séquence.
Can you write Python code for it?

class HLS:

    speed = 0 # Bits / second
    i = 0

    def __init__(self, uri):
        self.selector = StreamEngine(uri)

    def __iter__(self):
        return self

    def __next__(self):
        stream = self.selector.selectStream(self.speed)

        if self.i < len(stream.segments):
            startTime = time()
            buf = getSegment(stream.segments[self.i])
            self.speed = round((buf.getbuffer().nbytes*8) / (time() - startTime))
            print('%d bits/s' %self.speed)
            self.i += 1
            return buf
        else:
            raise StopIteration


I am working on the file "IntroductionStreamingAdaptatif/Introduction au streaming adaptatif.ipynb".
The first task is:
StreamEngine
Le StreamEngine choisi le prochain segment en fonction de la vitesse de transfert
Can you write Python code for it?

class StreamEngine:

    currentStream = None
    streamM3 = None
    streams = None

    def __init__(self, uri):
        self.streams = sorted([Stream(playlist.stream_info.bandwidth, playlist.uri)
                               for playlist in m3u8.load(uri).playlists])
        self.currentStream = self.streams[0]
        self.streamM3 = m3u8.load(self.currentStream.uri)

    def selectStream(self, speed):
        newStream = self.currentStream

        for stream in self.streams:
            if stream.bandwidth < speed:
                newStream = stream
            else:
                break

        if newStream != self.currentStream:
            self.currentStream = newStream
            self.streamM3 = m3u8.load(newStream.uri)
            print('Changing Streams: New BitRate %d' %newStream.bandwidth)

        return self.streamM3

def getSegment(segment):
    buf = BytesIO()
    r = get(segment.uri, stream=True)
    for chunk in r.iter_content(chunk_size=2048):
        if chunk:
            buf.write(chunk)
    return buf


I am working on the file "IntroductionToBayesianInference.ipynb".
The first task is:
Bayesian GMM
We can also work the Bayesian inference for GMM by putting a prior over the GMM parameters. Let $\Theta$ be the set of parameters of the GMM:
$$
\Theta = { \boldsymbol{\mu}, \boldsymbol{\lambda}, \boldsymbol{\pi} }
$$
The prior over the weights $\boldsymbol{\pi}$ will be a Dirichlet distribution:
$$
\DeclareMathOperator{\Dir}{Dir}
p(\boldsymbol{\pi}) = \Dir(\boldsymbol{\pi} \mid \boldsymbol{\alpha}_0)
$$
and the prior of the mean and precision of the component $k$ of the mixture will be a Normal-Gamma distribution:
$$
p(\mu_k, \lambda_k) = \Norm(\mu_k \mid m_0, (\kappa_0 \lambda_k)^{-1}) \Gam(\lambda_k \mid a_0, b_0)
$$
The joint distribution of the data, the latent variables and the parameters can be written as:
$$
\begin{align}
p(\mathbf{x}, \mathbf{z}, \Theta) &= p(\mathbf{x}, \mathbf{z} \mid \Theta)p(\Theta) \
                      &= \Bigg[ \prod_{i=0}^{N} p(x_i \mid \boldsymbol{\mu}, \boldsymbol{\lambda}, \boldsymbol{\pi}) \Bigg] \Bigg[ \Dir(\boldsymbol{\pi} \mid \boldsymbol{\alpha}0) \prod{k=0}^{K} \Norm(\mu_k \mid m_0, (\kappa_0 \lambda_k)^{-1}) \Gam(\lambda_k \mid a_0, b_0) \Bigg]
\end{align}
$$
Gibbs Sampling:
The EM algorithm cannot be applied directly to train the Bayesian GMM. Instead, we will use Gibbs Sampling (GS) to learn the posterior distribution of the parameters. Gibbs Sampling is a simple way to sample values from a complex distribution. Let's say that we want to sample values for the random variable $A$ and $B$ conditioned on $C$. Unfortunately, $p(A, B \mid C)$ is too complex to be sampled from directly. Alternatively we can sample in turn $a$ from $p(A | B = b, C)$ and $b$ from $p(B | A=a, C)$. It can be proven that if we keep sampling  long enough, the set of  $a$ and $b$  will be distributed according to $p(A, B | C)$. This is the Gibbs Sampling algorithm.
NOTE: Gibbs Sampling is not always applicable as we cannot always sample from the conditional distributions (it may be intractable).
In the following we just show the 3 conditional distributions we need for the Gibbs...
Can you write Python code for it?

# hyper-parameters [pi_1, pi_2, ...], m, kappa, a, b
# you may try to change this.
bgmm = models.BayesianGMM([1, 1, 1], 0, 1, 2, 1)

y_min = -0.01
y_max = .4
fig, ax = plotting.plotGMM(true_model, y_min=y_min, y_max=y_max, label='True model')

for i in range(5):
    # Sample the latent variables
    Z = bgmm.sampleLatentVariables(X)

    # Update the parameters
    bgmm.sampleMeansVariances(X, Z)
    bgmm.sampleWeights(Z)

    # Just for plotting, this is not part of the Gibbs Sampling algorithm.
    plotting.plotGMM(bgmm.gmm, fig=fig, ax=ax, color='b', lw=.5, label='sampled GMM')

gmm_avg = bgmm.averageGMM()
plotting.plotGMM(gmm_avg, fig=fig, ax=ax, color='r', lw=3, label='avg GMM')
ax.plot(X, Y, '+', color='b', label='data')
handles, labels = ax.get_legend_handles_labels()
labels, ids = np.unique(labels, return_index=True)
handles = [handles[i] for i in ids]
ax.legend(handles, labels, loc='upper left')


I am working on the file "IntroductionToBayesianInference.ipynb".
The first task is:
Because the Normal-Gamma is the conjugate prior of the Normal density, the posterior distribution $p(\mu, \lambda \mid \mathbf{x})$ has a closed form solution:
$$
p(\mu, \lambda \mid \mathbf{x}) = \Norm(\mu \mid m_n, (\kappa_n \lambda)^{-1}) \Gam(\lambda \mid a_n, b_n)
$$
where:
$$
\begin{align}
m_n &= \frac{\kappa_0 m_0 + N \bar{x}} {\kappa_0 +  N} \
\kappa_n &= \kappa_0 + N \
a_n &= a_0 + \frac{N}{2} \
b_n &= b_0 + \frac{N}{2} ( s + \frac{\kappa_0 (\bar{x} - m_0)^2}{\kappa_0 + N} ) \
\bar{x} &= \frac{1}{N} \sum_i x_i \
s &= \frac{1}{N} \sum_i (x_i - \bar{x})^2
\end{align}
$$
$N$ is the total number of point in the the training data and $m_n$, $\kappa_n$, $a_n$ and $b_n$ are the parameters of the posterior. Note that they are different from the hyper-parameters !!
TODO
Compute the posterior distribution with 1, 5, 10, 20 and 50  data points from the training data. What do you observe ?
Can you write Python code for it?

ng_prior = models.NormalGamma(0, 2, 5, 6)
ng_posterior = ng_prior.posterior(X[:1]) # <- Change '1' to the number of sample you want to use
x_min =-2
x_max = 2
y_min = 0.
y_max = 2
plotting.plotNormalGamma(ng_posterior, x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max)


I am working on the file "IntroductionToBayesianInference.ipynb".
The first task is:
Gaussian density
We can first try to model the observed data with a Gaussian density function. The Gaussian density function is defined as:
$$
\DeclareMathOperator{\Norm}{\mathcal{N}}
\DeclareMathOperator{\Gam}{Gam}
\DeclareMathOperator{\e}{exp}
p(x \mid \mu, \sigma^2) = \Norm(x \mid \mu, \sigma^2) = \frac{1}{\sqrt{2\pi\sigma^2}} \e{ \frac{-(x - \mu)^2}{2\sigma^2} }
$$
* $\mu$ is the mean of the Gaussian density
* $\sigma^2$ is the variance of the Gaussian density
TODO
Try to play with the mean and the variance of the Gaussian. How each parameter change the density ?
Can you write Python code for it?

gaussian = models.Gaussian(0., 1.) # <- 1st parameter is mean, second variance0
fig, ax = plotting.plotGaussian(gaussian)


I am working on the file "IntroductionToBayesianInference.ipynb".
The first task is:
Predictive probability
Now that we have our posterior distibution we can predict the probability of a new data point given the training data:
$$
p(x' \mid \mathbf{x}) = \int p(x' \mid \theta) p(\theta \mid \mathbf{x}) d \theta
$$
For the Gaussian with Normal-Gamma prior the marginal predictive distribution is the Student's t distribution:
$$
\newcommand{\diff}{\mathop{}!d}
\DeclareMathOperator{\St}{\mathcal{St}}
p(x' \mid \mathbf{x}) = \int_{-\infty}^{\infty} p(x|\mu, \lambda) p(\mu, \lambda \mid \mathbf{x}) \diff \mu \diff \lambda  = \St(x' \mid \mu_n, \nu, \gamma)
$$
where:
$$
\begin{align}
\nu &= 2a_n \
\gamma &= \frac{a_n \kappa_n}{b_n(\kappa_n + 1)} \
\St(x' \mid \mu_n, \nu, \gamma) &= \frac{\Gamma(\frac{\nu}{2} + \frac{1}{2})}{\Gamma(\frac{\nu}{2})} \Big( \frac{\gamma}{\pi \nu} \Big)^{\frac{1}{2}} \Big[ 1 + \frac{\gamma (x - \mu_n)^2}{\nu} \Big]^{-\frac{\nu}{2} - \frac{1}{2}}
\end{align}
$$
TODO
Compute the log-likelihood of the predictive distribution derived from a posterior trained with 1, 5, 10, 20  and 50 data points. How does it compare to the log-likelihood of the Gaussian trained with maximum likelihood ?
Can you write Python code for it?

ng_posterior = ng_prior.posterior(X[:1]) # <- Change '1' by the number of sample you want to use.
predict_pdf = ng_posterior.predictiveDensity()
llh = predict_pdf.logLikelihood(X)
print('log-likelihood:', llh)


I am working on the file "IntroductionToBayesianInference.ipynb".
The first task is:
Training data
In real scenario, we  observed a set of values, often called features, and we try to learn some model that can "explain" the observed features. Generally, we do not know the true distribution of the features. For this work we will simulate a set of single dimensional features sampled from a GMM. We will call this GMM the "true model" and in this notebook we will see different strategies to approximate the true model from the simulated training data.
Can you write Python code for it?

true_model = models.GMM([-4, 0, 2], [1, 2, .7], [0.2, 0.2, 0.6])
X = true_model.sampleData(50)
Y = np.zeros_like(X) # The random variable has a single dimension !
x_min = -8
x_max = 7
y_min = -0.01
y_max = .4
fig, ax = plotting.plotGMM(true_model, x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max,
                           label='True model')
ax.plot(X, Y, '+', color='b', label='data')
ax.legend(loc='upper left')


I am working on the file "IntroductionToPyConTextNLP.ipynb".
The first task is:
However, sentence splitting is a common NLP task and so most full-fledged NLP applications provide sentence splitters. We usually rely on the sentence splitter that is part of the TextBlob package, which in turn relies on the Natural Language Toolkit (NLTK). So before proceeding we need to download some NLTK resources with the following command.
Can you write Python code for it?


I am working on the file "IntroductionToPyConTextNLP.ipynb".
The first task is:
Sentence Splitting
pyConTextNLP operates on a sentence level and so the first step we need to take is to split our document into individual sentences. pyConTextNLP comes with a simple sentence splitter class.
Can you write Python code for it?

import pyConTextNLP.helpers as helpers
spliter = helpers.sentenceSplitter()
spliter.splitSentences("This is Dr. Chapman's first sentence. This is the 2.0 sentence.")


I am working on the file "IntroductionToPyConTextNLP.ipynb".
The first task is:
The task: Identify patients with pulmonary embolism from radiology reports
Step 1: how is the concept of pulmonary embolism represented in the reports - fill in the list below with literals you want to use.
Can you write Python code for it?

mytargets = itemData.itemData()
mytargets.extend([["pulmonary embolism", "CRITICAL_FINDING", "", ""],
                   ["pneumonia", "CRITICAL_FINDING", "", ""]])

print(mytargets)

!pip install -U radnlp==0.2.0.8


I am working on the file "Introduction_DILLMANN_ISEP2016.ipynb".
The first task is:
1.1) Declarative Knowledge on Palindromas
## "A palindroma is a word that can be spelled from both sides"

a palindroma is a character's string containing a chain of lenght n

"abc...cba" is a palindroma

"aba" is a palindroma

any word with two identical letters is a palindroma

1.2) Imperative Knowledge to check that a word is a Palindroma:

The word must be declared as a variable that alows to chain letters (string)
The lenght of the string must be known using a build in function len
The string must be readed letter by letter from both sides untill we meet
Check that both letters are identical, if yes then go to (3)
If both ends have been reached conclude that that word is a "True" Palindroma

* Pasta al dente* Cooking Recipies:

Start puting hot water on the pot add salt
When $T$ of the water is greater than 100$°$ Celcius put 150mg of Pasta
Otherwise check that the water is heating up properly and wait
When the Pasta is in the cooker wait until it is  al dente
Can you write Python code for it?

## Check if the french word "ressasser" is a palindroma
# We first declare some variables
word_to_test='ressasser'
# We need to define the indexes in the chain of characters
index_head=0
index_tail=len(word_to_test)-1
# Now we are going to print
print(word_to_test)
print("The lenght of the word is: {}".format(len(word_to_test)))
print("The first letter is: {}".format(word_to_test[index_head]))
print("The last letter: {}".format(word_to_test[index_tail]))

if (word_to_test[index_head]==word_to_test[index_tail]):

    if index_head < index_tail:
        print("Untill now it seems a palindroma")
        index_head=index_head+1
        index_tail=index_tail-1
        print(index_head)
        print(index_tail)
    else: print("The word '{}' is a palindroma".format(word_to_test))
else: print("The word '{}' is NOT a palindroma".format(word_to_test))


I am working on the file "Introduction_DILLMANN_ISEP2016.ipynb".
The first task is:
2.1) Declarative Knowledge on square roots
## "The square root $\displaystyle x=\sqrt{y}$, of a number $y$ is a number $x$ such as:
 # $\displaystyle x \times x = y $ "
2.2) Imperative Knowledge associated to solve square roots
## Here is a Reciple for deductig the square root of a number $ y $, attributed to Heron of Alexandria in the first century AD

Start with a guess for unknown $x$ called $g$
If $g \times g$ is close enought to $y$ : stop and return $g$
Otherwise make a new guess by averaging $g$ and ${y}/{g}$
Using the new guess, repeat the process to point (2)
Can you write Python code for it?

# Give a number and find the first guess
print('Enter the number y, from whom you are asked to find the square root')
y=eval(input('y = '))
print('Give a first guess for x, called g')
g=eval(input('g = '))
print("End of step 1")

print("(The square product of {0} is {1})".format(g,g*g))
print("The square of the average of the guess and y divided by guess is:")
print((0.5*(g+y/g))**2)

print("How accurate is this result ?")
print("Well, the square of your guess is {}".format(g*g))
if g*g == y:
    print("Whoah, it is a good guess")
    print("The solution is:")
    print((0.5*(g+y/g)))
else:
    g=0.5*(g+y/g)
    print("Sorry")
    print("Try again, with, g={} as new guess".format(g))
    print("The average of the new g and y divided by this new g is:")
    print(0.5*(g+y/g))


I am working on the file "Introduction_DILLMANN_ISEP2016.ipynb".
The first task is:
3) Conditional Instructions
-if
-else
-elif
3.1) What is a bissextile year
Can you write Python code for it?

#Check a number
nb = eval(input('Enter a number = '))
if nb < 0:
    print('{} is a negative number'.format(nb))
elif nb > 0:
    print('{} is a positive number'.format(nb))
else:
    print('{} is a null'.format(nb))

# Leap years are back all 4 years, except secular years if these are not multiple of 400
n = eval(input('Enter a year = '))
if n % 4 != 0:
    print('Year {} is NOT a bissextile year'.format(n))
elif n % 100 != 0:
    print('Year {} is a bissextile year'.format(n))
elif n % 400 != 0:
    print('Year {} is NOT a bissextile year'.format(n))
else:
    print('Year {} is a bissextile year'.format(n))

if ((n % 4 == 0) and (n % 100 != 0)) or (n % 400 == 0): print('Year {} is a bissextile year'.format(n))
else: print('Year {} is NOT a bissextile year'.format(n))


I am working on the file "Introduction_DILLMANN_ISEP2016.ipynb".
The first task is:
4) Usual operators
Putting statements x and y in relations trough the operators is the basics of formal logic
4.1) Comparison operators
| Sign | Meaning             |
|-----|----------------------|
| x == y | x Is equal to y             |
| x != y | x Is Not equal y        |
| x > y  | x Is greater than y          |
| x < y  | x Is smaller than y         |
| x >= y | x Is greater or equal than y  |
| x <= y | x Is smaller or equal than y  |
| -x | opposite to x (when x a number)|
| +x | positive to x (when x a number)|
| ~x | symmetric to x (when x a number)|
4.2) Logical operators
|relation between expressions| meaning|
|----------|--------|
| (x) and (y)  | x and y are expressions SIMULTANEOUSLY both True |
| not (x) | the negation of x is True |
| (x) or (y) | at least x or y is True INDEPENDENTELY |
Can you write Python code for it?

a=6
print(~a)

# There is an operator for defining an 'implication'
# p implies q means that
# p is a sufficient condition for q to be true
# p --> q is equivalent as (not(p) or q)

Je_fais_de_longues_etudes=True
Je_deviens_bon=False

# Je_fais_de_longues_etudes --> Je_deviens_bon
# This can be writen as the following proposition :
(not(Je_fais_de_longues_etudes) or (Je_deviens_bon))


I am working on the file "Introduction_DILLMANN_ISEP2016.ipynb".
The first task is:
4.3) Arithmetic operators
| Number operations |Arithmetics|
|-----|----------------------|
| y*x | multiplication of y times  x |
| y/x | division of y by x (both numbers)|
| y//x | floor division of y by x |
| y%x | Modulus, returns the decimal part (remainder) of the quotient of y divided by x.|
Can you write Python code for it?

a=10
b=3
c=a // b
d=a % b
print(a)
print(type(a))
print(b)
print(type(b))
print(c)
print(type(c))
print(d)
print(type(d))


I am working on the file "Introduction_DILLMANN_ISEP2016.ipynb".
The first task is:
4.4) Operations on lists and characters
| Character operation | Signification |
|---------------------|---------------|
| 'y'*x | repeat 'y' x times (when y a character)|
| x in y  | x belong to y (when y is a list)  |
Can you write Python code for it?

'y'*3

a='Je ne dois pas consulter Facebook en cours' + '//'
a*5

a = 5
b = 20
(a < b)


I am working on the file "Introduction_to_Algorithms/16_Greedy_Algorithms/note.ipynb".
The first task is:
Making the greedy choice
Intuition:
we should choose an activity that leaves the resource available for as many other activities as possible.
Hence, the first one should be the activity in $S$ with the earliest finish time.
Is the greedy choice always part of some optimal solution?
Theorem 16.1:
Let $S_k = {a_i \in S: s_i \geq f_k}$.
We have $a_m$ s.t. $f_m = min(f_i : a_i \in S)$
Then $a_m$ must be included in some maximum-size subset of mutually compatible activities of $S_k$.
Proof: (构造法）
Let $A_k$ be a maximum-size subset, and $a_j$ s.t. $f_j = min(f_i: a_i in A_k)$.
Because $f_m \leq f_j$,
hence we could replace $a_j$ with $a_m$, namely, $A'_k = (A_k - a_j) \cup a_m$.
then we get a new maximum-size subset $A'_k$ which includs $a_m$.
An recursive greedy algorithm
tail recursive
An iterative greedy algorithm
Can you write Python code for it?

df

def greedy_activity_selector(df):
    n = df.shape[1] + 1
    k = 1
    A = [k]

    for m in xrange(2, n):
        if df.loc['s', m] >= df.loc['f', k]:
            A.append(m)
            k = m
    return A

greedy_activity_selector(df)


I am working on the file "Introduction_to_Algorithms/18_B_Trees/note.ipynb".
The first task is:
18.1 Definition of B-trees
Properties

node attributes:
$x.n$: the number of keys.
$x.\text{keys}$: $x.\text{key}_1 \leq x.\text{key}_2 \leq \dotsb \leq x.\text{key}_n$.

$x.leaf$: True if it is, False otherwise.

Each internal node has $x.n+1$ points $x.c_1, \dotsc, x.c_{x.n+1}$ to its children.

$x.c_i.keys \leq x.key_i \leq x.c_{i+1}.keys$

All leaves have the same depth $h$.

minimum degree of the B-tree $T$: a fixed integer $t \geq 2$.

Every node other than the root must have at least $t-1$ keys.
Every node may contain at most $2t-1$ keys. FULL.

The simplest B-tree occurs when $t=2$: 2-3-4 tree.
The worst-case height of B-tree
If $n \geq 1$, then for any $n$-key B-tree $T$ of height $h$ and minimum degree $t \geq 2$.
$$h \leq \log_t \frac{n+1}{2}$$
Proof:
The root of a B-tree $T$ contains at least one key, and all other nodes contain at least $t-1$ keys.
Can you write Python code for it?


I am working on the file "Introduction_to_Algorithms/18_B_Trees/note.ipynb".
The first task is:
18.3 Deleting a key from a B-tree

When going down, if the node's child has only $t-1$ keys, move its key down to the child.

if the node is root, then delete the root, use its child as new root. (Height -1).

If the node to delete is a leaf node, then directly remove it.

If the node to delete is a internal node, then

If the key's left child has more than $t-1$ keys, move up the lastest key.
If the key's right child has more than $t-1$ keys, move up the first key.
otherwise, the sum of left and right child's keys is less than $2t-1$, merge them as a new node.

Base on the three priciples, we summary all possible cases as follows:
Can you write Python code for it?

plt.figure(figsize=(10,20))
plt.subplot(2,1,1)
plt.imshow(plt.imread('./res/fig18_8.png'))
plt.subplot(2,1,2)
plt.imshow(plt.imread('./res/fig18_9.png'))

#todo: exercise


I am working on the file "Introduction_to_Algorithms/18_B_Trees/note.ipynb".
The first task is:
Creating an empty B-tree
$O(1)$ time to allcate one disk page.
Can you write Python code for it?


I am working on the file "Introduction_to_Algorithms/18_B_Trees/note.ipynb".
The first task is:
If $T$ is full tree, then create a new root node, and split them. (Height increase 1).
(b)
```
B-TREE-INSERT(T,K)
    if T is FULL:
        S = B-TREE-CREATE()
        S.children = T
        R = B-TREE-SPLIT-CHILD(S)
    else
        R = T
B-TREE-INSERT-NONFULL(R,K)

```
Can you write Python code for it?


I am working on the file "Introduction_to_Algorithms/18_B_Trees/note.ipynb".
The first task is:
Inserting a key into a B-tree
As we trave down the tree, we split each full node we meet, in order to assure that the parent is always not full whenever we want to split a full node.
(a) B-TREE-SPLIT-CHILD(X,I)
Can you write Python code for it?


I am working on the file "Introduction_to_Algorithms/18_B_Trees/note.ipynb".
The first task is:
the number $n$ of keys statisfies the inequality:
$$ n \geq 1 + (t-1) \sum_{i=1}^h 2t^{i-1} = 2t^h -1$$
B-trees save a factor of about $\lg t$ over red-black trees in the number of nodes examined for most tree operations.
%maybe: exercise
18.2 Basic operations on B-trees
two conventions:

The root of the B-tree is always in main memory.

Any nodes that are passed as parameters must already have had a Disk-Read operation performed on them.

Searching a B-tree
We make a multiway branching decision according to the number of the node's children. $O(t lg_t n) = O(t) \times O(h)$.
Can you write Python code for it?


I am working on the file "Introduction_to_Algorithms/21_Data_Structures_for_Disjoint_Sets/note.ipynb".
The first task is:
MAKE-SET and FIND-SET requires $O(1)$ time.
note: FIND-SET(x), where $x$ is the pointer to the object expected.
UNION:

randomly merge: append one to another list, and update the related pointer. $O(n)$.

construct a sequence of $m$ operations on $n$ objects that requires $O(n^2)$ time.

weighted-union heuristic: append the shortest list onto the longer. $\Omega(n)$

each list need to include the length of the list.

construct a sequence of $m$ operations on $n$ objects that requires $O(m + n \lg n)$ time.
Can you write Python code for it?


I am working on the file "Introduction_to_Algorithms/21_Data_Structures_for_Disjoint_Sets/note.ipynb".
The first task is:
When we use both union by rank and path compression, the worst-case running time is $O(m \alpha(n))$, where $\alpha(n)$ is a very slowly growing function, which we define in Section 21.4.
Can you write Python code for it?


I am working on the file "Introduction_to_Algorithms/21_Data_Structures_for_Disjoint_Sets/note.ipynb".
The first task is:
path compression
FIND-SET: We make each node on the find path point directl to the root.
Can you write Python code for it?

plt.imshow(plt.imread('./res/fig21_5.png'))

# Pseudocode for disjoint-set forests
plt.imshow(plt.imread('./res/forest.png'))


I am working on the file "Introduction_to_Algorithms/23_Minimum_Spanning_Trees/note.ipynb".
The first task is:
Prim's algorithm
The set $A$ forms a single tree. The safe edge added to $A$ is always a least-weight edge connecting the tree to a vertex not in the tree.
In order to implement Prim’s algorithm efficiently, we need a fast way to select a new edge to add to the tree formed by the edges in $A$:
all vertices that are not in the tree reside in a min-priority queue $Q$ based on a $key$ attribute. For each vertex $v$, the attribute $v.\text{key}$ is the minimum weight of any edge connecting $v$ to a vertex in the tree.
$$A = \left {(v, v.\pi) : v \in V - {r} - Q \right }$$
Can you write Python code for it?

plt.imshow(plt.imread('./res/prim.png'))

plt.figure(figsize=(8,12))
plt.imshow(plt.imread('./res/fig23_5.png'))

# Exercise


I am working on the file "Introduction_to_Algorithms/23_Minimum_Spanning_Trees/note.ipynb".
The first task is:
rule for recognizing safe edges
define:

$\operatorname{cut}(S, V-S) \subset G$.

We say an edge $(u, v)$ crosses the cut $(S, V-S)$ if $u \in S, v \in V - S$.

We say a cut respects a set $A$ of edges if no dege in $A$ crosses the cut.

light edge: its weight is the minimum of any edge crosing the cut.

Our rule for recognizing safe edges is given by the following theorem:
Theorem 23.1
Let $G = (V, E)$ be a connected, undirected graph with a real-valued weight function $w$ defined on $E$. Let $A$ be a subset of $E$ that is included in some minimum spanning tree for $G$, let $(S, V - S)$ be any cut of $G$ that respects $A$, and let $(u, v)$ be a light edge crossing $(S, V - S)$. Then, edge $(u, v)$ is safe for $A$.
proof: construct.
details seen in textbook.
Corollary 23.2
Let $G = (V, E)$ be a connected, undirected graph with a real-valued weight function $w$ defined on $E$. Let $A$ be a subset of $E$ that is included in some minimum spanning tree for $G$, and let $C = (V_C, E_C)$ be a connected component (tree) in the forest $G_A = (V, A)$. If $(u, v)$ is a light edge connecting $C$ to some other component in $G_A$, then $(u, v)$ is safe for $A$.
Can you write Python code for it?


I am working on the file "Introduction_to_Algorithms/24_Single-Source_Shortest_Paths/note.ipynb".
The first task is:
24.2 Single-source shortest paths in directed acyclic graphs
By relaxing the edges of a weighted dag (directed acyclic graph) $G = (V, E)$ according to a topological sort of its vertices, we can compute shortest paths from a single source in $O(V + E)$ time.
Can you write Python code for it?


I am working on the file "Introduction_to_Algorithms/24_Single-Source_Shortest_Paths/note.ipynb".
The first task is:
24.4 Difference constraints and shortest paths
Linear programming
Systems of difference constraints
In a system of difference constraints, each row of the linear-programming matrix $A$ contains one 1 and one -1, and all other entries of $A$ are 0. $\to$ the form $x_j - x_i \leq b_k$.
Can you write Python code for it?


I am working on the file "Introduction_to_Algorithms/24_Single-Source_Shortest_Paths/note.ipynb".
The first task is:
Constraint graphs
Can you write Python code for it?