I am working on the file "Cours09_DILLMANN_ISEP2016.ipynb".
The first task is:
1.3) Concept informatique
Qu’est ce qu’est la généricité (le fait qu’un code soit générique) ?
Can you write Python code for it?

def Ajoute_dix(inputData):
    if isinstance(inputData, int):
        return inputData + 10
    elif isinstance(inputData, str):
        return int(inputData) + 10
    else:
        return 10

# applications avec un INT
a=15
print("Le type de a est : {}".format(type(a)))
b=Ajoute_dix(a)
print("En applicant la fonction Ajoute_dix à (a) on obtient : {}".format(b))
print("Qui est de type : {}".format(type(b)))
# applications avec un STR
a='15'
print("Le type de a est : {}".format(type(a)))
b=Ajoute_dix(a)
print("En applicant la fonction Ajoute_dix à (a) on obtient : {}".format(b))
print("Qui est de type : {}".format(type(b)))


I am working on the file "Cours09_DILLMANN_ISEP2016.ipynb".
The first task is:
2)  Partie 2 : Operateurs Arithmétiques
Can you write Python code for it?

a=5%2
print('a=5%2 is a {} of type {}'.format(a,type(a)))

a=[1]+3
print('a=[1]+3 is a {} of type {}'.format(a,type(a)))

a=[1]*3
print('a=a=[1]*3 is a {} of type {}'.format(a,type(a)))

a=9.0*9
print('a=9.0*9 is a {} of type {}'.format(a,type(a)))

a=9*"9"
print("a=9*“9” is a {} of type {}".format(a,type(a)))

a = 4 == "4"
print("a=4=“4” is a {}".format(a,type(a)))

a=4="4"
print("a=4=“4” is a {}".format(a)))


I am working on the file "Cours09_DILLMANN_ISEP2016.ipynb".
The first task is:
2.3 Question 3
Can you write Python code for it?


I am working on the file "Cours09_DILLMANN_ISEP2016.ipynb".
The first task is:
2.3.1 Question 3.1
Can you write Python code for it?

liste1[ len(liste1) ]

liste1[ - len(liste1) ]


I am working on the file "Cours09_DILLMANN_ISEP2016.ipynb".
The first task is:
2.4 Question 4
Can you write Python code for it?

a=2
while a <= 7:
    a = a*a + a + 1
print(a)


I am working on the file "Cours09_DILLMANN_ISEP2016.ipynb".
The first task is:
2.5 Question 5
Can you write Python code for it?

a=1
for it in range(5):
    a = a + it
print(a)


I am working on the file "Cours09_DILLMANN_ISEP2016.ipynb".
The first task is:
2.6 Question 6
Can you write Python code for it?

liste1 = [1,2,3]
liste2 = liste1
liste2.append(4)
print(liste1)


I am working on the file "Cours09_DILLMANN_ISEP2016.ipynb".
The first task is:
2.7 Question 7
Can you write Python code for it?

x=8
liste1 = [ ]
for it in range(x//2):
    liste1.append( x - it )
print( liste1 )


I am working on the file "Cours09_DILLMANN_ISEP2016.ipynb".
The first task is:
3.1.3 Exercice 1.3
Can you write Python code for it?

def somme_carre( n ):
    res = 0
i=0
while i < n:
i += 1
        res += i*i
      return res

def somme_carre( n ):
    res = 0
    i=0
    while i < n:
        i += 1
        res += i*i
    return res

somme_carre(10)


I am working on the file "Cours09_DILLMANN_ISEP2016.ipynb".
The first task is:
3.1.4) Exercice 1.4
Can you write Python code for it?

from math import srqt #Faute de frape
def somme_sqrt( n ):
res = 0 i=0 while i < n #On ne declare pas les vaiables sur la m ligne
i += 1#Faute d'indentation
        res += sqrt(i)
    return res

from math import sqrt #Faute de frape
def somme_sqrt( n ):
    res = 0
    i=0
    while (i < n):
        i += 1
        res += sqrt(i)
    return res

somme_sqrt(5)


I am working on the file "Cours09_DILLMANN_ISEP2016.ipynb".
The first task is:
3.2 Exercice 3
Can you write Python code for it?

def somme_racine_cubique( n ):
    res = 0
    for it in range(n):
        res += it**(1/3)
    return res

somme_racine_cubique(8)

def somme_racine_cubique( n ):
    res = 0
    for it in range(n+1):
        res += it**(1/3)
    return res

somme_racine_cubique(8)


I am working on the file "Cours09_DILLMANN_ISEP2016.ipynb".
The first task is:
4) Analyse de code
Dans cette partie les codes fonctionnent, le but étant de comprendre leur effet, ou d’être capable de les simplifier.
4.1 Exercice 1
Can you write Python code for it?


I am working on the file "Cours09_DILLMANN_ISEP2016.ipynb".
The first task is:
4.2 Exercice 2
Can you write Python code for it?

def f( n ):
    if n == 0:
        return False
    return not f( n - 1 )

a = f(12)
b = f(8)
c = f(13)
print(a,b,c)

for it in range(50):
    print('le nombre {0} est il impair ? {1}'.format(it,f(it)))


I am working on the file "Cours09_DILLMANN_ISEP2016.ipynb".
The first task is:
5) Codage
5.1 Exercice 1 : somme de multiples pairs
et evaluation de leur performance...
Can you write Python code for it?

from time import clock
def duree(fonction, n=10):
    debut = clock()
    fonction(n)
    fin = clock()
    return fin - debut

def somme_n_entiers1(n):
    ref=0
    l=range(n+1)
    for it in l:
        if it % 2 == 0:
            ref += it
    return ref

def somme_n_entiers2(n):
    it=0
    ref=0
    l=range(n+1)
    while (it < len(l)):
        if (l[it] % 2 == 0):
            ref += it
        it += 1
    return ref

def SumRec(n):
    if n  == 0:
        return(0)
    elif n % 2 != 0:
        return(n-1 + SumRec(n-3))
    else :
        return(n + SumRec(n-2))

def SommePair(n):
    return sum(range(0,n+1,2))

print('{:>25}> {:f} s'.format('utilisation de la fonction for ',duree(somme_n_entiers1) ))
print('{:>25}> {:f} s'.format('utilisation de la fonction while ',duree(somme_n_entiers2) ))
print('{:>25}> {:f} s'.format('utilisation la plus simple ',duree(SommePair) ))
print('{:>25}> {:f} s'.format('utilisation la recursivité ',duree(SumRec) ))


I am working on the file "Cours11-DILLMANN-ROUSSET-ISEP2016.ipynb".
The first task is:
3. Accès aux sous-ensembles
Cet accès se fait avec la notation [a:b]. La borne a étant incluse et la borne b excluse.
On peut omettre l'une ou l'autre des bornes, dans ce cas cela signifira "depuis le début de la liste" ou "jusqu'à la fin de la liste".
On peut utiliser des indices négatifs.
A noter que cette notation est "permissive". Il n'y a pas d'erreur si a ou b dépasse les capacités de la liste.
De même si la position de a dans la liste se situe après b, ou correspond à b, cela renverra une liste vide, sans erreur.
Par exemple :
Can you write Python code for it?

s = "elephant"
s1 = s[2:5] ## s1 vaut "eph"
print( s1 )

## Pour extraire la première moitié
print( s[:len(s)//2] )

s = "elephant"
s1 = s[:3] ## s1 vaut "ele"
print( s1 )

s = "elephant"
s1 = s[5:] ## s1 vaut "ant"
print( s1 )

s = "elephant"
s1 = s[:-2] ## s1 vaut "eleph"
print( s1 )

s = "elephant"
s1 = s[-100:100] ## s1 vaut "elephant"
s1

s = "elephant"
s1 = s[100:-100] ## s1 vaut ""
s1


I am working on the file "Cours11-DILLMANN-ROUSSET-ISEP2016.ipynb".
The first task is:
C'est un exemple de généricité, python possède un certain nombre d'opérations communes aux ensembles, quelque soit la nature de cet ensemble (une string est un ensemble de caractère, une liste est un ensemble d'éléments quelconques).
Les opérations en commun sont les suivantes :

addition
multiplication
boucle for
fonction len( ... )
accès aux éléments par indice
accès à un sous-ensemble par indice (revision prévue en TD aujourd'hui)

La principale différence est que les chaînes de caractères ne sont pas modifiables ! On ne peut donc PAS effectuer les opérations suivantes :
    - suppression d'un élément (del liste[i])
    - assignation à un élément (liste[i] = ...)
    - ajout d'un élément (append pour une liste)
1. Différence entre identité et égalité
Il y a ici une notion très importante en informatique, celle de différencier la variable et sa valeur.
Cette notion est particulièrement importante pour les listes, car on peut choisir de modifier l'un ou l'autre.
Il faut également distinguer identité et égalité
Deux variables sont identiques si elles pointent vers la même valeur.
Deux variables sont égales si leur valeurs sont égales.
Deux variables identiques sont forcément égales.
Dans les deux exemples ci-dessous, dans le premier les deux variables liste_a et liste_b sont identiques et égales, dans le deuxième seulement égales.
On peut savoir si deux variables sont identiques en utilisant la fonction id ou avec l'opérateur is
Can you write Python code for it?

liste_a = [1,2,3]
liste_b = liste_a
print( "liste_a", liste_a, id(liste_a) )
print( "liste_b", liste_b, id(liste_b) )
print( "liste_a == liste_b => ", liste_a == liste_b )
print( "liste_a is liste_b => ", liste_a is liste_b )

liste_a = [1,2,3]
liste_b = [1,2,3]
print( "liste_a", liste_a, id(liste_a) )
print( "liste_b", liste_b, id(liste_b) )
print( "liste_a == liste_b => ", liste_a == liste_b )
print( "liste_a is liste_b => ", liste_a is liste_b )


I am working on the file "Cours11-DILLMANN-ROUSSET-ISEP2016.ipynb".
The first task is:
Cette notation peut être utilisée pour palier l'impossibilité de modifier un élément d'une chaine de caractère. Par exemple :
Can you write Python code for it?

# Insertion d'une lettre en position 4
chaine_1 = "infomatique"
chaine_1 = chaine_1[:4] + "r" + chaine_1[4:]
print( chaine_1 )

# Modification d'une lettre en position 4
chaine_1 = "infolmatique"
chaine_1 = chaine_1[:4] + "r" + chaine_1[5:]
print( chaine_1 )


I am working on the file "Cours11-DILLMANN-ROUSSET-ISEP2016.ipynb".
The first task is:
La différence est importante lorsque l'on modifie la valeur d'une des deux variables, on modifie les deux.
Il faut ici aussi distinguer lorsque l'on modifie une variable ou lorsque l'on modifie sa valeur.
liste_a.append(4) modifie la valeur de la variable, celle-ci désigne toujours la même liste, mais celle-ci possède un élément en plus
liste_a = liste_a + [4] modifie la variable, mais pas sa valeur précédente. Elle créé une nouvelle liste, issue de la concaténation de deux listes, et assigne cette valeur à liste_a
Dans les 4 cas suivants, on ajoute à chaque fois un élément à a, mais une fois sur deux en modifiant la variable et l'autre en modifiant sa valeur, et on distingue si liste_a et liste_b sont identiques ou seulement égales.
Can you write Python code for it?

liste_a = [1,2,3]
liste_b = liste_a
print( "liste_a", liste_a, id(liste_a) )
print( "liste_b", liste_b, id(liste_b) )
liste_a.append( 4 )
print( "liste_a", liste_a, id(liste_a) )
print( "liste_b", liste_b, id(liste_b) )

liste_a = [1,2,3]
liste_b = liste_a
print( "liste_a", liste_a, id(liste_a) )
print( "liste_b", liste_b, id(liste_b) )
liste_a = liste_a + [ 4 ]
print( "liste_a", liste_a, id(liste_a) )
print( "liste_b", liste_b, id(liste_b) )

liste_a = [1,2,3]
liste_b = [1,2,3]
print( "liste_a", liste_a, id(liste_a) )
print( "liste_b", liste_b, id(liste_b) )
liste_a.append( 4 )
print( "liste_a", liste_a, id(liste_a) )
print( "liste_b", liste_b, id(liste_b) )

liste_a = [1,2,3]
liste_b = [1,2,3]
print( "liste_a", liste_a, id(liste_a) )
print( "liste_b", liste_b, id(liste_b) )
liste_a = liste_a + [ 4 ]
print( "liste_a", liste_a, id(liste_a) )
print( "liste_b", liste_b, id(liste_b) )


I am working on the file "Cours11-DILLMANN-ROUSSET-ISEP2016.ipynb".
The first task is:
Lors de l'importation de vos modules, nous avons vu au moins 3 methodes
1) Importer tout le module
~~~ python
from math import *
print(pi)
donnera
3.141592653589793
~~~
2) Importer les  fonctions d'un sous-module du module et le renomer
~~~ python
import matplotlib.pyplot as plt
plt.axes('equal')
plt.show()
renvoie une fenêtre où les axes ont la même taille
ici "plt" equivaut à "matplotlib.pyplot"
~~~
3) Importer tout (c'est pas toujours la meilleure idée, mais on peux faire...)
~~~ python
import turtle
myPen = turtle.Turtle() #permet d'ouvrir une fenêtre
...
turtle.TK.Scale.rowconfigure(10,0) #change le numero de lignes
ici a chaque fois que l'on veut demander une fonction graphique
on doit préciser tout le chemin pour y arriver
~~~
III) Quelques précisions sur "NONE"
L'objet None est utilisé pour représenter l'absence de d'une valeur. Il est semblable a null dans d'autres langages de programmation.
Tout comme d'autre valeurs "vides", tel que 0, [], ou encore la chaine de charactères nulle "", is peut être traduit par le boléen False, si l'on fait une opération de transtypage python.
Can you write Python code for it?


I am working on the file "Cours12-DILLMANN-ROUSSET-ISEP2016.ipynb".
The first task is:
2) Creer une liste de multiples de 3 de 0 à 20:
Can you write Python code for it?

