Example #1
0
        public string RunPrefNMF(int maxEpoch, double learnRate, double regularizationOfUser,
                                 double regularizationOfItem, int factorCount, int topN = 10)
        {
            if (!ReadyForOrdinal)
            {
                GetReadyForOrdinal();
            }
            StringBuilder log = new StringBuilder();

            log.AppendLine(Utils.PrintHeading("PrefNMF"));

            // Prediction
            Utils.StartTimer();
            DataMatrix R_predicted = PrefNMF.PredictRatings(PR_train, R_unknown,
                                                            maxEpoch, learnRate, regularizationOfUser, regularizationOfItem, factorCount);

            log.AppendLine(Utils.StopTimer());

            // Evaluation
            var topNItemsByUser = ItemRecommendationCore.GetTopNItemsByUser(R_predicted, topN);

            for (int n = 1; n <= topN; n++)
            {
                Utils.PrintValue("NCDG@" + n, NCDG.Evaluate(RelevantItemsByUser, topNItemsByUser, n).ToString("0.0000"));
            }
            for (int n = 1; n <= topN; n++)
            {
                log.AppendLine(Utils.PrintValue("MAP@" + n, MAP.Evaluate(RelevantItemsByUser, topNItemsByUser, n).ToString("0.0000")));
            }

            return(log.ToString());
        }
Example #2
0
        public string RunPrefNMFbasedOMF(int maxEpoch, double learnRate, double regularizationOfUser,
                                         double regularizationOfItem, int factorCount, List <double> quantizer, int topN)
        {
            if (!ReadyForOrdinal)
            {
                GetReadyForOrdinal();
            }
            StringBuilder log = new StringBuilder();

            log.AppendLine(Utils.PrintHeading("PrefNMF based OMF"));

            // =============PrefNMF prediction on Train+Unknown============
            // Get ratings from scorer, for both train and test
            // R_all contains indexes of all ratings both train and test
            // DataMatrix R_all = new DataMatrix(R_unknown.UserCount, R_unknown.ItemCount);
            // R_all.MergeNonOverlap(R_unknown);
            //R_all.MergeNonOverlap(R_train.IndexesOfNonZeroElements());
            //PrefRelations PR_unknown = PrefRelations.CreateDiscrete(R_all);

            // R_all is far too slow, change the data structure
            //Dictionary<int, List<Tuple<int, int>>> PR_unknown = new Dictionary<int, List<Tuple<int, int>>>();
            //Dictionary<int, List<int>> PR_unknown_cache = new Dictionary<int, List<int>>();
            Dictionary <int, List <int> > ItemsByUser_train   = R_train.GetItemsByUser();
            Dictionary <int, List <int> > ItemsByUser_unknown = R_unknown.GetItemsByUser();
            Dictionary <int, List <int> > PR_unknown          = new Dictionary <int, List <int> >(ItemsByUser_train);
            List <int> keys = new List <int>(ItemsByUser_train.Keys);

            foreach (var key in keys)
            {
                PR_unknown[key].AddRange(ItemsByUser_unknown[key]);
            }

            /*
             * foreach (var row in R_unknown.Matrix.EnumerateRowsIndexed())
             * {
             *  int indexOfUser = row.Item1;
             *  PR_unknown_cache[indexOfUser] = new List<int>();
             *  Vector<double> itemsOfUser = row.Item2;
             *  foreach (var item in itemsOfUser.EnumerateIndexed(Zeros.AllowSkip))
             *  {
             *      PR_unknown_cache[indexOfUser].Add(item.Item1);
             *  }
             * }
             * foreach (var row in R_train.Matrix.EnumerateRowsIndexed())
             * {
             *  int indexOfUser = row.Item1;
             *  Vector<double> itemsOfUser = row.Item2;
             *  foreach (var item in itemsOfUser.EnumerateIndexed(Zeros.AllowSkip))
             *  {
             *      PR_unknown_cache[indexOfUser].Add(item.Item1);
             *  }
             * }
             */


            Utils.StartTimer();
            SparseMatrix PR_predicted = PrefNMF.PredictPrefRelations(PR_train, PR_unknown,
                                                                     maxEpoch, learnRate, regularizationOfUser, regularizationOfItem, factorCount, quantizer);

            // Both predicted and train need to be quantized
            // otherwise OMF won't accept
            //PR_predicted.quantization(0, 1.0,
            //   new List<double> { Config.Preferences.LessPreferred,
            //            Config.Preferences.EquallyPreferred, Config.Preferences.Preferred });
            DataMatrix R_predictedByPrefNMF = new DataMatrix(PR_predicted);// new DataMatrix(PR_predicted.GetPositionMatrix());

            // PR_train itself is already in quantized form!
            //PR_train.quantization(0, 1.0, new List<double> { Config.Preferences.LessPreferred, Config.Preferences.EquallyPreferred, Config.Preferences.Preferred });
            DataMatrix R_train_positions = new DataMatrix(PR_train.GetPositionMatrix());

            R_train_positions.Quantization(quantizer[0], quantizer[quantizer.Count - 1] - quantizer[0], quantizer);
            log.AppendLine(Utils.StopTimer());

            // =============OMF prediction on Train+Unknown============
            log.AppendLine(Utils.PrintHeading("Ordinal Matrix Factorization with PrefNMF as scorer"));
            Utils.StartTimer();
            Dictionary <Tuple <int, int>, List <double> > OMFDistributionByUserItem;
            DataMatrix R_predicted;

            log.AppendLine(OMF.PredictRatings(R_train_positions.Matrix, R_unknown.Matrix, R_predictedByPrefNMF.Matrix,
                                              quantizer, out R_predicted, out OMFDistributionByUserItem));
            log.AppendLine(Utils.StopTimer());

            // TopN Evaluation
            var topNItemsByUser = ItemRecommendationCore.GetTopNItemsByUser(R_predicted, topN);

            for (int n = 1; n <= topN; n++)
            {
                log.AppendLine(Utils.PrintValue("NCDG@" + n, NCDG.Evaluate(RelevantItemsByUser, topNItemsByUser, n).ToString("0.0000")));
            }
            for (int n = 1; n <= topN; n++)
            {
                log.AppendLine(Utils.PrintValue("MAP@" + n, MAP.Evaluate(RelevantItemsByUser, topNItemsByUser, n).ToString("0.0000")));
            }

            // Save OMFDistribution to file
            if (!File.Exists(GetDataFileName("PrefOMF_")))
            {
                Utils.IO <Dictionary <Tuple <int, int>, List <double> > > .SaveObject(OMFDistributionByUserItem, GetDataFileName("PrefOMF_"));
            }

            return(log.ToString());
        }