I am working on the file "Python/Jupyter notebook/05.Simple Loops/Jupyter notebook/Simple Loops.ipynb".
The first task is:

09.Vowels Sum

Problem:
Write a program that reads a text (string) entered by the user and calculates and prints the amount of the values of the uppercase letters in the table below:
|  letter | a  |  e | i  | o  | u  |
|---|---|---|---|---|---|
|  value | 1  |  2 | 3  | 4  | 5 |

10.Element equal to the sum of the rest

Problem:
Write a program that reads n-number integer numbers entered by the user and checks if among
there is a number that is equal to the sum of all others. If there is such an item, print "Yes ,Sum =  + its value" otherwise the "No", "Diff =  + the difference between the largest element and the sum of the rest" (in absolute terms).
Can you write Python code for it?

import  math

num_of_loops = int(input())

sum = 0
max_num = 0
new_num = 0

for i in range(1 , num_of_loops + 1):

    new_num = int(input())

    if new_num > max_num:
        max_num = new_num

    sum += new_num

if sum / 2 == max_num:
    print(f"Yes, Sum = {max_num}")

else:
    print(f"No, diff = {math.fabs((sum - max_num) - max_num)}")


I am working on the file "Python/Jupyter notebook/05.Simple Loops/Jupyter notebook/Simple Loops.ipynb".
The first task is:

12.The same pairs

Problem:
Given are 2 * n-numbered numbers.
The first and the second form a pair, the third and the fourth also, etc.
Each pair has value - the sum of its numbers.
Write a program that checks if everyone pairs have the same value or print the maximum difference between two consecutive pairs.
If all pairs have the same value,print: "Yes, value = {Value} + value".
Otherwise print "No, maxdiff = {Difference} " + maximum difference.
Can you write Python code for it?

import  math

num_of_loops = int(input())

num_1 = 0
num_2 = 0

first_pair = 0
second_pair = 0

max_num_of_equal_pairs = 0
max_diff = 0
difference = 0

for i in range(1, num_of_loops + 1):

     num_1 = int(input())
     num_2 = int(input())

     if i % 2 != 0:
          first_pair = num_1 + num_2

     else:
          second_pair = num_1 + num_2

     if first_pair == second_pair:
          max_num_of_equal_pairs += 1

     difference = math.fabs(first_pair - second_pair)

     if difference > max_diff and i > 1:
          max_diff = difference

if (max_num_of_equal_pairs + 1) == num_of_loops:
     print(f"Yes, value = {first_pair}")

else:
     print(f"No, maxdiff = {max_diff}")


I am working on the file "Python/Jupyter notebook/06.Drawing Figures with Loops/Jupyter notebook/Drawing Figures with Loops.ipynb".
The first task is:

Drawing Figures with Loops

01.Rectangle of 10 x 10 Stars

Problem:
Write a program that draws a rectangle of 10 x 10 asterisks on the console.
Can you write Python code for it?

asterisk = 10
for i in range(0,10):
    print("*" * asterisk)


I am working on the file "Python/Jupyter notebook/06.Drawing Figures with Loops/Jupyter notebook/Drawing Figures with Loops.ipynb".
The first task is:

03.Square of Stars

Problem:
Write a program that reads the console number n and draws a square of n * n asterisks.
The Difference with the previous task is that there is a space between every two asterisks.
Can you write Python code for it?

n = int(input())

for i in range(0, n):
    print("* " * n)


I am working on the file "Python/Jupyter notebook/06.Drawing Figures with Loops/Jupyter notebook/Drawing Figures with Loops.ipynb".
The first task is:

04.Triangle of Dollars

Problem:
Write a program that enters a number n and triangle prints from dollars.
Can you write Python code for it?

n = int(input())

for i in range(0, n):
    print("$ " * (i + 1))


I am working on the file "Python/Jupyter notebook/06.Drawing Figures with Loops/Jupyter notebook/Drawing Figures with Loops.ipynb".
The first task is:

05.Square Frame

Problem:
Write a program that introduces a positive integer n and draws the console square frame with size n * n.
Can you write Python code for it?

n = int(input())

minuses = n - 2

print("+ " + "- " * minuses + "+")

for i in range(0, n - 2):
    print("| " + "- " * minuses + "|")

print("+ " + "- " * minuses + "+")


I am working on the file "Python/Jupyter notebook/06.Drawing Figures with Loops/Jupyter notebook/Drawing Figures with Loops.ipynb".
The first task is:

06.Rhombus of Stars

Problem:
Write a program that introduces a positive integer n and prints a diamond-sized nugget.
Can you write Python code for it?

n = int(input())

spaces = ' '
asterisk = '*'
for i in range(1, (n * 2) ):

    if i <= n:
        spaces = n - i
        asterisk = i
        print(" " * spaces + "* " * asterisk)

    else:
        spaces = i - n
        asterisk = n * 2 - i
        print(" " * spaces + "* " * asterisk)


I am working on the file "Python/Jupyter notebook/06.Drawing Figures with Loops/Jupyter notebook/Drawing Figures with Loops.ipynb".
The first task is:

07.Christmas Tree

Problem:
Write a program that enters a number n (1 ≤ n ≤ 100) and prints a Christmas tree size n.
Can you write Python code for it?

n = int(input())

spaces = " "

for i in range(0, n + 1):

    spaces = n - i
    print(" " * spaces + "*" * i + " | " + "*" * i + " " * spaces)


I am working on the file "Python/Jupyter notebook/06.Drawing Figures with Loops/Jupyter notebook/Drawing Figures with Loops.ipynb".
The first task is:

09.House

Problem:
Write a program that introduces a number n (2 ≤ n ≤ 100) and prints a cot with a size n x n.
Can you write Python code for it?

n = int(input())

asterisk = "*"
minuses = "-"

for i in range(1, n + 1):

    if n % 2 == 0 and i % 2 == 0:
        asterisk = i
        minuses = (n - i ) // 2
        print("-" * minuses + "*" * asterisk + "-" * minuses)

    elif n % 2 != 0 and i % 2 != 0:
        asterisk = i
        minuses = (n - i) // 2
        print("-" * minuses + "*" * asterisk + "-" * minuses)

