public void TestLabelEncoderFitTransform()
        {
            var le = new LabelEncoder<int>();
            var ret = le.FitTransform(new[] {1, 1, 4, 5, -1, 0});
            Assert.IsTrue(new[] {2, 2, 3, 4, 0, 1}.SequenceEqual(ret));

            var le1 = new LabelEncoder<string>();
            ret = le1.FitTransform(new[] {"paris", "paris", "tokyo", "amsterdam"});
            Assert.IsTrue(new[] {1, 1, 2, 0}.SequenceEqual(ret));
        }
Beispiel #2
0
        /// <summary>
        /// Compute sample weights such that the class distribution of y becomes
        /// balanced.
        /// </summary>
        /// <param name="?"></param>
        private static double[] BalanceWeights(int[] y)
        {
            var encoder = new LabelEncoder <int>();

            y = encoder.FitTransform(y);
            var bins = Np.BinCount(y);


            var weights = bins.ElementsAt(y).Select(v => 1.0 / v * bins.Min()).ToArray();

            return(weights);
        }
Beispiel #3
0
        public void TestLabelEncoderFitTransform()
        {
            var le  = new LabelEncoder <int>();
            var ret = le.FitTransform(new[] { 1, 1, 4, 5, -1, 0 });

            Assert.IsTrue(new[] { 2, 2, 3, 4, 0, 1 }.SequenceEqual(ret));

            var le1 = new LabelEncoder <string>();

            ret = le1.FitTransform(new[] { "paris", "paris", "tokyo", "amsterdam" });
            Assert.IsTrue(new[] { 1, 1, 2, 0 }.SequenceEqual(ret));
        }
 public void TestLabelEncoderStringLabels()
 {
     var le = new LabelEncoder<string>();
     le.Fit(new[] {"paris", "paris", "tokyo", "amsterdam"});
     Assert.IsTrue(new[] {"amsterdam", "paris", "tokyo"}.SequenceEqual(le.Classes));
     Assert.IsTrue(new[] {2, 2, 1}.SequenceEqual(le.Transform(new[] {"tokyo", "tokyo", "paris"})));
     Assert.IsTrue(new[] {"tokyo", "tokyo", "paris"}.SequenceEqual(le.InverseTransform(new[] {2, 2, 1})));
     
     try
     {
         le.Transform(new []{"london"});
         Assert.Fail("ArgumentException expected");
     }
     catch (Exception){}
 }
 public void TestLabelEncoder()
 {
     var le = new LabelEncoder<int>();
     le.Fit(new[] {1, 1, 4, 5, -1, 0});
     Assert.IsTrue(new[] {-1, 0, 1, 4, 5}.SequenceEqual(le.Classes));
     Assert.IsTrue(new []{1, 2, 3, 3, 4, 0, 0}.SequenceEqual(le.Transform(new []{0, 1, 4, 4, 5, -1, -1})));
     Assert.IsTrue(new[] {0, 1, 4, 4, 5, -1, -1}.SequenceEqual(le.InverseTransform(new[] {1, 2, 3, 3, 4, 0, 0})));
     try
     {
         le.Transform(new[] {0, 6});
         Assert.Fail("ArgumentException is expected");
     }
     catch (ArgumentException)
     {
     }
 }
Beispiel #6
0
        public void TestLabelEncoderStringLabels()
        {
            var le = new LabelEncoder <string>();

            le.Fit(new[] { "paris", "paris", "tokyo", "amsterdam" });
            Assert.IsTrue(new[] { "amsterdam", "paris", "tokyo" }.SequenceEqual(le.Classes));
            Assert.IsTrue(new[] { 2, 2, 1 }.SequenceEqual(le.Transform(new[] { "tokyo", "tokyo", "paris" })));
            Assert.IsTrue(new[] { "tokyo", "tokyo", "paris" }.SequenceEqual(le.InverseTransform(new[] { 2, 2, 1 })));

            try
            {
                le.Transform(new [] { "london" });
                Assert.Fail("ArgumentException expected");
            }
            catch (Exception) {}
        }
        public void TestLabelEncoderErrors()
        {
            var le = new LabelEncoder<int>();
            try
            {
                le.Transform(new int[0]);
                Assert.Fail("ArgumentException expected");
            }
            catch (Exception){}

            try
            {
                le.InverseTransform(new int[0]);
                Assert.Fail("ArgumentException expected");
            }
            catch (Exception){}
        }
