Example #1
0
        public object Clone()
        {
            OneClassSVM clone = new OneClassSVM();

            clone.copy(this);
            return(clone);
        }
Example #2
0
 public void copy(OneClassSVM rhs2)
 {
     param            = rhs2.param == null ? null : (SVMParam)rhs2.param.Clone();
     cross_validation = rhs2.cross_validation;
     quiet            = rhs2.quiet;
     _threshold       = rhs2._threshold;
     model            = rhs2.model == null ? null : (SVMModel)rhs2.model.Clone();
     if (model != null)
     {
         model.Param = param;
     }
 }
Example #3
0
        public static void SolveOutlierDetectionProblem()
        {
            List <KeyValuePair <double[], double> > trainingBatch = new List <KeyValuePair <double[], double> >();

            // add some normal data
            for (int i = 0; i < 100; ++i)
            {
                trainingBatch.Add(new KeyValuePair <double[], double>(new double[] { randn() * 0.3 + 2, randn() * 0.3 + 2 }, -1.0));
                trainingBatch.Add(new KeyValuePair <double[], double>(new double[] { randn() * 0.3 - 2, randn() * 0.3 - 2 }, -1.0));
            }

            List <KeyValuePair <double[], double> > crossValidationBatch = new List <KeyValuePair <double[], double> >();

            // add some validation data
            for (int i = 0; i < 20; ++i)
            {
                crossValidationBatch.Add(new KeyValuePair <double[], double>(new double[] { randn() * 0.3 + 2, randn() * 0.3 + 2 }, -1.0));
                crossValidationBatch.Add(new KeyValuePair <double[], double>(new double[] { randn() * 0.3 - 2, randn() * 0.3 - 2 }, -1.0));
            }

            List <KeyValuePair <double[], double> > outliers = new List <KeyValuePair <double[], double> >();

            // add some outliers data
            for (int i = 0; i < 20; ++i)
            {
                outliers.Add(new KeyValuePair <double[], double>(new double[] { rand(-4, 4), rand(-4, 4) }, 1.0));
                outliers.Add(new KeyValuePair <double[], double>(new double[] { rand(-4, 4), rand(-4, 4) }, 1.0));
            }

            OneClassSVM algorithm = new OneClassSVM();

            algorithm.set_gamma(0.1);
            algorithm.set_nu(0.1);

            var batches = from p in trainingBatch
                          select p.Key;

            algorithm.Fit(batches.ToList());

            for (int i = 0; i < crossValidationBatch.Count; ++i)
            {
                double predicted = algorithm.IsOutlier(crossValidationBatch[i].Key) ? 1.0 : -1.0;
                Console.WriteLine("predicted: " + predicted + "\texpected: " + crossValidationBatch[i].Value);
            }

            for (int i = 0; i < outliers.Count; ++i)
            {
                double predicted = algorithm.IsOutlier(crossValidationBatch[i].Key) ? 1.0 : -1.0;
                Console.WriteLine("predicted: " + predicted + "\texpected: " + outliers[i].Value);
            }
        }