[Test()] public void TestCreate()
        {
            var sparse_boolean_matrix = new SparseBooleanMatrix();

            sparse_boolean_matrix[0, 1] = true;
            sparse_boolean_matrix[0, 4] = true;
            sparse_boolean_matrix[1, 0] = true;
            sparse_boolean_matrix[1, 2] = true;
            sparse_boolean_matrix[1, 4] = true;
            sparse_boolean_matrix[3, 1] = true;
            sparse_boolean_matrix[3, 3] = true;
            sparse_boolean_matrix[3, 4] = true;

            var correlation_matrix = new BinaryCosine(sparse_boolean_matrix.NumberOfRows);

            correlation_matrix.ComputeCorrelations(sparse_boolean_matrix);

            Assert.AreEqual(4, correlation_matrix.NumberOfRows);
            Assert.IsTrue(correlation_matrix.IsSymmetric);

            Assert.AreEqual(1 / Math.Sqrt(6), correlation_matrix[0, 1], DELTA);
            Assert.AreEqual(1 / Math.Sqrt(6), correlation_matrix[1, 0], DELTA);
            Assert.AreEqual(1 / 3d, correlation_matrix[1, 3], DELTA);

            Assert.AreEqual(0f, correlation_matrix[2, 0]);
            Assert.AreEqual(0f, correlation_matrix[2, 1]);
            Assert.AreEqual(1f, correlation_matrix[2, 2]);
            Assert.AreEqual(0f, correlation_matrix[2, 3]);

            Assert.AreEqual(0f, correlation_matrix[0, 2]);
            Assert.AreEqual(0f, correlation_matrix[1, 2]);
            Assert.AreEqual(0f, correlation_matrix[3, 2]);
        }
        ///
        public override void LearnAttributeToFactorMapping()
        {
            BinaryCosine cosine_correlation = new BinaryCosine(MaxItemID + 1);

            Console.Error.WriteLine("training with max_item_id={0}", MaxItemID);
            cosine_correlation.ComputeCorrelations(item_attributes);
            this.item_correlation   = cosine_correlation;
            _MapToLatentFactorSpace = Utils.Memoize <int, float[]>(__MapToLatentFactorSpace);
        }
Exemple #3
0
 ///
 protected override void RetrainUser(int user_id)
 {
     base.RetrainUser(user_id);
     if (UpdateUsers)
     {
         for (int i = 0; i <= MaxUserID; i++)
         {
             correlation[user_id, i] = BinaryCosine.ComputeCorrelation(new HashSet <int>(data_user[user_id]), new HashSet <int>(data_user[i]));
         }
     }
 }
Exemple #4
0
 ///
 protected override void RetrainItem(int item_id)
 {
     base.RetrainUser(item_id);
     if (UpdateItems)
     {
         for (int i = 0; i <= MaxItemID; i++)
         {
             correlation[item_id, i] = BinaryCosine.ComputeCorrelation(new HashSet <int>(data_item[item_id]), new HashSet <int>(data_item[i]));
         }
     }
 }
Exemple #5
0
        ///
        public override void Train()
        {
            correlation = BinaryCosine.Create(user_attributes);

            int num_users = user_attributes.NumberOfRows;

            this.nearest_neighbors = new int[num_users][];
            for (int u = 0; u < num_users; u++)
            {
                nearest_neighbors[u] = correlation.GetNearestNeighbors(u, k);
            }
        }
Exemple #6
0
        ///
        public override void Train()
        {
            this.correlation = BinaryCosine.Create(Feedback.UserMatrix);

            int num_users = MaxUserID + 1;

            this.nearest_neighbors = new int[num_users][];
            for (int u = 0; u < num_users; u++)
            {
                nearest_neighbors[u] = correlation.GetNearestNeighbors(u, k);
            }
        }
