private MultinomialLogitModel(MultinomialLogitModel original, Cloner cloner)
   : base(original, cloner) {
   logitModel = new alglib.logitmodel();
   logitModel.innerobj.w = (double[])original.logitModel.innerobj.w.Clone();
   allowedInputVariables = (string[])original.allowedInputVariables.Clone();
   classValues = (double[])original.classValues.Clone();
 }
Exemple #2
0
 private MultinomialLogitModel(bool deserializing)
     : base(deserializing)
 {
     if (deserializing)
     {
         logitModel = new alglib.logitmodel();
     }
 }
 public MultinomialLogitModel(alglib.logitmodel logitModel, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues)
   : base(targetVariable) {
   this.name = ItemName;
   this.description = ItemDescription;
   this.logitModel = logitModel;
   this.allowedInputVariables = allowedInputVariables.ToArray();
   this.classValues = (double[])classValues.Clone();
 }
 private MultinomialLogitModel(MultinomialLogitModel original, Cloner cloner)
     : base(original, cloner)
 {
     logitModel            = new alglib.logitmodel();
     logitModel.innerobj.w = (double[])original.logitModel.innerobj.w.Clone();
     allowedInputVariables = (string[])original.allowedInputVariables.Clone();
     classValues           = (double[])original.classValues.Clone();
 }
Exemple #5
0
 private MultinomialLogitModel(MultinomialLogitModel original, Cloner cloner)
     : base(original, cloner)
 {
     logitModel            = new alglib.logitmodel();
     logitModel.innerobj.w = (double[])original.logitModel.innerobj.w.Clone();
     allowedInputVariables = (string[])original.allowedInputVariables.Clone();
     classValues           = (double[])original.classValues.Clone();
     this.factorVariables  = original.factorVariables.Select(kvp => new KeyValuePair <string, IEnumerable <string> >(kvp.Key, new List <string>(kvp.Value))).ToList();
 }
 public MultinomialLogitModel(alglib.logitmodel logitModel, string targetVariable, IEnumerable <string> allowedInputVariables, double[] classValues)
     : base(targetVariable)
 {
     this.name                  = ItemName;
     this.description           = ItemDescription;
     this.logitModel            = logitModel;
     this.allowedInputVariables = allowedInputVariables.ToArray();
     this.classValues           = (double[])classValues.Clone();
 }
Exemple #7
0
 public double[,] getCoefficients(alglib.logitmodel lm, out int nvars, out int nclasses)
 {
     if (lm == null)
     {
         getMnlModel();
     }
     double[,] coef;
     alglib.mnlunpack(lm, out coef, out nvars, out nclasses);
     return(coef);
 }
