Class for storing cosine similarities
http://en.wikipedia.org/wiki/Cosine_similarity
Inheritance: BinaryDataCorrelationMatrix
		[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);
 }
Beispiel #3
0
 /// <summary>Creates a Cosine similarity matrix from given data</summary>
 /// <param name="vectors">the boolean data</param>
 /// <returns>the similarity matrix based on the data</returns>
 public static CorrelationMatrix Create(IBooleanMatrix vectors)
 {
     BinaryDataCorrelationMatrix cm;
     int num_entities = vectors.NumberOfRows;
     try
     {
         cm = new BinaryCosine(num_entities);
     }
     catch (OverflowException)
     {
         Console.Error.WriteLine("Too many entities: " + num_entities);
         throw;
     }
     cm.ComputeCorrelations(vectors);
     return cm;
 }
Beispiel #4
0
        /// <summary>Creates a Cosine similarity matrix from given data</summary>
        /// <param name="vectors">the boolean data</param>
        /// <returns>the similarity matrix based on the data</returns>
        static public CorrelationMatrix Create(IBooleanMatrix vectors)
        {
            BinaryDataCorrelationMatrix cm;
            int num_entities = vectors.NumberOfRows;

            try
            {
                cm = new BinaryCosine(num_entities);
            }
            catch (OverflowException)
            {
                Console.Error.WriteLine("Too many entities: " + num_entities);
                throw;
            }
            cm.ComputeCorrelations(vectors);
            return(cm);
        }
        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);
        }
 public void TestComputeCorrelations()
 {
     // 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 cosine = new BinaryCosine(5);
     cosine.ComputeCorrelations(sparse_boolean_matrix);
     Assert.AreEqual(Math.Round(1 / Math.Sqrt(6), 4), Math.Round(cosine[0, 1], 4));
     Assert.AreEqual(Math.Round(1 / Math.Sqrt(6), 4), Math.Round(cosine[1, 0], 4));
     Assert.AreEqual(Math.Round(1 / 3d, 4), Math.Round(cosine[1, 3], 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);
		}