Csc 1302, Honors Principles of Computer Science II (Fall 2016)

Netflix - Part 1

Use Overall Mean Rating As Predicted Value

  1. Write a Java program, OverallMean.java, that reads all the 17770 ratings files and computes the average rating value across all customers and all movies and prints the value to the screen. The program should take the name of the training_set folder as a command line parameter. My program run is shown below:
    $ java OverallMean training_set
    Overall Mean = 3.604289964420661
    
    Pseudo-Code:
    
    import java.io.*;
    public class OverallMean {
      public static void main(String args[]) {
        double sum = 0.0
        int nentries = 0
        for i=1 to 17770:
          construct fname for file i (e.g. for i=7097, training_set/mv_0007097.txt) 
          open file, fname
          s = read line from file
          while s is not null:
            if line does not end with ":"
              extract rating and add to sum and increment nentries
            s = read line from file
          close file
        print sum/nentries
      }
    }
    
  2. Write a Java program, ProbeAnswers.java, that reads probe.txt file and produces a text file, called actualRatings.txt that contains the actual ratings given by the customer for the corresponding movie in the same order as in the probe.txt file. The program should take the name of the training_set folder and the name of the probe.txt file as command line parameters. Sample run:
    $ java ProbeAnswers training_set probe.txt
    
    Pseudo Code:
    
    import java.util.Map;
    import java.util.HashMap;
    import java.io.*
    
    public class ProbeAnswers {
    
      public static Map<Integer,Double> createCustRatingsDictionaryForMovie(int mid) {
        Map<Integer,Double> mRatings = new HashMap<Integer,Double>
        Construct file name, fname for mid
        open file, fname
        s = read line from file
        while s is not null:
          if line does not end with :
            extract customer id and rating from s
            add entry to mRatings
          s = read line from file
        close file
        return mRatings
      }
    
      public static void main(String args[]) {
        Map<Integer,Double> d = null;
        open file probe.txt
        s = read line from file
        while s is not null:
          if line ends with :
            extract mid from s
            d = createCustRatingsDictionaryForMovie(mid)
          else
            extract cid from s
            obtain rating from d for cid
            print rating
          s = read line from file
        close file
      }
    
    The above run should produce actualRatings.txt.
  3. Write a Java program, Netflix1.java, that predicts overall mean obtained earlier as the rating for all entries in probe.txt and stores the predicted ratings in an ArrayList, called predictedRatings. Next the program reads the file actualRatings.txt and stores these ratings in another ArrayList, called actualRatings. Finally, the program computes the rmse between predictedRatings and actualRatings and prints the rmse value. The program should take the name of the name of the probe.txt file and the name of the actualRatings.txt file as command line parameters. A sample run is shown below:
    $ java Netflix1 probe.txt actualRatings.txt
    RMSE=1.1295625326454617
    
    Pseudo Code:
    import java.util.ArrayList;
    import java.util.Map;
    import java.util.HashMap;
    import java.io.*;
    import java.util.Scanner;
    
    public class Netflix {
    
      public static ArrayList<Double> readActualRatings(String fname) {
        initialize array list
        open file
        s = read a line from file
        while s is not null:
          extract rating
          add to array list
          s = read a line from file
        close file
        return array list
      }
    
      public static ArrayList<Double> predictRatings(
        initialize array list
        open probe.txt file
        s = read line from file
        while s is not null:
          if s ends with : 
            extract mid
          else
            extract cid
            predict rating
            add rating to array list
          s = read line from file
        close file
        return array list
      }
    
      public static double rmse(ArrayList<Double> a, ArrayList<Double> p) {
        return square root of the sum of squares of the difference between a[i] amd p[i]
      }
    
      public static void main(String args[]) {
        ArrayList<Double> actualRatings = readActualRatings("actualRatings.txt");
        ArrayList<Double> predictedRatings = predictRatings();
        double e = rmse(actualRatings,predictedRatings);
        System.out.println("RMSE="+e);
      }
    }