Beispiel #8
0
        public void TestLabelEncoder()
        {
            var le = new LabelEncoder <int>();

            le.Fit(new[] { 1, 1, 4, 5, -1, 0 });
            Assert.IsTrue(new[] { -1, 0, 1, 4, 5 }.SequenceEqual(le.Classes));
            Assert.IsTrue(new [] { 1, 2, 3, 3, 4, 0, 0 }.SequenceEqual(le.Transform(new [] { 0, 1, 4, 4, 5, -1, -1 })));
            Assert.IsTrue(new[] { 0, 1, 4, 4, 5, -1, -1 }.SequenceEqual(le.InverseTransform(new[] { 1, 2, 3, 3, 4, 0, 0 })));
            try
            {
                le.Transform(new[] { 0, 6 });
                Assert.Fail("ArgumentException is expected");
            }
            catch (ArgumentException)
            {
            }
        }
Beispiel #9
0
        public void TestLabelEncoderErrors()
        {
            var le = new LabelEncoder <int>();

            try
            {
                le.Transform(new int[0]);
                Assert.Fail("ArgumentException expected");
            }
            catch (Exception) {}

            try
            {
                le.InverseTransform(new int[0]);
                Assert.Fail("ArgumentException expected");
            }
            catch (Exception) {}
        }
Beispiel #10
0
        /// <summary>
        /// Fit the model according to the given training data.
        /// </summary>
        /// <param name="x">[nSamples, nFeatures]. Training vectors,
        /// where nSamples is the number of samples and nFeatures
        /// is the number of features.</param>
        /// <param name="y">[nSamples] Target class labels.</param>
        /// <param name="sampleWeight">Sample weights.</param>
        /// <returns>Reference to itself.</returns>
        public virtual void Fit(Matrix <double> x, TLabel[] y, Vector <double> sampleWeight = null)
        {
            if (sampleWeight != null)
            {
                throw new ArgumentException("Sample weights are not supported by the classifier");
            }

            this.enc = new LabelEncoder <TLabel>();
            int[] y1 = this.enc.FitTransform(y);
            if (this.Classes.Length < 2)
            {
                throw new ArgumentException("The number of classes has to be greater than one.");
            }

            this.ClassWeight = (this.ClassWeightEstimator ?? ClassWeightEstimator <TLabel> .Auto)
                               .ComputeWeights(this.enc.Classes, y1)
                               .ToArray();

            base.Fit(x, DenseVector.OfEnumerable(y1.Select(Convert.ToDouble)));
        }
Beispiel #11
0
        /// <summary>
        /// Build a decision tree from the training set (X, y).
        /// </summary>
        /// <param name="x">[n_samples, n_features] The training input samples. </param>
        /// <param name="y"> [n_samples] The target values.</param>
        /// <param name="sampleWeight">[n_samples] or None
        /// Sample weights. If None, then samples are equally weighted. Splits
        /// that would create child nodes with net zero or negative weight are
        /// ignored while searching for a split in each node. In the case of
        /// classification, splits are also ignored if they would result in any
        /// single class carrying a negative weight in either child node.</param>
        /// <returns>
        /// Returns this.
        /// </returns>
        internal BaseDecisionTree <TLabel> FitClassification(
            Matrix <double> x,
            TLabel[] y,
            Vector <double> sampleWeight = null)
        {
            // Determine output settings
            int nSamples = x.RowCount;

            this.NFeatures = x.ColumnCount;

            if (y.Length != nSamples)
            {
                throw new ArgumentException(
                          string.Format(
                              "Number of labels={0} does not match number of samples={1}",
                              y.Length,
                              nSamples));
            }

            this.nOutputs = 1;
            int[] y_;

            this.ClassesInternal = new List <TLabel>();
            this.NClasses        = new List <uint>();

            var enc = new LabelEncoder <TLabel>();

            y_ = enc.FitTransform(y).ToArray();

            this.ClassesInternal.AddRange(enc.Classes);
            this.NClasses.Add((uint)enc.Classes.Length);

            var yMatrix = y_.Select(v => (double)v).ToArray().ToDenseVector().ToColumnMatrix();

            FitCommon(x, yMatrix, nSamples, sampleWeight, true);

            return(this);
        }
