Example #1
0
        ///
        public override void LoadModel(string filename)
        {
            using (StreamReader reader = Recommender.GetReader(filename, this.GetType()))
            {
                CorrelationMatrix correlation = CorrelationMatrix.ReadCorrelationMatrix(reader);

                base.Train();                 // train baseline model
                this.correlation = new BinaryCosine(correlation);
            }
        }
Example #2
0
        public void TestWrite()
        {
            // create a test CorrelationMatrix
            var matrix = new CorrelationMatrix(3);

            float[] row1 = { 1f, 0.1f, 0.2f };
            float[] row2 = { 0.1f, 1f, 0.3f };
            float[] row3 = { 0.2f, 0.3f, 1f };

            matrix.SetRow(0, row1);
            matrix.SetRow(1, row2);
            matrix.SetRow(2, row3);

            // test
            string filename = "testCorrelationMatrixWriter.txt";
            var    writer   = new StreamWriter(filename);

            matrix.Write(writer);
            writer.Close();

            var reader1 = new StreamReader(filename);

            Assert.AreEqual("3", reader1.ReadLine().Trim());
            Assert.AreEqual("0 1 0.1", reader1.ReadLine().Trim());
            Assert.AreEqual("0 2 0.2", reader1.ReadLine().Trim());
            Assert.AreEqual("1 2 0.3", reader1.ReadLine().Trim());

            var reader2     = new StreamReader(filename);
            var corr_matrix = CorrelationMatrix.ReadCorrelationMatrix(reader2);

            Assert.AreEqual(1f, corr_matrix[0, 0]);
            Assert.AreEqual(0.1f, corr_matrix[0, 1]);
            Assert.AreEqual(0.2f, corr_matrix[0, 2]);
            Assert.AreEqual(0.1f, corr_matrix[1, 0]);
            Assert.AreEqual(1f, corr_matrix[1, 1]);
            Assert.AreEqual(0.3f, corr_matrix[1, 2]);
            Assert.AreEqual(0.2f, corr_matrix[2, 0]);
            Assert.AreEqual(0.3f, corr_matrix[2, 1]);
            Assert.AreEqual(1f, corr_matrix[2, 2]);
            // close streams and delete the text file
            reader1.Close();
            reader2.Close();
            //File.Delete(filename);
        }
Example #3
0
        ///
        public override void LoadModel(string filename)
        {
            using (StreamReader reader = Recommender.GetReader(filename, this.GetType()))
            {
                int num_users         = int.Parse(reader.ReadLine());
                var nearest_neighbors = new int[num_users][];
                for (int u = 0; u < nearest_neighbors.Length; u++)
                {
                    string[] numbers = reader.ReadLine().Split(' ');

                    nearest_neighbors[u] = new int[numbers.Length];
                    for (int i = 0; i < numbers.Length; i++)
                    {
                        nearest_neighbors[u][i] = int.Parse(numbers[i]);
                    }
                }

                this.correlation       = CorrelationMatrix.ReadCorrelationMatrix(reader);
                this.k                 = (uint)nearest_neighbors[0].Length;
                this.nearest_neighbors = nearest_neighbors;
            }
        }
Example #4
0
        public void TestReadCorrelationMatrix()
        {
            // create test object
            const string filename = "correlation_matrix.txt";
            var          writer   = new StreamWriter(filename);

            writer.WriteLine(3);
            writer.WriteLine("0 1 0.1");
            writer.WriteLine("0 2 0.2");
            writer.WriteLine("1 2 0.3");
            writer.Close();

            var reader      = new StreamReader(filename);
            var corr_matrix = CorrelationMatrix.ReadCorrelationMatrix(reader);

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

            Assert.AreEqual(0.1f, corr_matrix[0, 1]);
            Assert.AreEqual(0.1f, corr_matrix[1, 0]);

            Assert.AreEqual(0.2f, corr_matrix[0, 2]);
            Assert.AreEqual(0.2f, corr_matrix[2, 0]);

            Assert.AreEqual(0.3f, corr_matrix[1, 2]);
            Assert.AreEqual(0.3f, corr_matrix[2, 1]);


            // TODO test Exception
            // test with wrong format

            // close streams an delete the text file
            reader.Close();
            //File.Delete(filename);
        }