1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.IO;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Text;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Problems.DataAnalysis;
|
---|
31 | using SVM;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
34 | /// <summary>
|
---|
35 | /// Represents a multinomial logit model for classification
|
---|
36 | /// </summary>
|
---|
37 | [StorableClass]
|
---|
38 | [Item("Multinomial Logit Model", "Represents a multinomial logit model for classification.")]
|
---|
39 | public sealed class MultinomialLogitModel : NamedItem, IClassificationModel {
|
---|
40 |
|
---|
41 | private alglib.logitmodel logitModel;
|
---|
42 | public alglib.logitmodel Model {
|
---|
43 | get { return logitModel; }
|
---|
44 | set {
|
---|
45 | if (value != logitModel) {
|
---|
46 | if (value == null) throw new ArgumentNullException();
|
---|
47 | logitModel = value;
|
---|
48 | OnChanged(EventArgs.Empty);
|
---|
49 | }
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | [Storable]
|
---|
54 | private string targetVariable;
|
---|
55 | [Storable]
|
---|
56 | private string[] allowedInputVariables;
|
---|
57 | [Storable]
|
---|
58 | private double[] classValues;
|
---|
59 | [StorableConstructor]
|
---|
60 | private MultinomialLogitModel(bool deserializing)
|
---|
61 | : base(deserializing) {
|
---|
62 | if (deserializing)
|
---|
63 | logitModel = new alglib.logitmodel();
|
---|
64 | }
|
---|
65 | private MultinomialLogitModel(MultinomialLogitModel original, Cloner cloner)
|
---|
66 | : base(original, cloner) {
|
---|
67 | logitModel = new alglib.logitmodel();
|
---|
68 | logitModel.innerobj.w = (double[])original.logitModel.innerobj.w.Clone();
|
---|
69 | targetVariable = original.targetVariable;
|
---|
70 | allowedInputVariables = (string[])original.allowedInputVariables.Clone();
|
---|
71 | this.classValues = (double[])original.classValues.Clone();
|
---|
72 | }
|
---|
73 | public MultinomialLogitModel(alglib.logitmodel logitModel, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues)
|
---|
74 | : base() {
|
---|
75 | this.name = ItemName;
|
---|
76 | this.description = ItemDescription;
|
---|
77 | this.logitModel = logitModel;
|
---|
78 | this.targetVariable = targetVariable;
|
---|
79 | this.allowedInputVariables = allowedInputVariables.ToArray();
|
---|
80 | this.classValues = (double[])classValues.Clone();
|
---|
81 | }
|
---|
82 |
|
---|
83 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
84 | return new MultinomialLogitModel(this, cloner);
|
---|
85 | }
|
---|
86 |
|
---|
87 | public IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) {
|
---|
88 | double[,] inputData = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables, rows);
|
---|
89 |
|
---|
90 | int n = inputData.GetLength(0);
|
---|
91 | int columns = inputData.GetLength(1);
|
---|
92 | double[] x = new double[columns];
|
---|
93 | double[] y = new double[classValues.Length];
|
---|
94 |
|
---|
95 | for (int row = 0; row < n; row++) {
|
---|
96 | for (int column = 0; column < columns; column++) {
|
---|
97 | x[column] = inputData[row, column];
|
---|
98 | }
|
---|
99 | alglib.mnlprocess(logitModel, x, ref y);
|
---|
100 | // find class for with the largest probability value
|
---|
101 | int maxProbClassIndex = 0;
|
---|
102 | double maxProb = y[0];
|
---|
103 | for (int i = 1; i < y.Length; i++) {
|
---|
104 | if (maxProb < y[i]) {
|
---|
105 | maxProb = y[i];
|
---|
106 | maxProbClassIndex = i;
|
---|
107 | }
|
---|
108 | }
|
---|
109 | yield return classValues[maxProbClassIndex];
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | #region events
|
---|
114 | public event EventHandler Changed;
|
---|
115 | private void OnChanged(EventArgs e) {
|
---|
116 | var handlers = Changed;
|
---|
117 | if (handlers != null)
|
---|
118 | handlers(this, e);
|
---|
119 | }
|
---|
120 | #endregion
|
---|
121 |
|
---|
122 | #region persistence
|
---|
123 | [Storable]
|
---|
124 | private double[] LogitModelW {
|
---|
125 | get {
|
---|
126 | return logitModel.innerobj.w;
|
---|
127 | }
|
---|
128 | set {
|
---|
129 | logitModel.innerobj.w = value;
|
---|
130 | }
|
---|
131 | }
|
---|
132 | #endregion
|
---|
133 | }
|
---|
134 | }
|
---|