Guest User

Untitled

a guest
Feb 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.15 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class MatrixFactorization {
  4.  
  5.     /**
  6.      * @param args
  7.      */
  8.    
  9.     public static double[][] FactorizeMatrix(double[][] array, double[][] P, double[][] Q, int K , int steps, double alpha, double beta)
  10.     {
  11.         double[][] QTranspose = TransposeMatrix(Q);
  12.         for (int step=0; step < steps; step++){
  13.             for (int i =0; i < array.length; i++){
  14.                 for (int j=0; j<array[i].length; j++){
  15.                     if (array[i][j] > 0.0){
  16.                        
  17.                         double dotPQ = 0.0;
  18.                         for (int z = 0; z < P[i].length; z++)
  19.                             dotPQ += P[i][z] * QTranspose[z][j];
  20.                        
  21.                         double eij = array[i][j] - dotPQ;
  22.                        
  23.                         for (int k = 0; k< K; k++){
  24.                             P[i][k] += alpha * (2 * eij * QTranspose[k][j] - beta * P[i][k]);
  25.                             QTranspose[k][j] += alpha * (2 * eij * P[i][k] - beta * QTranspose[k][j]);
  26.                         }                          
  27.                     }
  28.                 }
  29.             }
  30.             double e= 0.0;
  31.             for (int i=0; i< array.length; i++){
  32.                 for (int j=0; j<array[i].length; j++){
  33.                     if (array[i][j] > 0.0){
  34.                        
  35.                         double dotPQ = 0.0;                    
  36.                         for (int z = 0; z < P[i].length; z++)
  37.                             dotPQ += P[i][z] * QTranspose[z][j];
  38.                        
  39.                         e += Math.pow( ( array[i][j] - dotPQ) ,2);
  40.                        
  41.                         for (int k = 0; k< K; k++){
  42.                             e += (beta/2) * ( Math.pow(P[i][k],2) + Math.pow(QTranspose[k][j],2));
  43.                         }
  44.                     }
  45.                 }
  46.             }
  47.            
  48.             if (e < 0.001)
  49.                 break;
  50.        
  51.         }
  52.         //Debug(transposeArray);       
  53.         System.out.println("Matrix R:");
  54.         Debug(array);
  55.         System.out.println("Factorized Matrix R':");
  56.         Debug(MultiplyMatrices(P, QTranspose));
  57.         return P;
  58.     }
  59.    
  60.     private static double[][] MultiplyMatrices(double A[][], double B[][])
  61.     {
  62.         int rows = A.length;
  63.         int cols = B[0].length;
  64.         double C[][] = new double[rows][cols];
  65.         for (int i=0; i<rows; i++) {
  66.             for (int j=0; j<cols; j++) {
  67.                 double val = 0.0;
  68.                 for (int k=0; k<A[0].length; k++) {            
  69.                     val += A[i][k] * B[k][j];
  70.                 }
  71.             C[i][j] = val;
  72.             }
  73.         }
  74.         return C;
  75.     }
  76.    
  77.     private static double[][] TransposeMatrix(double array[][])
  78.     {
  79.         int rows = array.length;
  80.         int columns = array[0].length;
  81.         double[][] transposeArray = new double[columns][rows];
  82.         for (int i =0; i < rows; i++)
  83.             for (int j=0; j< columns; j++)
  84.                 transposeArray[j][i] = array[i][j];    
  85.        
  86.         return transposeArray;
  87.     }
  88.    
  89.     private static void Debug(double[][] array)
  90.     {
  91.         for ( int i =0; i < array.length; i++){
  92.             for (int j = 0; j < array[0].length; j++){
  93.                     System.out.print(array[i][j] + " ");
  94.             }
  95.             System.out.println();
  96.         }
  97.     }
  98.     public static void main(String[] args) {
  99.        
  100.         double[][] array = {{5,3,0,1},
  101.                             {4,0,0,1},
  102.                             {1,1,0,5},
  103.                             {1,0,0,4},
  104.                             {0,1,5,4}
  105.                         };
  106.        
  107.         int N = array.length;
  108.         int M = array[0].length;
  109.         int K =2;
  110.        
  111.         double[][] P = new double[N][K];
  112.         double[][] Q = new double[M][K];
  113.        
  114.         Random generator = new Random();
  115.        
  116.         for ( int i =0; i < N; i++){
  117.             for (int j = 0; j < K; j++){
  118.                 P[i][j]= generator.nextDouble();           
  119.             }          
  120.         }
  121.        
  122.         for ( int i =0; i < M; i++){
  123.             for (int j = 0; j < K; j++){
  124.                 Q[i][j]= generator.nextDouble();           
  125.             }          
  126.         }
  127.        
  128.         FactorizeMatrix(array, P, Q, K, 2500, 0.0002, 0.02);
  129.        
  130.        
  131.     }
  132.  
  133. }
Add Comment
Please, Sign In to add comment