for i in range(0 , n // 2):
    print("|" + "*" * (n -2) + "|")


I am working on the file "Python/Jupyter notebook/07.Advanced Loops/Jupyter notebook/07.Advanced Loops.ipynb".
The first task is:

Advanced Loops

01. Numbers 1...N with Step 3

Problem:
Write a program that enters a number and prints the numbers from 1 to n through 3 (step
Can you write Python code for it?

n = int(input())

for i in range(1,n + 1, 3):
    print(i)


I am working on the file "Python/Jupyter notebook/07.Advanced Loops/Jupyter notebook/07.Advanced Loops.ipynb".
The first task is:

02. Numbers N...1

Problem:
Write a program that sets a positive integer n and prints the numbers from n to 1 in reverse order (from the largest to the smallest).
Can you write Python code for it?

n = int(input())

for i in range(n, 0, -1):
    print(i)


I am working on the file "Python/Jupyter notebook/07.Advanced Loops/Jupyter notebook/07.Advanced Loops.ipynb".
The first task is:

03. Powers of Two

Problem:
Write a program that reads from the console an integer n and prints the numbers from 1 to 2n.
Can you write Python code for it?

n = int(input())

for i in range(0, n + 1, 1):

    print(pow(2,i))


I am working on the file "Python/Jupyter notebook/07.Advanced Loops/Jupyter notebook/07.Advanced Loops.ipynb".
The first task is:

06.Number in Range [1...100]

Problem:
Write a program that introduces a positive integer n in the range [1 ... 100].
When entering a number outside the specified range, an error message is printed and the user is prompted to enter a new number.
Can you write Python code for it?

n = int(input('Еnter a number in the range [1...100]: '))

while 1 > n or n > 100:
    print("Invalid number!")
    n = int(input('Еnter a number in the range [1...100]: '))

print("The number is: "+ str(n))


I am working on the file "Python/Jupyter notebook/07.Advanced Loops/Jupyter notebook/07.Advanced Loops.ipynb".
The first task is:

07.Greatest Common Divisor (CGD)

Problem:
Write a program that introduces two integer positive integers a and b and calculates and prints their largest common divisor (NOD).
Can you write Python code for it?

a = int(input())
b = int(input())

while b != 0:

    save = a % b
    a = b
    b = save

print(a)


I am working on the file "Python/Jupyter notebook/07.Advanced Loops/Jupyter notebook/07.Advanced Loops.ipynb".
The first task is:

08.Factorial

Problem:
Write a program that enters an integer n (1 ≤ n ≤ 12) and calculates and prints n! = 1 * 2 * ... * n (n factorial).
Can you write Python code for it?

n = int(input())

result = 1

for i in range(1, n + 1):

    result *= i

print(result)


I am working on the file "Python/Jupyter notebook/07.Advanced Loops/Jupyter notebook/07.Advanced Loops.ipynb".
The first task is:

10.Check Prime

Problem:
Write a program that enters an integer n and checks whether it is a simple number (whether it is divided by itself and per unit).
Print "Prime" or "Not prime".
Can you write Python code for it?

import  math

n = int(input())

is_prime = True

if n < 2:
    print('Not Prime')

else:
    end = int(math.sqrt(n))

    for i in range(2, end + 1):

        if n / i == n//i:
          is_prime = False

if is_prime:
    print('Prime')
else:
    print('Not Prime')


I am working on the file "Python/Jupyter notebook/07.Advanced Loops/Jupyter notebook/07.Advanced Loops.ipynb".
The first task is:

12.Fibonacci

Problem:
Write a program that enters an integer and calculates the Fibonacci nth number.
The zero figure of Fibonacci is 1, the first one is also 1, and the next is the sum of the previous two.
Can you write Python code for it?

n = int(input())

a = 1
b = 2

for i in range(1, n):
    new_b = a + b
    a = b
    b = new_b

print(a)


I am working on the file "Python/LifeCycle.ipynb".
The first task is:
lst1这个引用被我们删除了，但是我们发现，之前创建的那个列表对象依然存在。现在该对象的引用计数器为1。接下来我们把lst2这个引用也删除掉。
Can you write Python code for it?


I am working on the file "Python/LifeCycle.ipynb".
The first task is:
在上面这几行代码中，我们让python给我们分配了一段内存用于存储一个列表的对象，然后我们给这个对象起了两个名字，lst1和lst2.那么这个时候我们通过这两个名字都可以操作到这个对象。
Can you write Python code for it?

lst1.append(11)

print(lst2)

del lst2[0]

lst1


I am working on the file "Python/LifeCycle.ipynb".
The first task is:
在声明类的时候，这个类还没有对象，那么类的成员变量不在内存中，这时候相当于它还没有出生。
如果在初始化类对象的时候，有两种形式.和例1中给函数传对象一样，如果是一个有引用的对象，则在其所有引用都被删除的时候，这个对象会被销毁，如果传的是没有引用的对象，在类对象生命周期结束的时候，其成员变量指向的内存被销毁。
Can you write Python code for it?


I am working on the file "Python/LifeCycle.ipynb".
The first task is:
类成员变量的生命周期
Can you write Python code for it?

class C:
    def __init__(self, a, b):
        self.a = a
        self.b = b
        self.c = 0


I am working on the file "Python/Local-Search-Engine.ipynb".
The first task is:
Implementing a Local Search Engine
This notebook shows one important application of dictionaries and sets:
It implements a local search engine that can be used to index .pdf documents on the local file system.  The index can then be used to search for all documents that contain specified words.  The main data structure used is a so called
inverted index, which is a dictionary mapping words to the
sets of documents that contain these words.
The module subprocess enables us to execute shell command and to capture their output via a pipe from which we can read.
Can you write Python code for it?


I am working on the file "Python/Local-Search-Engine.ipynb".
The first task is:
Let us build an Index for a directory containing some literature regarding my lectures on algorithm.
Can you write Python code for it?

%%time
index = Index()
index.buildIndex("../Literature")


I am working on the file "Python/Local-Search-Engine.ipynb".
The first task is:
Let us check how many different words occur in the file that we have read above.
Can you write Python code for it?


I am working on the file "Python/Local-Search-Engine.ipynb".
The first task is:
The class Document represents a single file.  This class maintains three member variables:
  - path  is the absolut file path specifying the location of the file containing the pdf document,
  - docID is a natural number that serves as a unique identifier for the document,
  - Words is the set of words contained in the file.
This class only serves as a container of its member variables, hence it has no methods.
Can you write Python code for it?

class Document:
    def __init__(self, path, docID, Words):
        self.path  = path
        self.docID = docID
        self.Words = Words


I am working on the file "Python/Local-Search-Engine.ipynb".
The first task is:
The class Index contains three member variables:
  - InvertedIndex is a dictionary that maps every word to the set of documents containing this word.
    In this set, the documents are represented by their unique identifiers.
  - ID2Doc is a dictionary mapping the document identifiers to the corresponding Document objects.
  - fileCount is a counter that is needed to create unique document identifiers.
Can you write Python code for it?

class Index:
    def __init__(self):
        self.InvertedIndex = {}
        self.ID2Doc        = {}
        self.fileCount     = 0


I am working on the file "Python/Local-Search-Engine.ipynb".
The first task is:
The function _addToIndex takes a document identifier $d$ and a set of words $W$ occurring in the document specified by $d$ and extends the InvertedIndex so that for every word $w$ in Words we have that
$$ d \in \texttt{InvertedIndex}[w]. $$
Can you write Python code for it?

def _addToIndex(self, documentID, Words):
    for term in Words:
        try:
            docSet = self.InvertedIndex[term]
            docSet.add(documentID)
        except KeyError:
            self.InvertedIndex[term] = { documentID }

Index._addToIndex = _addToIndex


I am working on the file "Python/Local-Search-Engine.ipynb".
The first task is:
The function get_text takes a path specifiying a .pdf file.  It converts the .pdf file into a text file and returns the
resulting text.  This function assumes that the program pdftotext is installed.  This program can be dowloaded at
https://www.xpdfreader.com/download.html.
Can you write Python code for it?

def get_text(path):
    command = ['pdftotext', '-enc', 'UTF-8', '-q', path, '-']
    process = subprocess.Popen(command, stdout=subprocess.PIPE)
    Lines   = process.stdout.readlines()
    return ''.join([str(line, 'utf-8', 'ignore') for line in Lines])


I am working on the file "Python/Local-Search-Engine.ipynb".
The first task is:
The method $\texttt{self}.\texttt{buildIndex}(d)$ takes an Index $\texttt{self}$ and a directory $d$.  It traverses the directory $d$ recursively and collects all .pdf files contained in $d$ and its subdirectories.  These files are converted to text and their words are added to the InvertedIndex.
Can you write Python code for it?

def buildIndex(self, directory):
    for root, _, files in os.walk(directory):
        for fileName in files:
            if fileName[-4:] == '.pdf':
                fullpath = os.path.abspath(os.path.join(root, fileName))
                print('indexing', fullpath, end=' has ')
                try:
                    fileText = get_text(fullpath)
                    tokenSet = tokenize(fileText)
                    print(len(tokenSet), 'different words.')
                    self.fileCount += 1
                    document = Document(fullpath, self.fileCount, tokenSet)
                    self.ID2Doc[self.fileCount] = document
                    self._addToIndex(self.fileCount, tokenSet)
                except:
                    print('unable to read', path)
                    continue

Index.buildIndex = buildIndex


I am working on the file "Python/Local-Search-Engine.ipynb".
The first task is:
The method $\texttt{self}.\texttt{retrieve}(Q)$ takes an Index self and a query $Q$.  $Q$ is a string containing multiple words.
The method returns the set of those documents that contain all the words occurring in $Q$.
Can you write Python code for it?

def retrieve(self, Query):
    SearchStrings = list(tokenize(Query))
    result        = set()
    Documents     = self.InvertedIndex.get(SearchStrings[0], set())
    for word in SearchStrings[1:]:
        Documents &= self.InvertedIndex.get(word, set())
    return { self.ID2Doc[docID].path for docID in Documents }

Index.retrieve = retrieve

index.retrieve("trie avl")


I am working on the file "Python/Local-Search-Engine.ipynb".
The first task is:
We need the module os to traverse directories.  os is short for operating system.
Can you write Python code for it?


I am working on the file "Python/ML_DL/DL/Neural-Networks-Demystified-master/.ipynb_checkpoints/Part 2 Forward Propagation-checkpoint.ipynb".
The first task is:
Each input value, or element in matrix X, needs to be multiplied by a corresponding weight and then added together with all the other results for each neuron. This is a complex operation, but if we take the three outputs we're looking for as a single row of a matrix, and place all our individual weights into a matrix of weights, we can create the exact behavior we need by multiplying our input data matrix by our weight matrix. Using matrix multiplication allows us to pass mulitple inputs through at once by simply adding rows to the matrix X. From here on out, we'll refer to these matrics as X, W one, and z two, where z two the activity of our second layer. Notice that each entry in z is a sum of weighted inputs to each hidden neuron. Z is of size 3 by 3, one row for each example, and one column for each hidden unit.
We now have our first official formula, $z^{(2)} = XW^{(1)}$. Matrix notation is realy nice here, becuase it allows us to express the complex underlying process in a single line!
$$
z^{(2)} = XW^{(1)} \tag{1}\
$$
Now that we have the activities for our second layer, z two, we need to apply the activation function. We'll independently apply the function to each entry in matrix z using a python method for this called sigmoid, because we’re using a sigmoid as our activation function. Using numpy is really nice here, because we can pass in a scalar, vector, or matrix, Numpy will apply the activation function element-wise, and return a result of the same dimension as it was given.
Can you write Python code for it?

def sigmoid(z):
    #Apply sigmoid activation function to scalar, vector, or matrix
    return 1/(1+np.exp(-z))

testInput = np.arange(-6,6,0.01)
plot(testInput, sigmoid(testInput), linewidth= 2)
grid(1)

sigmoid(1)

sigmoid(np.array([-1,0,1]))

sigmoid(np.random.randn(3,3))


I am working on the file "Python/ML_DL/DL/Neural-Networks-Demystified-master/.ipynb_checkpoints/Part 4 Backpropagation-checkpoint.ipynb".
The first task is:
<h3 align = 'center'> Variables </h3>

|Code Symbol | Math Symbol | Definition | Dimensions
| :-: | :-: | :-: | :-: |
|X|$$X$$|Input Data, each row in an example| (numExamples, inputLayerSize)|
|y |$$y$$|target data|(numExamples, outputLayerSize)|
|W1 | $$W^{(1)}$$ | Layer 1 weights | (inputLayerSize, hiddenLayerSize) |
|W2 | $$W^{(2)}$$ | Layer 2 weights | (hiddenLayerSize, outputLayerSize) |
|z2 | $$z^{(2)}$$ | Layer 2 activation | (numExamples, hiddenLayerSize) |
|a2 | $$a^{(2)}$$ | Layer 2 activity | (numExamples, hiddenLayerSize) |
|z3 | $$z^{(3)}$$ | Layer 3 activation | (numExamples, outputLayerSize) |
|J | $$J$$ | Cost | (1, outputLayerSize) |
|dJdz3 | $$\frac{\partial J}{\partial z^{(3)} } = \delta^{(3)}$$ | Partial derivative of cost with respect to $z^{(3)}$ | (numExamples,outputLayerSize)|
|dJdW2|$$\frac{\partial J}{\partial W^{(2)}}$$|Partial derivative of cost with respect to $W^{(2)}$|(hiddenLayerSize, outputLayerSize)|
|dz3dz2|$$\frac{\partial z^{(3)}}{\partial z^{(2)}}$$|Partial derivative of $z^{(3)}$ with respect to $z^{(2)}$|(numExamples, hiddenLayerSize)|
|dJdW1|$$\frac{\partial J}{\partial W^{(1)}}$$|Partial derivative of cost with respect to $W^{(1)}$|(inputLayerSize, hiddenLayerSize)|
|delta2|$$\delta^{(2)}$$|Backpropagating Error 2|(numExamples,hiddenLayerSize)|
|delta3|$$\delta^{(3)}$$|Backpropagating Error 1|(numExamples,outputLayerSize)|
Last time, we decided to use gradient descent to train our Neural Network, so it could make better predictions of your score on a test based on how many hours you slept, and how many hours you studied the night before. To perform gradient descent, we need an equation and some code for our gradient, dJ/dW.
Our weights, W, are spread across two matrices, W1 and W2. We’ll separate our dJ/dW computation in the same way, by computing dJdW1 and dJdW2 independently. We should have just as many gradient values as weight values, so when we’re done, our matrices dJdW1 and dJdW2 will be the same size as W1 and W2.
$$
\frac{\partial J}{\partial W^{(2)}} =...
Can you write Python code for it?

%pylab inline
#Import code from last time
from partTwo import *

def sigmoid(z):
     #Apply sigmoid activation function to scalar, vector, or matrix
    return 1/(1+np.exp(-z))

def sigmoidPrime(z):
    #Derivative of sigmoid function
    return np.exp(-z)/((1+np.exp(-z))**2)

testValues = np.arange(-5,5,0.01)
plot(testValues, sigmoid(testValues), linewidth=2)
plot(testValues, sigmoidPrime(testValues), linewidth=2)
grid(1)
legend(['sigmoid', 'sigmoidPrime'])


I am working on the file "Python/ML_DL/DL/Neural-Networks-Demystified-master/.ipynb_checkpoints/Part 4 Backpropagation-checkpoint.ipynb".
The first task is:
If we move this way by adding a scalar times our derivative to our weights, our cost will increase, and if we do the opposite, subtract our gradient from our weights, we will move downhill and reduce our cost. This simple step downhill is the core of gradient descent and a key part of how even very sophisticated learning algorithms are trained.
Can you write Python code for it?

scalar = 3
NN.W1 = NN.W1 + scalar*dJdW1
NN.W2 = NN.W2 + scalar*dJdW2
cost2 = NN.costFunction(X,y)

print cost1, cost2

dJdW1, dJdW2 = NN.costFunctionPrime(X,y)
NN.W1 = NN.W1 - scalar*dJdW1
NN.W2 = NN.W2 - scalar*dJdW2
cost3 = NN.costFunction(X, y)

print cost2, cost3


I am working on the file "Python/ML_DL/DL/Neural-Networks-Demystified-master/.ipynb_checkpoints/Part 4 Backpropagation-checkpoint.ipynb".
The first task is:
So how should we change our W’s to decrease our cost? We can now compute dJ/dW, which tells us which way is uphill in our 9 dimensional optimization space.
Can you write Python code for it?

NN = Neural_Network()

cost1 = NN.costFunction(X,y)

dJdW1, dJdW2 = NN.costFunctionPrime(X,y)

dJdW1

dJdW2


I am working on the file "Python/ML_DL/DL/Neural-Networks-Demystified-master/.ipynb_checkpoints/Part 4 Backpropagation-checkpoint.ipynb".
The first task is:
We have one final term to compute: dJ/dW1. The derivation begins the same way, computing the derivative through our final layer: first dJ/dyHat, then dyHat/dz3, and we called these two taken together form our backpropagating error, delta3. We now take the derivative “across” our synapses, this is a little different from out job last time, computing the derivative with respect to the weights on our synapses.
$$
\frac{\partial J}{\partial W^{(1)}} = (y-\hat{y})
\frac{\partial \hat{y}}{\partial W^{(1)}}
$$
$$
\frac{\partial J}{\partial W^{(1)}} = (y-\hat{y})
\frac{\partial \hat{y}}{\partial z^{(3)}}
\frac{\partial z^{(3)}}{\partial W^{(1)}}
$$
$$
\frac{\partial J}{\partial W^{(1)}} = -(y-\hat{y}) f^\prime(z^{(3)}) \frac{\partial z^{(3)}}{\partial W^{(1)}}
$$
$$
\frac{\partial z^{(3)}}{\partial W^{(1)}} = \frac{\partial z^{(3)}}{\partial a^{(2)}}\frac{\partial a^{(2)}}{\partial W^{(1)}}
$$
There’s still a nice linear relationship along each synapse, but now we’re interested in the rate of change of z(3) with respect to a(2). Now the slope is just equal to the weight value for that synapse. We can achieve this mathematically by multiplying by W(2) transpose.
$$
\frac{\partial J}{\partial W^{(1)}} = \delta^{(3)}
(W^{(2)})^{T}
\frac{\partial a^{(2)}}{\partial W^{(1)}}
$$
$$
\frac{\partial J}{\partial W^{(1)}} = \delta^{(3)}
(W^{(2)})^{T}
\frac{\partial a^{(2)}}{\partial z^{(2)}}
\frac{\partial z^{(2)}}{\partial W^{(1)}}
$$
Our next term to work on is da(2)/dz(2) – this step is just like the derivative across our layer 3 neurons, so we can just multiply by f prime(z2).
$$
\frac{\partial J}{\partial W^{(1)}} = \delta^{(3)}
(W^{(2)})^{T}
f^\prime(z^{(2)})
\frac{\partial z^{(2)}}{\partial W^{(1)}}
$$
Our final computation here is dz2/dW1. This is very similar to our dz3/dW2 computation, there is a simple linear relationship on the synapses between z2 and w1, in this case though, the slope is the input value, X. We can use the same technique as last time by multiplying by X transpose, effectively applying the...
Can you write Python code for it?

# Whole Class with additions:
class Neural_Network(object):
    def __init__(self):
        #Define Hyperparameters
        self.inputLayerSize = 2
        self.outputLayerSize = 1
        self.hiddenLayerSize = 3

        #Weights (parameters)
        self.W1 = np.random.randn(self.inputLayerSize,self.hiddenLayerSize)
        self.W2 = np.random.randn(self.hiddenLayerSize,self.outputLayerSize)

    def forward(self, X):
        #Propogate inputs though network
        self.z2 = np.dot(X, self.W1)
        self.a2 = self.sigmoid(self.z2)
        self.z3 = np.dot(self.a2, self.W2)
        yHat = self.sigmoid(self.z3)
        return yHat

    def sigmoid(self, z):
        #Apply sigmoid activation function to scalar, vector, or matrix
        return 1/(1+np.exp(-z))

    def sigmoidPrime(self,z):
        #Gradient of sigmoid
        return np.exp(-z)/((1+np.exp(-z))**2)

    def costFunction(self, X, y):
        #Compute cost for given X,y, use weights already stored in class.
        self.yHat = self.forward(X)
        J = 0.5*sum((y-self.yHat)**2)
        return J

    def costFunctionPrime(self, X, y):
        #Compute derivative with respect to W and W2 for a given X and y:
        self.yHat = self.forward(X)

        delta3 = np.multiply(-(y-self.yHat), self.sigmoidPrime(self.z3))
        dJdW2 = np.dot(self.a2.T, delta3)

        delta2 = np.dot(delta3, self.W2.T)*self.sigmoidPrime(self.z2)
        dJdW1 = np.dot(X.T, delta2)

        return dJdW1, dJdW2


I am working on the file "Python/ML_DL/DL/Neural-Networks-Demystified-master/.ipynb_checkpoints/Part 5 Numerical Gradient Checking-checkpoint.ipynb".
The first task is:
A nice way to do this is to divide the norm of the difference by the norm of the sum of the vectors we would like to compare. Typical results should be on the order of 10^-8 or less if you’ve computed your gradient correctly.
Can you write Python code for it?


I am working on the file "Python/ML_DL/DL/Neural-Networks-Demystified-master/.ipynb_checkpoints/Part 5 Numerical Gradient Checking-checkpoint.ipynb".
The first task is:
We’ll repeat this process across all our weights, and when we’re done we’ll have a numerical gradient vector, with the same number of values as we have weights. It’s this vector we would like to compare to our official gradient calculation. We see that our vectors appear very similar, which is a good sign, but we need to quantify just how similar they are.
Can you write Python code for it?

NN = Neural_Network()

numgrad = computeNumericalGradient(NN, X, y)
numgrad

grad = NN.computeGradients(X,y)
grad


I am working on the file "Python/ML_DL/DL/Neural-Networks-Demystified-master/.ipynb_checkpoints/Part 7 Overfitting, Testing, and Regularization-checkpoint.ipynb".
The first task is:
Last time, we trained our Neural Network, and it made suspiciously good predictions of your test score based on how many hours you slept, and how many hours you studied the night before. Before we celebrate and begin changing our sleep and study habits, we need some way to ensure that our model reflects the real world.
To do this, let’s first spend some time thinking about data. Like a lot of data, our input and output values come from real world observations. The assumption here is that there is some underlying process, and our observations give us insight into the process - BUT our observations are not the same thing as the process, they are just a sample.
Our observation says that when we sleep for 3 hours and study for 5 hours, the grade we earned was a 75. But does this mean that every time you sleep for 3 hours and study for 5 hours you will earn a 75? Of course not, because there are other variables that matter here, such as the difficulty of test, or whether you’ve been paying attention in lectures – we could quantify these variables to build a better model, but even if we did, there would still an element of uncertainty that we could never explicitly model – for example, maybe the test was multiple choice, and you guessed on a few problems.
One way to think about this problem is that observations are composed of signal and noise. Nate Silver, the guy who correctly predicted the US election results for 50 out of 50 US states in 2012, wrote a great book on exactly this. The idea is that we’re interested in an underlying process, the signal, but in real data, our signal will always be obscured by some level of noise.
An interesting example of this shows up when comparing the SAT scores of students who take the SAT both Junior and Senior year. Right on the college board’s website it says: “The higher a student's scores as a junior, the more likely that student's subsequent scores will drop”. Why would this be? It seems like students who did well junior year would also do well senior year. We can make...
Can you write Python code for it?

%pylab inline
from partSix import *

NN = Neural_Network()

# X = (hours sleeping, hours studying), y = Score on test
X = np.array(([3,5], [5,1], [10,2], [6,1.5]), dtype=float)
y = np.array(([75], [82], [93], [70]), dtype=float)

#Plot projections of our new data:
fig = figure(0,(8,3))

subplot(1,2,1)
scatter(X[:,0], y)
grid(1)
xlabel('Hours Sleeping')
ylabel('Test Score')

subplot(1,2,2)
scatter(X[:,1], y)
grid(1)
xlabel('Hours Studying')
ylabel('Test Score')

#Normalize
X = X/np.amax(X, axis=0)
y = y/100 #Max test score is 100

#Train network with new data:
T = trainer(NN)
T.train(X,y)

#Plot cost during training:
plot(T.J)
grid(1)
xlabel('Iterations')
ylabel('Cost')

#Test network for various combinations of sleep/study:
hoursSleep = linspace(0, 10, 100)
hoursStudy = linspace(0, 5, 100)

#Normalize data (same way training data way normalized)
hoursSleepNorm = hoursSleep/10.
hoursStudyNorm = hoursStudy/5.

#Create 2-d versions of input for plotting
a, b  = meshgrid(hoursSleepNorm, hoursStudyNorm)

#Join into a single input matrix:
allInputs = np.zeros((a.size, 2))
allInputs[:, 0] = a.ravel()
allInputs[:, 1] = b.ravel()

allOutputs = NN.forward(allInputs)

#Contour Plot:
yy = np.dot(hoursStudy.reshape(100,1), np.ones((1,100)))
xx = np.dot(hoursSleep.reshape(100,1), np.ones((1,100))).T

CS = contour(xx,yy,100*allOutputs.reshape(100, 100))
clabel(CS, inline=1, fontsize=10)
xlabel('Hours Sleep')
ylabel('Hours Study')

#3D plot:
#Uncomment to plot out-of-notebook (you'll be able to rotate)
#%matplotlib qt

from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.gca(projection='3d')

#Scatter training examples:
ax.scatter(10*X[:,0], 5*X[:,1], 100*y, c='k', alpha = 1, s=30)

surf = ax.plot_surface(xx, yy, 100*allOutputs.reshape(100, 100), \
                       cmap=cm.jet, alpha = 0.5)

ax.set_xlabel('Hours Sleep')
ax.set_ylabel('Hours Study')
ax.set_zlabel('Test Score')


I am working on the file "Python/ML_DL/DL/Neural-Networks-Demystified-master/.ipynb_checkpoints/Part 7 Overfitting, Testing, and Regularization-checkpoint.ipynb".
The first task is:
So it appears our model is overfitting, but how do we know for sure? A widely accepted method is to split our data into 2 portions: training and testing. We won’t touch our testing data while training the model, and only use it to see how we’re doing – our testing data is a simulation of the real world. We can plot the error on our training and testing sets as we train our model and identify the exact point at which overfitting begins. We can also plot testing and training error as a function of model complexity a see similar behavior.
Can you write Python code for it?

#Training Data:
trainX = np.array(([3,5], [5,1], [10,2], [6,1.5]), dtype=float)
trainY = np.array(([75], [82], [93], [70]), dtype=float)

#Testing Data:
testX = np.array(([4, 5.5], [4.5,1], [9,2.5], [6, 2]), dtype=float)
testY = np.array(([70], [89], [85], [75]), dtype=float)

#Normalize:
trainX = trainX/np.amax(trainX, axis=0)
trainY = trainY/100 #Max test score is 100

#Normalize by max of training data:
testX = testX/np.amax(trainX, axis=0)
testY = testY/100 #Max test score is 100

##Need to modify trainer class a bit to check testing error during training:
class trainer(object):
    def __init__(self, N):
        #Make Local reference to network:
        self.N = N

    def callbackF(self, params):
        self.N.setParams(params)
        self.J.append(self.N.costFunction(self.X, self.y))
        self.testJ.append(self.N.costFunction(self.testX, self.testY))

    def costFunctionWrapper(self, params, X, y):
        self.N.setParams(params)
        cost = self.N.costFunction(X, y)
        grad = self.N.computeGradients(X,y)

        return cost, grad

    def train(self, trainX, trainY, testX, testY):
        #Make an internal variable for the callback function:
        self.X = trainX
        self.y = trainY

        self.testX = testX
        self.testY = testY

        #Make empty list to store training costs:
        self.J = []
        self.testJ = []

        params0 = self.N.getParams()

        options = {'maxiter': 200, 'disp' : True}
        _res = optimize.minimize(self.costFunctionWrapper, params0, jac=True, method='BFGS', \
                                 args=(trainX, trainY), options=options, callback=self.callbackF)

        self.N.setParams(_res.x)
        self.optimizationResults = _res

#Train network with new data:
NN = Neural_Network()

T = trainer(NN)
T.train(trainX, trainY, testX, testY)

#Plot cost during training:
plot(T.J)
plot(T.testJ)
grid(1)
xlabel('Iterations')
ylabel('Cost')


I am working on the file "Python/ML_DL/DL/Neural-Networks-Demystified-master/.ipynb_checkpoints/Part 7 Overfitting, Testing, and Regularization-checkpoint.ipynb".
The first task is:
So now that we know overfitting is a problem, but how do we fix it? One way is to throw more data at the problem. A simple rule of thumb as presented by Yaser Abu-Mostaf is his excellent machine learning course available from Caltech, is that you should have at least 10 times as many examples as the degrees for freedom in your model. For us, since we have 9 weights that can change, we would need 90 observations, which we certainly don’t have.
Link to course: https://work.caltech.edu/telecourse.html
Another popular and effective way to mitigate overfitting is to use a technique called regularization. One way to implement regularization is to add a term to our cost function that penalizes overly complex models. A simple, but effective way to do this is to add together the square of our weights to our cost function, this way, models with larger magnitudes of weights, cost more. We’ll need to normalize the other part of our cost function to ensure that our ratio of the two error terms does not change with respect to the number of examples. We’ll introduce a regularization hyper parameter, lambda, that will allow us to tune the relative cost – higher values of lambda will impose bigger penalties for high model complexity.
Can you write Python code for it?

#Regularization Parameter:
Lambda = 0.0001

#Need to make changes to costFunction and costFunctionPrim:
def costFunction(self, X, y):
    #Compute cost for given X,y, use weights already stored in class.
    self.yHat = self.forward(X)
    J = 0.5*sum((y-self.yHat)**2)/X.shape[0] + (self.lambd/2)*(sum(self.W1**2)+sum(self.W2**2))
    return J

def costFunctionPrime(self, X, y):
    #Compute derivative with respect to W and W2 for a given X and y:
    self.yHat = self.forward(X)

    delta3 = np.multiply(-(y-self.yHat), self.sigmoidPrime(self.z3))
    #Add gradient of regularization term:
    dJdW2 = np.dot(self.a2.T, delta3)/X.shape[0] + self.lambd*self.W2

    delta2 = np.dot(delta3, self.W2.T)*self.sigmoidPrime(self.z2)
    #Add gradient of regularization term:
    dJdW1 = np.dot(X.T, delta2)/X.shape[0] + self.lambd*self.W1

    return dJdW1, dJdW2

#New complete class, with changes:
class Neural_Network(object):
    def __init__(self, Lambda=0):
        #Define Hyperparameters
        self.inputLayerSize = 2
        self.outputLayerSize = 1
        self.hiddenLayerSize = 3

        #Weights (parameters)
        self.W1 = np.random.randn(self.inputLayerSize,self.hiddenLayerSize)
        self.W2 = np.random.randn(self.hiddenLayerSize,self.outputLayerSize)

        #Regularization Parameter:
        self.Lambda = Lambda

    def forward(self, X):
        #Propogate inputs though network
        self.z2 = np.dot(X, self.W1)
        self.a2 = self.sigmoid(self.z2)
        self.z3 = np.dot(self.a2, self.W2)
        yHat = self.sigmoid(self.z3)
        return yHat

    def sigmoid(self, z):
        #Apply sigmoid activation function to scalar, vector, or matrix
        return 1/(1+np.exp(-z))

    def sigmoidPrime(self,z):
        #Gradient of sigmoid
        return np.exp(-z)/((1+np.exp(-z))**2)

    def costFunction(self, X, y):
        #Compute cost for given X,y, use weights already stored in class.
        self.yHat = self.forward(X)
        J =...


I am working on the file "Python/ML_DL/DL/Neural-Networks-Demystified-master/Part 1 Data and Architecture.ipynb".
The first task is:
Now that we have some data, we’re going to use it to train a model to predict how you will do on your next test, based on how many hours you sleep and how many hours you study. This is called a supervised regression problem. It’s supervised because our examples have inputs and outputs. It’s a regression problem because we’re predicting your test score, which is a continuous output. If we we’re predicting your letter grade, this would be called a classification problem, and not a regression problem.
There are an overwhelming number of models within machine learning, here we’re going to use a particularly interesting one called an artificial neural network. These guys are loosely based on how the neurons in your brain work, and have been particularly successful recently at solving really big, really hard problems.
Before we throw our data into the model, we need to account for the differences in the units of our data. Both of our inputs are in hours, but our output is a test score, scaled between 0 and 100. Neural networks are smart, but not smart enough to guess the units of our data. It’s kind of like asking our model to compare apples to oranges, where most learning models really only want to compare apples to apples. The solution is to scale our data, this way our model only sees standardized units. Here, we're going to take advantage of the fact that all of our data is positive, and simply divide by the maximum value for each variable, effectively scaling the result between 0 and 1.
Can you write Python code for it?

X = X/np.amax(X, axis=0)
y = y/100 #Max test score is 100

X

y


I am working on the file "Python/ML_DL/DL/Neural-Networks-Demystified-master/Part 1 Data and Architecture.ipynb".
The first task is:
Now we can build our Neural Net. We know our network must have 2 inputs and 1 output, because these are the dimensions of our data. We’ll call our output y hat, because it’s an estimate of y, but not the same as y. Any layer between our input and output layer is called a hidden layer. Recently, researchers have built networks with many many hidden layers. These are known as a deep belief networks, giving rise to the term deep learning. Here, we’re going to use 1 hidden layer with 3 hidden units, but if we wanted to build a deep neural network, we would just stack a bunch of layers together.
Can you write Python code for it?

from IPython.display import Image
i = Image(filename='images/simpleNetwork.png')
i


I am working on the file "Python/ML_DL/DL/Neural-Networks-Demystified-master/Part 2 Forward Propagation.ipynb".
The first task is:
<h3 align = 'center'> Variables </h3>

|Code Symbol | Math Symbol | Definition | Dimensions
| :-: | :-: | :-: | :-: |
|X|$$X$$|Input Data, each row in an example| (numExamples, inputLayerSize)|
|y |$$y$$|target data|(numExamples, outputLayerSize)|
|W1 | $$W^{(1)}$$ | Layer 1 weights | (inputLayerSize, hiddenLayerSize) |
|W2 | $$W^{(2)}$$ | Layer 2 weights | (hiddenLayerSize, outputLayerSize) |
|z2 | $$z^{(2)}$$ | Layer 2 activation | (numExamples, hiddenLayerSize) |
|a2 | $$a^{(2)}$$ | Layer 2 activity | (numExamples, hiddenLayerSize) |
|z3 | $$z^{(3)}$$ | Layer 3 activation | (numExamples, outputLayerSize) |
Last time, we setup our neural network on paper. This time, we’ll implement it in the programming language python. We’ll build our network as a python class and our init method will take care of instantiating important constants and variables. We’ll make these values accessible to the whole class by placing a self dot in front of each variable name.
Our network has 2 inputs, 3 hidden units, and 1 output. These are examples of hyperparameters. Hyperparameters are constants that establish the structure and behavior of a neural network, but are not updated as we train the network. Our learning algorithm is not capable of, for example, deciding that it needs another hidden unit, this is something that WE must decide on before training. What a neural network does learn are parameters, specifically the weights on the synapses.
We’ll take care of moving data through our network in a method called forward. Rather than pass inputs through the network one at a time, we’re going to use matrices to pass through multiple inputs at once. Doing this allows for big computational speedups, especially when using tools like MATLAB or Numpy. Our input data matrix, X, is of dimension 3 by 2, because we have 3, 2-dimensional examples. Our corresponding output data, y, is of dimension 3 by 1.
Can you write Python code for it?

#Import code from last time
%pylab inline
from partOne import *

print X.shape, y.shape

class Neural_Network(object):
    def __init__(self):
        #Define Hyperparameters
        self.inputLayerSize = 2
        self.outputLayerSize = 1
        self.hiddenLayerSize = 3

    def forward(self, X):
        #Propagate inputs though network


I am working on the file "Python/ML_DL/DL/Neural-Networks-Demystified-master/Part 3 Gradient Descent.ipynb".
The first task is:
<h3 align = 'center'> Variables </h3>

|Code Symbol | Math Symbol | Definition | Dimensions
| :-: | :-: | :-: | :-: |
|X|$$X$$|Input Data, each row in an example| (numExamples, inputLayerSize)|
|y |$$y$$|target data|(numExamples, outputLayerSize)|
|W1 | $$W^{(1)}$$ | Layer 1 weights | (inputLayerSize, hiddenLayerSize) |
|W2 | $$W^{(2)}$$ | Layer 2 weights | (hiddenLayerSize, outputLayerSize) |
|z2 | $$z^{(2)}$$ | Layer 2 activation | (numExamples, hiddenLayerSize) |
|a2 | $$a^{(2)}$$ | Layer 2 activity | (numExamples, hiddenLayerSize) |
|z3 | $$z^{(3)}$$ | Layer 3 activation | (numExamples, outputLayerSize) |
|J | $$J$$ | Cost | (1, outputLayerSize) |
Last time we built a neural network in python that made really bad predictions of your score on a test based on how many hours you slept and how many hours you studied the night before. This time we'll focus on the theory of making those predictions better.
We can initialize the network we built last time and pass in our normalized data, X, using our forward method, and have a look at our estimate of y, yHat.
Can you write Python code for it?

%pylab inline

#Import code from last time:
from partTwo import *

NN = Neural_Network()

yHat = NN.forward(X)

yHat

y

#Compare estimate, yHat, to actually score
bar([0,1,2], y, width = 0.35, alpha=0.8)
bar([0.35,1.35,2.35],yHat, width = 0.35, color='r', alpha=0.8)
grid(1)
legend(['y', 'yHat'])


I am working on the file "Python/ML_DL/DL/Neural-Networks-Demystified-master/Part 3 Gradient Descent.ipynb".
The first task is:
After our 1 million evaluations we’ve found our solution, but it took an agonizing 40 seconds! The real curse of dimensionality kicks in as we continue to add dimensions. Searching through three weights would take a billion evaluations, or 11 hours! Searching through all 9 weights we need for our simple network would take 1,268,391,679,350,583.5 years. (Over a quardrillion years). So for that reason, the "just try everything" or brute force optimization method is clearly not going to work.
Can you write Python code for it?


I am working on the file "Python/ML_DL/DL/Neural-Networks-Demystified-master/Part 3 Gradient Descent.ipynb".
The first task is:
It takes about 0.04 seconds to check 1000 different weight values for our neural network. Since we’ve computed the cost for a wide range values of W, we can just pick the one with the smallest cost, let that be our weight, and we’ve trained our network.
Can you write Python code for it?

plot(weightsToTry, costs)
grid(1)
ylabel('Cost')
xlabel('Weight')


I am working on the file "Python/ML_DL/DL/Neural-Networks-Demystified-master/Part 3 Gradient Descent.ipynb".
The first task is:
Right now our predictions are pretty inaccurate. To improve our model, we first need to quantify exactly how wrong our predictions are. We'll do this with a cost function. A cost function allows us to express exactly how wrong or "costly" our models is, given our examples.
One way to compute an overall cost is to take each error value, square it, and add these values together. Multiplying by one half will make things simpler down the road. Now that we have a cost, or job is to minimize it. When someone says they’re training a network, what they really mean is that they're minimizing a cost function.
$$
J = \sum \frac{1}{2}(y-\hat{y})^2 \tag{5}
$$
OUR cost is a function of two things, our examples, and the weights on our synapses. We don't have much control of our data, so we'll minimize our cost by changing the weights.
Conceptually, this is pretty simple concept. We have a collection of 9 individual weights, and we're saying that there is some combination of w's that will make our cost, J, as small as possible. When I first saw this problem in machine learning, I thought, I'll just try ALL THE WEIGHTS UNTIL I FIND THE BEST ONE! After all I have a computer!
Enter the CURSE OF DIMENSIONALITY. Here's the problem. Let's pretend for a second that we only have 1 weight, instead of 9. To find the ideal value of our weight that will minimize our cost, we need to try a bunch of values for W, let's say we test 1000 values. That doesn't seem so bad, after all, my computer is pretty fast.
Can you write Python code for it?

import time

weightsToTry = np.linspace(-5,5,1000)
costs = np.zeros(1000)

startTime = time.clock()
for i in range(1000):
    NN.W1[0,0] = weightsToTry[i]
    yHat = NN.forward(X)
    costs[i] = 0.5*sum((y-yHat)**2)

endTime = time.clock()

timeElapsed = endTime-startTime
timeElapsed


I am working on the file "Python/ML_DL/DL/Neural-Networks-Demystified-master/Part 4 Backpropagation.ipynb".
The first task is:
<h3 align = 'center'> Variables </h3>

|Code Symbol | Math Symbol | Definition | Dimensions
| :-: | :-: | :-: | :-: |
|X|$$X$$|Input Data, each row in an example| (numExamples, inputLayerSize)|
|y |$$y$$|target data|(numExamples, outputLayerSize)|
|W1 | $$W^{(1)}$$ | Layer 1 weights | (inputLayerSize, hiddenLayerSize) |
|W2 | $$W^{(2)}$$ | Layer 2 weights | (hiddenLayerSize, outputLayerSize) |
|z2 | $$z^{(2)}$$ | Layer 2 activation | (numExamples, hiddenLayerSize) |
|a2 | $$a^{(2)}$$ | Layer 2 activity | (numExamples, hiddenLayerSize) |
|z3 | $$z^{(3)}$$ | Layer 3 activation | (numExamples, outputLayerSize) |
|J | $$J$$ | Cost | (1, outputLayerSize) |
|dJdz3 | $$\frac{\partial J}{\partial z^{(3)} } = \delta^{(3)}$$ | Partial derivative of cost with respect to $z^{(3)}$ | (numExamples,outputLayerSize)|
|dJdW2|$$\frac{\partial J}{\partial W^{(2)}}$$|Partial derivative of cost with respect to $W^{(2)}$|(hiddenLayerSize, outputLayerSize)|
|dz3dz2|$$\frac{\partial z^{(3)}}{\partial z^{(2)}}$$|Partial derivative of $z^{(3)}$ with respect to $z^{(2)}$|(numExamples, hiddenLayerSize)|
|dJdW1|$$\frac{\partial J}{\partial W^{(1)}}$$|Partial derivative of cost with respect to $W^{(1)}$|(inputLayerSize, hiddenLayerSize)|
|delta2|$$\delta^{(2)}$$|Backpropagating Error 2|(numExamples,hiddenLayerSize)|
|delta3|$$\delta^{(3)}$$|Backpropagating Error 1|(numExamples,outputLayerSize)|
Last time, we decided to use gradient descent to train our Neural Network, so it could make better predictions of your score on a test based on how many hours you slept, and how many hours you studied the night before. To perform gradient descent, we need an equation and some code for our gradient, dJ/dW.
Our weights, W, are spread across two matrices, W1 and W2. We’ll separate our dJ/dW computation in the same way, by computing dJdW1 and dJdW2 independently. We should have just as many gradient values as weight values, so when we’re done, our matrices dJdW1 and dJdW2 will be the same size as W1 and W2.
$$
\frac{\partial J}{\partial W^{(2)}} =...
Can you write Python code for it?


I am working on the file "Python/ML_DL/DL/Neural-Networks-Demystified-master/Part 4 Backpropagation.ipynb".
The first task is:
We can now replace dyHat/dz3 with f prime of z 3.
$$
\frac{\partial J}{\partial W^{(2)}}=
-(y-\hat{y}) f^\prime(z^{(3)}) \frac{\partial z^{(3)}}{\partial W^{(2)}}
$$
Our final piece of the puzzle is dz3dW2, this term represents the change of z, our third layer activity, with respect to the weights in the second layer.
Z three is the matrix product of our activities, a two, and our weights, w two. The activities from layer two are multiplied by their correspond weights and added together to yield z3. If we focus on a single synapse for a moment, we see a simple linear relationship between W and z, where a is the slope. So for each synapse, dz/dW(2) is just the activation, a on that synapse!
$$
z^{(3)} = a^{(2)}W^{(2)} \tag{3}\
$$
Another way to think about what the calculus is doing here is that it is “backpropagating” the error to each weight, by multiplying by the activity on each synapses, the weights that contribute more to the error will have larger activations, and yield larger dJ/dW2 values, and those weights will be changed more when we perform gradient descent.
We need to be careful with our dimensionality here, and if we’re clever, we can take care of that summation we got rid of earlier.
The first part of our equation, y minus yHat is of the same dimension as our output data, 3 by 1.
F prime of z three is of the same size, 3 by 1, and our first operation is scalar multiplication. Our resulting 3 by 1 matrix is referred to as the backpropagating error, delta 3.
We determined that dz3/dW2 is equal to the activity of each synapse. Each value in delta 3 needs to be multiplied by each activity. We can achieve this by transposing a2 and matrix multiplying by delta3.
$$
\frac{\partial J}{\partial W^{(2)}} =
(a^{(2)})^T\delta^{(3)}\tag{6}
$$
$$
\delta^{(3)} = -(y-\hat{y}) f^\prime(z^{(3)})
$$
What’s cool here is that the matrix multiplication also takes care of our earlier omission – it adds up the dJ/dW terms across all our examples.
Another way to think about what’s happening here is that is that...
Can you write Python code for it?

# Part of NN Class (won't work alone, needs to be included in class as
# shown in below and in partFour.py):

def costFunctionPrime(self, X, y):
    #Compute derivative with respect to W and W2 for a given X and y:
    self.yHat = self.forward(X)

    delta3 = np.multiply(-(y-self.yHat), self.sigmoidPrime(self.z3))
    dJdW2 = np.dot(self.a2.T, delta3)


I am working on the file "Python/ML_DL/DL/Neural-Networks-Demystified-master/Part 4 Backpropagation.ipynb".
The first task is:
We have one final term to compute: dJ/dW1. The derivation begins the same way, computing the derivative through our final layer: first dJ/dyHat, then dyHat/dz3, and we called these two taken together form our backpropagating error, delta3. We now take the derivative “across” our synapses, this is a little different from out job last time, computing the derivative with respect to the weights on our synapses.
$$
\frac{\partial J}{\partial W^{(1)}} = (y-\hat{y})
\frac{\partial \hat{y}}{\partial W^{(1)}}
$$
$$
\frac{\partial J}{\partial W^{(1)}} = (y-\hat{y})
\frac{\partial \hat{y}}{\partial z^{(3)}}
\frac{\partial z^{(3)}}{\partial W^{(1)}}
$$
$$
\frac{\partial J}{\partial W^{(1)}} = -(y-\hat{y}) f^\prime(z^{(3)}) \frac{\partial z^{(3)}}{\partial W^{(1)}}
$$
$$
\frac{\partial z^{(3)}}{\partial W^{(1)}} = \frac{\partial z^{(3)}}{\partial a^{(2)}}\frac{\partial a^{(2)}}{\partial W^{(1)}}
$$
There’s still a nice linear relationship along each synapse, but now we’re interested in the rate of change of z(3) with respect to a(2). Now the slope is just equal to the weight value for that synapse. We can achieve this mathematically by multiplying by W(2) transpose.
$$
\frac{\partial J}{\partial W^{(1)}} = \delta^{(3)}
(W^{(2)})^{T}
\frac{\partial a^{(2)}}{\partial W^{(1)}}
$$
$$
\frac{\partial J}{\partial W^{(1)}} = \delta^{(3)}
(W^{(2)})^{T}
\frac{\partial a^{(2)}}{\partial z^{(2)}}
\frac{\partial z^{(2)}}{\partial W^{(1)}}
$$
Our next term to work on is da(2)/dz(2) – this step is just like the derivative across our layer 3 neurons, so we can just multiply by f prime(z2).
$$
\frac{\partial J}{\partial W^{(1)}} = \delta^{(3)}
(W^{(2)})^{T}
f^\prime(z^{(2)})
\frac{\partial z^{(2)}}{\partial W^{(1)}}
$$
Our final computation here is dz2/dW1. This is very similar to our dz3/dW2 computation, there is a simple linear relationship on the synapses between z2 and w1, in this case though, the slope is the input value, X. We can use the same technique as last time by multiplying by X transpose, effectively applying the...
Can you write Python code for it?


I am working on the file "Python/ML_DL/DL/Neural-Networks-Demystified-master/Part 5 Numerical Gradient Checking.ipynb".
The first task is:
A nice way to do this is to divide the norm of the difference by the norm of the sum of the vectors we would like to compare. Typical results should be on the order of 10^-8 or less if you’ve computed your gradient correctly.
Can you write Python code for it?


I am working on the file "Python/ML_DL/DL/Neural-Networks-Demystified-master/Part 5 Numerical Gradient Checking.ipynb".
The first task is:
Last time, we did a bunch of calculus to find the rate of change of our cost, J, with respect to our parameters, W. Although each calculus step was pretty straight forward, it’s still easy to make mistakes. What’s worse, is that our network doesn’t have a good way to tell us that it’s broken – code with incorrectly implemented gradients may appear to be functioning just fine.
This is the most nefarious kind of error when building complex systems. Big, in-your-face errors suck initially, but it’s clear that you must fix this error for your work to succeed. More subtle errors can be more troublesome because they hide in your code and steal hours of your time, slowly degrading performance, while you wonder what the problem is.
A good solution here is to test the gradient computation part of our code, just as developer would unit test new portions of their code. We’ll combine a simple understanding of the derivative with some mild cleverness to perform numerical gradient checking. If our code passes this test, we can be quite confident that we have computed and coded up our gradients correctly.
To get started, let’s quickly review derivatives. Derivates tell us the slope, or how steep a function is. Once you’re familiar with calculus, it’s easy to take for granted the inner workings of the derivative - we just accept that the derivative of x^2 is 2x by the power rule. However, depending on how mean your calculus teacher was, you may have spent months not being taught the power rule, and instead required to compute derivatives using the definition. Taking derivatives this way is a bit tedious, but still important - it provides us a deeper understanding of what a derivative is, and it’s going to help us solve our current problem.
The definition of the derivative is really a glorified slope formula. The numerator gives us the change in y values, while the denominator is convenient way to express the change in x values. By including the limit, we are applying the slope formula across an infinitely small region –...
Can you write Python code for it?

%pylab inline
#Import Code from previous videos:
from partFour import *

def f(x):
    return x**2

epsilon = 1e-4
x = 1.5

numericalGradient = (f(x+epsilon)- f(x-epsilon))/(2*epsilon)

numericalGradient, 2*x


I am working on the file "Python/ML_DL/DL/Neural-Networks-Demystified-master/Part 5 Numerical Gradient Checking.ipynb".
The first task is:
We can use the same approach to numerically evaluate the gradient of our neural network. It’s a little more complicated this time, since we have 9 gradient values, and we’re interested in the gradient of our cost function. We’ll make things simpler by testing one gradient at a time. We’ll “perturb” each weight - adding epsilon to the current value and  computing the cost function, subtracting epsilon from the current value and computing the cost function, and then computing the slope between these two values.
Can you write Python code for it?

def computeNumericalGradient(N, X, y):
        paramsInitial = N.getParams()
        numgrad = np.zeros(paramsInitial.shape)
        perturb = np.zeros(paramsInitial.shape)
        e = 1e-4

        for p in range(len(paramsInitial)):
            #Set perturbation vector
            perturb[p] = e
            N.setParams(paramsInitial + perturb)
            loss2 = N.costFunction(X, y)

            N.setParams(paramsInitial - perturb)
            loss1 = N.costFunction(X, y)

            #Compute Numerical Gradient
            numgrad[p] = (loss2 - loss1) / (2*e)

            #Return the value we changed to zero:
            perturb[p] = 0

        #Return Params to original value:
        N.setParams(paramsInitial)

        return numgrad


I am working on the file "Python/ML_DL/DL/Neural-Networks-Demystified-master/Part 5 Numerical Gradient Checking.ipynb".
The first task is:
We’ll repeat this process across all our weights, and when we’re done we’ll have a numerical gradient vector, with the same number of values as we have weights. It’s this vector we would like to compare to our official gradient calculation. We see that our vectors appear very similar, which is a good sign, but we need to quantify just how similar they are.
Can you write Python code for it?


I am working on the file "Python/ML_DL/DL/Neural-Networks-Demystified-master/Part 6 Training.ipynb".
The first task is:
So far we’ve built a neural network in python, computed a cost function to let us know how well our network is performing, computed the gradient of our cost function so we can train our network, and last time we numerically validated our gradient computations. After all that work, it’s finally time to train our neural network.
Back in part 3, we decided to train our network using gradient descent. While gradient descent is conceptually pretty straight forward, its implementation can actually be quite complex- especially as we increase the size and number of layers in our neural network. If we just march downhill with consistent step sizes, we may get stuck in a local minimum or flat spot, we may move too slowly and never reach our minimum, or we may move to quickly and bounce out of our minimum. And remember, all this must happen in high-dimensional space, making things significantly more complex. Gradient descent is a wonderfully clever method, but provides no guarantees that we will converge to a good solution, that we will converge to a solution in a certain amount of time, or that we will converge to a solution at all.
The good and bad news here is that this problem is not unique to Neural Networks - there’s an entire field dedicated to finding the best combination of inputs to minimize the output of an objective function: the field of Mathematical Optimization. The bad news is that optimization can be a bit overwhelming; there are many different techniques we could apply to our problem.
Part of what makes the optimization challenging is the broad range of approaches covered - from very rigorous, theoretical methods to hands-on, more heuristics-driven methods. Yann Lecun’s 1998 publication Efficient BackProp presents an excellent review of various optimization techniques as applied to neural networks.
Here, we’re going to use a more sophisticated variant on gradient descent, the popular Broyden-Fletcher-Goldfarb-Shanno numerical optimization algorithm. The BFGS algorithm overcomes some of the...
Can you write Python code for it?

%pylab inline
#Import code from previous videos:
from partFive import *

from scipy import optimize

class trainer(object):
    def __init__(self, N):
        #Make Local reference to network:
        self.N = N

    def callbackF(self, params):
        self.N.setParams(params)
        self.J.append(self.N.costFunction(self.X, self.y))

    def costFunctionWrapper(self, params, X, y):
        self.N.setParams(params)
        cost = self.N.costFunction(X, y)
        grad = self.N.computeGradients(X,y)

        return cost, grad

    def train(self, X, y):
        #Make an internal variable for the callback function:
        self.X = X
        self.y = y

        #Make empty list to store costs:
        self.J = []

        params0 = self.N.getParams()

        options = {'maxiter': 200, 'disp' : True}
        _res = optimize.minimize(self.costFunctionWrapper, params0, jac=True, method='BFGS', \
                                 args=(X, y), options=options, callback=self.callbackF)

        self.N.setParams(_res.x)
        self.optimizationResults = _res


I am working on the file "Python/ML_DL/DL/Neural-Networks-Demystified-master/Part 7 Overfitting, Testing, and Regularization.ipynb".
The first task is:
Last time, we trained our Neural Network, and it made suspiciously good predictions of your test score based on how many hours you slept, and how many hours you studied the night before. Before we celebrate and begin changing our sleep and study habits, we need some way to ensure that our model reflects the real world.
To do this, let’s first spend some time thinking about data. Like a lot of data, our input and output values come from real world observations. The assumption here is that there is some underlying process, and our observations give us insight into the process - BUT our observations are not the same thing as the process, they are just a sample.
Our observation says that when we sleep for 3 hours and study for 5 hours, the grade we earned was a 75. But does this mean that every time you sleep for 3 hours and study for 5 hours you will earn a 75? Of course not, because there are other variables that matter here, such as the difficulty of test, or whether you’ve been paying attention in lectures – we could quantify these variables to build a better model, but even if we did, there would still an element of uncertainty that we could never explicitly model – for example, maybe the test was multiple choice, and you guessed on a few problems.
One way to think about this problem is that observations are composed of signal and noise. Nate Silver, the guy who correctly predicted the US election results for 50 out of 50 US states in 2012, wrote a great book on exactly this. The idea is that we’re interested in an underlying process, the signal, but in real data, our signal will always be obscured by some level of noise.
An interesting example of this shows up when comparing the SAT scores of students who take the SAT both Junior and Senior year. Right on the college board’s website it says: “The higher a student's scores as a junior, the more likely that student's subsequent scores will drop”. Why would this be? It seems like students who did well junior year would also do well senior year. We can make...
Can you write Python code for it?


I am working on the file "Python/ML_DL/DL/Neural-Networks-Demystified-master/Part 7 Overfitting, Testing, and Regularization.ipynb".
The first task is:
So it appears our model is overfitting, but how do we know for sure? A widely accepted method is to split our data into 2 portions: training and testing. We won’t touch our testing data while training the model, and only use it to see how we’re doing – our testing data is a simulation of the real world. We can plot the error on our training and testing sets as we train our model and identify the exact point at which overfitting begins. We can also plot testing and training error as a function of model complexity a see similar behavior.
Can you write Python code for it?


I am working on the file "Python/Machine Learning/Kaggle/AdvancedHousingPrediction/KaggleAdvancedHousingPrediction.ipynb".
The first task is:
DecisionTree Regressor is performing much better than Linear Regression here, perhaps capturing some non-linearity in data.
Can you write Python code for it?

from sklearn.ensemble import RandomForestRegressor

rf = RandomForestRegressor()
scores = cross_val_score(rf, X_train, y_train, scoring="neg_mean_squared_error", cv=10, verbose=1)
printscorespretty(scores)


I am working on the file "Python/Machine Learning/Kaggle/AdvancedHousingPrediction/KaggleAdvancedHousingPrediction.ipynb".
The first task is:
Fit Models

Linear Regression
RandomForest
Can you write Python code for it?

from sklearn.model_selection import train_test_split
# split into 10% for validation at end
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.1)

# Linear Regression
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import cross_val_score
from sklearn.metrics import mean_squared_error as mse

linreg = LinearRegression()
scores = cross_val_score(linreg, X_train, y_train, scoring="neg_mean_squared_error", cv=10, verbose=1)

def printscorespretty(scores):
    sc = np.sqrt(-scores)
    print("Scores:", sc)
    print("Mean:", np.mean(sc))
    print("SD:", np.sqrt(np.var(sc)))

printscorespretty(scores)

#Decision Tree Regressor
from sklearn.tree import DecisionTreeRegressor

dtr = DecisionTreeRegressor()
scores = cross_val_score(dtr, X_train, y_train, scoring="neg_mean_squared_error", cv=10, verbose=1)
printscorespretty(scores)


I am working on the file "Python/Machine Learning/Kaggle/AdvancedHousingPrediction/KaggleAdvancedHousingPrediction.ipynb".
The first task is:
Many features (e.g. LotArea, GarageCars) are indeed correlated highly with SalePrice.
Can you write Python code for it?

tmp = cor['SalePrice'].sort_values(ascending=False)
tmp[1:11].plot(kind='bar', figsize=(8,8))

# we will have to remove SalePrice before imputing
train_num_wsp = train_num.drop('SalePrice',axis=1)
train_num_tr = Imp.fit_transform(train_num_wsp)
train_num_tr

X = np.concatenate([train_num_tr, train_cat_transformed],axis=1)
y = train_num['SalePrice'].values
print("Shape of X:", X.shape)
print("Shape of y:", y.shape)


I am working on the file "Python/Machine Learning/Kaggle/AdvancedHousingPrediction/KaggleAdvancedHousingPrediction.ipynb".
The first task is:
We have 1460 observations, and 81 columns.  There quite a few strings too, which we would have to encode later on.
Let's have a look at the number of missing values.
Can you write Python code for it?

tmp = train.isnull().sum()

# get top 10 results
tmp.sort_values(ascending=False).head(10).plot(kind='bar', figsize=(8,8))


I am working on the file "Python/Minimize-FP.ipynb".
The first task is:
Given a state $p$, find the equivalence class of $p$.  Here, eqClasses is a set of sets of states.
Can you write Python code for it?

def find_equivalence_class(p, eqClasses):
    return arb({ C for C in eqClasses if p in C })


I am working on the file "Python/Minimize-FP.ipynb".
The first task is:
The function cart_prod(A, B) computes the Cartesian product $A \times B$ of the sets $A$ and $B$ where
$$ A \times B := { (x, y) \mid x \in A \wedge y \in B }. $$
Can you write Python code for it?

def cart_prod(A, B):
    return { (x, y) for x in A for y in B }


I am working on the file "Python/Minimize-FP.ipynb".
The first task is:
The function minimize(A) takes a deterministic
Fsm F as its input.
Here F is a 5-tuple of the form
$$ F = (Q, \Sigma, \delta, q_0, A) $$
The algorithm performs the following steps:
1. All unreachable states are eliminated.
2. All accepting states are separated form all non-accepting states.
3. States are separated as long as possible.
   Two states $p_1$ and $p_2$ are separable if there is a character
   $c \in \Sigma$ such that
   $$\delta(p_1,c) = q_1, \quad \delta(p_2,c) = q_2, \quad \textrm{and} \quad
     \mbox{$q_1$ and $q_2$ are separable.}
   $$
4. States that are not separable are equivalent and are therefore identified and grouped
   in equivalence classes.
Can you write Python code for it?

def minimize(F):
    States, Σ, 𝛿, q0, Accepting = F
    States       = fixpoint({q0}, lambda q: { 𝛿[q, c] for c in Σ })
    Separable    = cart_prod(States - Accepting, Accepting) | \
                   cart_prod(Accepting, States - Accepting)
    AllSeparable = fixpoint2(Separable, lambda Pairs: separate(Pairs, States, Σ, 𝛿))
    Equivalent   = cart_prod(States, States) - AllSeparable
    EquivClasses = { frozenset({ p for p in States if (p, q) in Equivalent })
                     for q in States
                   }
    newQ0        = arb({ M for M in EquivClasses if q0 in M })
    newAccept    = { M for M in EquivClasses if arb(M) in Accepting }
    newDelta         = {}
    for q in States:
        for c in Σ:
            p = 𝛿.get((q, c))
            if p != None:
                classOfP = find_equivalence_class(p, EquivClasses)
                classOfQ = find_equivalence_class(q, EquivClasses)
                newDelta[(classOfQ, c)] = classOfP
            else:
                newDelta[(classOfQ, c)] = frozenset()
    return EquivClasses, Σ, newDelta, newQ0, newAccept


I am working on the file "Python/Minimize-FP.ipynb".
The first task is:
This function takes three arguments:
- Pairs  a set Pairs of pairs of states from some Fsm $F$.
   If $(p_1, p_2) \in \texttt{Pairs}$, then $p_1$ and $p_2$ are known to be separable,
- States is the set of states of the Fsm $F$,
- Sigma  is the alphabet of the underlying Fsm $F$.
The function separate(Pairs) computes the set of pairs of states $(q_1, q_2)$ that are separable because there is some
$c \in \Sigma$ such that
$$\delta(q_1,c) = p_1, \quad \delta(q_2,c) = p_2. $$
Can you write Python code for it?

def separate(Pairs, States, Σ, 𝛿):
    Result = { (q1, q2) for q1 in States
                        for q2 in States
                        for c  in Σ
                        if (𝛿[q1, c], 𝛿[q2, c]) in Pairs
             }
    return Result


I am working on the file "Python/Minimize.ipynb".
The first task is:
Given a state $p$, find the equivalence class of $p$.  Here, eqClasses is a set of sets of states.
Can you write Python code for it?


I am working on the file "Python/Minimize.ipynb".
The first task is:
Minimizing a Fsm
Can you write Python code for it?

%run FixedPoint.ipynb

def arb(M):
    for x in M:
        return x
    assert False, 'Error: arb called with empty set!'


I am working on the file "Python/Minimize.ipynb".
The first task is:
Minimizing a Fsm
The function arb(M) takes a non-empty set M as its argument and returns an arbitrary element from this set.
The set M is not changed.
Can you write Python code for it?

def arb(M):
    for x in M:
        return x
    assert False, 'Error: arb called with empty set!'


I am working on the file "Python/Minimize.ipynb".
The first task is:
The function all_separable(Q, A, Σ, 𝛿) takes four arguments:
* Q is the set of states of the Fsm.
* A is the set of all accepting states,
* Σ  is the alphabet.
* 𝛿 is the transition function.
𝛿 is represented as a dictionary.
The function computes the set of all Pairs (p, q) such that p and q are separable, i.e. all pairs such that
$$ \exists s \in \Sigma^: \bigl(\delta^(p, s) \in A \wedge \delta^(q,s) \not\in A\bigr) \vee
                           \bigl(\delta^(p, s) \not\in A \wedge \delta^*(q,s) \in A\bigr).
$$
Can you write Python code for it?

def all_separable(Q, A, Σ, 𝛿):
        Separable = cart_prod(Q - A, A) | cart_prod(A, Q - A)
        while True:
            NewPairs = separate(Separable, Q, Σ, 𝛿)
            if NewPairs <= Separable:
                return Separable
            Separable |= NewPairs


I am working on the file "Python/Minimize.ipynb".
The first task is:
The function cart_prod(A, B) computes the Cartesian product $A \times B$ of the sets $A$ and $B$ where $A \times B$ is defined as follows:
$$ A \times B := { (x, y) \mid x \in A \wedge y \in B }. $$
Can you write Python code for it?


I am working on the file "Python/Minimize.ipynb".
The first task is:
The function minimize(A) takes a deterministic
Fsm F as its input.
Here F is a 5-tuple of the form
$$ F = (Q, \Sigma, \delta, q_0, A) $$
The algorithm performs the following steps:
1. All unreachable states are eliminated.
2. All accepting states are separated form all non-accepting states.
3. States are separated as long as possible.
   Two states $p_1$ and $p_2$ are separable if there is a character
   $c \in \Sigma$ such that
   $$\delta(p_1,c) = q_1, \quad \delta(p_2,c) = q_2, \quad \textrm{and} \quad
     \mbox{$q_1$ and $q_2$ are separable.}
   $$
4. States that are not separable are equivalent and are therefore identified and grouped
   in equivalence classes.
Can you write Python code for it?


I am working on the file "Python/Minimize.ipynb".
The first task is:
The function reachable(q0, Σ, 𝛿) takes three arguments:
* q0 is the start state of an Fsm,
* Σ  is the alphabet.
* 𝛿  is the transition function.  The transition function is assumed to be complete. 𝛿 is represented as a dictionary.
It returns the set of all states that can be reached from the start state q0 by reading strings of characters from Σ.
Can you write Python code for it?

def reachable(q0, Σ, 𝛿):
    Result = { q0 }
    while True:
        NewStates = { 𝛿[p, c] for p in Result for c in Σ }
        if NewStates <= Result:
            return Result
        Result |= NewStates


I am working on the file "Python/Minimize.ipynb".
The first task is:
The function separate takes four arguments:
- Pairs  a set Pairs of pairs of states from some given Fsm $F$.
If $(p_1, p_2) \in \texttt{Pairs}$, then $p_1$ and $p_2$ are known to be separable.
- States is the set of all states of the Fsm $F$,
- Σ is the alphabet of the Fsm $F$.
- 𝛿 is the transition function of the Fsm
The function separate(Pairs, States, Σ, 𝛿) computes the set of pairs of states $(q_1, q_2)$ that are separable because there is some character $c \in \Sigma$ such that
$$\delta(q_1,c) = p_1, \quad \textrm{but} \quad \delta(q_2,c) = p_2. $$
Can you write Python code for it?


I am working on the file "Python/Minimize.ipynb".
The first task is:
This function takes three arguments:
- Pairs  a set Pairs of pairs of states from some Fsm $F$.
   If $(p_1, p_2) \in \texttt{Pairs}$, then $p_1$ and $p_2$ are known to be separable,
- States is the set of states of the Fsm $F$,
- Sigma  is the alphabet of the underlying Fsm $F$.
The function separate(Pairs) computes the set of pairs of states $(q_1, q_2)$ that are separable because there is some
$c \in \Sigma$ such that
$$\delta(q_1,c) = p_1, \quad \delta(q_2,c) = p_2. $$
Can you write Python code for it?


I am working on the file "Python/MnistDigitsKeras.ipynb".
The first task is:
EVALUATION AND PREDICTION
Can you write Python code for it?

score = model.evaluate(X_test,Y_test,verbose=0)
print('Test score:', score[0])
print('Test accuracy:',score[1])


I am working on the file "Python/MnistDigitsKeras.ipynb".
The first task is:
MODEL DEFINITION
Can you write Python code for it?

model = Sequential()
model.add(Dense(512,input_shape=(784,)))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(10))
model.add(Activation('softmax'))

