Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.75 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.lang.*;
  4. import Jama.*;
  5. public class pcafaces{
  6.     public static void main(String args[]){
  7.     double[][] xmatrix = new double[644][280];
  8.     double sum = 0;
  9.     //int pixels=644;
  10.     //int photos=280;
  11.     double diagSum = 0;
  12.     double dimension = 0;
  13.     int r = 0;
  14.     double[][] amatrix = new double[644][280];
  15.     //file handlers
  16.     Matrix Y;
  17.     Matrix zDash;
  18.     if(args.length!=4){
  19.         throw new NullPointerException();
  20.         }
  21.     String trainPath = args[0];
  22.     String testPath = args[1];
  23.     String varth = args[2];
  24.     String k = args[3];
  25.     int realK =(Integer.parseInt(k));
  26.     for(int i=0;i<40;i++){
  27.         for(int j=0;j<7;j++){
  28.         try{
  29.         BufferedReader in = new BufferedReader(new FileReader(trainPath+"/s"+i+"_"+j+".pgm"));
  30.         in.readLine(); // This reads 'P2'.
  31.         in.readLine(); // This reads the comment.
  32.         in.readLine(); // This reads the image dimensions.
  33.         in.readLine(); // This reads the intensity range.
  34.     for(int p = 0;p<644;p++){
  35.         xmatrix[p][i*7+j]= Double.parseDouble(in.readLine());
  36.         }
  37.         //System.out.println(xmatrix[300][170]);
  38.         in.close();
  39.         }catch(IOException e){}
  40.             }
  41.         }
  42.         double[][] xMean = new double[644][1];
  43.         double[][] xBar = new double[644][280];
  44.         for(int i=0;i<644;i++){
  45.             for(int j=0;j<280;j++){
  46.                 sum = i+j;
  47.                 xMean[i][0] = sum*(1/(280));
  48.                 xBar[i][j] = xmatrix[i][j] - xMean[i][0];
  49.         }
  50.     }
  51.  
  52.         //Initialises matrix A with the values in the double array vals.
  53.         Matrix A = new Matrix(xBar);
  54.         // Invokes SVD from the Jama package.
  55.         SingularValueDecomposition s = A.svd();
  56.         Matrix D = s.getS();//get sigma diagonal matrix and add values to sum
  57.         for(int i=0;i<280;i++){
  58.         diagSum+=D.get(i,i);
  59.         }
  60.         for(int i=0;i<280;i++){
  61.         dimension += D.get(i,i);            //(dimension/diagSum)*100
  62.         if(((dimension/diagSum)*100)>=Double.parseDouble(varth)){
  63.             r = i;
  64.             break;
  65.             }
  66.         }
  67.         Matrix B = new Matrix(xMean);
  68.         Matrix U = s.getU();
  69.         Matrix Ur = U.getMatrix(0, U.getRowDimension()-1, 0, r);
  70.         Y = ((Ur).transpose()).times(A);
  71.  
  72.        
  73.    
  74.     for(int i1=0;i1<40;i1++){
  75.         for(int j1=0;j1<3;j1++){
  76.             try{
  77.         BufferedReader br = new BufferedReader(new FileReader(testPath+"/s"+i1+"_"+j1+".pgm"));
  78.         br.readLine(); // This reads 'P2'.
  79.         br.readLine(); // This reads the comment.
  80.         br.readLine(); // This reads the image dimensions.
  81.         br.readLine(); // This reads the intensity range.
  82.        
  83.        
  84.         double[][] zmatrix = new double[23*28][1];
  85.     for(int p =0;p<644;p++){
  86.         zmatrix[p][0]= Double.parseDouble(br.readLine());
  87.         }
  88.         Matrix Z = new Matrix(zmatrix);
  89.         zDash = Ur.transpose().times(Z.minus(B));
  90.        
  91.         //perform comparison with test files and train files
  92.     /*8. Perform classification using Z' and Y , e.g., find the K nearest
  93.     neighbours of Z' among Y .*/
  94.     double distance;
  95.     int[] label = new int[realK];
  96.     double[] nndis = new double[realK];
  97.     for(int i=0;i<280;i++){
  98.             distance=0;
  99.         for(int j=0;j<r;j++){
  100.             distance +=Math.pow((zDash.get(j,0)-(Y.get(j,i))),2);
  101.             }
  102.                 for(int j=0;j<realK;j++){
  103.                     if(distance<nndis[j]){
  104.                         for(int l=realK-2;l>=j;l--){
  105.                         nndis[l+1]=nndis[l];
  106.                         label[l + 1]  = label[l];//nnlab[k]=total photos/photosper person
  107.                         }
  108.                     nndis[realK] = distance;
  109.                     label[realK] = i/7;
  110.                     break;
  111.                     }
  112.                 }
  113.             }
  114.         //return majority value of label
  115.        
  116.         double max=0;
  117.         int index=0;
  118.         boolean check = false;
  119.         int[] result = new int[40];
  120.         double maxResult=0;
  121.         for (int i = 0; i <40; ++i) {
  122.          result[i]= 0;
  123.          }
  124.         for (int i = 0; i<realK; i++) {
  125.             result[label[i]]++;
  126.             }
  127.         for(int i=0;i<40;i++){
  128.             if (result[i] > maxResult) {
  129.             maxResult = result[i];
  130.             index = result[i];
  131.             check = true;
  132.             }
  133.         }
  134.     if(check){
  135.     System.out.println(-1);
  136.         }else{
  137.     System.out.println(result[index]);
  138.         }
  139.         br.close();
  140.         }catch(IOException e){}
  141.             }
  142.         }
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement