Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13921 was 13921, checked in by bburlacu, 8 years ago

#2604: Revert changes to DataAnalysisSolution and IDataAnalysisSolution and implement the desired properties in model classes that implement IDataAnalysisModel, IRegressionModel and IClassificationModel.

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.DataAnalysis;
29
30namespace HeuristicLab.Algorithms.DataAnalysis {
31  /// <summary>
32  /// Represents a multinomial logit model for classification
33  /// </summary>
34  [StorableClass]
35  [Item("Multinomial Logit Model", "Represents a multinomial logit model for classification.")]
36  public sealed class MultinomialLogitModel : NamedItem, IClassificationModel {
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
50    public IEnumerable<string> VariablesUsedForPrediction {
51      get { return allowedInputVariables; }
52    }
53
54    public string TargetVariable {
55      get { return targetVariable; }
56    }
57
58    [Storable]
59    private string targetVariable;
60    [Storable]
61    private string[] allowedInputVariables;
62    [Storable]
63    private double[] classValues;
64    [StorableConstructor]
65    private MultinomialLogitModel(bool deserializing)
66      : base(deserializing) {
67      if (deserializing)
68        logitModel = new alglib.logitmodel();
69    }
70    private MultinomialLogitModel(MultinomialLogitModel original, Cloner cloner)
71      : base(original, cloner) {
72      logitModel = new alglib.logitmodel();
73      logitModel.innerobj.w = (double[])original.logitModel.innerobj.w.Clone();
74      targetVariable = original.targetVariable;
75      allowedInputVariables = (string[])original.allowedInputVariables.Clone();
76      classValues = (double[])original.classValues.Clone();
77    }
78    public MultinomialLogitModel(alglib.logitmodel logitModel, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues)
79      : base() {
80      this.name = ItemName;
81      this.description = ItemDescription;
82      this.logitModel = logitModel;
83      this.targetVariable = targetVariable;
84      this.allowedInputVariables = allowedInputVariables.ToArray();
85      this.classValues = (double[])classValues.Clone();
86    }
87
88    public override IDeepCloneable Clone(Cloner cloner) {
89      return new MultinomialLogitModel(this, cloner);
90    }
91
92    public IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {
93      double[,] inputData = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables, rows);
94
95      int n = inputData.GetLength(0);
96      int columns = inputData.GetLength(1);
97      double[] x = new double[columns];
98      double[] y = new double[classValues.Length];
99
100      for (int row = 0; row < n; row++) {
101        for (int column = 0; column < columns; column++) {
102          x[column] = inputData[row, column];
103        }
104        alglib.mnlprocess(logitModel, x, ref y);
105        // find class for with the largest probability value
106        int maxProbClassIndex = 0;
107        double maxProb = y[0];
108        for (int i = 1; i < y.Length; i++) {
109          if (maxProb < y[i]) {
110            maxProb = y[i];
111            maxProbClassIndex = i;
112          }
113        }
114        yield return classValues[maxProbClassIndex];
115      }
116    }
117
118    public MultinomialLogitClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
119      return new MultinomialLogitClassificationSolution(new ClassificationProblemData(problemData), this);
120    }
121
122
123
124    IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {
125      return CreateClassificationSolution(problemData);
126    }
127
128    #region events
129    public event EventHandler Changed;
130    private void OnChanged(EventArgs e) {
131      var handlers = Changed;
132      if (handlers != null)
133        handlers(this, e);
134    }
135    #endregion
136
137    #region persistence
138    [Storable]
139    private double[] LogitModelW {
140      get {
141        return logitModel.innerobj.w;
142      }
143      set {
144        logitModel.innerobj.w = value;
145      }
146    }
147    #endregion
148
149  }
150}
Note: See TracBrowser for help on using the repository browser.