model.summary()


I am working on the file "Python/MnistDigitsKeras.ipynb".
The first task is:
Now lets predict one single sample
Can you write Python code for it?

X_test_0 = X_test[0,:].reshape(1,784)
Y_test_0 = Y_test[0,:]
print(Y_test_0)
plt.imshow(X_test_0.reshape(28,28))

pred = model.predict(X_test_0)
print('Label of testing sample:', np.argmax(Y_test_0))
print('\nOutput of the softmax layer:',pred[0])
print('\nNeural Network prediction:', np.argmax(pred[0]))


I am working on the file "Python/NFA-2-DFA.ipynb".
The first task is:
Converting a Non-Deterministic Fsm into a Deterministic Fsm
In this notebook we show how a non-deterministic Fsm
$$ F = \langle Q, \Sigma, \delta, q_0, A \rangle $$
can be transformed into a deterministic Fsm $\texttt{det}(F)$ such that both Fsms accept the
same language, that is we have
$$ L(F) = L\bigl(\texttt{det}(F)\bigr). $$
The idea behind this transformation is that the Fsm $\texttt{det}(F)$ has to
compute the set of all states that the Fsm $F$ could be in.
Hence the states of the deterministic Fsm $\texttt{det}(F)$ are
sets of states of the non-deterministic Fsm $F$.  A set of these states contains all those states that the non-deterministic Fsm
$F$ could have reached.  Furthermore, a set $M$ of states of the Fsm $F$ is an accepting state of the Fsm $\texttt{det}(F)$ if the set $M$ contains an accepting state of the Fsm $F$.

