Exemplo n.º 1
0
        public static RangeTransform Read(Stream stream)
        {
            TemporaryCulture.Start();
            StreamReader streamReader = new StreamReader(stream);
            int          num          = int.Parse(streamReader.ReadLine());

            double[] array  = new double[num];
            double[] array2 = new double[num];
            string[] array3 = streamReader.ReadLine().Split();
            for (int i = 0; i < num; i++)
            {
                array[i] = double.Parse(array3[i]);
            }
            array3 = streamReader.ReadLine().Split();
            for (int j = 0; j < num; j++)
            {
                array2[j] = double.Parse(array3[j]);
            }
            array3 = streamReader.ReadLine().Split();
            double outputStart = double.Parse(array3[0]);
            double outputScale = double.Parse(array3[1]);

            TemporaryCulture.Stop();
            return(new RangeTransform(array, array2, outputStart, outputScale, num));
        }
Exemplo n.º 2
0
        public static Problem Read(Stream stream)
        {
            TemporaryCulture.Start();
            StreamReader  streamReader = new StreamReader(stream);
            List <double> list         = new List <double>();
            List <Node[]> list2        = new List <Node[]>();
            int           num          = 0;

            while (streamReader.Peek() > -1)
            {
                string[] array = streamReader.ReadLine().Trim().Split();
                list.Add(double.Parse(array[0]));
                int    num2   = array.Length - 1;
                Node[] array2 = new Node[num2];
                for (int i = 0; i < num2; i++)
                {
                    array2[i] = new Node();
                    string[] array3 = array[i + 1].Split(':');
                    array2[i].Index = int.Parse(array3[0]);
                    array2[i].Value = double.Parse(array3[1]);
                }
                if (num2 > 0)
                {
                    num = Math.Max(num, array2[num2 - 1].Index);
                }
                list2.Add(array2);
            }
            TemporaryCulture.Stop();
            return(new Problem(list.Count, list.ToArray(), list2.ToArray(), num));
        }
Exemplo n.º 3
0
        public static void Write(Stream stream, GaussianTransform transform)
        {
            TemporaryCulture.Start();
            StreamWriter streamWriter = new StreamWriter(stream);

            streamWriter.WriteLine(transform._means.Length);
            for (int i = 0; i < transform._means.Length; i++)
            {
                streamWriter.WriteLine("{0} {1}", transform._means[i], transform._stddevs[i]);
            }
            streamWriter.Flush();
            TemporaryCulture.Stop();
        }
Exemplo n.º 4
0
        public static GaussianTransform Read(Stream stream)
        {
            TemporaryCulture.Start();
            StreamReader streamReader = new StreamReader(stream);
            int          num          = int.Parse(streamReader.ReadLine(), CultureInfo.InvariantCulture);

            double[] array  = new double[num];
            double[] array2 = new double[num];
            for (int i = 0; i < num; i++)
            {
                string[] array3 = streamReader.ReadLine().Split();
                array[i]  = double.Parse(array3[0], CultureInfo.InvariantCulture);
                array2[i] = double.Parse(array3[1], CultureInfo.InvariantCulture);
            }
            TemporaryCulture.Stop();
            return(new GaussianTransform(array, array2));
        }
Exemplo n.º 5
0
        public static void Write(Stream stream, Problem problem)
        {
            TemporaryCulture.Start();
            StreamWriter streamWriter = new StreamWriter(stream);

            for (int i = 0; i < problem.Count; i++)
            {
                streamWriter.Write(problem.Y[i]);
                for (int j = 0; j < problem.X[i].Length; j++)
                {
                    streamWriter.Write(" {0}:{1}", problem.X[i][j].Index, problem.X[i][j].Value);
                }
                streamWriter.WriteLine();
            }
            streamWriter.Flush();
            TemporaryCulture.Stop();
        }
Exemplo n.º 6
0
        public static void Write(Stream stream, RangeTransform r)
        {
            TemporaryCulture.Start();
            StreamWriter streamWriter = new StreamWriter(stream);

            streamWriter.WriteLine(r._length);
            streamWriter.Write(r._inputStart[0]);
            for (int i = 1; i < r._inputStart.Length; i++)
            {
                streamWriter.Write(" " + r._inputStart[i]);
            }
            streamWriter.WriteLine();
            streamWriter.Write(r._inputScale[0]);
            for (int j = 1; j < r._inputScale.Length; j++)
            {
                streamWriter.Write(" " + r._inputScale[j]);
            }
            streamWriter.WriteLine();
            streamWriter.WriteLine("{0} {1}", r._outputStart, r._outputScale);
            streamWriter.Flush();
            TemporaryCulture.Stop();
        }
