/// <summary> /// Reads a Range transform from a stream. /// </summary> /// <param name="stream">The stream to read from</param> /// <returns>The Range transform</returns> public static RangeTransform Read(Stream stream) { TemporaryCulture.Start(); StreamReader input = new StreamReader(stream); int length = int.Parse(input.ReadLine()); double[] inputStart = new double[length]; double[] inputScale = new double[length]; string[] parts = input.ReadLine().Split(); for (int i = 0; i < length; i++) { inputStart[i] = double.Parse(parts[i]); } parts = input.ReadLine().Split(); for (int i = 0; i < length; i++) { inputScale[i] = double.Parse(parts[i]); } parts = input.ReadLine().Split(); double outputStart = double.Parse(parts[0]); double outputScale = double.Parse(parts[1]); TemporaryCulture.Stop(); return(new RangeTransform(inputStart, inputScale, outputStart, outputScale, length)); }
/// <summary> /// Reads a problem from a stream. /// </summary> /// <param name="stream">Stream to read from</param> /// <returns>The problem</returns> public static Problem Read(Stream stream) { TemporaryCulture.Start(); StreamReader input = new StreamReader(stream); List <double> vy = new List <double>(); List <Node[]> vx = new List <Node[]>(); int max_index = 0; while (input.Peek() > -1) { string[] parts = input.ReadLine().Trim().Split(); vy.Add(double.Parse(parts[0])); int m = parts.Length - 1; Node[] x = new Node[m]; for (int j = 0; j < m; j++) { x[j] = new Node(); string[] nodeParts = parts[j + 1].Split(':'); x[j].Index = int.Parse(nodeParts[0]); x[j].Value = double.Parse(nodeParts[1]); } if (m > 0) { max_index = System.Math.Max(max_index, x[m - 1].Index); } vx.Add(x); } TemporaryCulture.Stop(); return(new Problem(vy.Count, vy.ToArray(), vx.ToArray(), max_index)); }
/// <summary> /// Saves the transform to the disk. The samples are not stored, only the /// statistics. /// </summary> /// <param name="stream">The destination stream</param> /// <param name="transform">The transform</param> public static void Write(Stream stream, GaussianTransform transform) { TemporaryCulture.Start(); StreamWriter output = new StreamWriter(stream); output.WriteLine(transform._means.Length); for (int i = 0; i < transform._means.Length; i++) { output.WriteLine("{0} {1}", transform._means[i], transform._stddevs[i]); } output.Flush(); TemporaryCulture.Stop(); }
/// <summary> /// Writes a problem to a stream. /// </summary> /// <param name="stream">The stream to write the problem to.</param> /// <param name="problem">The problem to write.</param> public static void Write(Stream stream, Problem problem) { TemporaryCulture.Start(); StreamWriter output = new StreamWriter(stream); for (int i = 0; i < problem.Count; i++) { output.Write(problem.Y[i]); for (int j = 0; j < problem.X[i].Length; j++) { output.Write(" {0}:{1}", problem.X[i][j].Index, problem.X[i][j].Value); } output.WriteLine(); } output.Flush(); TemporaryCulture.Stop(); }
/// <summary> /// Reads a GaussianTransform from the provided stream. /// </summary> /// <param name="stream">The source stream</param> /// <returns>The transform</returns> public static GaussianTransform Read(Stream stream) { TemporaryCulture.Start(); StreamReader input = new StreamReader(stream); int length = int.Parse(input.ReadLine(), CultureInfo.InvariantCulture); double[] means = new double[length]; double[] stddevs = new double[length]; for (int i = 0; i < length; i++) { string[] parts = input.ReadLine().Split(); means[i] = double.Parse(parts[0], CultureInfo.InvariantCulture); stddevs[i] = double.Parse(parts[1], CultureInfo.InvariantCulture); } TemporaryCulture.Stop(); return(new GaussianTransform(means, stddevs)); }
/// <summary> /// Writes this Range transform to a stream. /// </summary> /// <param name="stream">The stream to write to</param> /// <param name="r">The range to write</param> public static void Write(Stream stream, RangeTransform r) { TemporaryCulture.Start(); StreamWriter output = new StreamWriter(stream); output.WriteLine(r._length); output.Write(r._inputStart[0]); for (int i = 1; i < r._inputStart.Length; i++) { output.Write(" " + r._inputStart[i]); } output.WriteLine(); output.Write(r._inputScale[0]); for (int i = 1; i < r._inputScale.Length; i++) { output.Write(" " + r._inputScale[i]); } output.WriteLine(); output.WriteLine("{0} {1}", r._outputStart, r._outputScale); output.Flush(); TemporaryCulture.Stop(); }
/// <summary> /// Writes a model to the provided stream. /// </summary> /// <param name="stream">The output stream</param> /// <param name="model">The model to write</param> public static void Write(Stream stream, Model model) { TemporaryCulture.Start(); StreamWriter output = new StreamWriter(stream); Parameter param = model.Parameter; output.Write("svm_type " + param.SvmType + "\n"); output.Write("kernel_type " + param.KernelType + "\n"); if (param.KernelType == KernelType.POLY) { output.Write("degree " + param.Degree + "\n"); } if (param.KernelType == KernelType.POLY || param.KernelType == KernelType.RBF || param.KernelType == KernelType.SIGMOID) { output.Write("gamma " + param.Gamma + "\n"); } if (param.KernelType == KernelType.POLY || param.KernelType == KernelType.SIGMOID) { output.Write("coef0 " + param.Coefficient0 + "\n"); } int nr_class = model.NumberOfClasses; int l = model.SupportVectorCount; output.Write("nr_class " + nr_class + "\n"); output.Write("total_sv " + l + "\n"); { output.Write("rho"); for (int i = 0; i < nr_class * (nr_class - 1) / 2; i++) { output.Write(" " + model.Rho[i]); } output.Write("\n"); } if (model.ClassLabels != null) { output.Write("label"); for (int i = 0; i < nr_class; i++) { output.Write(" " + model.ClassLabels[i]); } output.Write("\n"); } if (model.PairwiseProbabilityA != null) // regression has probA only { output.Write("probA"); for (int i = 0; i < nr_class * (nr_class - 1) / 2; i++) { output.Write(" " + model.PairwiseProbabilityA[i]); } output.Write("\n"); } if (model.PairwiseProbabilityB != null) { output.Write("probB"); for (int i = 0; i < nr_class * (nr_class - 1) / 2; i++) { output.Write(" " + model.PairwiseProbabilityB[i]); } output.Write("\n"); } if (model.NumberOfSVPerClass != null) { output.Write("nr_sv"); for (int i = 0; i < nr_class; i++) { output.Write(" " + model.NumberOfSVPerClass[i]); } output.Write("\n"); } output.Write("SV\n"); double[][] sv_coef = model.SupportVectorCoefficients; Node[][] SV = model.SupportVectors; for (int i = 0; i < l; i++) { for (int j = 0; j < nr_class - 1; j++) { output.Write(sv_coef[j][i] + " "); } Node[] p = SV[i]; if (p.Length == 0) { output.WriteLine(); continue; } if (param.KernelType == KernelType.PRECOMPUTED) { output.Write("0:{0}", (int)p[0].Value); } else { output.Write("{0}:{1}", p[0].Index, p[0].Value); for (int j = 1; j < p.Length; j++) { output.Write(" {0}:{1}", p[j].Index, p[j].Value); } } output.WriteLine(); } output.Flush(); TemporaryCulture.Stop(); }
/// <summary> /// Reads a Model from the provided stream. /// </summary> /// <param name="stream">The stream from which to read the Model.</param> /// <returns>the Model</returns> public static Model Read(Stream stream) { TemporaryCulture.Start(); StreamReader input = new StreamReader(stream); // read parameters Model model = new Model(); Parameter param = new Parameter(); model.Parameter = param; model.Rho = null; model.PairwiseProbabilityA = null; model.PairwiseProbabilityB = null; model.ClassLabels = null; model.NumberOfSVPerClass = null; bool headerFinished = false; while (!headerFinished) { string line = input.ReadLine(); string cmd, arg; int splitIndex = line.IndexOf(' '); if (splitIndex >= 0) { cmd = line.Substring(0, splitIndex); arg = line.Substring(splitIndex + 1); } else { cmd = line; arg = ""; } arg = arg.ToLower(); int i, n; switch (cmd) { case "svm_type": param.SvmType = (SvmType)Enum.Parse(typeof(SvmType), arg.ToUpper()); break; case "kernel_type": param.KernelType = (KernelType)Enum.Parse(typeof(KernelType), arg.ToUpper()); break; case "degree": param.Degree = int.Parse(arg); break; case "gamma": param.Gamma = double.Parse(arg); break; case "coef0": param.Coefficient0 = double.Parse(arg); break; case "nr_class": model.NumberOfClasses = int.Parse(arg); break; case "total_sv": model.SupportVectorCount = int.Parse(arg); break; case "rho": n = model.NumberOfClasses * (model.NumberOfClasses - 1) / 2; model.Rho = new double[n]; string[] rhoParts = arg.Split(); for (i = 0; i < n; i++) { model.Rho[i] = double.Parse(rhoParts[i]); } break; case "label": n = model.NumberOfClasses; model.ClassLabels = new int[n]; string[] labelParts = arg.Split(); for (i = 0; i < n; i++) { model.ClassLabels[i] = int.Parse(labelParts[i]); } break; case "probA": n = model.NumberOfClasses * (model.NumberOfClasses - 1) / 2; model.PairwiseProbabilityA = new double[n]; string[] probAParts = arg.Split(); for (i = 0; i < n; i++) { model.PairwiseProbabilityA[i] = double.Parse(probAParts[i]); } break; case "probB": n = model.NumberOfClasses * (model.NumberOfClasses - 1) / 2; model.PairwiseProbabilityB = new double[n]; string[] probBParts = arg.Split(); for (i = 0; i < n; i++) { model.PairwiseProbabilityB[i] = double.Parse(probBParts[i]); } break; case "nr_sv": n = model.NumberOfClasses; model.NumberOfSVPerClass = new int[n]; string[] nrsvParts = arg.Split(); for (i = 0; i < n; i++) { model.NumberOfSVPerClass[i] = int.Parse(nrsvParts[i]); } break; case "SV": headerFinished = true; break; default: throw new Exception("Unknown text in model file"); } } // read sv_coef and SV int m = model.NumberOfClasses - 1; int l = model.SupportVectorCount; model.SupportVectorCoefficients = new double[m][]; for (int i = 0; i < m; i++) { model.SupportVectorCoefficients[i] = new double[l]; } model.SupportVectors = new Node[l][]; for (int i = 0; i < l; i++) { string[] parts = input.ReadLine().Trim().Split(); for (int k = 0; k < m; k++) { model.SupportVectorCoefficients[k][i] = double.Parse(parts[k]); } int n = parts.Length - m; model.SupportVectors[i] = new Node[n]; for (int j = 0; j < n; j++) { string[] nodeParts = parts[m + j].Split(':'); model.SupportVectors[i][j] = new Node(); model.SupportVectors[i][j].Index = int.Parse(nodeParts[0]); model.SupportVectors[i][j].Value = double.Parse(nodeParts[1]); } } TemporaryCulture.Stop(); return(model); }