In order to present the construction of $\texttt{det}(F)$ we first have to define three auxiliary functions.
We start with the function bigUnion.  Given a set M that contains frozensets, the epression bigUnion(M)
returns the union of all sets in M, i.e. we have
$$ \texttt{bigUnion}(M) = \bigcup M = \bigl{ x \bigm| \exists A \in M: x \in A \bigr}. $$
The resulting set is returned as a frozenset.
Can you write Python code for it?

def bigUnion(M):
    return frozenset({ x for A in M for x in A })

bigUnion({ frozenset({1,2,3}), frozenset({2,3,4}), frozenset({3,4,5}) })


I am working on the file "Python/NFA-2-DFA.ipynb".
The first task is:
Converting a Non-Deterministic Fsm into a Deterministic Fsm
In this notebook we show how a non-deterministic Fsm
$$ F = \langle Q, \Sigma, \delta, q_0, A \rangle $$
can be transformed into a deterministic Fsm $\texttt{det}(F)$ such that both Fsms accept the
same language, that is we have
$$ L(F) = L\bigl(\texttt{det}(F)\bigr). $$
The idea behind this transformation is that the Fsm $\texttt{det}(F)$ has to
compute the set of all states that the Fsm $F$ could be in.
Hence the states of the deterministic Fsm $\texttt{det}(F)$ are
sets of states of the non-deterministic Fsm $F$.  A set of these states contains all those states that the non-deterministic Fsm
$F$ could have reached.  Furthermore, a set $M$ of states of the Fsm $F$ is an accepting state of the Fsm $\texttt{det}(F)$ if the set $M$ contains an accepting state of the Fsm $F$.

