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); }
/// <summary> /// Creates a <see cref="LibSvmModel"/> from an existing <see cref="SupportVectorMachine{Linear}"/>. /// </summary> /// /// <param name="svm">The vector machine from which a libSVM model definition should be created.</param> /// /// <returns> /// A <see cref="LibSvmModel"/> class representing a support vector machine in LibSVM format. /// </returns> /// public static LibSvmModel FromMachine(SupportVectorMachine <Linear> svm) { var model = new LibSvmModel() { Solver = LibSvmSolverType.Unknown, Bias = -1, NumberOfClasses = svm.NumberOfClasses, NumberOfInputs = svm.NumberOfInputs + 1, Labels = new[] { 1, -1 } }; if (svm.SupportVectors.Length == 1) { model.Weights = svm.Threshold.Concatenate(svm.SupportVectors[0]); } else { model.Weights = svm.Weights; model.Vectors = svm.SupportVectors.InsertColumn(svm.Threshold, index: 0); } return(model); }
/// <summary> /// Loads a model specified using LibSVM's model format from a stream. /// </summary> /// /// <param name="stream">The stream from where the model should be loaded.</param> /// /// <returns>The <see cref="LibSvmModel"/> stored on <paramref name="stream"/>.</returns> /// public static LibSvmModel Load(Stream stream) { LibSvmModel model = new LibSvmModel(); using (StreamReader reader = new StreamReader(stream)) { while (!reader.EndOfStream) { string line = reader.ReadLine(); string[] words = line.Split(' '); if (words[0] == "solver_type") { string key = words[1].ToUpperInvariant(); if (!model.map.TryGetValue(key, out model.type)) { model.type = LibSvmSolverType.Unknown; } } else if (words[0] == "nr_class") { model.Classes = Int32.Parse(words[1]); } else if (words[0] == "nr_feature") { model.Dimension = Int32.Parse(words[1]); } else if (words[0] == "bias") { model.Bias = Double.Parse(words[1], CultureInfo.InvariantCulture); } else if (words[0] == "w") { break; } else if (words[0] == "label") { model.Labels = new int[words.Length - 1]; for (int i = 1; i < words.Length; i++) { model.Labels[i - 1] = Int32.Parse(words[i]); } } else { System.Diagnostics.Trace.WriteLine("Unknown field: " + words[0]); } } List <double> weights = new List <double>(); while (!reader.EndOfStream) { string line = reader.ReadLine(); weights.Add(Double.Parse(line, CultureInfo.InvariantCulture)); } model.Weights = weights.ToArray(); } return(model); }
/// <summary> /// Loads a model specified using LibSVM's model format from a stream. /// </summary> /// /// <param name="stream">The stream from where the model should be loaded.</param> /// /// <returns>The <see cref="LibSvmModel"/> stored on <paramref name="stream"/>.</returns> /// public static LibSvmModel Load(Stream stream) { LibSvmModel model = new LibSvmModel(); using (StreamReader reader = new StreamReader(stream)) { while (!reader.EndOfStream) { string line = reader.ReadLine(); string[] words = line.Split(' '); if (words[0] == "solver_type") { string key = words[1].ToUpperInvariant(); if (!model.map.TryGetValue(key, out model.type)) model.type = LibSvmSolverType.Unknown; } else if (words[0] == "nr_class") model.Classes = Int32.Parse(words[1]); else if (words[0] == "nr_feature") model.Dimension = Int32.Parse(words[1]); else if (words[0] == "bias") model.Bias = Double.Parse(words[1], CultureInfo.InvariantCulture); else if (words[0] == "w") break; else if (words[0] == "label") { model.Labels = new int[words.Length - 1]; for (int i = 1; i < words.Length; i++) model.Labels[i - 1] = Int32.Parse(words[i]); } else { System.Diagnostics.Trace.WriteLine("Unknown field: " + words[0]); } } List<double> weights = new List<double>(); while (!reader.EndOfStream) { string line = reader.ReadLine(); weights.Add(Double.Parse(line, CultureInfo.InvariantCulture)); } model.Weights = weights.ToArray(); } return model; }