res=[]
for it in range(20):
    if (it % 3 == 0): res += [it]
print(res)

a=[i for i in range(20) if i%3 == 0]
print(a)


I am working on the file "Cours12-DILLMANN-ROUSSET-ISEP2016.ipynb".
The first task is:
3) Creer une liste de nombre entiers impairs j'usqu'a 20:
Can you write Python code for it?

res=[]
for it in range(20):
    if (it % 2 != 0): res += [it]
print(res)

b=[i for i in range(20) if i % 2 != 0]
print(b)


I am working on the file "Cours12-DILLMANN-ROUSSET-ISEP2016.ipynb".
The first task is:
Cours algorithmie comparée
Nombres premiers
Can you write Python code for it?

def isprime(startnumber):
    startnumber*=1.0
    prime = True
    for divisor in range(2,startnumber**0.5+1):
        if startnumber/divisor==int(startnumber/divisor):
            prime=False
    return prime


I am working on the file "Cours12-DILLMANN-ROUSSET-ISEP2016.ipynb".
The first task is:
La comparaison de des fonctions 'natives' et des opérateurs prouve que les opérateurs sont plus rapides
Can you write Python code for it?

def est_premier_1(n):
    if n < 2:
        return False
    for it in range(2,n):
        if n % it == 0:
            return False
    return True

def est_premier_2(n):
    if n < 2:
        return False
    for it in range(2,int(n**0.5)+2):
        if n % it == 0:
            return False
    return True

def isprime(test_number):
    for divisor in range(2,int(test_number**0.5)+1):
        if test_number/divisor==int(test_number/divisor):
            return False
    return True

def trouve_premier_1(n):
    while not est_premier_1(n):
        n += 1
    return n

def trouve_premier_2(n):
    while not est_premier_2(n):
        n += 1
    return n

def trouve_premier_3(n):
    while not isprime(n):
        n += 1
    return n

print("comparaison de la recherche sequentielle de tous les premiers entre 2**15 et 2**20")
print("Methode brutale en O(n)")
for it in range(15,20):
    print( '{0: <13}'.format(2**it), "=> temps moyen : ", \
          '{0: 1.1f} µs'.format(1e6*temps_moyen(trouve_premier_1, 2**it)))

print("Methode fine en O(sqrt(n))")
for it in range(15,20):
    print( '{0: <13}'.format(2**it), "=> temps moyen : ", \
          '{0: 1.1f} µs'.format(1e6*temps_moyen(trouve_premier_2, 2**it)))

print("Methode implicant l'appel à une fonction int")
for it in range(15,20):
    print( '{0: <13}'.format(2**it), "=> temps moyen : ", \
          '{0: 1.1f} µs'.format(1e6*temps_moyen(trouve_premier_3, 2**it)))


I am working on the file "Cours12-DILLMANN-ROUSSET-ISEP2016.ipynb".
The first task is:
Les deux fonctions ci-dessus possèdent des complexités en :

$\mathcal{O}(n)$ pour somme_1 (on exécute n additions)
$\mathcal{O}(1)$ pour somme_a (on exécute 3 opérations, quelque soit n)

Mesure de la complexité
Ici la complexité se calcule par rapport au terme maximum de la suite que l'on veut caculer. Elle peut se calculer par rapport à différentes valeurs, suivant ce que l'on veut calculer, mais toujours par rapport à une grandeur qui caractérise la complexité du calcul / sa difficulté, par exemple :

la taille d'une liste (très fréquent)
une précision (ou son inverse)
la valeur d'un nombre
la dimension d'une matrice (son nombre d'élément où le plus grand nombre entre son nombre de ligne et son nombre de colonnes ??)
le nombre de décimale de $\pi$ que l'on souhaite calculer.
etc ...

Complexité et temps de calcul
La complexité ne donne pas le temps de calcul ! Celui-ci dépend du type d'opérations que vous souhaitez effectuer, de la puissance de votre machine, du langage, etc ...
A noter que la puissance actuelle des ordinateurs fait que vous n'êtes pas forcément sensibilisé à cette notion, car on peut avoir l'impression que tout est "instantané" Par exemple en python, pour des listes de 10000, voire 100000 éléments toute opération, pas trop compliquée, sera faite instantanément.
Pour pouvoir réfléchir de façon plus poussée qu'un simple chronométrage de temps d'exécution, on a besoin de s'abstraire des éléments matériels.
La complexité est propre à l'algorithme, et non pas au programme (il faut toutefois faire attention que l'implémentation de l'algorithme est correcte).
La rêgle génerale est que les fonctions 'natives' sont mieux optimisées que les fonctions 'maison'
Can you write Python code for it?

import random
def get_r_liste(n):
    liste = list(range(n))
    random.shuffle(liste)
    return liste

def minimum(l):
    mini = l[0]
    for it in range(len(l)):
        if l[it] < mini:
            mini = l[it]
    return mini

print("Test de la fonction 'min' dans des listes contenant entre 100 et 10000 élements")
for it in range(2,5):
    l = get_r_liste(10**it)
    print( '{0: <10}'.format(10**it), "=> temps moyen : ",  '{0: 1.1f} µs'.format(1e6*temps_moyen(min, l)))

print("Test de la fonction 'minimum' dans des listes contenant entre 100 et 10000 élements")
for it in range(2,5):
    l = get_r_liste(10**it)
    print( '{0: <10}'.format(10**it), "=> temps moyen : ", '{0: 1.1f} µs'.format(1e6*temps_moyen(minimum, l)))


I am working on the file "Cours12-DILLMANN-ROUSSET-ISEP2016.ipynb".
The first task is:
Nous allons proposer un algorithme qui décompose les entiers en une liste de nombre premiers...
$$100=225*5$$
Can you write Python code for it?

Ma_decomposition=[]
it_1 = largest_prime_factor(100)
Ma_decomposition.append(int(it_1))
it_2 = largest_prime_factor(100/it_1)
Ma_decomposition.append(int(it_2))
it_3 = largest_prime_factor(100/(it_1*it_2))
Ma_decomposition.append(int(it_3))
it_4 = largest_prime_factor(100/(it_1*it_2*it_3))
Ma_decomposition.append(int(it_4))

Ma_decomposition.reverse()
print(Ma_decomposition)

Ma_decomposition=[]
nombre_a_factoriser=9

it=largest_prime_factor(nombre_a_factoriser)
Ma_decomposition.append(int(it))

while it < nombre_a_factoriser:
    it1 = largest_prime_factor(nombre_a_factoriser/it)
    Ma_decomposition.append(int(it1))
    it *= it1

Ma_decomposition.reverse()
print(Ma_decomposition)

resultat = 1
for it in Ma_decomposition: resultat *= it
print(resultat)

def liste_decomposition(nombre_a_factoriser):
    Ma_decomposition=[]
    it=0
    if nombre_a_factoriser != 2:
        it=largest_prime_factor(nombre_a_factoriser)
        Ma_decomposition.append(int(it))
        while it < nombre_a_factoriser:
            it1 = largest_prime_factor(nombre_a_factoriser/it)
            Ma_decomposition.append(int(it1))
            it *= it1
        Ma_decomposition.reverse()

        return Ma_decomposition
    else:
        Ma_decomposition=[2]
        return Ma_decomposition

liste_decomposition(100)


I am working on the file "Cours12-DILLMANN-ROUSSET-ISEP2016.ipynb".
The first task is:
On remarque que cet algorithme est itératif, il implique un test systematique sur TOUS les entiers compris entre 2 et l'entier arrondi supérieur de la racine carrée de test_number, on dit par exemple que cet algoritme utilise un espace asymptotique en : $$\mathcal{O}\Big(\sqrt{n}\Big)$$
C'est la notation de LANDAU
Can you write Python code for it?

for a in range(2,100):
    if isprime(a):
        print(a,end=" ")


I am working on the file "Cours13-DILLMANN-ISEP2016.ipynb".
The first task is:
2) Visualisation géométrique
Can you write Python code for it?

from mpl_toolkits.mplot3d import *
import matplotlib.pyplot as plt
import numpy as np
from random import random, seed
from matplotlib import cm

#%%%%%%%%% Presentation d'une bulle rouge %%%%%%%%#
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)

x = 10 * np.outer(np.cos(u), np.sin(v))
y = 10 * np.outer(np.sin(u), np.sin(v))
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))

ax.plot_surface(x, y, z, rstride=2, cstride=2, linewidth=0, alpha=1, color='y', antialiased=True, edgecolor=(0,0,0,0))

plt.show()


I am working on the file "Cours13-DILLMANN-ISEP2016.ipynb".
The first task is:
Lambda est un racourci pour créer des fonctions anonymes
Elles ne sont pas plus faciles à ecrire
Can you write Python code for it?

a=(lambda x: x*x)(8)
print(a)

def polynome(x):
    return x**2 + 5*x + 4

racine=-4
print("La racine d'un polynome est la valeur pour laquelle est {0} " \
      .format(polynome(racine)))
print("Avec un lambda c'est plus simple : ",end="")
print((lambda x:x**2 + 5*x + 4)(-4))

X=np.linspace(-10,10,50,endpoint = True)
plt.plot(X,(lambda x:x**2 + 5*x + 4)(X))
plt.show()


I am working on the file "Cours13-DILLMANN-ISEP2016.ipynb".
The first task is:
Par exemple si nous nous intéressons a l'évolution du cours des actions (valeurs) de différentes entreprises cette année, nous verrons que la pluspart des outils existent et sont disponibles.
Valeurs recherchées :

IBM
YELP
GOOGLE
BRUKER
Can you write Python code for it?

symbols_list = ['IBM','YELP', 'GOOG']

for ticker in symbols_list:
    d[ticker] = DataReader(ticker, "yahoo", '2016-01-01')
# L'execution de cette fonction précise que vous ayez accés à Internet

pan = pandas.Panel(d)
df1 = pan.minor_xs('Adj Close')
px=df1.asfreq('B',method='pad')
rets = px.pct_change()
((1+ rets).cumprod() -1).plot()


I am working on the file "Cours13-DILLMANN-ISEP2016.ipynb".
The first task is:
Une autre manière de définir les fonctions : les Lambdas
Can you write Python code for it?

def my_funct(f,arg):
    return f(arg)

my_funct(lambda x : 2*x*x,5)


I am working on the file "Cours13-DILLMANN-ISEP2016.ipynb".
The first task is:
Visualisation d'un phénomène physique
L'equation de Biot-et-Savart
Can you write Python code for it?

import numpy as np

# Constantes
my0=4*np.pi*1e-7;   # perméabilité du vide
I0=-1;              #  Amplitude du courant
                    # le courant circule de gauche à droite

# Dimensions
d=25 # Diametre de la spire (mm)
segments=100 # discretization de la spire
alpha = 2*np.pi/(segments-1) # discretization de l'angle