In order to present the construction of $\texttt{det}(F)$ we first have to define two auxiliary functions.
We start with the $\varepsilon$-closure of a given state.
The function epsClosure takes two arguments:
- s is a state,
- delta is the transition function of the non-deterministic Fsm $F$.
The function computes the set of all those states that can be reached from the state
s via $\varepsilon$-transitions.
Formally, the set $\texttt{epsClosure}(q)$ is defined...
Can you write Python code for it?

def epsClosure(s, delta):
    Result = { s }
    while True:
        NewStates = { p for q in Result
                        for p in delta.get((q, ''), set())
                    }
        if NewStates <= Result:
            return frozenset(Result)
        Result |= NewStates


I am working on the file "Python/NFA-2-DFA.ipynb".
The first task is:
Now we are ready to formally define how the deterministic Fsm $\texttt{det}(F)$
is computed from the non-deterministic Fsm
$F = \bigl\langle Q, \Sigma, \delta, q_0, A \bigr\rangle$.
We define:
$$ \texttt{det}(F) := \bigl\langle \texttt{allStates}(\texttt{epsClosure}(q_0)), \Sigma, \Delta, \texttt{epsClosure}(q_0), \widehat{A} \bigr\rangle $$
where the components of this tuple are given as follows:
- The set of states of $\texttt{det}(F)$ is the set of all states that can be reached from the set $\texttt{epsClosure}(q_0)$.
- The input alphabet $\Sigma$ does not change when going from $F$ to $\texttt{det}(F)$.
  After all, the deterministic Fsm $\texttt{det}(F)$ has to recognize the same language as the non-deterministic
  Fsm $F$.