Exemple #7
0
        ///
        public override void Train()
        {
            correlation = BinaryCosine.Create(Feedback.ItemMatrix);

            int num_items = MaxItemID + 1;

            this.nearest_neighbors = new int[num_items][];
            for (int i = 0; i < num_items; i++)
            {
                nearest_neighbors[i] = correlation.GetNearestNeighbors(i, k);
            }
        }
        [Test()] public void TestComputeCorrelation()
        {
            // create test objects
            var vector1 = new HashSet <int>();

            vector1.Add(0);
            vector1.Add(2);
            vector1.Add(4);
            var vector2 = new HashSet <int>();

            vector2.Add(1);
            vector2.Add(3);
            vector2.Add(4);
            // test
            Assert.AreEqual(Math.Round(1 / 3d, 4), Math.Round(BinaryCosine.ComputeCorrelation(vector1, vector2), 4));
        }
        [Test()] public void TestComputeCorrelation()
        {
            var vector1 = new HashSet <int>();

            vector1.Add(0);
            vector1.Add(2);
            vector1.Add(4);
            var vector2 = new HashSet <int>();

            vector2.Add(1);
            vector2.Add(3);
            vector2.Add(4);

            var cosine = new BinaryCosine(4);

            Assert.AreEqual(1 / 3f, cosine.ComputeCorrelation(vector1, vector2), DELTA);
            Assert.AreEqual(0f, cosine.ComputeCorrelation(vector1, new HashSet <int>()), DELTA);
        }
        [Test()] public void TestComputeCorrelations()
        {
            var sparse_boolean_matrix = new SparseBooleanMatrix();

            sparse_boolean_matrix[0, 1] = true;
            sparse_boolean_matrix[0, 4] = true;
            sparse_boolean_matrix[1, 0] = true;
            sparse_boolean_matrix[1, 2] = true;
            sparse_boolean_matrix[1, 4] = true;
            sparse_boolean_matrix[3, 1] = true;
            sparse_boolean_matrix[3, 3] = true;
            sparse_boolean_matrix[3, 4] = true;

            var correlation = new BinaryCosine(4);

            correlation.ComputeCorrelations(sparse_boolean_matrix);
            Assert.AreEqual(1 / Math.Sqrt(6), correlation[0, 1], DELTA);
            Assert.AreEqual(1 / Math.Sqrt(6), correlation[1, 0], DELTA);
            Assert.AreEqual(1 / 3d, correlation[1, 3], DELTA);
        }
        [Test()] public void TestCreate()
        {
            // create test objects
            var sparse_boolean_matrix = new SparseBooleanMatrix();

            sparse_boolean_matrix[0, 1] = true;
            sparse_boolean_matrix[0, 4] = true;
            sparse_boolean_matrix[1, 0] = true;
            sparse_boolean_matrix[1, 2] = true;
            sparse_boolean_matrix[1, 4] = true;
            sparse_boolean_matrix[3, 1] = true;
            sparse_boolean_matrix[3, 3] = true;
            sparse_boolean_matrix[3, 4] = true;
            // test
            var correlation_matrix = BinaryCosine.Create(sparse_boolean_matrix);

            Assert.AreEqual(Math.Round(1 / Math.Sqrt(6), 4), Math.Round(correlation_matrix[0, 1], 4));
            Assert.AreEqual(Math.Round(1 / Math.Sqrt(6), 4), Math.Round(correlation_matrix[1, 0], 4));
            Assert.AreEqual(Math.Round(1 / 3d, 4), Math.Round(correlation_matrix[1, 3], 4));
        }
 ///
 public override void Train()
 {
     base.Train();
     this.correlation = BinaryCosine.Create(user_attributes);
 }
Exemple #13
0
 ///
 public override void Train()
 {
     base.Train();
     this.correlation = BinaryCosine.Create(data_user);
 }
Exemple #14
0
 ///
 public override void Train()
 {
     base.Train();
     this.correlation = BinaryCosine.Create(data_item);
     this.GetPositivelyCorrelatedEntities = Utils.Memoize <int, IList <int> >(correlation.GetPositivelyCorrelatedEntities);
 }