# initialisarion de la spire
x=[i*0 for i in range(segments)]
y=[d/2*np.sin(i*alpha) for i in range(segments)]
z=[-d/2*np.cos(i*alpha) for i in range(segments)]

#Distance caracteristique, des filaments

distance_char=np.sqrt((z[2]-z[1])**2+(y[2]-y[1])**2);

# Definition du sens du positif du courant : gauche -> droite
# pour le calcul les longeurs sont exprimées en m

x_spire=np.array([x])*1e-3;
y_spire=np.array([y])*1e-3;
z_spire=np.array([z])*1e-3;

#%%%%%%%%%%%%%%%       Affichage de la spire    %%%%%%%%%%%%%%%%%%%%%%%%%%%#

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.gca(projection='3d')

plt.plot(([0,0]),([0,0]),([-0.05,0.05]),  'b-', label='ligne', linewidth=2)
plt.plot(y_spire, z_spire,  'g.', label='spire 1', linewidth=2)

plt.show()

#%%%%%%%%%% Calcul du champ magnetique en utilisant Biot et Savart

ndp=50 # Nombre de points

# limites x
xmin=-0.05
xmax= 0.05

# limites y
ymin=-0.05
ymax=+0.05

# limites z
zmin=-0.05
zmax=+0.05

dx=(xmax-xmin)/(ndp-1) #increment x
dy=(ymax-ymin)/(ndp-1) # increment y
dz=(zmax-zmin)/(ndp-1) # increment z

#%%%%%%%%%%%%%%%        Calcul magnetostatique    %%%%%%%%%%%%%%%%%%%%%%%%%%%#

bxf=np.zeros(ndp)  # initialization de la composante  Bx du champ
byf=np.zeros(ndp)  # initialization de la composante  By du champ
bzf=np.zeros(ndp)  # initialization de la composante  Bz du champ

I0f1=my0*I0/(4*np.pi) # Magnétostatique (on multiplie le courrant
                        #% $$ \mu_0/(4.\pi)par $$)

# Intégation du champ induit en un point de la ligne bleue
# par le courant circulant sur chanque segment de  Boucle verte

bfx=0
bfy=0
bfz=0

nseg=np.size(z_spire)-1

for i in...


I am working on the file "Cours14-DILLMANN-ISEP2017.ipynb".
The first task is:
Donc attention aux nom donné aux paramêtres formels, il peut être aussi utile de se premunir contre des fautes, en utilisant la commande try qui permet la gestion des exceptions
Can you write Python code for it?

def tronquer_liste( ma_liste ):
    try:
        return ma_liste[1:]
    except TypeError:
        print("Cette fonction n'accepte que des listes ou des chaînes de caractères")

l=[1,2,3]
tronquer_liste(l)


I am working on the file "Cours14-DILLMANN-ISEP2017.ipynb".
The first task is:
En cherchant une solution plus détaillé, on peut inclure un test conditionnel
Can you write Python code for it?

def extraire_avant_premier(s):
    if str.find(s,",") == -1:
        return s
    return s[ : str.find(s,",") ]

print( extraire_avant_premier("chat") ) ## Affiche chat


I am working on the file "Cours14-DILLMANN-ISEP2017.ipynb".
The first task is:
Erreurs classiques sur les listes
Can you write Python code for it?

#Les Delistes
def tronquer_2(n):
    if n== []:
        return []
    else:
        del n[0]
        return  n
liste_1 = [1,2,3]
liste_2 = tronquer_2( liste_1 )
print( liste_1 ) ## affiche [2,3]
print( liste_2 ) ## affiche [2,3]

#Les Popistes
def tronquer_2(a):
    if a == [] :
        return []
    a.pop(0)
    return a

liste_1 = [1,2,3]
liste_2 = tronquer_2( liste_1 )
print( liste_1 ) ## affiche [2,3]
print( liste_2 ) ## affiche [2,3]


I am working on the file "Cours14-DILLMANN-ISEP2017.ipynb".
The first task is:
Les chaines de caractères, sont d'abord utiles pour retravailler la présentation d'un tableau
Can you write Python code for it?

def completer_2(s,n):
    return(s.ljust(n,' '))


I am working on the file "Cours14-DILLMANN-ISEP2017.ipynb".
The first task is:
On peut également utiliser une déclaration comme try on appelle cela un catch

On peut utiliser un catch de ZeroDivisionError
Can you write Python code for it?

def extraire_avant_premier(s):
    try:
        1/(1+str.find(s,","))
        return s[ : str.find(s,",") ]
    except ZeroDivisionError:
        return s

print( extraire_avant_premier("chat, chien") ) ## Affiche chat


I am working on the file "Cours14-DILLMANN-ISEP2017.ipynb".
The first task is:
Visualisation d'un phénomène physique
Visualisation d'un phénomène physique
$$B(x)=\frac{\mu_o}{4\pi}.I_o.\frac{r^2}{2 (r^2 +x^2)^{3/2} }$$
L'equation de Biot-et-Savart
Can you write Python code for it?

# -*- coding: utf-8 -*-

import numpy as np

# Constantes
my0=4*np.pi*1e-7;   # permeabilite du vide
I0=-1;              # intensité du courant
                    # le courant circule de gauche a droite

# Dimensions
d=25*1e-3 # Diametre de la spire (m)
r=d/2 # rayon de la spire (m)
segments=100 # discretisation de la spire
alpha = 2*np.pi/(segments-1) # discretisation de l'angle

# initialisation de la spire
x=[i*0 for i in range(segments)]
y=[r*np.sin(i*alpha) for i in range(segments)]
z=[-r*np.cos(i*alpha) for i in range(segments)]

# Definition du sens du positif du courant : gauche -> droite
# pour le calcul les longeurs sont exprimees en m

x_spire=np.array([x]);
y_spire=np.array([y]);
z_spire=np.array([z]);

%matplotlib inline

#%%%%%%%%%%%%%%%       Affichage de la spire en 3D   %%%%%%%%%%%%%%%%#

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import matplotlib.patches as patches

fig = plt.figure()
ax = fig.gca(projection='3d')

#titre du graphe
plt.title ('Simulation numerique d une spire', color='b')

# spire
plt.plot(y_spire [0], z_spire[0],  'g.',  linewidth=1, label='spire')
plt.plot(y_spire, z_spire,  'g.',  linewidth=1)

# vecteur champ magnetique Bx
plt.plot(([0.003,0.003]),([0,0]),([-.05,0.05]), 'b-',label='axe de la spire',linewidth=1)
plt.plot(([0.003,0.003]),([0,0]),([-.01,0.02]), 'm-',label='champ magnetique',linewidth=1)
plt.plot([0.003],[0],[0.025], 'm', marker='^', linewidth=2)
ax.text(0.004,0,0,'Bx', color='m')

#courant
plt.plot([0],[0.0125],[0], 'r', marker='<')
ax.text(0,0.0125,0.01,'I', color='r')

#legende du graphe
plt.legend (loc='lower left', prop={'size':8})

plt.show()

#%%%%%%%%%% Calcul du champ magnetique d apres Biot et Savart

ndp=50 # Nombre de points

# limites x
xmin,xmax=-0.05,0.05

# limites y
ymin, ymax=-0.05, 0.05

# limites z
zmin, zmax=-0.05,0.05

dx=(xmax-xmin)/(ndp-1) #increment x
dy=(ymax-ymin)/(ndp-1) # increment y
dz=(zmax-zmin)/(ndp-1) # increment z

#%%%%%%%%%%%%%%%        Calcul magnetostatique   ...


I am working on the file "Cours14-DILLMANN-ISEP2017.ipynb".
The first task is:
dans cet exemple on va faire l'appel
Can you write Python code for it?

Mon_Nom1 = 'DE SAINT-EXUPERY'
Mon_Nom2 = 'CAMUS'
#? Mon_Nom.ljust
print(Mon_Nom1.ljust(25,' '),'present !')
print(Mon_Nom2.ljust(25,' '),"c'est moi")


I am working on the file "Course 1/Building your Deep Neural Network Step by Step v5.ipynb".
The first task is:
2 - Outline of the Assignment
To build your neural network, you will be implementing several "helper functions". These helper functions will be used in the next assignment to build a two-layer neural network and an L-layer neural network. Each small helper function you will implement will have detailed instructions that will walk you through the necessary steps. Here is an outline of this assignment, you will:

Initialize the parameters for a two-layer network and for an $L$-layer neural network.
Implement the forward propagation module (shown in purple in the figure below).
Complete the LINEAR part of a layer's forward propagation step (resulting in $Z^{[l]}$).
We give you the ACTIVATION function (relu/sigmoid).
Combine the previous two steps into a new [LINEAR->ACTIVATION] forward function.
Stack the [LINEAR->RELU] forward function L-1 time (for layers 1 through L-1) and add a [LINEAR->SIGMOID] at the end (for the final layer $L$). This gives you a new L_model_forward function.

Compute the loss.
Implement the backward propagation module (denoted in red in the figure below).
Complete the LINEAR part of a layer's backward propagation step.
We give you the gradient of the ACTIVATE function (relu_backward/sigmoid_backward)
Combine the previous two steps into a new [LINEAR->ACTIVATION] backward function.
Stack [LINEAR->RELU] backward L-1 times and add [LINEAR->SIGMOID] backward in a new L_model_backward function

Finally update the parameters.

 Figure 1

Note that for every forward function, there is a corresponding backward function. That is why at every step of your forward module you will be storing some values in a cache. The cached values are useful for computing gradients. In the backpropagation module you will then use the cache to calculate the gradients. This assignment will show you exactly how to carry out each of these steps.
3 - Initialization
You will write two helper functions that...
Can you write Python code for it?

# GRADED FUNCTION: initialize_parameters

def initialize_parameters(n_x, n_h, n_y):
    """
    Argument:
    n_x -- size of the input layer
    n_h -- size of the hidden layer
    n_y -- size of the output layer

    Returns:
    parameters -- python dictionary containing your parameters:
                    W1 -- weight matrix of shape (n_h, n_x)
                    b1 -- bias vector of shape (n_h, 1)
                    W2 -- weight matrix of shape (n_y, n_h)
                    b2 -- bias vector of shape (n_y, 1)
    """

    np.random.seed(1)

    ### START CODE HERE ### (≈ 4 lines of code)
    W1 = np.random.randn(n_h, n_x) * 0.01
    b1 = np.zeros((n_h, 1))
    W2 = np.random.randn(n_y, n_h) * 0.01
    b2 = np.zeros((n_y, 1))
    ### END CODE HERE ###

    assert(W1.shape == (n_h, n_x))
    assert(b1.shape == (n_h, 1))
    assert(W2.shape == (n_y, n_h))
    assert(b2.shape == (n_y, 1))

    parameters = {"W1": W1,
                  "b1": b1,
                  "W2": W2,
                  "b2": b2}

    return parameters

parameters = initialize_parameters(3,2,1)
print("W1 = " + str(parameters["W1"]))
print("b1 = " + str(parameters["b1"]))
print("W2 = " + str(parameters["W2"]))
print("b2 = " + str(parameters["b2"]))


I am working on the file "Course 1/Building your Deep Neural Network Step by Step v5.ipynb".
The first task is:
Expected Output:

     **dA_prev**
     [[ 0.51822968 -0.19517421]
 [-0.40506361  0.15255393]
 [ 2.37496825 -0.89445391]]

         **dW**
         [[-0.10076895  1.40685096  1.64992505]]

         **db**
         [[ 0.50629448]]

6.2 - Linear-Activation backward
Next, you will create a function that merges the two helper functions: linear_backward and the backward step for the activation linear_activation_backward.
To help you implement linear_activation_backward, we provided two backward functions:
- sigmoid_backward: Implements the backward propagation for SIGMOID unit. You can call it as follows:
python
dZ = sigmoid_backward(dA, activation_cache)

relu_backward: Implements the backward propagation for RELU unit. You can call it as follows:

python
dZ = relu_backward(dA, activation_cache)
If $g(.)$ is the activation function,
sigmoid_backward and relu_backward compute $$dZ^{[l]} = dA^{[l]} * g'(Z^{[l]}) \tag{11}$$.
Exercise: Implement the backpropagation for the LINEAR->ACTIVATION layer.
Can you write Python code for it?

# GRADED FUNCTION: linear_activation_backward