- The function $\Delta$, that has been defined previously, specified how the set of states change when a
  character is read.
- The start state $\texttt{epsClosure}(q_0)$ of the non-deterministic Fsm $\texttt{det}(F)$ is the set of all states
  that can be reached from the start state $q_0$ of the non-deterministic Fsm $F$
  via $\varepsilon$-transitions.
- The set of accepting states $\widehat{A}$ is the set of those subsets of $Q$ that contain an accepting
  state of the Fsm $F$:
  $$\widehat{A} := \bigl{ M \in 2^Q \mid M \cap A \not= {} \bigl}. $$
Can you write Python code for it?

def nfa2dfa(nfa):
    States, Sigma, delta, q0, Final = nfa
    newStart   = epsClosure(q0, delta)
    NewStates  = allStates(newStart, delta, Sigma)
    newDelta   = { (M, c): capitalDelta(M, c, delta) for M in NewStates
                                                     for c in Sigma
                 }
    NewFinal   = { M for M in NewStates
                     if  M & Final != set()
                 }
    return NewStates, Sigma, newDelta, newStart, NewFinal


I am working on the file "Python/NFA-2-DFA.ipynb".
The first task is:
Now we are ready to formally define how the deterministic Fsm $\texttt{det}(F)$
is computed from the non-deterministic Fsm
$F = \bigl\langle Q, \Sigma, \delta, q_0, A \bigr\rangle$.
We define:
$$ \texttt{det}(F) := \bigl\langle \texttt{allStates}(\texttt{epsClosure}(q_0)), \Sigma, \Delta, \texttt{epsClosure}(q_0), \widehat{A} \bigr\rangle $$
where the components of this tuple are given as follows:
- The set of states of $\texttt{det}(F)$ is the set of all states that can be reached from the set $\texttt{epsClosure}(q_0)$.
- The input alphabet $\Sigma$ does not change when going from $F$ to $\texttt{det}(F)$.
  After all, the deterministic Fsm $\texttt{det}(F)$ has to recognize the same language as the non-deterministic
  Fsm $F$.
