TP2 : Mini Projet : Programmation Avancée sous Pyhton
#
Activité 1 : Arbre de décision
1. Sous Spyder, Saisir le code qui suit sous le nom « dec\_tree ».
2. Définir le rôle de : numpy ; matplotlib ; pyplot ; sklearn
3. **A quoi sert les deux lignes de codes :
with open("simpleTree.dot", 'w') as f:
f = tree.export\_graphviz(simpleTree, out\_file=f)**
4. Combien de fichiers output retourne cet exemple ?
5. Pouvez vous proposer un autre fichier en output et lequel ?
import numpy
import matplotlib.pyplot as plot
from sklearn import tree
from sklearn.tree import DecisionTreeRegressor
#from sklearn.externals.six import StringIO
#Build a simple data set with y = x + random
nPoints = 100
#x values for plotting
xPlot = [(float(i)/float(nPoints) - 0.5) for i in range(nPoints + 1)]
Publicité
#x needs to be list of lists.
x = [[s] for s in xPlot]
#y (labels) has random noise added to x-value
#set seed
numpy.random.seed(1)
y = [s + numpy.random.normal(scale=0.1) for s in xPlot]
plot.plot(xPlot,y)
plot.xlabel('x')
plot.ylabel('y')
plot.show()
simpleTree = DecisionTreeRegressor(max\_depth=1)
simpleTree.fit(x, y)
#\\\\\\\\\\\\\\\\\\\\\\\\\\draw the tree
with open("simpleTree.dot", 'w') as f:
f = tree.export\_graphviz(simpleTree, out\_file=f)
#compare prediction from tree with true values
yHat = simpleTree.predict(x)
plot.figure()
plot.plot(xPlot, y, label='True y')
Publicité
plot.plot(xPlot, yHat, label='Tree Prediction ', linestyle='--')
plot.legend(bbox\_to\_anchor=(1,0.2))
plot.xlabel('x')
plot.ylabel('y')
plot.show()
simpleTree2 = DecisionTreeRegressor(max\_depth=2)
simpleTree2.fit(x, y)
#draw the tree
with open("simpleTree2.dot", 'w') as f:
f = tree.export\_graphviz(simpleTree2, out\_file=f)
#compare prediction from tree with true values
yHat = simpleTree2.predict(x)
plot.figure()
plot.plot(xPlot, y, label='True y')
plot.plot(xPlot, yHat, label='Tree Prediction ', linestyle='--')
plot.legend(bbox\_to\_anchor=(1,0.2))
plot.xlabel('x')
plot.ylabel('y')
plot.show()
Publicité
Activité 2 : Régression linéaire
1. Saisir ce code sous Reg\_lin :
2. Exécuter le programme de Reg\_lin
3. Supprimer les 3 # des lignes de codes en gras
4. Ré exécuter le code et préciser les différences.
from sklearn import linear\_model
import matplotlib.pyplot as plt
import numpy as np
import random
Step 1: training data
X = [i for i in range(10)]
Y = [random.gauss(x,0.75) for x in X]
X = np.asarray(X)
Y = np.asarray(Y)
X = X[:,np.newaxis]
Y = Y[:,np.newaxis]
#plt.scatter(X,Y)
Step 2: define and train a model
model = linear\_model.LinearRegression()
Publicité
model.fit(X, Y)
print(model.coef\_)
print(model.intercept\_)
Step 3: prediction
x\_new\_min = 0.0
x\_new\_max = 10.0
X\_NEW = np.linspace(x\_new\_min, x\_new\_max, 100)
X\_NEW = X\_NEW[:,np.newaxis]
Y\_NEW = model.predict(X\_NEW)
plt.plot(X\_NEW, Y\_NEW, color='coral', linewidth=3)
#plt.grid()
plt.xlim(x\_new\_min,x\_new\_max)
plt.ylim(0,20)
plt.title("Simple Linear Regression using scikit-learn and python 3",fontsize=10)
plt.xlabel('Abscise')
plt.ylabel('Ordonnée’)
#plt.savefig("simple\_linear\_regression.png", bbox\_inches='tight')
plt.show()