[Test()] public void TestGetEntriesByRow()
        {
            var matrix = new SparseBooleanMatrix();

            for (int i = 0; i < 5; i++)
            {
                if (i != 2 && i != 3)
                {
                    matrix[i, 1] = true;
                    matrix[i, 4] = true;
                }
            }

            Assert.AreEqual(2, matrix.GetEntriesByRow(0).Count);
            Assert.AreEqual(2, matrix.GetEntriesByRow(1).Count);
            Assert.AreEqual(0, matrix.GetEntriesByRow(2).Count);
            Assert.AreEqual(0, matrix.GetEntriesByRow(3).Count);
            Assert.AreEqual(2, matrix.GetEntriesByRow(4).Count);
        }
Beispiel #2
0
        /// <summary>
        /// Compute the fit of the mapping
        /// </summary>
        /// <returns>
        /// an array of doubles containing the RMSE on the training data for each latent factor
        /// </returns>
        protected double[] ComputeMappingFit()
        {
            double rmse    = 0;
            double penalty = 0;
            var    rmse_and_penalty_per_factor = new double[num_factors];

            int num_items = 0;

            for (int i = 0; i < MaxItemID + 1; i++)
            {
                var item_users = Feedback.ItemMatrix.GetEntriesByRow(i);
                var item_attrs = item_attributes.GetEntriesByRow(i);
                if (item_users.Count == 0 || item_attrs.Count == 0)
                {
                    continue;
                }

                num_items++;

                double[] est_factors = MapToLatentFactorSpace(i);
                for (int j = 0; j < num_factors; j++)
                {
                    double error    = Math.Pow(est_factors[j] - item_factors[i, j], 2);
                    double reg_term = reg_mapping * VectorUtils.EuclideanNorm(attribute_to_factor.GetColumn(j));
                    rmse    += error;
                    penalty += reg_term;
                    rmse_and_penalty_per_factor[j] += error + reg_term;
                }
            }

            for (int i = 0; i < num_factors; i++)
            {
                rmse_and_penalty_per_factor[i] = (double)rmse_and_penalty_per_factor[i] / num_items;
                Console.Error.Write("{0,0:0.####} ", rmse_and_penalty_per_factor[i]);
            }
            rmse    = (double)rmse / (num_factors * num_items);
            penalty = (double)penalty / (num_factors * num_items);
            Console.Error.WriteLine(" > {0,0:0.####} ({1,0:0.####})", rmse, penalty);

            return(rmse_and_penalty_per_factor);
        }