Commit d15d7d92 authored by Lacoin Achille's avatar Lacoin Achille
Browse files

ajout du TP2 (non terminé)

parent 0882d90b
load("uspsasi.mat");
ind = find(y==2);
carac = x(ind,:);
figure(1)
subplot(211)
imagesc(reshape(carac(198, :), 16, 16)'); colormap(gray);
%imagesc(corrcoef(carac)), colorbar;
[valpropres, U, moy] = mypca(carac);
figure(2)
bar(valpropres/sum(valpropres));
title('Valeurs propres par ordre décroissant');
%dimension des données projetées
d=2;
P= U(:,1:d);
C = projpca(carac, moy, P);
figure(3)
plot(C(:,1),C(:,2),'ro')
index = 21;
xhat=reconstructpca(C(index,:),P,moy);
figure(1)
subplot(212)
imagesc(reshape(xhat(198, :), 16, 16)'); colormap(gray);
load("Subset1YaleFaces.mat");
function [errVal] = erreursPrediction(xapp,yapp,x,n,y)
MatDist=[];
errVal=1:1:n;
for k=1:n
[yValPred,MatDist] = kppv(x,xapp,yapp,k,MatDist);
errVal(k)= mean(y ~= yValPred);
end
end
\ No newline at end of file
<!DOCTYPE html
PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!--
This HTML was auto-generated from MATLAB code.
To make changes, update the MATLAB code and republish this document.
--><title>TP1_2</title><meta name="generator" content="MATLAB 9.3"><link rel="schema.DC" href="http://purl.org/dc/elements/1.1/"><meta name="DC.date" content="2018-09-10"><meta name="DC.source" content="TP1_2.m"><style type="text/css">
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,font,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td{margin:0;padding:0;border:0;outline:0;font-size:100%;vertical-align:baseline;background:transparent}body{line-height:1}ol,ul{list-style:none}blockquote,q{quotes:none}blockquote:before,blockquote:after,q:before,q:after{content:'';content:none}:focus{outine:0}ins{text-decoration:none}del{text-decoration:line-through}table{border-collapse:collapse;border-spacing:0}
html { min-height:100%; margin-bottom:1px; }
html body { height:100%; margin:0px; font-family:Arial, Helvetica, sans-serif; font-size:10px; color:#000; line-height:140%; background:#fff none; overflow-y:scroll; }
html body td { vertical-align:top; text-align:left; }
h1 { padding:0px; margin:0px 0px 25px; font-family:Arial, Helvetica, sans-serif; font-size:1.5em; color:#d55000; line-height:100%; font-weight:normal; }
h2 { padding:0px; margin:0px 0px 8px; font-family:Arial, Helvetica, sans-serif; font-size:1.2em; color:#000; font-weight:bold; line-height:140%; border-bottom:1px solid #d6d4d4; display:block; }
h3 { padding:0px; margin:0px 0px 5px; font-family:Arial, Helvetica, sans-serif; font-size:1.1em; color:#000; font-weight:bold; line-height:140%; }
a { color:#005fce; text-decoration:none; }
a:hover { color:#005fce; text-decoration:underline; }
a:visited { color:#004aa0; text-decoration:none; }
p { padding:0px; margin:0px 0px 20px; }
img { padding:0px; margin:0px 0px 20px; border:none; }
p img, pre img, tt img, li img, h1 img, h2 img { margin-bottom:0px; }
ul { padding:0px; margin:0px 0px 20px 23px; list-style:square; }
ul li { padding:0px; margin:0px 0px 7px 0px; }
ul li ul { padding:5px 0px 0px; margin:0px 0px 7px 23px; }
ul li ol li { list-style:decimal; }
ol { padding:0px; margin:0px 0px 20px 0px; list-style:decimal; }
ol li { padding:0px; margin:0px 0px 7px 23px; list-style-type:decimal; }
ol li ol { padding:5px 0px 0px; margin:0px 0px 7px 0px; }
ol li ol li { list-style-type:lower-alpha; }
ol li ul { padding-top:7px; }
ol li ul li { list-style:square; }
.content { font-size:1.2em; line-height:140%; padding: 20px; }
pre, code { font-size:12px; }
tt { font-size: 1.2em; }
pre { margin:0px 0px 20px; }
pre.codeinput { padding:10px; border:1px solid #d3d3d3; background:#f7f7f7; }
pre.codeoutput { padding:10px 11px; margin:0px 0px 20px; color:#4c4c4c; }
pre.error { color:red; }
@media print { pre.codeinput, pre.codeoutput { word-wrap:break-word; width:100%; } }
span.keyword { color:#0000FF }
span.comment { color:#228B22 }
span.string { color:#A020F0 }
span.untermstring { color:#B20000 }
span.syscmd { color:#B28C00 }
.footer { width:auto; padding:10px 0px; margin:25px 0px 0px; border-top:1px dotted #878787; font-size:0.8em; line-height:140%; font-style:italic; color:#878787; text-align:left; float:none; }
.footer p { margin:0px; }
.footer a { color:#878787; }
.footer a:hover { color:#878787; text-decoration:underline; }
.footer a:visited { color:#878787; }
table th { padding:7px 5px; text-align:left; vertical-align:middle; border: 1px solid #d6d4d4; font-weight:bold; }
table td { padding:7px 5px; text-align:left; vertical-align:top; border:1px solid #d6d4d4; }
</style></head><body><div class="content"><pre class="codeinput">ind = find(y==2);
carac = x(ind,:);
figure(1)
subplot(211)
imagesc(reshape(carac(198, :), 16, 16)'); colormap(gray);
<span class="comment">%imagesc(corrcoef(carac)), colorbar;</span>
[valpropres, U, moy] = mypca(carac);
figure(2)
bar(valpropres/sum(valpropres));
title(<span class="string">'Valeurs propres par ordre d&eacute;croissant'</span>);
<span class="comment">%dimension des donn&eacute;es projet&eacute;es</span>
d=2;
P= U(:,1:d);
C = projpca(carac, moy, P);
figure(3)
plot(C(:,1),C(:,2),<span class="string">'ro'</span>)
index = 21;
xhat=reconstructpca(C(index,:),P,moy);
figure(1)
subplot(212)
imagesc(reshape(xhat(198, :), 16, 16)'); colormap(gray);
</pre><pre class="codeoutput">Warning: Using only the real component of complex data.
</pre><img vspace="5" hspace="5" src="TP1_2_01.png" alt=""> <img vspace="5" hspace="5" src="TP1_2_02.png" alt=""> <img vspace="5" hspace="5" src="TP1_2_03.png" alt=""> <p class="footer"><br><a href="http://www.mathworks.com/products/matlab/">Published with MATLAB&reg; R2017b</a><br></p></div><!--
##### SOURCE BEGIN #####
ind = find(y==2);
carac = x(ind,:);
figure(1)
subplot(211)
imagesc(reshape(carac(198, :), 16, 16)'); colormap(gray);
%imagesc(corrcoef(carac)), colorbar;
[valpropres, U, moy] = mypca(carac);
figure(2)
bar(valpropres/sum(valpropres));
title('Valeurs propres par ordre décroissant');
%dimension des données projetées
d=2;
P= U(:,1:d);
C = projpca(carac, moy, P);
figure(3)
plot(C(:,1),C(:,2),'ro')
index = 21;
xhat=reconstructpca(C(index,:),P,moy);
figure(1)
subplot(212)
imagesc(reshape(xhat(198, :), 16, 16)'); colormap(gray);
##### SOURCE END #####
--></body></html>
\ No newline at end of file
function [valprop, U, moy] = mypca(X)
[n, ~] = size(X);
moy = mean(X);
Xc = X - ones(n, 1)*moy;
matCov = 1/n * Xc' * X;
[U,D] = eig(matCov);
[valprop, index] = sort(diag(D), 'descend');
U = U(:,index);
end
\ No newline at end of file
function Ct = projpca(Z, moy, P)
[m, ~] = size(Z);
Zc = Z - ones(m,1) * moy;
Ct = Zc * P;
end
function Xhat = reconstructpca(C, P, moy)
[m, ~] =size(P);
Xhat = C * P' + ones(m ,1) .* moy;
end
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment