Ejemplo n.º 1
0
        public override void Train(List <T> records)
        {
            HashSet <string> class_labels = new HashSet <string>();

            foreach (T rec in records)
            {
                class_labels.Add(rec.Label);
            }

            mClassFieldLabels = class_labels.ToList();
            mTheta.Clear();
            mKernelCentroids.Clear();

            int m = records.Count;

            if (m == 0)
            {
                throw new ArgumentException("sample count==0");
            }
            int dimension = records[0].Dimension;

            int[] Y = new int[m];

            for (int i = 0; i < m; ++i)
            {
                T rec = records[i];
                mKernelCentroids.Add((double[])rec.Data.Clone());
            }

            double[] theta_0 = new double[m + 1];
            foreach (string class_label in mClassFieldLabels)
            {
                for (int i = 0; i < m; ++i)
                {
                    T rec = records[i];
                    Y[i] = rec.Label == class_label ? 1 : 0;
                }
                for (int d = 0; d < dimension; ++d)
                {
                    theta_0[d] = 0;
                }


                KernelSVMCostFunction <T> f = new KernelSVMCostFunction <T>(records, Y, m, mKernel);
                f.C = mC;
                ContinuousSolution solution = mLocalSearcher.Minimize(theta_0, f, mMaxSolverIteration);
                mTheta[class_label] = solution.Values;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Method used to find the error in terms of SVM cost function
        /// </summary>
        /// <param name="data_set"></param>
        /// <returns></returns>
        public override double ComputeCost(List <T> data_set)
        {
            int sample_count = data_set.Count;

            int[] Y = new int[sample_count];

            double total_error = 0;

            foreach (string class_label in mClassFieldLabels)
            {
                for (int i = 0; i < sample_count; ++i)
                {
                    T rec = data_set[i];
                    Y[i] = rec.Label == class_label ? 1 : 0;
                }

                KernelSVMCostFunction <T> f = new KernelSVMCostFunction <T>(data_set, Y, sample_count, mKernel);
                f.C = mC;

                double error = f.Evaluate(mTheta[class_label]);
                total_error += error;
            }
            return(total_error);
        }