This commit is contained in:
Chouiya Asma 2021-12-15 19:28:05 +01:00
commit 6d7e8ea262
2 changed files with 308 additions and 0 deletions

110
TP1/KNN.py Normal file
View file

@ -0,0 +1,110 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 15 19:07:59 2021
@author: chouiya
"""
from time import time
import numpy as np
import matplotlib.pyplot as plt
from sklearn import neighbors
from sklearn.datasets import fetch_openml
from sklearn.model_selection import train_test_split, KFold
from sklearn.metrics import accuracy_score
#**********Echantillons de données "data" avec une taille de 5000 échantillons **********
mnist = fetch_openml('mnist_784', as_frame=False)
index= np.random.randint(70000, size=5000)
data = mnist.data[index]
target = mnist.target[index]
# *************utilisation de 80% de la base de données pour le training ***********
xtrain, xtest, ytrain, ytest = train_test_split(data, target, train_size=0.8)
# **********classifieur k-nn avec k=10 ********
xtrain, xtest, ytrain, ytest = train_test_split(data, target, train_size=0.8, test_size=0.2)
clf = neighbors.KNeighborsClassifier(10)
clf.fit(xtrain,ytrain)
prediction = clf.predict(xtest)
score = clf.score(xtest, ytest)
# **********Classe de l'image 4 et sa classe prédite ****************
print("Prédiction : {}, Valeur : {}, Score : {}".format(prediction[4], ytest[4], score))
#*********Taux d'erreur sur les données d'apprentissage *******
xtrain, xtest, ytrain, ytest = train_test_split(data, target, train_size=0.8, test_size=0.2)
clf = neighbors.KNeighborsClassifier(10)
clf.fit(xtrain,ytrain)
prediction = clf.predict(xtrain)
score = clf.score(xtrain, ytrain)
print("score: ", score*100)
# **********Variation du nombre de voisins k de 2 à 15 en utilisant une boucle*****
xtrain, xtest, ytrain, ytest = train_test_split(data, target, train_size=0.8, test_size=0.2)
tab_scores=[]
for i in range (2,16):
clf = neighbors.KNeighborsClassifier(i)
clf.fit(xtrain, ytrain)
prediction = clf.predict(xtest)
score = clf.score(xtest, ytest)
tab_scores.append(score)
print("K : {}, Score: {}".format(i, score*100))
#plot score=f(k)
range_tab=range(2,16)
plt.plot(range_tab,tab_scores)
plt.xlabel("valeurs de K pour KNN")
plt.ylabel("score")
# ******** Variation du nombre de voisins k de 2 à 15 en utilisant la fonction KFold******
kf = KFold(14,shuffle=True)
kf.get_n_splits(data)
k = 2
for train_index, test_index in kf.split(data):
xtrain, xtest = data[train_index], data[test_index]
ytrain, ytest = target[train_index], target[test_index]
clf = neighbors.KNeighborsClassifier(k)
clf.fit(xtrain,ytrain)
prediction = clf.predict(xtest)
score = clf.score(xtest, ytest)
print("K : {}, Score : {}".format(k, score*100))
k = k + 1
# *********Variation du pourcentage des échantillons du training et test************
change_percent = range (2,10)
for s in change_percent:
xtrain, xtest, ytrain, ytest = train_test_split(data, target, train_size=(s/10))
clasifier = neighbors.KNeighborsClassifier(5)
clasifier.fit(xtrain,ytrain)
prediction = clasifier.predict(xtest)
print("Training size = {} %, Score = {} ".format((s/10), clasifier.score(xtest, ytest)))

198
TP2/MLP.py Normal file
View file

@ -0,0 +1,198 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 15 18:26:31 2021
@author: chouiya
"""
#********import***************
from sklearn.datasets import fetch_openml
from sklearn import datasets
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import numpy as np
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import precision_score
from sklearn.metrics import zero_one_loss
import time
#*******************MLP pour une seule couche de 50 neurons*****************
mnist = fetch_openml('mnist_784',as_frame=False)
data=mnist.data
target=mnist.target
xtrain, xtest, ytrain, ytest = train_test_split(data, target, train_size=0.7)
clf = MLPClassifier(hidden_layer_sizes=(50))
clf.fit(xtrain, ytrain)
prediction = clf.predict(xtest)
score = clf.score(xtest, ytest)
precision = precision_score(ytest, prediction, average ='micro')
loss1_0 = zero_one_loss(ytest, prediction)
# Print & Test :------
print("This MLP model, with one layer of 50, has a score of : ", score*100, "%.")
print(" 4 th image : Prediction ",prediction[3], "Vs Reel : ", ytest[3])
# Showing the 4th image:
images = xtest.reshape((-1, 28, 28))
plt.imshow(images[3],cmap=plt.cm.gray_r,interpolation="nearest")
plt.show()
# Metrics :
print ("This MLP model has a precision of :", precision*100, "%.")
print ("This MLP model has a zero-one_loss of :",loss1_0*100, "%.")
#*******************Variation de nombre de couche de 2 à 100*******
hidden_layer =(50,)*100
Scor = []
Pred= []
Loss= []
for i in range (100):
clf = MLPClassifier(hidden_layer_sizes = hidden_layer[0:i])
clf.fit(xtrain, ytrain)
prediction = clf.predict(xtest)
score = clf.score(xtest, ytest)
precision = precision_score(ytest, prediction, average='micro')
loss0_1 = zero_one_loss(ytest, prediction)
Scor.append(score)
Pred.append(precision)
Loss.append(loss0_1)
print("For ", i, "hidden layer (s), The score = ", score *100, "%", ", Precision = ", precision*100, "% .." )
fig, ax = plt.subplots(3, sharex=True, figsize=(10,10))
ax[0].plot(range(100), Scor)
ax[0].set_title('Number of hidden layers from 1 to 99')
ax[0].set_ylabel('Score')
ax[1].plot(range(100), Pred)
ax[1].set_ylabel('Precision')
ax[2].plot(range(100), Loss)
ax[2].set_ylabel('Zero-to-one Loss')
#**************les 5 modeles de classification*********
clf1 = MLPClassifier(hidden_layer_sizes=(300))
# 2 layers
clf2 = MLPClassifier(hidden_layer_sizes=(20, 50))
# 4 layers
clf4 = MLPClassifier(hidden_layer_sizes=(20,50, 100, 150))
# 6 layers
clf6 = MLPClassifier(hidden_layer_sizes=( 20, 50, 150, 100, 50, 10))
# 8 layers, increase neurals :
clf8 = MLPClassifier(hidden_layer_sizes=(20, 40, 60, 120, 150, 180, 200, 250))
ClassifierList = ("clf1", "clf2","clf4", "clf6", "clf8")
Score =[]
Precision = []
Loss = []
TimeTraining = []
TimePrediction = []
def clfs(clf, i):
#Training :
startTrain =time.time()
clf.fit(xtrain, ytrain)
endTrain = time.time()
#Prediction :
startpred= time.time()
predict = clf.predict(xtest)
endpred = time.time()
#Metrics :
score = clf.score(xtest,ytest)
precision = precision_score(ytest, predict, average='micro')
loss01 = zero_one_loss(ytest, predict)
timetrain = endTrain - startTrain
timePred = endpred - startpred
#Saving results
Score.append(score*100)
Precision.append(precision*100)
Loss.append(loss01)
TimePrediction.append(timePred)
TimeTraining.append(timetrain)
#Prints :
print("For the", i," model we have, score = ", score*100, "%, precision =",precision*100, "%." )
print(" Training's time = ", timetrain, " and prediction's time = ", timePred, "." )
#***********************plot*******
fig, ax = plt.subplots(5, sharex=True, figsize=(10,10))
ax[0].scatter(range(5), Score, c='orange')
ax[0].set_title('The five classifiers with 1,2,4,6,8 hidden layers')
ax[0].set_ylabel('Score (%)')
ax[1].scatter(range(5), Precision, c='red')
ax[1].set_ylabel('Precision (%)')
ax[2].scatter(range(5), Loss, c='blue')
ax[2].set_ylabel('Zero-to-one Loss')
ax[3].scatter(range(5), TimeTraining, c='pink')
ax[3].set_ylabel('Training Time (s)')
ax[4].scatter(range(5), TimePrediction, c='purple')
ax[4].set_ylabel('¨Prediction Time (s)')
plt.show()
#*****Etude de la convergence des algorithmes d'optimisation : adam, sgd, lbfgs***
tab1=['adam','sgd','lbfgs']
tab2=['relu','tanh','logistic','identity']
for i in tab1:# solver
for j in tab2:
#activation function
clf = MLPClassifier(hidden_layer_sizes =50,activation=j,solver=i,verbose=False)
clf.fit(xtrain, ytrain)
prediction = clf.predict(xtest)
score = clf.score(xtest, ytest)
precision = precision_score(ytest, prediction, average='micro')
loss0_1 = zero_one_loss(ytest, prediction)
print('the result of the solver',i,'and the activation function',j)
Scor.append(score)
Pred.append(precision)
Loss.append(loss0_1)
print('score :',Scor)
print('prediction',Pred)
print('loss',Loss)
#**********Variation de alpha**************
alphas = np.logspace(-5, 3, 5)
for i in alphas:
clf = MLPClassifier(hidden_layer_sizes =50,activation='relu',solver='adam',alpha=i,verbose=False)
clf.fit(xtrain, ytrain)
prediction = clf.predict(xtest)
score = clf.score(xtest, ytest)
precision = precision_score(ytest, prediction, average='micro')
loss0_1 = zero_one_loss(ytest, prediction)
print('for alpha equal to: ',i)
Scor.append(score)
Pred.append(precision)
Loss.append(loss0_1)
print('score :',Scor)
print('prediction',Pred)
print('loss',Loss)
#***************
TP2_CNN.py
Affichage de TP2_CNN.py en cours...