Esempio n. 1
0
        /// <summary>
        ///   Reads a sample from the file and returns it as a
        ///   <see cref="Sparse{T}"/> sparse vector, together with
        ///   its associated output value.
        /// </summary>
        ///
        /// <returns>A tuple containing the sparse vector as the first item
        ///   and its associated output value as the second item.</returns>
        ///
        public void Read(out Sparse <double> sample, out bool output)
        {
            var values = ReadLine();

            output = double.Parse(values.Item2) > 0;
            sample = Sparse.Parse(values.Item1, Intercept);
        }
Esempio n. 2
0
        /// <summary>
        ///   Reads a sample from the file and returns it as a
        ///   <see cref="Sparse{T}"/> sparse vector, together with
        ///   its associated output value.
        /// </summary>
        ///
        /// <returns>A tuple containing the sparse vector as the first item
        ///   and its associated output value as the second item.</returns>
        ///
        public void Read(out Sparse <double> sample, out int output)
        {
            var values = ReadLine();

            output = Int32.Parse(values.Item2, System.Globalization.CultureInfo.InvariantCulture);
            sample = Sparse.Parse(values.Item1, Intercept);
        }
Esempio n. 3
0
        /// <summary>
        ///   Reads a sample from the file and returns it as a
        ///   <see cref="Sparse{T}"/> sparse vector, together with
        ///   its associated output value.
        /// </summary>
        ///
        /// <returns>A tuple containing the sparse vector as the first item
        ///   and its associated output value as the second item.</returns>
        ///
        public void Read(out Sparse <double> sample, out double output)
        {
            var values = ReadLine();

            output = Double.Parse(values.Item2, CultureInfo.InvariantCulture);
            sample = Sparse.Parse(values.Item1, Intercept);
        }
Esempio n. 4
0
        /// <summary>
        ///   Reads a sample from the file and returns it as a
        ///   <see cref="Sparse{T}"/> sparse vector, together with
        ///   its associated output value.
        /// </summary>
        ///
        /// <returns>A tuple containing the sparse vector as the first item
        ///   and its associated output value as the second item.</returns>
        ///
        public Tuple <Sparse <double>, double> ReadSparse()
        {
            var values = ReadLine();
            var output = Double.Parse(values.Item2, CultureInfo.InvariantCulture);
            var sample = Sparse.Parse(values.Item1, Intercept);

            return(Tuple.Create(sample, output));
        }
Esempio n. 5
0
        public void ParseTest()
        {
            double[]        v;
            Sparse <double> actual;
            Sparse <double> expected;
            string          s;

            v        = new double[] { 1, 2, 3, 0, 0, 6 };
            expected = Sparse.FromDense(v);
            s        = expected.ToString();
            actual   = Sparse.Parse(s);
            Assert.AreEqual(expected, actual);

            v        = new double[] { 0, 2, 3, 0, 0, 6 };
            expected = Sparse.FromDense(v);
            s        = expected.ToString();
            actual   = Sparse.Parse(s);
            Assert.AreEqual(expected, actual);


            v        = new double[] { 1, 2, 3, 0, 0, 6 };
            expected = Sparse.FromDense(v);
            s        = expected.ToString();
            actual   = Sparse.Parse(s, insertValueAtBeginning: 0);
            Assert.AreEqual(expected, actual);

            v        = new double[] { 0, 2, 3, 0, 0, 6 };
            expected = Sparse.FromDense(v);
            s        = expected.ToString();
            actual   = Sparse.Parse(s, insertValueAtBeginning: 0);
            Assert.AreEqual(expected, actual);



            v        = new double[] { 1, 2, 3, 0, 0, 6 };
            expected = Sparse.FromDense(v);
            s        = expected.ToString();
            actual   = Sparse.Parse(s, insertValueAtBeginning: 1);
            expected = Sparse.Parse("1:1 2:1 3:2 4:3 7:6");
            Assert.AreEqual(expected, actual);

            v        = new double[] { 0, 2, 3, 0, 0, 6 };
            expected = Sparse.FromDense(v);
            s        = expected.ToString();
            actual   = Sparse.Parse(s, insertValueAtBeginning: 42);
            expected = Sparse.Parse("1:42 3:2 4:3 7:6");
            Assert.AreEqual(expected, actual);
        }
        public static void SparseTest2 <Ta, Tb>(Ta sparseKernel, Tb denseKernel)
            where Ta : IDistance, IKernel, IKernel <Sparse <double> >, IDistance <Sparse <double> >
            where Tb : IDistance, IKernel
        {
            Sparse <double>[] sparse =
            {
                Sparse.Parse("109:1 389:1 429:1 488:1 538:1 566:1 598:2 659:1 741:1 1240:1 1302:1 1568:1 1962:1 2122:1 2362:1 2433:1 2474:2 2539:2 2638:1 2812:1 2871:1 2930:1 2960:1 3158:1 4468:1 5023:1 5520:1 6894:1 7076:1 7369:1 9062:2 9086:1 9422:1 10004:1 11187:2 13191:1 13384:1 14131:1 14196:1 14219:1 14608:1 15472:1 16018:2 17370:1 18603:1 18604:2 18605:2 18606:2"),
                Sparse.Parse("77:9 137:1 248:1 271:1 357:1 377:3 400:1 412:1 678:1 824:1 880:16 955:1 960:1 970:3 971:2 972:1 1007:2 1035:2 1166:1 1304:16 1354:1 1628:1 1686:2 1721:1 1877:1 2025:1 2219:1 2497:2 2539:4 2550:1 2874:1 2930:2 3378:1 3411:1 3572:17 3626:2 3688:1 3818:1 4039:1 4479:1 4526:2 4748:1 4822:1 4966:1 5479:1 5562:1 5583:2 5844:2 5848:1 6092:1 6096:1 6149:1 6268:1 6819:1 6894:18 7822:1 8139:1 8626:10 9824:17 10461:1 10609:2 10851:1 11463:1 11874:1 11875:2 12397:16 12412:1 13381:1 13384:1 13590:12 13755:2 14074:17 14166:1 14184:2 14517:1 14904:2 15400:1 15531:1 15579:17 15580:16 15936:16 16488:1 16579:1 16642:29 16793:2 17083:2 17458:1 20543:1 20544:2 21252:1 21358:2 22296:1 22479:3 23590:1 25024:17 25809:1 26235:1 26236:2 26237:1 26238:1 26239:1 26240:1 26241:2"),
            };

            double[][] dense =
            {
                sparse[0].ToDense(30000),
                sparse[1].ToDense(30000),
            };

            for (int i = 0; i < sparse.Length; i++)
            {
                for (int j = 0; j < sparse.Length; j++)
                {
                    double expected = denseKernel.Function(dense[i], dense[j]);
                    double actual   = sparseKernel.Function(sparse[i], sparse[j]);

                    Assert.AreEqual(expected, actual);
                }
            }

            for (int i = 0; i < sparse.Length; i++)
            {
                for (int j = 0; j < sparse.Length; j++)
                {
                    double expected = denseKernel.Distance(dense[i], dense[j]);
                    double actual   = sparseKernel.Distance(sparse[i], sparse[j]);

                    Assert.AreEqual(expected, actual);
                }
            }
        }