code Python analyse discrimante

code Python analyse discrimante

par Marin Jean Michel,
Nombre de réponses : 0
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import datasets

digits = datasets.load_digits()

images_and_labels = list(zip(digits.images,digits.target))
for index, (image, label) in enumerate(images_and_labels[:8]):
     plt.subplot(2, 4, index + 1)
     plt.axis('off')
     plt.imshow(image, cmap=plt.cm.gray_r,interpolation='nearest')
plt.title('Training: %i' % label)

X=digits.data
y=digits.target
target_name=[0,1,2,3,4,5,6,7,8,9]

# on partage les donnes en deux groupes

from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.25,random_state=14)

# analyse discriminante lineaire

from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
lda = LinearDiscriminantAnalysis()
lda.fit(X_train, y_train)
print(1-lda.score(X_test,y_test))
 
# analyse discriminante quadratique

from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
qda = QuadraticDiscriminantAnalysis()
qda.fit(X_train, y_train)
print(1-qda.score(X_test,y_test))

from sklearn.model_selection import cross_val_score
res = cross_val_score(lda, X_train, y_train, cv=10)
print(res)

from sklearn.model_selection import RepeatedKFold
Valid_croisee = RepeatedKFold(n_splits=10, n_repeats=50)
from sklearn.model_selection import cross_val_score
scores = cross_val_score(LinearDiscriminantAnalysis(), X_train, y_train, cv = Valid_croisee)
print("Accuracy: %.2f%%" % (scores.mean()*100.0))

scores = cross_val_score(QuadraticDiscriminantAnalysis(), X_train, y_train, cv = Valid_croisee)
print("Accuracy: %.2f%%" % (scores.mean()*100.0))