
X = randi(20,9,8)
X = [X X(:,1:3)]
X = (X - mean(X))*10
[N,D] = size(X)
SIGMA = cov(X)
avgXTX = X'*X / (N-1)
pause;

sumXTX = zeros(D);
for i=1:N
	sumXTX=sumXTX + X(i,:)'*X(i,:);
end
sumXTX = sumXTX / (N-1)

pause;

[E,D] = eig(SIGMA)
S_vip = X*E
[C,S] = pca(X)
pause;












%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% HOUSEING DATA
A  = importdata('../../../../CS6140/data/housing_train.txt');
X = A(:,1:13);
%X = A(:,1:13);
L = A(:,14);
[N,D]= size(X)

[C,P] = pca(X);  
size(P) 
X3 = P(:,1:3); 
figure(31); clf;
scatter3(X3(:,1), X3(:,2), X3(:,3), 25, L, 'filled');
title ('HOUSING PCA')
pause;




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%. SPAMBASE
A = importdata('../../../../CS6140/data/spambase/spambase.data');
X = A(:,1:57);
L= A(:,58);
L(find(L>0))=1;
L(find(L<=0))=0;
[N,D]= size(X)
[C,P] = pca(X);  
size(P) 
X3 = P(:,1:3); 
figure(33); clf;
scatter3(X3(:,1), X3(:,2), X3(:,3), 25, L, 'filled');
title ('SPAMBASE PCA')
pause;











%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%. MNIST
A = load('../../../data/mnist-original.mat');
X = A.data' ;
[N,D] = size(X)
X = double(X);
L = A.label;
%% sample dataset
Nnew= 20000;
r = randperm(N);
ind = r(1:Nnew)';
X = X(ind, :);
L = L(ind);
N=Nnew


ind2 = find(L==2);
ind7 = find(L==7);
ind5 = find(L==5);
size(ind2)

whos
pause;

y = kmeans(X',kseeds(X',10));

%[counts, groupnames] = groupcounts(y);[groupnames counts]

CONFMAT = confusionmat (y, L)

figure(1)
histogram(y(ind2))
purity_CL = purity(y,L)
purity_LC = purity(L,y)
 
 
 
 
numGroups = length(unique(L));
clr = hsv(numGroups)
 
 
 
 [C,P] = pca(X);  
 size(P) 
  X3 = P(:,1:3);  
 size(X3)
 no_dims = size(X3,2);
 figure(11); clf
   plot3 (X3(ind5,1), X3(ind5,2), X3(ind5,3), 'g.')
   grid on
   hold on
   plot3 (X3(ind2,1), X3(ind2,2), X3(ind2,3), 'r.')
   plot3 (X3(ind7,1), X3(ind7,2), X3(ind7,3), 'b.') 
  
figure(12); clf;
  if no_dims == 1
      scatter(X3, X3, 25, L, 'filled');
  elseif no_dims == 2
      scatter(X3(:,1), X3(:,2), 25, L, 'filled');
  else
      scatter3(X3(:,1), X3(:,2), X3(:,3), 25, L, 'filled');
  end
  pause;
 
 
 
 
 
 y3 = kmeans(X3',kseeds(X3',10));  
 figure(2)
 histogram(y3(ind2))
 title('3 dim')
 CONFMAT3 = confusionmat (y3, L)
 
 X10 = P(:,1:10);     
 y10 = kmeans(X10',kseeds(X10',10)); 
 figure(21);
 histogram(y10(ind2))  
 title('10 dim')
 CONFMAT10 = confusionmat (y10, L)
  
 X20 = P(:,1:20);     
 y20 = kmeans(X20',kseeds(X20',10)); 
 figure(3);
 histogram(y20(ind2))  
 title('20 dim')
 CONFMAT20 = confusionmat (y20, L)
  
 X100 = P(:,1:100);     
 y100 = kmeans(X100',kseeds(X100',10)); 
 figure(4);
 histogram(y100(ind2))  
 title('100 dim')
 CONFMAT100 = confusionmat (y100, L)
 