Exemplo n.º 7
0
        public static void Write(Stream stream, Model model)
        {
            TemporaryCulture.Start();
            StreamWriter streamWriter = new StreamWriter(stream);
            Parameter    parameter    = model.Parameter;

            streamWriter.Write("svm_type " + parameter.SvmType + "\n");
            streamWriter.Write("kernel_type " + parameter.KernelType + "\n");
            if (parameter.KernelType == KernelType.POLY)
            {
                streamWriter.Write("degree " + parameter.Degree + "\n");
            }
            if (parameter.KernelType == KernelType.POLY || parameter.KernelType == KernelType.RBF || parameter.KernelType == KernelType.SIGMOID)
            {
                streamWriter.Write("gamma " + parameter.Gamma + "\n");
            }
            if (parameter.KernelType == KernelType.POLY || parameter.KernelType == KernelType.SIGMOID)
            {
                streamWriter.Write("coef0 " + parameter.Coefficient0 + "\n");
            }
            int numberOfClasses    = model.NumberOfClasses;
            int supportVectorCount = model.SupportVectorCount;

            streamWriter.Write("nr_class " + numberOfClasses + "\n");
            streamWriter.Write("total_sv " + supportVectorCount + "\n");
            streamWriter.Write("rho");
            for (int i = 0; i < numberOfClasses * (numberOfClasses - 1) / 2; i++)
            {
                streamWriter.Write(" " + model.Rho[i]);
            }
            streamWriter.Write("\n");
            if (model.ClassLabels != null)
            {
                streamWriter.Write("label");
                for (int j = 0; j < numberOfClasses; j++)
                {
                    streamWriter.Write(" " + model.ClassLabels[j]);
                }
                streamWriter.Write("\n");
            }
            if (model.PairwiseProbabilityA != null)
            {
                streamWriter.Write("probA");
                for (int k = 0; k < numberOfClasses * (numberOfClasses - 1) / 2; k++)
                {
                    streamWriter.Write(" " + model.PairwiseProbabilityA[k]);
                }
                streamWriter.Write("\n");
            }
            if (model.PairwiseProbabilityB != null)
            {
                streamWriter.Write("probB");
                for (int l = 0; l < numberOfClasses * (numberOfClasses - 1) / 2; l++)
                {
                    streamWriter.Write(" " + model.PairwiseProbabilityB[l]);
                }
                streamWriter.Write("\n");
            }
            if (model.NumberOfSVPerClass != null)
            {
                streamWriter.Write("nr_sv");
                for (int m = 0; m < numberOfClasses; m++)
                {
                    streamWriter.Write(" " + model.NumberOfSVPerClass[m]);
                }
                streamWriter.Write("\n");
            }
            streamWriter.Write("SV\n");
            double[][] supportVectorCoefficients = model.SupportVectorCoefficients;
            Node[][]   supportVectors            = model.SupportVectors;
            for (int n = 0; n < supportVectorCount; n++)
            {
                for (int num = 0; num < numberOfClasses - 1; num++)
                {
                    streamWriter.Write(supportVectorCoefficients[num][n] + " ");
                }
                Node[] array = supportVectors[n];
                if (array.Length == 0)
                {
                    streamWriter.WriteLine();
                }
                else
                {
                    if (parameter.KernelType == KernelType.PRECOMPUTED)
                    {
                        streamWriter.Write("0:{0}", (int)array[0].Value);
                    }
                    else
                    {
                        streamWriter.Write("{0}:{1}", array[0].Index, array[0].Value);
                        for (int num2 = 1; num2 < array.Length; num2++)
                        {
                            streamWriter.Write(" {0}:{1}", array[num2].Index, array[num2].Value);
                        }
                    }
                    streamWriter.WriteLine();
                }
            }
            streamWriter.Flush();
            TemporaryCulture.Stop();
        }