Beispiel #12
0
        /// <summary>
        /// Fit the model according to the given training data.
        /// </summary>
        /// <param name="x">shape = [n_samples, n_features]
        ///    Training vector, where n_samples in the number of samples and
        ///    n_features is the number of features.</param>
        /// <param name="y">shape = [n_samples]
        ///    Target vector relative to X</param>
        /// <param name="sampleWeight">Sample weights.</param>
        public void Fit(Matrix <double> x, TLabel[] y, Vector <double> sampleWeight = null)
        {
            if (sampleWeight != null)
            {
                throw new ArgumentException("Sample weights are not supported by the classifier");
            }

            Linear.random = this.random;

            this._enc = new LabelEncoder <TLabel>();
            int[] y1 = this._enc.FitTransform(y);
            if (this.Classes.Length < 2)
            {
                throw new ArgumentException("The number of classes has to be greater than one.");
            }

            Vector classWeight = this.ClassWeightEstimator.ComputeWeights(this.Classes, y1);

            if (x.RowCount != y.Length)
            {
                throw new ArgumentException(
                          string.Format("X and y have incompatible shapes.\n X has {0} samples, but y has {1}.",
                                        x.RowCount,
                                        y.Length));
            }

            if (this.verbose > 0)
            {
                Console.WriteLine("[LibLinear]");
            }

            Problem problem = new Problem();

            problem.bias = this.Bias;
            var samples = new List <Feature> [x.RowCount];

            for (int i = 0; i < samples.Length; i++)
            {
                samples[i] = new List <Feature>();
            }
            foreach (var i in x.IndexedEnumerator())
            {
                samples[i.Item1].Add(new Feature(i.Item2 + 1, i.Item3));
            }

            if (this.Bias > 0)
            {
                for (int i = 0; i < x.RowCount; i++)
                {
                    samples[i].Add(new Feature(x.ColumnCount + 1, this.Bias));
                }
            }

            problem.x = samples.Select(s => s.ToArray()).ToArray();
            problem.y = y1.Select(v => (double)v).ToArray();
            problem.l = x.RowCount;
            problem.n = this.Bias > 0 ? x.ColumnCount + 1 : x.ColumnCount;

            Parameter prm = new Parameter(this.solverType, this.C, this.tol);

            prm.weightLabel = Enumerable.Range(0, this.Classes.Length).ToArray();
            prm.weight      = classWeight.ToArray();

            this.model = Linear.train(problem, prm);

            //
            int nrClass   = model.getNrClass();
            int nrFeature = model.getNrFeature();

            if (Bias > 0)
            {
                nrFeature = nrFeature + 1;
            }

            Matrix <double> r;

            if (nrClass == 2)
            {
                r = DenseMatrix.OfColumnMajor(1, nrFeature, model.getFeatureWeights());
            }
            else
            {
                r = DenseMatrix.OfColumnMajor(nrClass, nrFeature, model.getFeatureWeights());
            }

            if (nrClass > 2)
            {
                var rClone = r.Clone();
                var labels = model.getLabels();
                for (int i = 0; i < labels.Length; i++)
                {
                    rClone.SetRow(labels[i], r.Row(i));
                }

                r = rClone;
            }
            else
            {
                r.MapInplace(v => v * -1);
            }

            if (this.FitIntercept)
            {
                this.Coef      = r.SubMatrix(0, r.RowCount, 0, r.ColumnCount - 1);
                this.Intercept = r.Column(r.ColumnCount - 1) * this.interceptScaling;
            }
            else
            {
                this.Coef      = r;
                this.Intercept = new DenseVector(r.RowCount);
            }
        }