- The function $\Delta$, that has been defined previously, specified how the set of states change when a
  character is read.
- The start state $\texttt{epsClosure}(q_0)$ of the non-deterministic Fsm $\texttt{det}(F)$ is the set of all states
  that can be reached from the start state $q_0$ of the non-deterministic Fsm $F$
  via $\varepsilon$-transitions.
- The set of accepting states $\widehat{F}$ is the set of those subsets of $Q$ that contain an accepting
  state of the Fsm $F$:
  $$\widehat{A} := \bigl{ M \in 2^Q \mid M \cap A \not= {} \bigl}. $$
Can you write Python code for it?


I am working on the file "Python/NFA-2-DFA.ipynb".
The first task is:
The function epsClosure takes two arguments:
- s is a state,
- delta is the transition function of the non-deterministic Fsm $F$.
The function computes the set of all those states that can be reached from the state
s via $\varepsilon$-transitions.
Formally, the set $\texttt{epsClosure}(q)$ is defined inductively:
- $s \in \texttt{epsClosure}(s)$.
- $p \in \texttt{epsClosure}(s) \wedge r \in \delta(p, \varepsilon) \;\rightarrow\; r \in \texttt{epsClosure}(s)$.
If the state $p$ is an element of the $\varepsilon$-closure of the state $s$
  and there is an $\varepsilon$-transition from $p$ to some state $r$, then $r$
  is also an element of the $\varepsilon$-transition of $s$.
The implementationof epsClosure uses a fixed-point algorithm.
Can you write Python code for it?

def epsClosure(s, delta):
    Result = frozenset({ s })
    while True:
        NewStates = bigUnion({ frozenset(delta.get((q, ''), set())) for q in Result })
        if NewStates <= Result:
            return Result
        Result |= NewStates


I am working on the file "Python/NotebookExample.ipynb".
The first task is:
Aha, this is good.  You can't value a FRA with a discounting model because its cashflow depends on 3 month Jibar and your model does not know anything about 3 month Jibar.
With this type of constraint (which is deeply embedded in the library):

You will never work under the wrong numeraire again
You will never use the wrong curve to forecast a rate or asset price
You will never incorrectly combine cashflows in different currencies
etc.

For our problem at hand we need to fix the model by setting it up to forecast some rates:
Can you write Python code for it?

from QuantSA.Core.MarketData import *

# add a forecast curve
forwardRates = [0.070614, 0.070614]
forecastCurve = ForecastCurve(valueDate, jibar, dates, forwardRates) # use flat 7% rates for forecasting
numeraireModel = DeterministicCurves(discountCurve);
numeraireModel.AddRateForecast(forecastCurve) # add the forecast curve to the model
otherModels = List[Simulator]() # no model other than discounting for now.
coordinator = Coordinator(numeraireModel, otherModels, 1)

# value the product
value = coordinator.Value(fra, valueDate)
print("value is: {:.2f}".format(value))


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

from QuantSA.CoreExtensions.Data import *
from QuantSA.Shared.State import *

SharedDataLoader.LoadFromFolder(r"C:\Dev\QuantSAFolder\QuantSA\QuantSA\QuantSA.Excel.AddIn\bin\Debug")
zar = QuantSAState.GetCurrency("ZAR")
jibar = QuantSAState.GetFloatRateIndex("ZAR.JIBAR.3M")

print(zar)
print(jibar)


I am working on the file "Python/NotebookExample.ipynb".
The first task is:
Making a product
Can you write Python code for it?

from QuantSA.Core.Products.Rates import *

nearDate = Date("2021-09-14")
farDate = Date("2021-12-14")
fra = FRA(1000000, 0.25, 0.07, True, nearDate, farDate, jibar)

print(fra)


I am working on the file "Python/Object-Identification.ipynb".
The first task is:
Given a coordinate pair $p = (x,y)$, the function $\texttt{extract_component}(p)$ extracts the set of all coordinates that point to equivalent pixels.  It returns a pair $(c, i)$ where $c$ is the number of equivalent pixels and $i$ is an image that only contains the pixels equivalent to the pixel at $(x,y)$.
Can you write Python code for it?

def extract_component(p):
    x, y      = p
    new_image = image.copy()
    m, n, _   = image.shape
    root_xy   = UF.find((x, y))
    count     = 0
    mean      = [0, 0, 0]
    for a in range(0, m):
        for b in range(0, n):
            root_ab = UF.find((a, b))
            if root_xy != root_ab:
                for c in range(3):
                    new_image[a, b, c] = 255
            else:
                for c in range(3):
                    mean[c] += image[a, b, c]
                count += 1
    for c in range(3):
        mean[c] /= count
    print(f'component at {p} has {count} pixels')
    return (count, new_image, mean)


