Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/MultinomialLogitModel.cs @ 17399

Last change on this file since 17399 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 5.5 KB
RevLine 
[6567]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6567]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
[16565]27using HEAL.Attic;
[6567]28using HeuristicLab.Problems.DataAnalysis;
29
30namespace HeuristicLab.Algorithms.DataAnalysis {
31  /// <summary>
32  /// Represents a multinomial logit model for classification
33  /// </summary>
[16565]34  [StorableType("AC4174A4-9FBC-4B07-9239-1E0E6F86034D")]
[6575]35  [Item("Multinomial Logit Model", "Represents a multinomial logit model for classification.")]
[13941]36  public sealed class MultinomialLogitModel : ClassificationModel {
[6567]37
38    private alglib.logitmodel logitModel;
39    public alglib.logitmodel Model {
40      get { return logitModel; }
41      set {
42        if (value != logitModel) {
43          if (value == null) throw new ArgumentNullException();
44          logitModel = value;
45          OnChanged(EventArgs.Empty);
46        }
47      }
48    }
49
[13941]50    public override IEnumerable<string> VariablesUsedForPrediction {
[13921]51      get { return allowedInputVariables; }
52    }
53
[6567]54    [Storable]
55    private string[] allowedInputVariables;
56    [Storable]
57    private double[] classValues;
[14826]58    [Storable]
59    private List<KeyValuePair<string, IEnumerable<string>>> factorVariables;
60
[6567]61    [StorableConstructor]
[16565]62    private MultinomialLogitModel(StorableConstructorFlag _) : base(_) {
63      logitModel = new alglib.logitmodel();
[6567]64    }
[6576]65    private MultinomialLogitModel(MultinomialLogitModel original, Cloner cloner)
[6567]66      : base(original, cloner) {
67      logitModel = new alglib.logitmodel();
68      logitModel.innerobj.w = (double[])original.logitModel.innerobj.w.Clone();
69      allowedInputVariables = (string[])original.allowedInputVariables.Clone();
[6633]70      classValues = (double[])original.classValues.Clone();
[14826]71      this.factorVariables = original.factorVariables.Select(kvp => new KeyValuePair<string, IEnumerable<string>>(kvp.Key, new List<string>(kvp.Value))).ToList();
[6567]72    }
[14826]73    public MultinomialLogitModel(alglib.logitmodel logitModel, string targetVariable, IEnumerable<string> doubleInputVariables, IEnumerable<KeyValuePair<string, IEnumerable<string>>> factorVariables, double[] classValues)
[13941]74      : base(targetVariable) {
[6567]75      this.name = ItemName;
76      this.description = ItemDescription;
77      this.logitModel = logitModel;
[14826]78      this.allowedInputVariables = doubleInputVariables.ToArray();
79      this.factorVariables = factorVariables.Select(kvp => new KeyValuePair<string, IEnumerable<string>>(kvp.Key, new List<string>(kvp.Value))).ToList();
[6567]80      this.classValues = (double[])classValues.Clone();
81    }
82
[14826]83    [StorableHook(HookType.AfterDeserialization)]
84    private void AfterDeserialization() {
85      // BackwardsCompatibility3.3
86      #region Backwards compatible code, remove with 3.4
87      factorVariables = new List<KeyValuePair<string, IEnumerable<string>>>();
88      #endregion
89    }
90
[6567]91    public override IDeepCloneable Clone(Cloner cloner) {
[6576]92      return new MultinomialLogitModel(this, cloner);
[6567]93    }
94
[13941]95    public override IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {
[14826]96
[14843]97      double[,] inputData = dataset.ToArray(allowedInputVariables, rows);
98      double[,] factorData = dataset.ToArray(factorVariables, rows);
[6567]99
[14826]100      inputData = factorData.HorzCat(inputData);
101
[6567]102      int n = inputData.GetLength(0);
103      int columns = inputData.GetLength(1);
104      double[] x = new double[columns];
105      double[] y = new double[classValues.Length];
106
107      for (int row = 0; row < n; row++) {
108        for (int column = 0; column < columns; column++) {
109          x[column] = inputData[row, column];
110        }
111        alglib.mnlprocess(logitModel, x, ref y);
112        // find class for with the largest probability value
113        int maxProbClassIndex = 0;
114        double maxProb = y[0];
115        for (int i = 1; i < y.Length; i++) {
116          if (maxProb < y[i]) {
117            maxProb = y[i];
118            maxProbClassIndex = i;
119          }
120        }
121        yield return classValues[maxProbClassIndex];
122      }
123    }
124
[13941]125    public override IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
126      return new MultinomialLogitClassificationSolution(this, new ClassificationProblemData(problemData));
[6604]127    }
[13921]128
[6567]129    #region events
130    public event EventHandler Changed;
131    private void OnChanged(EventArgs e) {
132      var handlers = Changed;
133      if (handlers != null)
134        handlers(this, e);
135    }
136    #endregion
137
138    #region persistence
139    [Storable]
140    private double[] LogitModelW {
141      get {
142        return logitModel.innerobj.w;
143      }
144      set {
145        logitModel.innerobj.w = value;
146      }
147    }
148    #endregion
[13921]149
[6567]150  }
151}
Note: See TracBrowser for help on using the repository browser.