Exemplo n.º 8
0
        public static Model Read(Stream stream)
        {
            TemporaryCulture.Start();
            StreamReader streamReader = new StreamReader(stream);
            Model        model        = new Model();
            Parameter    parameter2   = model.Parameter = new Parameter();

            model.Rho = null;
            model.PairwiseProbabilityA = null;
            model.PairwiseProbabilityB = null;
            model.ClassLabels          = null;
            model.NumberOfSVPerClass   = null;
            bool flag = false;

            while (!flag)
            {
                string text = streamReader.ReadLine();
                int    num  = text.IndexOf(' ');
                string text2;
                string text3;
                if (num >= 0)
                {
                    text2 = text.Substring(0, num);
                    text3 = text.Substring(num + 1);
                }
                else
                {
                    text2 = text;
                    text3 = "";
                }
                text3 = text3.ToLower();
                switch (text2)
                {
                case "svm_type":
                    parameter2.SvmType = (SvmType)Enum.Parse(typeof(SvmType), text3.ToUpper());
                    break;

                case "kernel_type":
                    parameter2.KernelType = (KernelType)Enum.Parse(typeof(KernelType), text3.ToUpper());
                    break;

                case "degree":
                    parameter2.Degree = int.Parse(text3);
                    break;

                case "gamma":
                    parameter2.Gamma = double.Parse(text3);
                    break;

                case "coef0":
                    parameter2.Coefficient0 = double.Parse(text3);
                    break;

                case "nr_class":
                    model.NumberOfClasses = int.Parse(text3);
                    break;

                case "total_sv":
                    model.SupportVectorCount = int.Parse(text3);
                    break;

                case "rho":
                {
                    int numberOfClasses = model.NumberOfClasses * (model.NumberOfClasses - 1) / 2;
                    model.Rho = new double[numberOfClasses];
                    string[] array5 = text3.Split();
                    for (int i = 0; i < numberOfClasses; i++)
                    {
                        model.Rho[i] = double.Parse(array5[i]);
                    }
                    break;
                }

                case "label":
                {
                    int numberOfClasses = model.NumberOfClasses;
                    model.ClassLabels = new int[numberOfClasses];
                    string[] array4 = text3.Split();
                    for (int i = 0; i < numberOfClasses; i++)
                    {
                        model.ClassLabels[i] = int.Parse(array4[i]);
                    }
                    break;
                }

                case "probA":
                {
                    int numberOfClasses = model.NumberOfClasses * (model.NumberOfClasses - 1) / 2;
                    model.PairwiseProbabilityA = new double[numberOfClasses];
                    string[] array3 = text3.Split();
                    for (int i = 0; i < numberOfClasses; i++)
                    {
                        model.PairwiseProbabilityA[i] = double.Parse(array3[i]);
                    }
                    break;
                }

                case "probB":
                {
                    int numberOfClasses = model.NumberOfClasses * (model.NumberOfClasses - 1) / 2;
                    model.PairwiseProbabilityB = new double[numberOfClasses];
                    string[] array2 = text3.Split();
                    for (int i = 0; i < numberOfClasses; i++)
                    {
                        model.PairwiseProbabilityB[i] = double.Parse(array2[i]);
                    }
                    break;
                }

                case "nr_sv":
                {
                    int numberOfClasses = model.NumberOfClasses;
                    model.NumberOfSVPerClass = new int[numberOfClasses];
                    string[] array = text3.Split();
                    for (int i = 0; i < numberOfClasses; i++)
                    {
                        model.NumberOfSVPerClass[i] = int.Parse(array[i]);
                    }
                    break;
                }

                case "SV":
                    flag = true;
                    break;

                default:
                    throw new Exception("Unknown text in model file");
                }
            }
            int num2 = model.NumberOfClasses - 1;
            int supportVectorCount = model.SupportVectorCount;

            model.SupportVectorCoefficients = new double[num2][];
            for (int j = 0; j < num2; j++)
            {
                model.SupportVectorCoefficients[j] = new double[supportVectorCount];
            }
            model.SupportVectors = new Node[supportVectorCount][];
            for (int k = 0; k < supportVectorCount; k++)
            {
                string[] array6 = streamReader.ReadLine().Trim().Split();
                for (int l = 0; l < num2; l++)
                {
                    model.SupportVectorCoefficients[l][k] = double.Parse(array6[l]);
                }
                int num3 = array6.Length - num2;
                model.SupportVectors[k] = new Node[num3];
                for (int m = 0; m < num3; m++)
                {
                    string[] array7 = array6[num2 + m].Split(':');
                    model.SupportVectors[k][m]       = new Node();
                    model.SupportVectors[k][m].Index = int.Parse(array7[0]);
                    model.SupportVectors[k][m].Value = double.Parse(array7[1]);
                }
            }
            TemporaryCulture.Stop();
            return(model);
        }