def linear_activation_backward(dA, cache, activation):
    """
    Implement the backward propagation for the LINEAR->ACTIVATION layer.

    Arguments:
    dA -- post-activation gradient for current layer l
    cache -- tuple of values (linear_cache, activation_cache) we store for computing backward propagation efficiently
    activation -- the activation to be used in this layer, stored as a text string: "sigmoid" or "relu"

    Returns:
    dA_prev -- Gradient of the cost with respect to the activation (of the previous layer l-1), same shape as A_prev
    dW -- Gradient of the cost with respect to W (current layer l), same shape as W
    db -- Gradient of the cost with respect to b (current layer l), same shape as b
    """
    linear_cache, activation_cache = cache

    if activation == "relu":
        ### START CODE HERE ### (≈ 2 lines of code)
        dZ = relu_backward(dA, activation_cache)
        dA_prev, dW, db = linear_backward(dZ, linear_cache)
        ### END CODE HERE ###

    elif activation == "sigmoid":
        ### START CODE HERE ### (≈ 2 lines of code)
        dZ = sigmoid_backward(dA, activation_cache)
        dA_prev, dW, db = linear_backward(dZ, linear_cache)
        ### END CODE HERE ###

    return dA_prev, dW, db

AL, linear_activation_cache = linear_activation_backward_test_case()

dA_prev, dW, db = linear_activation_backward(AL, linear_activation_cache, activation = "sigmoid")
print ("sigmoid:")
print ("dA_prev = "+ str(dA_prev))
print ("dW = " + str(dW))
print ("db = " + str(db) + "\n")

dA_prev, dW, db = linear_activation_backward(AL, linear_activation_cache, activation = "relu")
print ("relu:")
print ("dA_prev = "+ str(dA_prev))
print ("dW = " + str(dW))
print ("db = " + str(db))


I am working on the file "Course 1/Building your Deep Neural Network Step by Step v5.ipynb".
The first task is:
Expected output with sigmoid:

     dA_prev
           [[ 0.11017994  0.01105339]
 [ 0.09466817  0.00949723]
 [-0.05743092 -0.00576154]]

     dW
            [[ 0.10266786  0.09778551 -0.01968084]]

     db
            [[-0.05729622]]

Expected output with relu:

     dA_prev
            [[ 0.44090989  0.        ]
 [ 0.37883606  0.        ]
 [-0.2298228   0.        ]]

     dW
            [[ 0.44513824  0.37371418 -0.10478989]]

     db
            [[-0.20837892]]

6.3 - L-Model Backward
Now you will implement the backward function for the whole network. Recall that when you implemented the L_model_forward function, at each iteration, you stored a cache which contains (X,W,b, and z). In the back propagation module, you will use those variables to compute the gradients. Therefore, in the L_model_backward function, you will iterate through all the hidden layers backward, starting from layer $L$. On each step, you will use the cached values for layer $l$ to backpropagate through layer $l$. Figure 5 below shows the backward pass.

  Figure 5 : Backward pass

 Initializing backpropagation:
