[6567] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16057] | 3 | * Copyright (C) 2002-2018 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
| 31 | /// <summary>
|
---|
| 32 | /// Represents a multinomial logit model for classification
|
---|
| 33 | /// </summary>
|
---|
| 34 | [StorableClass]
|
---|
[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]
|
---|
[6576] | 62 | private MultinomialLogitModel(bool deserializing)
|
---|
[6567] | 63 | : base(deserializing) {
|
---|
| 64 | if (deserializing)
|
---|
| 65 | logitModel = new alglib.logitmodel();
|
---|
| 66 | }
|
---|
[6576] | 67 | private MultinomialLogitModel(MultinomialLogitModel original, Cloner cloner)
|
---|
[6567] | 68 | : base(original, cloner) {
|
---|
| 69 | logitModel = new alglib.logitmodel();
|
---|
| 70 | logitModel.innerobj.w = (double[])original.logitModel.innerobj.w.Clone();
|
---|
| 71 | allowedInputVariables = (string[])original.allowedInputVariables.Clone();
|
---|
[6633] | 72 | classValues = (double[])original.classValues.Clone();
|
---|
[14826] | 73 | this.factorVariables = original.factorVariables.Select(kvp => new KeyValuePair<string, IEnumerable<string>>(kvp.Key, new List<string>(kvp.Value))).ToList();
|
---|
[6567] | 74 | }
|
---|
[14826] | 75 | public MultinomialLogitModel(alglib.logitmodel logitModel, string targetVariable, IEnumerable<string> doubleInputVariables, IEnumerable<KeyValuePair<string, IEnumerable<string>>> factorVariables, double[] classValues)
|
---|
[13941] | 76 | : base(targetVariable) {
|
---|
[6567] | 77 | this.name = ItemName;
|
---|
| 78 | this.description = ItemDescription;
|
---|
| 79 | this.logitModel = logitModel;
|
---|
[14826] | 80 | this.allowedInputVariables = doubleInputVariables.ToArray();
|
---|
| 81 | this.factorVariables = factorVariables.Select(kvp => new KeyValuePair<string, IEnumerable<string>>(kvp.Key, new List<string>(kvp.Value))).ToList();
|
---|
[6567] | 82 | this.classValues = (double[])classValues.Clone();
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[14826] | 85 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 86 | private void AfterDeserialization() {
|
---|
| 87 | // BackwardsCompatibility3.3
|
---|
| 88 | #region Backwards compatible code, remove with 3.4
|
---|
| 89 | factorVariables = new List<KeyValuePair<string, IEnumerable<string>>>();
|
---|
| 90 | #endregion
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[6567] | 93 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[6576] | 94 | return new MultinomialLogitModel(this, cloner);
|
---|
[6567] | 95 | }
|
---|
| 96 |
|
---|
[13941] | 97 | public override IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {
|
---|
[14826] | 98 |
|
---|
[14843] | 99 | double[,] inputData = dataset.ToArray(allowedInputVariables, rows);
|
---|
| 100 | double[,] factorData = dataset.ToArray(factorVariables, rows);
|
---|
[6567] | 101 |
|
---|
[14826] | 102 | inputData = factorData.HorzCat(inputData);
|
---|
| 103 |
|
---|
[6567] | 104 | int n = inputData.GetLength(0);
|
---|
| 105 | int columns = inputData.GetLength(1);
|
---|
| 106 | double[] x = new double[columns];
|
---|
| 107 | double[] y = new double[classValues.Length];
|
---|
| 108 |
|
---|
| 109 | for (int row = 0; row < n; row++) {
|
---|
| 110 | for (int column = 0; column < columns; column++) {
|
---|
| 111 | x[column] = inputData[row, column];
|
---|
| 112 | }
|
---|
| 113 | alglib.mnlprocess(logitModel, x, ref y);
|
---|
| 114 | // find class for with the largest probability value
|
---|
| 115 | int maxProbClassIndex = 0;
|
---|
| 116 | double maxProb = y[0];
|
---|
| 117 | for (int i = 1; i < y.Length; i++) {
|
---|
| 118 | if (maxProb < y[i]) {
|
---|
| 119 | maxProb = y[i];
|
---|
| 120 | maxProbClassIndex = i;
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 | yield return classValues[maxProbClassIndex];
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[13941] | 127 | public override IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
|
---|
| 128 | return new MultinomialLogitClassificationSolution(this, new ClassificationProblemData(problemData));
|
---|
[6604] | 129 | }
|
---|
[13921] | 130 |
|
---|
[6567] | 131 | #region events
|
---|
| 132 | public event EventHandler Changed;
|
---|
| 133 | private void OnChanged(EventArgs e) {
|
---|
| 134 | var handlers = Changed;
|
---|
| 135 | if (handlers != null)
|
---|
| 136 | handlers(this, e);
|
---|
| 137 | }
|
---|
| 138 | #endregion
|
---|
| 139 |
|
---|
| 140 | #region persistence
|
---|
| 141 | [Storable]
|
---|
| 142 | private double[] LogitModelW {
|
---|
| 143 | get {
|
---|
| 144 | return logitModel.innerobj.w;
|
---|
| 145 | }
|
---|
| 146 | set {
|
---|
| 147 | logitModel.innerobj.w = value;
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 | #endregion
|
---|
[13921] | 151 |
|
---|
[6567] | 152 | }
|
---|
| 153 | }
|
---|