I am working on the file "Python/Object-Identification.ipynb".
The first task is:
Neighbouring pixels that differ by less than threshold will be considered equivalent, i.e. we assume that they are part of the same component.
Can you write Python code for it?


I am working on the file "Python/Object-Identification.ipynb".
The first task is:
Now we are left with $9$ components.  Lets view them.
Can you write Python code for it?

Mean = {}
for r in Roots:
    size, component, mean = extract_component(r)
    Mean[r] = mean
    plt.imshow(component)
    plt.show()


I am working on the file "Python/Object-Identification.ipynb".
The first task is:
Since the picture has been taken in bright sunlight, the illumination is inhomogeneous: The front part of the car is much brighter than the sides.  Hence we have to join these parts manually.
Can you write Python code for it?


I am working on the file "Python/Object-Identification.ipynb".
The first task is:
The function $\texttt{simlar}(p_1, p_2)$ takes two coordinate pairs $p_1 = (x_1,y_1)$ and $p_2 = (x_2,y_2)$ and checks,
whether the pixels at these coordinates are equivalent.
Can you write Python code for it?

def similar(p1, p2):
    x1, y1 = p1
    x2, y2 = p2
    X = image[x1, y1]
    Y = image[x2, y2]
    return distance(X, Y) < threshold


I am working on the file "Python/Object-Identification.ipynb".
The first task is:
The function all_components computes all those components that have more than $2000$ pixels.
Can you write Python code for it?

def all_components():
    Roots = set()
    for x in range(0, m):
        for y in range(0, n):
            root = UF.find((x, y))
            if UF.mSize[root] > 2000 and not root in Roots:
                print(UF.mHeight[root])
                Roots.add(root)
    return Roots

Roots = all_components()
len(Roots)


I am working on the file "Python/Object-Identification.ipynb".
The first task is:
The image has a height of 959 pixels and a width of 1439 pixels:
Can you write Python code for it?


I am working on the file "Python/Object-Identification.ipynb".
The first task is:
We have found $28$ different components that have at least 2000 pixels.  Let us extract the corresponding images.
Can you write Python code for it?

Mean = {}
for r in Roots:
    size, component, mean = extract_component(r)
    Mean[r] = mean


I am working on the file "Python/Object-Identification.ipynb".
The first task is:
We iterate over all pixels and for every pixel over all of its neighbouring pixels.  If a pixel is similar to a neighbouring pixel,
we declare these pixels as equivalent in our union-find data structure UF.
Can you write Python code for it?


I am working on the file "Python/Object-Identification.ipynb".
The first task is:
We use the union-find data structure implemented earlier.
Can you write Python code for it?


I am working on the file "Python/Parse-Table.ipynb".
The first task is:
A Parse Table for a Shift-Reduce Parser
This notebook contains the parse table that is needed for a shift reduce parser that parses the following grammar:
$$
  \begin{eqnarray}
  \mathrm{expr}        & \rightarrow & \mathrm{expr}\;\;\texttt{'+'}\;\;\mathrm{product}   \
                       & \mid        & \mathrm{expr}\;\;\texttt{'-'}\;\;\mathrm{product}   \
                       & \mid        & \mathrm{product}                                    \[0.2cm]
  \mathrm{product}     & \rightarrow & \mathrm{product}\;\;\texttt{''}\;\;\mathrm{factor} \
                       & \mid        & \mathrm{product}\;\;\texttt{'/'}\;\;\mathrm{factor} \
                       & \mid        & \mathrm{factor}                                     \[0.2cm]
  \mathrm{factor}      & \rightarrow & \texttt{'('} \;\;\mathrm{expr} \;\;\texttt{')'}     \
                       & \mid        & \texttt{NUMBER}
  \end{eqnarray*}
$$
Below, we define the grammar rules.
Can you write Python code for it?


I am working on the file "Python/Parse-Table.ipynb".
The first task is:
Below is the definition of the goto table.
Can you write Python code for it?

gotoTable   = {}

gotoTable['s0', 'E'] = 's4'
gotoTable['s0', 'P'] = 's3'
gotoTable['s0', 'F'] = 's1'

gotoTable['s5', 'E'] = 's6'
gotoTable['s5', 'P'] = 's3'
gotoTable['s5', 'F'] = 's1'

gotoTable['s8', 'P'] = 's15'
gotoTable['s8', 'F'] = 's1'

gotoTable['s9', 'P'] = 's10'
gotoTable['s9', 'F'] = 's1'

gotoTable['s11', 'F'] = 's14'
gotoTable['s12', 'F'] = 's13'


I am working on the file "Python/Parse-Table.ipynb".
The first task is:
Finally, we define the state table.  This is table is only used for pretty printing.
Can you write Python code for it?


I am working on the file "Python/Parse-Table.ipynb".
The first task is:
Finally, we define the state table.  This is table is only used for pretty printing.  This table gives us a clue what is actually the information that is stored in the different stats.
Can you write Python code for it?


I am working on the file "Python/Parse-Table.ipynb".
The first task is:
Next, we define the action table as a dictionary.
Can you write Python code for it?

actionTable = {}

actionTable['s0', '('  ] = ('shift', 's5')
actionTable['s0', 'NUMBER'] = ('shift', 's2')

actionTable['s1', 'EOF'] = ('reduce', r6)
actionTable['s1', '+'  ] = ('reduce', r6)
actionTable['s1', '-'  ] = ('reduce', r6)
actionTable['s1', '*'  ] = ('reduce', r6)
actionTable['s1', '/'  ] = ('reduce', r6)
actionTable['s1', ')'  ] = ('reduce', r6)

actionTable['s2', 'EOF'] = ('reduce', r8)
actionTable['s2', '+'  ] = ('reduce', r8)
actionTable['s2', '-'  ] = ('reduce', r8)
actionTable['s2', '*'  ] = ('reduce', r8)
actionTable['s2', '/'  ] = ('reduce', r8)
actionTable['s2', ')'  ] = ('reduce', r8)

actionTable['s3', 'EOF'] = ('reduce', r3)
actionTable['s3', '+'  ] = ('reduce', r3)
actionTable['s3', '-'  ] = ('reduce', r3)
actionTable['s3', '*'  ] = ('shift', 's12')
actionTable['s3', '/'  ] = ('shift', 's11')
actionTable['s3', ')'  ] = ('reduce', r3)

actionTable['s4', 'EOF'] = 'accept'
actionTable['s4', '+'  ] = ('shift', 's8')
actionTable['s4', '-'  ] = ('shift', 's9')

actionTable['s5', '('  ] = ('shift', 's5')
actionTable['s5', 'NUMBER'] = ('shift', 's2')

actionTable['s6', '+'  ] = ('shift', 's8')
actionTable['s6', '-'  ] = ('shift', 's9')
actionTable['s6', ')'  ] = ('shift', 's7')

actionTable['s7', 'EOF'] = ('reduce', r7)
actionTable['s7', '+'  ] = ('reduce', r7)
actionTable['s7', '-'  ] = ('reduce', r7)
actionTable['s7', '*'  ] = ('reduce', r7)
actionTable['s7', '/'  ] = ('reduce', r7)
actionTable['s7', ')'  ] = ('reduce', r7)

actionTable['s8', '('  ] = ('shift', 's5')
actionTable['s8', 'NUMBER'] = ('shift', 's2')

actionTable['s9', '('  ] = ('shift', 's5')
actionTable['s9', 'NUMBER'] = ('shift', 's2')

actionTable['s10', 'EOF'] = ('reduce', r2)
actionTable['s10', '+' ] = ('reduce', r2)
actionTable['s10', '-' ] = ('reduce', r2)
actionTable['s10', '*' ] = ('shift', 's12')
actionTable['s10', '/' ] = ('shift', 's11')
actionTable['s10', ')' ] = ('reduce', r2)

actionTable['s11', '('  ] = ('shift', 's5')
actionTable['s11', 'NUMBER'] = ('shift', 's2')

actionTable['s12', '('  ] = ('shift',...


I am working on the file "Python/Personalized Medicine.ipynb".
The first task is:
Check for missing values in both training and testing data columns
Can you write Python code for it?

import missingno as msno
msno.bar(training_merge_df)

msno.bar(testing_merge_df)


I am working on the file "Python/Personalized Medicine.ipynb".
The first task is:
Getting 60% accuracy with only LinearSVC. Try different ensemble models to get more accurate model.
Can you write Python code for it?

y_test_predicted = text_clf.predict(X_test)
np.mean(y_test_predicted == y_test)


I am working on the file "Python/Personalized Medicine.ipynb".
The first task is:
Preparing Predictive Results data
Can you write Python code for it?

predictive_results_df = testing_merge_df[["ID",1,2,3,4,5,6,7,8,9]]
predictive_results_df.columns = ['ID', 'class1','class2','class3','class4','class5','class6','class7','class8','class9']
predictive_results_df.head(5)

predictive_results_df.to_csv('data/predictiveresults.csv', index=False)


I am working on the file "Python/Personalized Medicine.ipynb".
The first task is:
Split the training data to train and test for checking the model accuracy
Can you write Python code for it?

from sklearn.model_selection import train_test_split

train ,test = train_test_split(training_merge_df,test_size=0.2)
np.random.seed(0)
train


I am working on the file "Python/Personalized Medicine.ipynb".
The first task is:
Y Training and Y Test Data Counts
Can you write Python code for it?

import seaborn as sb
sb.set(style="whitegrid", color_codes=True)
sb.countplot(x="Class",data=train,palette="Greens_d")

sb.countplot(x="Class",data=test,palette="Blues")

X_train = train['Text'].values
X_test = test['Text'].values
y_train = train['Class'].values
y_test = test['Class'].values

from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn import svm


I am working on the file "Python/Plotly/PlotlyTutorial.ipynb".
The first task is:
Annotations
We added a plot title as well as titles for all the axes. For fun we could add some text annotation as well in order to indicate the maximum point that's been plotted on the current plotting surface.
Can you write Python code for it?

# highest point
layout.update(dict(
    annotations=[Annotation(
        text="Highest Point",
        x=3,
        y=6)]
)
             )
py.offline.iplot(Figure(data=data, layout=layout), filename='pyguide_4')

#lowest point
layout.update(dict(
annotations = [Annotation(
text = "lowest point",
x=1,
y=4)]))

py.offline.iplot(Figure(data=data,layout=layout))


I am working on the file "Python/Plotly/PlotlyTutorial.ipynb".
The first task is:
Data
We see that data is actually a list object in Python. Data will actually contain all the traces that you wish to plot. Now the question may be, what is a trace? A trace is just the name we give a collection of data and the specifications of which we want that data plotted. Notice that a trace will also be an object itself, and these will be named according to how you want the data displayed on the plotting surface.
Can you write Python code for it?

# generate data
x = np.linspace(0,np.pi*8,100)
y = np.sin(x)
z = np.cos(x)

layout = Layout(
    title="My First Plotly Graph",
    xaxis = dict(
        title="x"),
    yaxis = dict(title="sin(x)")
)

trace1 = Scatter(
    x = x,
    y = y,
    mode = "lines",
    marker = dict(
        size=8,
        color="navy"
    ),
    name="Sin(x)"
)

trace2 = Scatter(
    x = x,
    y = z,
    mode = "markers+lines",
    marker = dict(
        size=8,
        color="red"
    ),
    name="Cos(x)",
    opacity=0.5
)

# load data and fig with nec
data = Data([trace1,trace2])
fig = Figure(data=data, layout=layout)

#plot
py.offline.iplot(fig)

# look at hover text

x = np.arange(1,3.2,0.2)
y = 6*np.sin(x)

layout = Layout(
    title="My Second Plotly Graph",
    xaxis = dict(
        title="x"),
    yaxis = dict(title="6 * sin(x)")
)

trace1 = Scatter(
    x=[1,2,3],
    y=[4,5,6],
    marker={'color': 'red', 'symbol': 104, 'size': "10"},
    mode="markers+lines",
    text=["one","two","three"],
name="first trace")

trace2 = Scatter(x=x,
                 y=y,
                 marker={'color': 'blue', 'symbol': 'star', 'size': 10},
                 mode='markers',
                 name='2nd trace')

data = Data([trace1,trace2])
fig = Figure(data=data,layout=layout)

py.offline.iplot(fig)


I am working on the file "Python/Plotly/PlotlyTutorial.ipynb".
The first task is:
Layout
The Layout object will define the look of the plot, and plot features which are unrelated to the data. So we will be able to change things like the title, axis titles, spacing, font and even draw shapes on top of your plot!
Can you write Python code for it?