close all; clear; clc; load data-1800 X=data(:,1:end-1); Y=categorical(data(:,end)); rng(1) clear xapp yapp xtest ytest % 10 distances pour KNN Dist= {'euclidean' 'Mahalanobis' 'cosine' 'correlation' 'spearman' 'hamming' 'jaccard' 'minkowski' 'cityblock' 'chebychev' }; nbre=1; for d=1:10 for k_nn=1:5 CVO = cvpartition(Y,'holdout',0.4); trIdx = CVO.training; tsIdx = CVO.test; % base d'apprentissage et de test XTrain = X(trIdx,:); YTrain = categorical(Y(trIdx)'); XTest = X(tsIdx,:); YTest = categorical(Y(tsIdx)'); % Phase de training tic; MonModele = fitcknn(X(trIdx,:), Y(trIdx),'NSMethod','exhaustive','Distance',Dist{d},... 'NumNeighbors',k_nn,'Standardize',1); accuracy_tr = (1-resubLoss(MonModele))*100; time_tr =toc; % Phase de testing tic; Y_ts_pred = predict(MonModele,X(tsIdx,:)); %Y_ts_pred = Y_ts_pred'; accuracy_ts = sum(Y(tsIdx) == Y_ts_pred)/length(Y(tsIdx))*100; time_ts=toc; % stocker les résultats Performance_KNN=[accuracy_tr time_tr accuracy_ts time_ts]; acc_tr=mean(Performance_KNN(:,1)); t_tr=mean(Performance_KNN(:,2)); acc_ts=mean(Performance_KNN(:,3)); t_ts=mean(Performance_KNN(:,4)); Performance_KNN_global(nbre,:)=[d k_nn acc_tr t_tr acc_ts t_ts]; nbre=nbre+1; end end [val , ordre]=max(Performance_KNN_global(:,5)); % Meilleur resultat sous forme [distance k_nn acc_tr time_tr acc_ts time_ts] Performance_KNN_global(ordre,:)