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;
}