Exemple #8
0
 public MultinomialLogitModel(alglib.logitmodel logitModel, string targetVariable, IEnumerable <string> doubleInputVariables, IEnumerable <KeyValuePair <string, IEnumerable <string> > > factorVariables, double[] classValues)
     : base(targetVariable)
 {
     this.name                  = ItemName;
     this.description           = ItemDescription;
     this.logitModel            = logitModel;
     this.allowedInputVariables = doubleInputVariables.ToArray();
     this.factorVariables       = factorVariables.Select(kvp => new KeyValuePair <string, IEnumerable <string> >(kvp.Key, new List <string>(kvp.Value))).ToList();
     this.classValues           = (double[])classValues.Clone();
 }
        public static IClassificationSolution CreateLogitClassificationSolution(IClassificationProblemData problemData, out double rmsError, out double relClassError)
        {
            var               dataset             = problemData.Dataset;
            string            targetVariable      = problemData.TargetVariable;
            var               doubleVariableNames = problemData.AllowedInputVariables.Where(dataset.VariableHasType <double>);
            var               factorVariableNames = problemData.AllowedInputVariables.Where(dataset.VariableHasType <string>);
            IEnumerable <int> rows = problemData.TrainingIndices;

            double[,] inputMatrix = dataset.ToArray(doubleVariableNames.Concat(new string[] { targetVariable }), rows);

            var factorVariableValues = dataset.GetFactorVariableValues(factorVariableNames, rows);
            var factorMatrix         = dataset.ToArray(factorVariableValues, rows);

            inputMatrix = factorMatrix.HorzCat(inputMatrix);

            if (inputMatrix.Cast <double>().Any(x => double.IsNaN(x) || double.IsInfinity(x)))
            {
                throw new NotSupportedException("Multinomial logit classification does not support NaN or infinity values in the input dataset.");
            }

            alglib.logitmodel lm  = new alglib.logitmodel();
            alglib.mnlreport  rep = new alglib.mnlreport();
            int nRows             = inputMatrix.GetLength(0);
            int nFeatures         = inputMatrix.GetLength(1) - 1;

            double[] classValues = dataset.GetDoubleValues(targetVariable).Distinct().OrderBy(x => x).ToArray();
            int      nClasses    = classValues.Count();
            // map original class values to values [0..nClasses-1]
            Dictionary <double, double> classIndices = new Dictionary <double, double>();

            for (int i = 0; i < nClasses; i++)
            {
                classIndices[classValues[i]] = i;
            }
            for (int row = 0; row < nRows; row++)
            {
                inputMatrix[row, nFeatures] = classIndices[inputMatrix[row, nFeatures]];
            }
            int info;

            alglib.mnltrainh(inputMatrix, nRows, nFeatures, nClasses, out info, out lm, out rep);
            if (info != 1)
            {
                throw new ArgumentException("Error in calculation of logit classification solution");
            }

            rmsError      = alglib.mnlrmserror(lm, inputMatrix, nRows);
            relClassError = alglib.mnlrelclserror(lm, inputMatrix, nRows);

            MultinomialLogitClassificationSolution solution = new MultinomialLogitClassificationSolution(new MultinomialLogitModel(lm, targetVariable, doubleVariableNames, factorVariableValues, classValues), (IClassificationProblemData)problemData.Clone());

            return(solution);
        }
Exemple #10
0
 private void createRegChart()
 {
     if (lm == null)
     {
         lm = getMnlModel();
     }
     Forms.Stats.frmChart          hist       = (Forms.Stats.frmChart)ModelHelper.generateProbabilityGraphic(IndependentFieldNames);
     System.Windows.Forms.ComboBox cmbPrimary = (System.Windows.Forms.ComboBox)hist.Controls["cmbPrimary"];
     cmbPrimary.SelectedValueChanged += new EventHandler(cmbPrimary_SelectedValueChanged);
     System.Windows.Forms.TrackBar tb = (System.Windows.Forms.TrackBar)hist.Controls["tbQ"];
     tb.Scroll += new EventHandler(tb_RegionChanged);
     hist.chrHistogram.Show();
     cmbPrimary.SelectedItem = IndependentFieldNames[0];
     hist.Show();
 }
 private MultinomialLogitModel(bool deserializing)
   : base(deserializing) {
   if (deserializing)
     logitModel = new alglib.logitmodel();
 }
 private void createRegChart()
 {
     if (lm == null) lm = getMnlModel();
     Forms.Stats.frmChart hist = (Forms.Stats.frmChart)ModelHelper.generateProbabilityGraphic(IndependentFieldNames);
     System.Windows.Forms.ComboBox cmbPrimary = (System.Windows.Forms.ComboBox)hist.Controls["cmbPrimary"];
     cmbPrimary.SelectedValueChanged += new EventHandler(cmbPrimary_SelectedValueChanged);
     System.Windows.Forms.TrackBar tb = (System.Windows.Forms.TrackBar)hist.Controls["tbQ"];
     tb.Scroll += new EventHandler(tb_RegionChanged);
     hist.chrHistogram.Show();
     cmbPrimary.SelectedItem = IndependentFieldNames[0];
     hist.Show();
 }
 private MultinomialLogitModel(StorableConstructorFlag _) : base(_)
 {
     logitModel = new alglib.logitmodel();
 }