To backpropagate through this network, we know that the output is,
$A^{[L]} = \sigma(Z^{[L]})$. Your code thus needs to compute dAL $= \frac{\partial \mathcal{L}}{\partial A^{[L]}}$.
To do so, use this formula (derived using calculus which you don't need in-depth knowledge of):
python
dAL = - (np.divide(Y, AL) - np.divide(1 - Y, 1 - AL)) # derivative of cost with respect to AL
You can then use this post-activation gradient dAL to keep going backward. As seen in Figure 5, you can now feed in dAL into the...
Can you write Python code for it?

# GRADED FUNCTION: L_model_backward

def L_model_backward(AL, Y, caches):
    """
    Implement the backward propagation for the [LINEAR->RELU] * (L-1) -> LINEAR -> SIGMOID group

    Arguments:
    AL -- probability vector, output of the forward propagation (L_model_forward())
    Y -- true "label" vector (containing 0 if non-cat, 1 if cat)
    caches -- list of caches containing:
                every cache of linear_activation_forward() with "relu" (it's caches[l], for l in range(L-1) i.e l = 0...L-2)
                the cache of linear_activation_forward() with "sigmoid" (it's caches[L-1])

    Returns:
    grads -- A dictionary with the gradients
             grads["dA" + str(l)] = ...
             grads["dW" + str(l)] = ...
             grads["db" + str(l)] = ...
    """
    grads = {}
    L = len(caches) # the number of layers
    m = AL.shape[1]
    Y = Y.reshape(AL.shape) # after this line, Y is the same shape as AL

    # Initializing the backpropagation
    ### START CODE HERE ### (1 line of code)
    dAL = - (np.divide(Y, AL) - np.divide(1 - Y, 1 - AL)) # derivative of cost with respect to AL
    ### END CODE HERE ###

    # Lth layer (SIGMOID -> LINEAR) gradients. Inputs: "AL, Y, caches". Outputs: "grads["dAL"], grads["dWL"], grads["dbL"]
    ### START CODE HERE ### (approx. 2 lines)
    current_cache = caches[L-1]
    grads["dA" + str(L)], grads["dW" + str(L)], grads["db" + str(L)] = linear_activation_backward(dAL, current_cache, "sigmoid")
    ### END CODE HERE ###

    for l in reversed(range(L-1)):
        # lth layer: (RELU -> LINEAR) gradients.
        # Inputs: "grads["dA" + str(l + 2)], caches". Outputs: "grads["dA" + str(l + 1)] , grads["dW" + str(l + 1)] , grads["db" + str(l + 1)]
        ### START CODE HERE ### (approx. 5 lines)
        current_cache = caches[l]
        dA_prev_temp, dW_temp, db_temp = linear_activation_backward(grads["dA" + str(l+2)], current_cache, "relu")
        grads["dA" + str(l + 1)] = dA_prev_temp
        grads["dW" + str(l + 1)] =...


I am working on the file "Course 1/Building your Deep Neural Network Step by Step v5.ipynb".
The first task is:
Expected output:

     **Z**
     [[ 3.26295337 -1.23429987]]

4.2 - Linear-Activation Forward
In this notebook, you will use two activation functions:

Sigmoid: $\sigma(Z) = \sigma(W A + b) = \frac{1}{ 1 + e^{-(W A + b)}}$. We have provided you with the sigmoid function. This function returns two items: the activation value "a" and a "cache" that contains "Z" (it's what we will feed in to the corresponding backward function). To use it you could just call:
python
A, activation_cache = sigmoid(Z)

ReLU: The mathematical formula for ReLu is $A = RELU(Z) = max(0, Z)$. We have provided you with the relu function. This function returns two items: the activation value "A" and a "cache" that contains "Z" (it's what we will feed in to the corresponding backward function). To use it you could just call:
python
A, activation_cache = relu(Z)

For more convenience, you are going to group two functions (Linear and Activation) into one function (LINEAR->ACTIVATION). Hence, you will implement a function that does the LINEAR forward step followed by an ACTIVATION forward step.
Exercise: Implement the forward propagation of the LINEAR->ACTIVATION layer. Mathematical relation is: $A^{[l]} = g(Z^{[l]}) = g(W^{[l]}A^{[l-1]} +b^{[l]})$ where the activation "g" can be sigmoid() or relu(). Use linear_forward() and the correct activation function.
Can you write Python code for it?

# GRADED FUNCTION: linear_activation_forward

def linear_activation_forward(A_prev, W, b, activation):
    """
    Implement the forward propagation for the LINEAR->ACTIVATION layer

    Arguments:
    A_prev -- activations from previous layer (or input data): (size of previous layer, number of examples)
    W -- weights matrix: numpy array of shape (size of current layer, size of previous layer)
    b -- bias vector, numpy array of shape (size of the current layer, 1)
    activation -- the activation to be used in this layer, stored as a text string: "sigmoid" or "relu"

    Returns:
    A -- the output of the activation function, also called the post-activation value
    cache -- a python dictionary containing "linear_cache" and "activation_cache";
             stored for computing the backward pass efficiently
    """

    if activation == "sigmoid":
        # Inputs: "A_prev, W, b". Outputs: "A, activation_cache".
        ### START CODE HERE ### (≈ 2 lines of code)
        Z, linear_cache = linear_forward(A_prev, W, b)
        A, activation_cache = sigmoid(Z)
        ### END CODE HERE ###

    elif activation == "relu":
        # Inputs: "A_prev, W, b". Outputs: "A, activation_cache".
        ### START CODE HERE ### (≈ 2 lines of code)
        Z, linear_cache = linear_forward(A_prev, W, b)
        A, activation_cache = relu(Z)
        ### END CODE HERE ###

    assert (A.shape == (W.shape[0], A_prev.shape[1]))
    cache = (linear_cache, activation_cache)

    return A, cache

A_prev, W, b = linear_activation_forward_test_case()

A, linear_activation_cache = linear_activation_forward(A_prev, W, b, activation = "sigmoid")
print("With sigmoid: A = " + str(A))

A, linear_activation_cache = linear_activation_forward(A_prev, W, b, activation = "relu")
print("With ReLU: A = " + str(A))


I am working on the file "Course 1/Building your Deep Neural Network Step by Step v5.ipynb".
The first task is:
Expected output:

     **With sigmoid: A **
     [[ 0.96890023  0.11013289]]

     **With ReLU: A **
     [[ 3.43896131  0.        ]]

Note: In deep learning, the "[LINEAR->ACTIVATION]" computation is counted as a single layer in the neural network, not two layers.
d) L-Layer Model
For even more convenience when implementing the $L$-layer Neural Net, you will need a function that replicates the previous one (linear_activation_forward with RELU) $L-1$ times, then follows that with one linear_activation_forward with SIGMOID.

 Figure 2 : [LINEAR -> RELU] $\times$ (L-1) -> LINEAR -> SIGMOID model

Exercise: Implement the forward propagation of the above model.
Instruction: In the code below, the variable AL will denote $A^{[L]} = \sigma(Z^{[L]}) = \sigma(W^{[L]} A^{[L-1]} + b^{[L]})$. (This is sometimes also called Yhat, i.e., this is $\hat{Y}$.)
Tips:
- Use the functions you had previously written
- Use a for loop to replicate [LINEAR->RELU] (L-1) times
- Don't forget to keep track of the caches in the "caches" list. To add a new value c to a list, you can use list.append(c).
Can you write Python code for it?

# GRADED FUNCTION: L_model_forward

def L_model_forward(X, parameters):
    """
    Implement forward propagation for the [LINEAR->RELU]*(L-1)->LINEAR->SIGMOID computation

    Arguments:
    X -- data, numpy array of shape (input size, number of examples)
    parameters -- output of initialize_parameters_deep()

    Returns:
    AL -- last post-activation value
    caches -- list of caches containing:
                every cache of linear_relu_forward() (there are L-1 of them, indexed from 0 to L-2)
                the cache of linear_sigmoid_forward() (there is one, indexed L-1)
    """

    caches = []
    A = X
    L = len(parameters) // 2                  # number of layers in the neural network

    # Implement [LINEAR -> RELU]*(L-1). Add "cache" to the "caches" list.
    for l in range(1, L):
        A_prev = A
        ### START CODE HERE ### (≈ 2 lines of code)
        A, cache = linear_activation_forward(A_prev, parameters["W" + str(l)], parameters["b" + str(l)], "relu")
        caches.append(cache)

        ### END CODE HERE ###

    # Implement LINEAR -> SIGMOID. Add "cache" to the "caches" list.
    ### START CODE HERE ### (≈ 2 lines of code)
    AL, cache = linear_activation_forward(A, parameters["W" + str(L)], parameters["b" + str(L)], "sigmoid")
    caches.append(cache)

    ### END CODE HERE ###

    assert(AL.shape == (1,X.shape[1]))

    return AL, caches

X, parameters = L_model_forward_test_case_2hidden()
AL, caches = L_model_forward(X, parameters)
print("AL = " + str(AL))
print("Length of caches list = " + str(len(caches)))


I am working on the file "Course 1/Logistic Regression with a Neural Network mindset.ipynb".
The first task is:
2 - Overview of the Problem set
Problem Statement: You are given a dataset ("data.h5") containing:
    - a training set of m_train images labeled as cat (y=1) or non-cat (y=0)
    - a test set of m_test images labeled as cat or non-cat
    - each image is of shape (num_px, num_px, 3) where 3 is for the 3 channels (RGB). Thus, each image is square (height = num_px) and (width = num_px).
You will build a simple image-recognition algorithm that can correctly classify pictures as cat or non-cat.
Let's get more familiar with the dataset. Load the data by running the following code.
Can you write Python code for it?

# Loading the data (cat/non-cat)
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()


I am working on the file "Course 1/Logistic Regression with a Neural Network mindset.ipynb".
The first task is:

What you need to remember:
Common steps for pre-processing a new dataset are:
- Figure out the dimensions and shapes of the problem (m_train, m_test, num_px, ...)
- Reshape the datasets such that each example is now a vector of size (num_px * num_px * 3, 1)
- "Standardize" the data
3 - General Architecture of the learning algorithm
It's time to design a simple algorithm to distinguish cat images from non-cat images.
You will build a Logistic Regression, using a Neural Network mindset. The following Figure explains why Logistic Regression is actually a very simple Neural Network!

Mathematical expression of the algorithm:
For one example $x^{(i)}$:
$$z^{(i)} = w^T x^{(i)} + b \tag{1}$$
$$\hat{y}^{(i)} = a^{(i)} = sigmoid(z^{(i)})\tag{2}$$
$$ \mathcal{L}(a^{(i)}, y^{(i)}) =  - y^{(i)}  \log(a^{(i)}) - (1-y^{(i)} )  \log(1-a^{(i)})\tag{3}$$
The cost is then computed by summing over all training examples:
$$ J = \frac{1}{m} \sum_{i=1}^m \mathcal{L}(a^{(i)}, y^{(i)})\tag{6}$$
Key steps:
In this exercise, you will carry out the following steps:
    - Initialize the parameters of the model
    - Learn the parameters for the model by minimizing the cost
    - Use the learned parameters to make predictions (on the test set)
    - Analyse the results and conclude
4 - Building the parts of our algorithm ##
The main steps for building a Neural Network are:
1. Define the model structure (such as number of input features)
2. Initialize the model's parameters
3. Loop:
    - Calculate current loss (forward propagation)
    - Calculate current gradient (backward propagation)
    - Update parameters (gradient descent)
You often build 1-3 separately and integrate them into one function we call model().
4.1 - Helper functions
Exercise: Using your code from "Python Basics", implement sigmoid(). As you've seen in the figure above, you need to compute $sigmoid( w^T x + b) = \frac{1}{1 + e^{-(w^T x + b)}}$ to make predictions. Use np.exp().
Can you write Python code for it?

# GRADED FUNCTION: sigmoid

def sigmoid(z):
    """
    Compute the sigmoid of z

    Arguments:
    z -- A scalar or numpy array of any size.

    Return:
    s -- sigmoid(z)
    """

    ### START CODE HERE ### (≈ 1 line of code)
    s = 1/(1+ np.exp(-z))
    ### END CODE HERE ###

    return s

print ("sigmoid([0, 2]) = " + str(sigmoid(np.array([0,2]))))


I am working on the file "Course 1/Logistic Regression with a Neural Network mindset.ipynb".
The first task is:
Expected Output:

          ** dw **
       [[ 0.99845601]
     [ 2.39507239]]

          ** db **
         0.00145557813678

          ** cost **
         5.801545319394553

d) Optimization

You have initialized your parameters.
You are also able to compute a cost function and its gradient.
Now, you want to update the parameters using gradient descent.

Exercise: Write down the optimization function. The goal is to learn $w$ and $b$ by minimizing the cost function $J$. For a parameter $\theta$, the update rule is $ \theta = \theta - \alpha \text{ } d\theta$, where $\alpha$ is the learning rate.
Can you write Python code for it?

# GRADED FUNCTION: optimize

def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost = False):
    """
    This function optimizes w and b by running a gradient descent algorithm

    Arguments:
    w -- weights, a numpy array of size (num_px * num_px * 3, 1)
    b -- bias, a scalar
    X -- data of shape (num_px * num_px * 3, number of examples)
    Y -- true "label" vector (containing 0 if non-cat, 1 if cat), of shape (1, number of examples)
    num_iterations -- number of iterations of the optimization loop
    learning_rate -- learning rate of the gradient descent update rule
    print_cost -- True to print the loss every 100 steps

    Returns:
    params -- dictionary containing the weights w and bias b
    grads -- dictionary containing the gradients of the weights and bias with respect to the cost function
    costs -- list of all the costs computed during the optimization, this will be used to plot the learning curve.

    Tips:
    You basically need to write down two steps and iterate through them:
        1) Calculate the cost and the gradient for the current parameters. Use propagate().
        2) Update the parameters using gradient descent rule for w and b.
    """

    costs = []

    for i in range(num_iterations):

        # Cost and gradient calculation (≈ 1-4 lines of code)
        ### START CODE HERE ###
        grads, cost = propagate(w, b, X, Y)
        ### END CODE HERE ###

        # Retrieve derivatives from grads
        dw = grads["dw"]
        db = grads["db"]

        # update rule (≈ 2 lines of code)
        ### START CODE HERE ###
        w = w - learning_rate * dw
        b = b - learning_rate * db
        ### END CODE HERE ###

        # Record the costs
        if i % 100 == 0:
            costs.append(cost)

        # Print the cost every 100 training examples
        if print_cost and i % 100 == 0:
            print ("Cost after iteration %i: %f" %(i, cost))

    params = {"w": w,
        ...


I am working on the file "Course 1/Logistic Regression with a Neural Network mindset.ipynb".
The first task is:
Expected Output:

             **predictions**

            [[ 1.  1.  0.]]

What to remember:
You've implemented several functions that:
- Initialize (w,b)
- Optimize the loss iteratively to learn parameters (w,b):
    - computing the cost and its gradient
    - updating the parameters using gradient descent
- Use the learned (w,b) to predict the labels for a given set of examples
5 - Merge all functions into a model
You will now see how the overall model is structured by putting together all the building blocks (functions implemented in the previous parts) together, in the right order.
Exercise: Implement the model function. Use the following notation:
    - Y_prediction for your predictions on the test set
    - Y_prediction_train for your predictions on the train set
    - w, costs, grads for the outputs of optimize()
Can you write Python code for it?

# GRADED FUNCTION: model

def model(X_train, Y_train, X_test, Y_test, num_iterations = 2000, learning_rate = 0.5, print_cost = False):
    """
    Builds the logistic regression model by calling the function you've implemented previously

    Arguments:
    X_train -- training set represented by a numpy array of shape (num_px * num_px * 3, m_train)
    Y_train -- training labels represented by a numpy array (vector) of shape (1, m_train)
    X_test -- test set represented by a numpy array of shape (num_px * num_px * 3, m_test)
    Y_test -- test labels represented by a numpy array (vector) of shape (1, m_test)
    num_iterations -- hyperparameter representing the number of iterations to optimize the parameters
    learning_rate -- hyperparameter representing the learning rate used in the update rule of optimize()
    print_cost -- Set to true to print the cost every 100 iterations

    Returns:
    d -- dictionary containing information about the model.
    """

    ### START CODE HERE ###

    # initialize parameters with zeros (≈ 1 line of code)
    w, b = initialize_with_zeros(X_train.shape[0])

    # Gradient descent (≈ 1 line of code)
    parameters, grads, costs = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost)

    # Retrieve parameters w and b from dictionary "parameters"
    w = parameters["w"]
    b = parameters["b"]

    # Predict test/train set examples (≈ 2 lines of code)
    Y_prediction_test = predict(w, b, X_test)
    Y_prediction_train = predict(w, b, X_train)

    ### END CODE HERE ###

    # Print train/test Errors
    print("train accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_train - Y_train)) * 100))
    print("test accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_test - Y_test)) * 100))

    d = {"costs": costs,
         "Y_prediction_test": Y_prediction_test,
         "Y_prediction_train" : Y_prediction_train,
         "w" : w,
         "b" : b,
         "learning_rate" : learning_rate,
        ...


I am working on the file "Course 1/Logistic Regression with a Neural Network mindset.ipynb".
The first task is:
Expected Output:

         **Cost after iteration 0 **
         0.693147

          $\vdots$
          $\vdots$

         **Train Accuracy**
         99.04306220095694 %

        **Test Accuracy**
         70.0 %

Comment: Training accuracy is close to 100%. This is a good sanity check: your model is working and has high enough capacity to fit the training data. Test error is 68%. It is actually not bad for this simple model, given the small dataset we used and that logistic regression is a linear classifier. But no worries, you'll build an even better classifier next week!
Also, you see that the model is clearly overfitting the training data. Later in this specialization you will learn how to reduce overfitting, for example by using regularization. Using the code below (and changing the index variable) you can look at predictions on pictures of the test set.
Can you write Python code for it?

# Example of a picture that was wrongly classified.
index = 1
plt.imshow(test_set_x[:,index].reshape((num_px, num_px, 3)))
print ("y = " + str(test_set_y[0,index]) + ", you predicted that it is a \"" + classes[d["Y_prediction_test"][0,index]].decode("utf-8") +  "\" picture.")


I am working on the file "Course 1/Logistic Regression with a Neural Network mindset.ipynb".
The first task is:
Interpretation:
You can see the cost decreasing. It shows that the parameters are being learned. However, you see that you could train the model even more on the training set. Try to increase the number of iterations in the cell above and rerun the cells. You might see that the training set accuracy goes up, but the test set accuracy goes down. This is called overfitting.
6 - Further analysis (optional/ungraded exercise)
Congratulations on building your first image classification model. Let's analyze it further, and examine possible choices for the learning rate $\alpha$.
Choice of learning rate
Reminder:
In order for Gradient Descent to work you must choose the learning rate wisely. The learning rate $\alpha$  determines how rapidly we update the parameters. If the learning rate is too large we may "overshoot" the optimal value. Similarly, if it is too small we will need too many iterations to converge to the best values. That's why it is crucial to use a well-tuned learning rate.
Let's compare the learning curve of our model with several choices of learning rates. Run the cell below. This should take about 1 minute. Feel free also to try different values than the three we have initialized the learning_rates variable to contain, and see what happens.
Can you write Python code for it?

learning_rates = [0.01, 0.001, 0.0001]
models = {}
for i in learning_rates:
    print ("learning rate is: " + str(i))
    models[str(i)] = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations = 1500, learning_rate = i, print_cost = False)
    print ('\n' + "-------------------------------------------------------" + '\n')

for i in learning_rates:
    plt.plot(np.squeeze(models[str(i)]["costs"]), label= str(models[str(i)]["learning_rate"]))

plt.ylabel('cost')
plt.xlabel('iterations')

legend = plt.legend(loc='upper center', shadow=True)
frame = legend.get_frame()
frame.set_facecolor('0.90')
plt.show()


I am working on the file "Course 1/Logistic Regression with a Neural Network mindset.ipynb".
The first task is:
Interpretation:
- Different learning rates give different costs and thus different predictions results.
- If the learning rate is too large (0.01), the cost may oscillate up and down. It may even diverge (though in this example, using 0.01 still eventually ends up at a good value for the cost).
- A lower cost doesn't mean a better model. You have to check if there is possibly overfitting. It happens when the training accuracy is a lot higher than the test accuracy.
- In deep learning, we usually recommend that you:
    - Choose the learning rate that better minimizes the cost function.
    - If your model overfits, use other techniques to reduce overfitting. (We'll talk about this in later videos.)
7 - Test with your own image (optional/ungraded exercise)
Congratulations on finishing this assignment. You can use your own image and see the output of your model. To do that:
    1. Click on "File" in the upper bar of this notebook, then click "Open" to go on your Coursera Hub.
    2. Add your image to this Jupyter Notebook's directory, in the "images" folder
    3. Change your image's name in the following code
    4. Run the code and check if the algorithm is right (1 = cat, 0 = non-cat)!
Can you write Python code for it?

## START CODE HERE ## (PUT YOUR IMAGE NAME)
my_image = "1.jpg"   # change this to the name of your image file
## END CODE HERE ##

# We preprocess the image to fit your algorithm.
fname = "images/" + my_image
image = np.array(ndimage.imread(fname, flatten=False))
my_image = scipy.misc.imresize(image, size=(num_px,num_px)).reshape((1, num_px*num_px*3)).T
my_predicted_image = predict(d["w"], d["b"], my_image)

plt.imshow(image)
print("y = " + str(np.squeeze(my_predicted_image)) + ", your algorithm predicts a \"" + classes[int(np.squeeze(my_predicted_image)),].decode("utf-8") +  "\" picture.")


I am working on the file "Course 1/Logistic Regression with a Neural Network mindset.ipynb".
The first task is:
Let's also plot the cost function and the gradients.
Can you write Python code for it?

# Plot learning curve (with costs)
costs = np.squeeze(d['costs'])
plt.plot(costs)
plt.ylabel('cost')
plt.xlabel('iterations (per hundreds)')
plt.title("Learning rate =" + str(d["learning_rate"]))
plt.show()


I am working on the file "Course 1/Logistic Regression with a Neural Network mindset.ipynb".
The first task is:
Many software bugs in deep learning come from having matrix/vector dimensions that don't fit. If you can keep your matrix/vector dimensions straight you will go a long way toward eliminating many bugs.
Exercise: Find the values for:
    - m_train (number of training examples)
    - m_test (number of test examples)
    - num_px (= height = width of a training image)
Remember that train_set_x_orig is a numpy-array of shape (m_train, num_px, num_px, 3). For instance, you can access m_train by writing train_set_x_orig.shape[0].
Can you write Python code for it?

### START CODE HERE ### (≈ 3 lines of code)
m_train = train_set_x_orig.shape[0]
m_test = test_set_x_orig.shape[0]
num_px = train_set_x_orig.shape[1]
### END CODE HERE ###

print ("Number of training examples: m_train = " + str(m_train))
print ("Number of testing examples: m_test = " + str(m_test))
print ("Height/Width of each image: num_px = " + str(num_px))
print ("Each image is of size: (" + str(num_px) + ", " + str(num_px) + ", 3)")
print ("train_set_x shape: " + str(train_set_x_orig.shape))
print ("train_set_y shape: " + str(train_set_y.shape))
print ("test_set_x shape: " + str(test_set_x_orig.shape))
print ("test_set_y shape: " + str(test_set_y.shape))


I am working on the file "Course 1/Logistic Regression with a Neural Network mindset.ipynb".
The first task is:
Run the following cell to train your model.
Can you write Python code for it?

d = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations = 2000, learning_rate = 0.005, print_cost = True)


I am working on the file "Course 1/Logistic Regression with a Neural Network mindset.ipynb".
The first task is:
We added "_orig" at the end of image datasets (train and test) because we are going to preprocess them. After preprocessing, we will end up with train_set_x and test_set_x (the labels train_set_y and test_set_y don't need any preprocessing).
Each line of your train_set_x_orig and test_set_x_orig is an array representing an image. You can visualize an example by running the following code. Feel free also to change the index value and re-run to see other images.
Can you write Python code for it?

# Example of a picture
index = 25
plt.imshow(train_set_x_orig[index])
print ("y = " + str(train_set_y[:, index]) + ", it's a '" + classes[np.squeeze(train_set_y[:, index])].decode("utf-8") +  "' picture.")


I am working on the file "Course 2 - ML, Regression/Overfitting_Demo_Ridge_Lasso.ipynb".
The first task is:
Above: Fit looks pretty wild, too.  Here's a clear example of how overfitting is associated with very large magnitude estimated coefficients.

#
#
Ridge Regression
Ridge regression aims to avoid overfitting by adding a cost to the RSS term of standard least squares that depends on the 2-norm of the coefficients $\|w\|$.  The result is penalizing fits with large coefficients.  The strength of this penalty, and thus the fit vs. model complexity balance, is controled by a parameter lambda (here called "L2_penalty").
Define our function to solve the ridge objective for a polynomial regression model of any degree:
Can you write Python code for it?

def polynomial_ridge_regression(data, deg, l2_penalty):
    model = graphlab.linear_regression.create(polynomial_features(data,deg),
                                              target='Y', l2_penalty=l2_penalty,
                                              validation_set=None,verbose=False)
    return model


I am working on the file "Course 2 - ML, Regression/Overfitting_Demo_Ridge_Lasso.ipynb".
The first task is:
Add random Gaussian noise to y
Can you write Python code for it?

random.seed(1)
e = graphlab.SArray([random.gauss(0,1.0/3.0) for i in range(n)])
y = y + e


I am working on the file "Course 2 - ML, Regression/Overfitting_Demo_Ridge_Lasso.ipynb".
The first task is:
Create a function to plot the data, since we'll do it many times
Can you write Python code for it?

def plot_data(data):
    plt.plot(data['X1'],data['Y'],'k.')
    plt.xlabel('x')
    plt.ylabel('y')

plot_data(data)


I am working on the file "Course 2 - ML, Regression/Overfitting_Demo_Ridge_Lasso.ipynb".
The first task is:
Create random values for x in interval [0,1)
Can you write Python code for it?

random.seed(98103)
n = 30
x = graphlab.SArray([random.random() for i in range(n)]).sort()


I am working on the file "Course 2 - ML, Regression/Overfitting_Demo_Ridge_Lasso.ipynb".
The first task is:
Define a function to fit a polynomial linear regression model of degree "deg" to the data in "data":
Can you write Python code for it?

def polynomial_regression(data, deg):
    model = graphlab.linear_regression.create(polynomial_features(data,deg),
                                              target='Y', l2_penalty=0.,l1_penalty=0.,
                                              validation_set=None,verbose=False)
    return model


I am working on the file "Course 2 - ML, Regression/Overfitting_Demo_Ridge_Lasso.ipynb".
The first task is:
Define function to plot data and predictions made, since we are going to use it many times.
Can you write Python code for it?

def plot_poly_predictions(data, model):
    plot_data(data)

    # Get the degree of the polynomial
    deg = len(model.coefficients['value'])-1

    # Create 200 points in the x axis and compute the predicted value for each point
    x_pred = graphlab.SFrame({'X1':[i/200.0 for i in range(200)]})
    y_pred = model.predict(polynomial_features(x_pred,deg))

    # plot predictions
    plt.plot(x_pred['X1'], y_pred, 'g-', label='degree ' + str(deg) + ' fit')
    plt.legend(loc='upper left')
    plt.axis([0,1,-1.5,2])


I am working on the file "Course 2 - ML, Regression/Overfitting_Demo_Ridge_Lasso.ipynb".
The first task is:
Define some useful polynomial regression functions
Define a function to create our features for a polynomial regression model of any degree:
Can you write Python code for it?

def polynomial_features(data, deg):
    data_copy=data.copy()
    for i in range(1,deg):
        data_copy['X'+str(i+1)]=data_copy['X'+str(i)]*data_copy['X1']
    return data_copy


I am working on the file "Course 2 - ML, Regression/Overfitting_Demo_Ridge_Lasso.ipynb".
The first task is:
Explore the lasso solution as a function of a few different penalty strengths
We refer to lambda in the lasso case below as "l1_penalty"
Can you write Python code for it?

for l1_penalty in [0.0001, 0.01, 0.1, 10]:
    model = polynomial_lasso_regression(data, deg=16, l1_penalty=l1_penalty)
    print 'l1_penalty = %e' % l1_penalty
    print 'number of nonzeros = %d' % (model.coefficients['value']).nnz()
    print_coefficients(model)
    print '\n'
    plt.figure()
    plot_poly_predictions(data,model)
    plt.title('LASSO, lambda = %.2e, # nonzeros = %d' % (l1_penalty, (model.coefficients['value']).nnz()))


I am working on the file "Course 2 - ML, Regression/Overfitting_Demo_Ridge_Lasso.ipynb".
The first task is:
Fit a degree-16 polynomial
Can you write Python code for it?

model = polynomial_regression(data, deg=16)
print_coefficients(model)


I am working on the file "Course 2 - ML, Regression/Overfitting_Demo_Ridge_Lasso.ipynb".
The first task is:
Fit a degree-2 polynomial
Fit our degree-2 polynomial to the data generated above:
Can you write Python code for it?

model = polynomial_regression(data, deg=2)


I am working on the file "Course 2 - ML, Regression/Overfitting_Demo_Ridge_Lasso.ipynb".
The first task is:
Fit a degree-4 polynomial
Can you write Python code for it?

model = polynomial_regression(data, deg=4)
print_coefficients(model)
plot_poly_predictions(data,model)


I am working on the file "Course 2 - ML, Regression/Overfitting_Demo_Ridge_Lasso.ipynb".
The first task is:
Inspect learned parameters
Can you write Python code for it?


I am working on the file "Course 2 - ML, Regression/Overfitting_Demo_Ridge_Lasso.ipynb".
The first task is:
Lasso Regression
Lasso regression jointly shrinks coefficients to avoid overfitting, and implicitly performs feature selection by setting some coefficients exactly to 0 for sufficiently large penalty strength lambda (here called "L1_penalty").  In particular, lasso takes the RSS term of standard least squares and adds a 1-norm cost of the coefficients $\|w\|$.
Define our function to solve the lasso objective for a polynomial regression model of any degree:
Can you write Python code for it?

def polynomial_lasso_regression(data, deg, l1_penalty):
    model = graphlab.linear_regression.create(polynomial_features(data,deg),
                                              target='Y', l2_penalty=0.,
                                              l1_penalty=l1_penalty,
                                              validation_set=None,
                                              solver='fista', verbose=False,
                                              max_iterations=3000, convergence_threshold=1e-10)
    return model


I am working on the file "Course 2 - ML, Regression/Overfitting_Demo_Ridge_Lasso.ipynb".
The first task is:
Perform a ridge fit of a degree-16 polynomial using a "good" penalty strength
We will learn about cross validation later in this course as a way to select a good value of the tuning parameter (penalty strength) lambda.  Here, we consider "leave one out" (LOO) cross validation, which one can show approximates average mean square error (MSE).  As a result, choosing lambda to minimize the LOO error is equivalent to choosing lambda to minimize an approximation to average MSE.
Can you write Python code for it?

# LOO cross validation -- return the average MSE
def loo(data, deg, l2_penalty_values):
    # Create polynomial features
    data = polynomial_features(data, deg)

    # Create as many folds for cross validatation as number of data points
    num_folds = len(data)
    folds = graphlab.cross_validation.KFold(data,num_folds)

    # for each value of l2_penalty, fit a model for each fold and compute average MSE
    l2_penalty_mse = []
    min_mse = None
    best_l2_penalty = None
    for l2_penalty in l2_penalty_values:
        next_mse = 0.0
        for train_set, validation_set in folds:
            # train model
            model = graphlab.linear_regression.create(train_set,target='Y',
                                                      l2_penalty=l2_penalty,
                                                      validation_set=None,verbose=False)

            # predict on validation set
            y_test_predicted = model.predict(validation_set)
            # compute squared error
            next_mse += ((y_test_predicted-validation_set['Y'])**2).sum()

        # save squared error in list of MSE for each l2_penalty
        next_mse = next_mse/num_folds
        l2_penalty_mse.append(next_mse)
        if min_mse is None or next_mse < min_mse:
            min_mse = next_mse
            best_l2_penalty = l2_penalty

    return l2_penalty_mse,best_l2_penalty


I am working on the file "Course 2 - ML, Regression/Overfitting_Demo_Ridge_Lasso.ipynb".
The first task is:
Perform a ridge fit of a degree-16 polynomial using a very large penalty strength
Can you write Python code for it?

model = polynomial_ridge_regression(data, deg=16, l2_penalty=100)
print_coefficients(model)

plot_poly_predictions(data,model)


I am working on the file "Course 2 - ML, Regression/Overfitting_Demo_Ridge_Lasso.ipynb".
The first task is:
Perform a ridge fit of a degree-16 polynomial using a very small penalty strength
Can you write Python code for it?

model = polynomial_ridge_regression(data, deg=16, l2_penalty=1e-25)
print_coefficients(model)

plot_poly_predictions(data,model)


I am working on the file "Course 2 - ML, Regression/Overfitting_Demo_Ridge_Lasso.ipynb".
The first task is:
Plot results of estimating LOO for each value of lambda
Can you write Python code for it?

plt.plot(l2_penalty_values,l2_penalty_mse,'k-')
plt.xlabel('$\ell_2$ penalty')
plt.ylabel('LOO cross validation error')
plt.xscale('log')
plt.yscale('log')


I am working on the file "Course 2 - ML, Regression/Overfitting_Demo_Ridge_Lasso.ipynb".
The first task is:
Put data into an SFrame to manipulate later
Can you write Python code for it?

data = graphlab.SFrame({'X1':x,'Y':y})
data


I am working on the file "Course 2 - ML, Regression/Overfitting_Demo_Ridge_Lasso.ipynb".
The first task is:
Run LOO cross validation for "num" values of lambda, on a log scale
Can you write Python code for it?

l2_penalty_values = numpy.logspace(-4, 10, num=10)
l2_penalty_mse,best_l2_penalty = loo(data, 16, l2_penalty_values)


I am working on the file "Course 2 - ML, Regression/numpy-tutorial.ipynb".
The first task is:
Accessing array elements
Accessing an array is straight forward. For vectors you access the index by referring to it inside square brackets. Recall that indices in Python start with 0.
Can you write Python code for it?


I am working on the file "Course 2 - ML, Regression/numpy-tutorial.ipynb".
The first task is:
For 2D arrays you can select specific columns and specific rows. Passing ':' selects all rows/columns
Can you write Python code for it?

select_cols = np.array([True, False, True]) # 1st and 3rd column
select_rows = np.array([False, True]) # 2nd row

print my_matrix[select_rows, :] # just 2nd row but all columns

print my_matrix[:, select_cols] # all rows and just the 1st and 3rd column


I am working on the file "Course 2 - ML, Regression/numpy-tutorial.ipynb".
The first task is:
Recall that the Euclidean length (or magnitude) of a vector is the squareroot of the sum of the squares of the components. This is just the squareroot of the dot product of the vector with itself:
Can you write Python code for it?

array1_mag = np.sqrt(np.dot(array1, array1))
print array1_mag

print np.sqrt(np.sum(array1*array1))


I am working on the file "Course 2 - ML, Regression/numpy-tutorial.ipynb".
The first task is:
Sequences of indices can be accessed using ':' for example
Can you write Python code for it?

print my_matrix[0:2, 2] # recall 0:2 = [0, 1]

print my_matrix[0, 0:3]


I am working on the file "Course 2 - ML, Regression/numpy-tutorial.ipynb".
The first task is:
The dot product
An important mathematical operation in linear algebra is the dot product.
When we compute the dot product between two vectors we are simply multiplying them elementwise and adding them up. In numpy you can do this with np.dot()
Can you write Python code for it?

array1 = np.array([1., 2., 3., 4.])
array2 = np.array([2., 3., 4., 5.])
print np.dot(array1, array2)

print np.sum(array1*array2)


I am working on the file "Course 2 - ML, Regression/numpy-tutorial.ipynb".
The first task is:
You can also pass a list of indices.
Can you write Python code for it?

fib_indices = np.array([1, 1, 2, 3])
random_vector = np.random.random(10) # 10 random numbers between 0 and 1
print random_vector

print random_vector[fib_indices]


I am working on the file "Course 2 - ML, Regression/numpy-tutorial.ipynb".
The first task is:
You can also use true/false values to select values
Can you write Python code for it?

my_vector = np.array([1, 2, 3, 4])
select_index = np.array([True, False, True, False])
print my_vector[select_index]


I am working on the file "Course 2 - ML, Regression/numpy-tutorial.ipynb".
The first task is:
You can compute the sum with np.sum() and the average with np.average()
Can you write Python code for it?

print np.sum(my_array)

print np.average(my_array)

print np.sum(my_array)/len(my_array)


I am working on the file "Course 2 - ML, Regression/numpy-tutorial.ipynb".
The first task is:
You can initialize an array (of any dimension) of all ones or all zeroes with the ones() and zeros() functions:
Can you write Python code for it?

one_vector = np.ones(4)
print one_vector # using print removes the array() portion

one2Darray = np.ones((2, 4)) # an 2D array with 2 "rows" and 4 "columns"
print one2Darray

zero_vector = np.zeros(4)
print zero_vector


I am working on the file "Course 2 - ML, Regression/week-2-multiple-regression-assignment-1-blank.ipynb".
The first task is:
Learning Multiple Models
Now we will learn the weights for three (nested) models for predicting house prices. The first model will have the fewest features the second model will add one more feature and the third will add a few more:
* Model 1: squarefeet, # bedrooms, # bathrooms, latitude & longitude
* Model 2: add bedrooms*bathrooms
* Model 3: Add log squarefeet, bedrooms squared, and the (nonsensical) latitude + longitude
Can you write Python code for it?

model_1_features = ['sqft_living', 'bedrooms', 'bathrooms', 'lat', 'long']
model_2_features = model_1_features + ['bed_bath_rooms']
model_3_features = model_2_features + ['bedrooms_squared', 'log_sqft_living', 'lat_plus_long']


I am working on the file "Course 2 - ML, Regression/week-2-multiple-regression-assignment-1-blank.ipynb".
The first task is:
Making Predictions
In the gradient descent notebook we use numpy to do our regression. In this book we will use existing graphlab create functions to analyze multiple regressions.
Recall that once a model is built we can use the .predict() function to find the predicted values for data we pass. For example using the example model above:
Can you write Python code for it?

example_predictions = example_model.predict(train_data)
print example_predictions[0] # should be 271789.505878


I am working on the file "Course 2 - ML, Regression/week-2-multiple-regression-assignment-1-blank.ipynb".
The first task is:
Now that we have fitted the model we can extract the regression weights (coefficients) as an SFrame as follows:
Can you write Python code for it?

example_weight_summary = example_model.get("coefficients")
print example_weight_summary


I am working on the file "Course 2 - ML, Regression/week-2-multiple-regression-assignment-1-blank.ipynb".
The first task is:
Now that you have the features, learn the weights for the three different models for predicting target = 'price' using graphlab.linear_regression.create() and look at the value of the weights/coefficients:
Can you write Python code for it?

# Learn the three models: (don't forget to set validation_set = None)
model_1 = graphlab.linear_regression.create(train_data, target = 'price', features = model_1_features,
                                                  validation_set = None)
model_2 = graphlab.linear_regression.create(train_data, target = 'price', features = model_2_features,
                                                  validation_set = None)
model_3 = graphlab.linear_regression.create(train_data, target = 'price', features = model_3_features,
                                                  validation_set = None)

# Examine/extract each model's coefficients:
print 'model 1'
model_1.get("coefficients")

print 'model 2'
model_2.get("coefficients")

print 'model 3'
model_3.get("coefficients")


I am working on the file "Course 2 - ML, Regression/week-2-multiple-regression-assignment-1-blank.ipynb".
The first task is:
Quiz Question: Which model (1, 2 or 3) has lowest RSS on TRAINING Data? Is this what you expected?
Now compute the RSS on on TEST data for each of the three models.
Can you write Python code for it?

# Compute the RSS on TESTING data for each of the three models and record the values:
print 'model 1: %.9f model 2: %.9f model 3: %.9f' % (get_residual_sum_of_squares(model_1, test_data, test_data['price']),
                                                 get_residual_sum_of_squares(model_2, test_data, test_data['price']),
                                                 get_residual_sum_of_squares(model_3, test_data, test_data['price']))


I am working on the file "Course 2 - ML, Regression/week-2-multiple-regression-assignment-1-blank.ipynb".
The first task is:
Squaring bedrooms will increase the separation between not many bedrooms (e.g. 1) and lots of bedrooms (e.g. 4) since 1^2 = 1 but 4^2 = 16. Consequently this feature will mostly affect houses with many bedrooms.
bedrooms times bathrooms gives what's called an "interaction" feature. It is large when both of them are large.
Taking the log of squarefeet has the effect of bringing large values closer together and spreading out small values.
Adding latitude to longitude is totally non-sensical but we will do it anyway (you'll see why)

Quiz Question: What is the mean (arithmetic average) value of your 4 new features on TEST data? (round to 2 digits)
Can you write Python code for it?

print 'bedrooms_squared %f' % round(sum(test_data['bedrooms_squared'])/len(test_data['bedrooms_squared']),2)
print 'bed_bath_rooms %f' % round(sum(test_data['bed_bath_rooms'])/len(test_data['bed_bath_rooms']),2)
print 'log_sqft_living %f' % round(sum(test_data['log_sqft_living'])/len(test_data['log_sqft_living']),2)
print 'lat_plus_long %f' % round(sum(test_data['lat_plus_long'])/len(test_data['lat_plus_long']),2)


I am working on the file "Course 2 - ML, Regression/week-2-multiple-regression-assignment-1-blank.ipynb".
The first task is:
Test your function by computing the RSS on TEST data for the example model:
Can you write Python code for it?

rss_example_train = get_residual_sum_of_squares(example_model, test_data, test_data['price'])
print rss_example_train # should be 2.7376153833e+14


I am working on the file "Course 2 - ML, Regression/week-2-multiple-regression-assignment-2-blank.ipynb".
The first task is:
A few things to note before we run the gradient descent. Since the gradient is a sum over all the data points and involves a product of an error and a feature the gradient itself will be very large since the features are large (squarefeet) and the output is large (prices). So while you might expect "tolerance" to be small, small is only relative to the size of the features.
For similar reasons the step size will be much smaller than you might expect but this is because the gradient has such large values.
Running the Gradient Descent as Simple Regression
First let's split the data into training and test data.
Can you write Python code for it?


I am working on the file "Course 2 - ML, Regression/week-2-multiple-regression-assignment-2-blank.ipynb".
The first task is:
Although the gradient descent is designed for multiple regression since the constant is now a feature we can use the gradient descent function to estimat the parameters in the simple regression on squarefeet. The folowing cell sets up the feature_matrix, output, initial weights and step size for the first model:
Can you write Python code for it?

# let's test out the gradient descent
simple_features = ['sqft_living']
my_output = 'price'
(simple_feature_matrix, output) = get_numpy_data(train_data, simple_features, my_output)
initial_weights = np.array([-47000., 1.])
step_size = 7e-12
tolerance = 2.5e7


I am working on the file "Course 2 - ML, Regression/week-2-multiple-regression-assignment-2-blank.ipynb".
The first task is:
For testing let's use the 'sqft_living' feature and a constant as our features and price as our output:
Can you write Python code for it?

(example_features, example_output) = get_numpy_data(sales, ['sqft_living'], 'price') # the [] around 'sqft_living' makes it a list
print example_features[0,:] # this accesses the first row of the data the ':' indicates 'all columns'
print example_output[0] # and the corresponding output


I am working on the file "Course 2 - ML, Regression/week-2-multiple-regression-assignment-2-blank.ipynb".
The first task is:
Gradient Descent
Now we will write a function that performs a gradient descent. The basic premise is simple. Given a starting point we update the current weights by moving in the negative gradient direction. Recall that the gradient is the direction of increase and therefore the negative gradient is the direction of decrease and we're trying to minimize a cost function.
The amount by which we move in the negative gradient direction  is called the 'step size'. We stop when we are 'sufficiently close' to the optimum. We define this by requiring that the magnitude (length) of the gradient vector to be smaller than a fixed 'tolerance'.
With this in mind, complete the following gradient descent function below using your derivative function above. For each step in the gradient descent we update the weight for each feature befofe computing our stopping criteria
Can you write Python code for it?

from math import sqrt # recall that the magnitude/length of a vector [g[0], g[1], g[2]] is sqrt(g[0]^2 + g[1]^2 + g[2]^2)

def regression_gradient_descent(feature_matrix, output, initial_weights, step_size, tolerance):
    converged = False
    weights = np.array(initial_weights) # make sure it's a numpy array
    while not converged:
        # compute the predictions based on feature_matrix and weights using your predict_output() function
        predictions = predict_output(feature_matrix, weights)
        # compute the errors as predictions - output
        errors = predictions - output
        gradient_sum_squares = 0 # initialize the gradient sum of squares
        # while we haven't reached the tolerance yet, update each feature's weight
        for i in range(len(weights)): # loop over each weight
            # Recall that feature_matrix[:, i] is the feature column associated with weights[i]
            # compute the derivative for weight[i]:
            derivative = feature_derivative(errors, feature_matrix[:, i])
            # add the squared value of the derivative to the gradient sum of squares (for assessing convergence)
            gradient_sum_squares = gradient_sum_squares + derivative * derivative
            # subtract the step size times the derivative from the current weight
            weights[i] = weights[i] - derivative*step_size
        # compute the square-root of the gradient sum of squares to get the gradient magnitude:
        gradient_magnitude = sqrt(gradient_sum_squares)
        if gradient_magnitude < tolerance:
            converged = True
    return(weights)


I am working on the file "Course 2 - ML, Regression/week-2-multiple-regression-assignment-2-blank.ipynb".
The first task is:
How do your weights compare to those achieved in week 1 (don't expect them to be exactly the same)?
Quiz Question: What is the value of the weight for sqft_living -- the second element of ‘simple_weights’ (rounded to 1 decimal place)?
Use your newly estimated weights and your predict_output() function to compute the predictions on all the TEST data (you will need to create a numpy array of the test feature_matrix and test output first:
Can you write Python code for it?

(test_simple_feature_matrix, test_output) = get_numpy_data(test_data, simple_features, my_output)


I am working on the file "Course 2 - ML, Regression/week-2-multiple-regression-assignment-2-blank.ipynb".
The first task is:
Load in house sales data
Dataset is from house sales in King County, the region where the city of Seattle, WA is located.
Can you write Python code for it?


I am working on the file "Course 2 - ML, Regression/week-2-multiple-regression-assignment-2-blank.ipynb".
The first task is:
Next run your gradient descent with the above parameters.
Can you write Python code for it?

weights = regression_gradient_descent(simple_feature_matrix, output, initial_weights, step_size, tolerance)
print weights


I am working on the file "Course 2 - ML, Regression/week-2-multiple-regression-assignment-2-blank.ipynb".
The first task is:
Now compute your predictions using test_simple_feature_matrix and your weights from above.
Can you write Python code for it?

predictions = predict_output(test_simple_feature_matrix, weights)
print predictions


I am working on the file "Course 2 - ML, Regression/week-2-multiple-regression-assignment-2-blank.ipynb".
The first task is:
Now that you have the predictions on test data, compute the RSS on the test data set. Save this value for comparison later. Recall that RSS is the sum of the squared errors (difference between prediction and output).
Can you write Python code for it?

residuals = test_output - predictions
RSS = sum(residuals*residuals)
print RSS


I am working on the file "Course 2 - ML, Regression/week-2-multiple-regression-assignment-2-blank.ipynb".
The first task is:
Quiz Question: What is the predicted price for the 1st house in the TEST data set for model 1 (round to nearest dollar)?
Can you write Python code for it?


I am working on the file "Course 2 - ML, Regression/week-2-multiple-regression-assignment-2-blank.ipynb".
The first task is:
Quiz Question: What is the predicted price for the 1st house in the TEST data set for model 2 (round to nearest dollar)?
Can you write Python code for it?


I am working on the file "Course 2 - ML, Regression/week-2-multiple-regression-assignment-2-blank.ipynb".
The first task is:
Quiz Question: Which estimate was closer to the true price for the 1st house on the TEST data set, model 1 or model 2?
Now use your predictions and the output to compute the RSS for model 2 on TEST data.
Can you write Python code for it?

residuals_2 = test_output - predicted
RSS_2 = sum(residuals_2**2)
print RSS_2


I am working on the file "Course 2 - ML, Regression/week-2-multiple-regression-assignment-2-blank.ipynb".
The first task is:
Running a multiple regression
Now we will use more than one actual feature. Use the following code to produce the weights for a second model with the following parameters:
Can you write Python code for it?

model_features = ['sqft_living', 'sqft_living15'] # sqft_living15 is the average squarefeet for the nearest 15 neighbors.
my_output = 'price'
(feature_matrix, output) = get_numpy_data(train_data, model_features, my_output)
initial_weights = np.array([-100000., 1., 1.])
step_size = 4e-12
tolerance = 1e9


I am working on the file "Course 2 - ML, Regression/week-2-multiple-regression-assignment-2-blank.ipynb".
The first task is:
To test your feature derivartive run the following:
Can you write Python code for it?

(example_features, example_output) = get_numpy_data(sales, ['sqft_living'], 'price')
my_weights = np.array([0., 0.]) # this makes all the predictions 0
test_predictions = predict_output(example_features, my_weights)
# just like SFrames 2 numpy arrays can be elementwise subtracted with '-':
errors = test_predictions - example_output # prediction errors in this case is just the -example_output
feature = example_features[:,0] # let's compute the derivative with respect to 'constant', the ":" indicates "all rows"
derivative = feature_derivative(errors, feature)
print derivative
print -np.sum(example_output)*2 # should be the same as derivative


I am working on the file "Course 2 - ML, Regression/week-2-multiple-regression-assignment-2-blank.ipynb".
The first task is:
Use your newly estimated weights and the predict_output function to compute the predictions on the TEST data. Don't forget to create a numpy array for these features from the test set first!
Can you write Python code for it?

(test_feature_matrix, test_output) = get_numpy_data(test_data, model_features, my_output)
predicted = predict_output(test_feature_matrix, weights_more_features)


I am working on the file "Course 2 - ML, Regression/week-2-multiple-regression-assignment-2-blank.ipynb".
The first task is:
What is the actual price for the 1st house in the test data set?
Can you write Python code for it?


I am working on the file "Course 2 - ML, Regression/week-2-multiple-regression-assignment-2-blank.ipynb".
The first task is:
np.dot() also works when dealing with a matrix and a vector. Recall that the predictions from all the observations is just the RIGHT (as in weights on the right) dot product between the features matrix and the weights vector. With this in mind finish the following predict_output function to compute the predictions for an entire matrix of features given the matrix and the weights:
Can you write Python code for it?

def predict_output(feature_matrix, weights):
    # assume feature_matrix is a numpy matrix containing the features as columns and weights is a corresponding numpy array
    # create the predictions vector by using np.dot()
    predictions = np.dot(feature_matrix, weights)
    return(predictions)


I am working on the file "Course 2 - ML, Regression/week-3-polynomial-regression-assignment-blank.ipynb".
The first task is:
As in Week 3, we will use the sqft_living variable. For plotting purposes (connecting the dots), you'll need to sort by the values of sqft_living. For houses with identical square footage, we break the tie by their prices.
Can you write Python code for it?

sales = sales.sort(['sqft_living', 'price'])


I am working on the file "Course 2 - ML, Regression/week-3-polynomial-regression-assignment-blank.ipynb".
The first task is:
Fit a 15th degree polynomial on set_1, set_2, set_3, and set_4 using sqft_living to predict prices. Print the coefficients and make a plot of the resulting model.
Can you write Python code for it?

set1_poly15_data = polynomial_sframe(set_1['sqft_living'], 15)
set1_poly15_data['price'] = set_1['price']
set1_model15 = graphlab.linear_regression.create(set1_poly15_data, target = 'price', features = my_features15, validation_set = None)
set1_model15.get("coefficients")

plt.plot(set1_poly15_data['power_1'],set1_poly15_data['price'],'.',
        set1_poly15_data['power_1'], set1_model15.predict(set1_poly15_data),'-')

set2_poly15_data = polynomial_sframe(set_2['sqft_living'], 15)
set2_poly15_data['price'] = set_2['price']
set2_model15 = graphlab.linear_regression.create(set2_poly15_data, target = 'price', features = my_features15, validation_set = None)
set2_model15.get("coefficients")

plt.plot(set2_poly15_data['power_1'],set2_poly15_data['price'],'.',
        set2_poly15_data['power_1'], set2_model15.predict(set2_poly15_data),'-')

set3_poly15_data = polynomial_sframe(set_3['sqft_living'], 15)
set3_poly15_data['price'] = set_3['price']
set3_model15 = graphlab.linear_regression.create(set3_poly15_data, target = 'price', features = my_features15, validation_set = None)
set3_model15.get("coefficients")

plt.plot(set3_poly15_data['power_1'],set3_poly15_data['price'],'.',
        set3_poly15_data['power_1'], set3_model15.predict(set3_poly15_data),'-')

set4_poly15_data = polynomial_sframe(set_4['sqft_living'], 15)
set4_poly15_data['price'] = set_4['price']
set4_model15 = graphlab.linear_regression.create(set4_poly15_data, target = 'price', features = my_features15, validation_set = None)
set4_model15.get("coefficients")


I am working on the file "Course 2 - ML, Regression/week-3-polynomial-regression-assignment-blank.ipynb".
The first task is:
Let's start with a degree 1 polynomial using 'sqft_living' (i.e. a line) to predict 'price' and plot what it looks like.
Can you write Python code for it?

poly1_data = polynomial_sframe(sales['sqft_living'], 1)
poly1_data['price'] = sales['price'] # add price to the data since it's the target


I am working on the file "Course 2 - ML, Regression/week-3-polynomial-regression-assignment-blank.ipynb".
The first task is:
Let's unpack that plt.plot() command. The first pair of SArrays we passed are the 1st power of sqft and the actual price we then ask it to print these as dots '.'. The next pair we pass is the 1st power of sqft and the predicted values from the linear model. We ask these to be plotted as a line '-'.
We can see, not surprisingly, that the predicted values all fall on a line, specifically the one with slope 280 and intercept -43579. What if we wanted to plot a second degree polynomial?
Can you write Python code for it?

poly2_data = polynomial_sframe(sales['sqft_living'], 2)
my_features = poly2_data.column_names() # get the name of the features
poly2_data['price'] = sales['price'] # add price to the data since it's the target
model2 = graphlab.linear_regression.create(poly2_data, target = 'price', features = my_features, validation_set = None)

model2.get("coefficients")

plt.plot(poly2_data['power_1'],poly2_data['price'],'.',
        poly2_data['power_1'], model2.predict(poly2_data),'-')


I am working on the file "Course 2 - ML, Regression/week-3-polynomial-regression-assignment-blank.ipynb".
The first task is:
NOTE: for all the models in this notebook use validation_set = None to ensure that all results are consistent across users.
Can you write Python code for it?

model1 = graphlab.linear_regression.create(poly1_data, target = 'price', features = ['power_1'], validation_set = None)

#let's take a look at the weights before we plot
model1.get("coefficients")

import matplotlib.pyplot as plt
%matplotlib inline

plt.plot(poly1_data['power_1'],poly1_data['price'],'.',
        poly1_data['power_1'], model1.predict(poly1_data),'-')


I am working on the file "Course 2 - ML, Regression/week-3-polynomial-regression-assignment-blank.ipynb".
The first task is:
Next you should write a loop that does the following:
* For degree in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] (to get this in python type range(1, 15+1))
    * Build an SFrame of polynomial data of train_data['sqft_living'] at the current degree
    * hint: my_features = poly_data.column_names() gives you a list e.g. ['power_1', 'power_2', 'power_3'] which you might find useful for graphlab.linear_regression.create( features = my_features)
    * Add train_data['price'] to the polynomial SFrame
    * Learn a polynomial regression model to sqft vs price with that degree on TRAIN data
    * Compute the RSS on VALIDATION data (here you will want to use .predict()) for that degree and you will need to make a polynmial SFrame using validation data.
* Report which degree had the lowest RSS on validation data (remember python indexes from 0)
(Note you can turn off the print out of linear_regression.create() with verbose = False)
Can you write Python code for it?

RSS = {}
for i in range(0, 15):
    poly_data = polynomial_sframe(train_data['sqft_living'], i)
    my_features = poly_data.column_names()
    poly_data['price'] = train_data['price']
    train_model = graphlab.linear_regression.create(poly_data, target = 'price', features = my_features, validation_set = None)
    poly_validation = polynomial_sframe(validation_data['sqft_living'], i)
    RSS[i] = sum(train_model.predict(poly_validation))
print RSS

key=0
value=0
for k,v in RSS.iteritems():
    if(k == 0):
        value = v
    else:
        if(v<value):
            key = k
            value = v
print key, value


I am working on the file "Course 2 - ML, Regression/week-3-polynomial-regression-assignment-blank.ipynb".
The first task is:
Now try a 15th degree polynomial:
Can you write Python code for it?

poly15_data = polynomial_sframe(sales['sqft_living'], 15)
my_features15 = poly15_data.column_names()
poly15_data['price'] = sales['price']
model15 = graphlab.linear_regression.create(poly15_data, target = 'price', features = my_features15, validation_set = None)

plt.plot(poly15_data['power_1'],poly15_data['price'],'.',
        poly15_data['power_1'], model15.predict(poly15_data),'-')


I am working on the file "Course 2 - ML, Regression/week-3-polynomial-regression-assignment-blank.ipynb".
The first task is:
Polynomial_sframe function
Using the hints above complete the following function to create an SFrame consisting of the powers of an SArray up to a specific degree:
Can you write Python code for it?

def polynomial_sframe(feature, degree):
    # assume that degree >= 1
    # initialize the SFrame:
    poly_sframe = graphlab.SFrame()
    # and set poly_sframe['power_1'] equal to the passed feature
    poly_sframe['power_1'] = feature
    # first check if degree > 1
    if degree > 1:
        # then loop over the remaining degrees:
        # range usually starts at 0 and stops at the endpoint-1. We want it to start at 2 and stop at degree
        for power in range(2, degree+1):
            # first we'll give the column a name:
            name = 'power_' + str(power)
            # then assign poly_sframe[name] to the appropriate power of feature
            poly_sframe[name] = feature**power
    return poly_sframe