Save() public method

Saves this model to disk using LibSVM's model format.
public Save ( Stream stream ) : void
stream Stream The stream where the file should be written.
return void
コード例 #1
0
        public void WriteLinearMachineTest()
        {
            MemoryStream destination = new MemoryStream();

            var model = new LibSvmModel()
            {
                 Bias = -1,
                 Classes = 2, 
                 Dimension = 123,
                 Labels = new[] { +1, -1 },
                 Solver = LibSvmSolverType.L1RegularizedLogisticRegression,
                 Weights = a9a_weights
            };

            model.Save(destination);

            destination.Seek(0, SeekOrigin.Begin);
            TextReader textReader = new StreamReader(destination);
            string[] actual = textReader.ReadToEnd()
                .Split(new[] { Environment.NewLine }, StringSplitOptions.None);

            string[] expected = Resources.L1R_LR_a9a
                .Split(new[] { "\r\n" }, StringSplitOptions.None);

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

            for (int i = 0; i < 7; i++)
                Assert.AreEqual(expected[i], actual[i]);

            for (int i = 6; i < expected.Length; i++)
            {
                if (expected[i] == actual[i])
                    continue;

                double a = Double.Parse(expected[i], CultureInfo.InvariantCulture);
                double b = Double.Parse(actual[i], CultureInfo.InvariantCulture);

                Assert.AreEqual(a, b);
            }

            Assert.AreEqual(expected[expected.Length - 1], String.Empty);
        }