[6567] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 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.")]
|
---|
[6576] | 36 | public sealed class MultinomialLogitModel : NamedItem, IClassificationModel {
|
---|
[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 |
|
---|
| 50 | [Storable]
|
---|
| 51 | private string targetVariable;
|
---|
| 52 | [Storable]
|
---|
| 53 | private string[] allowedInputVariables;
|
---|
| 54 | [Storable]
|
---|
| 55 | private double[] classValues;
|
---|
| 56 | [StorableConstructor]
|
---|
[6576] | 57 | private MultinomialLogitModel(bool deserializing)
|
---|
[6567] | 58 | : base(deserializing) {
|
---|
| 59 | if (deserializing)
|
---|
| 60 | logitModel = new alglib.logitmodel();
|
---|
| 61 | }
|
---|
[6576] | 62 | private MultinomialLogitModel(MultinomialLogitModel original, Cloner cloner)
|
---|
[6567] | 63 | : base(original, cloner) {
|
---|
| 64 | logitModel = new alglib.logitmodel();
|
---|
| 65 | logitModel.innerobj.w = (double[])original.logitModel.innerobj.w.Clone();
|
---|
| 66 | targetVariable = original.targetVariable;
|
---|
| 67 | allowedInputVariables = (string[])original.allowedInputVariables.Clone();
|
---|
[6633] | 68 | classValues = (double[])original.classValues.Clone();
|
---|
[6567] | 69 | }
|
---|
[6576] | 70 | public MultinomialLogitModel(alglib.logitmodel logitModel, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues)
|
---|
[6567] | 71 | : base() {
|
---|
| 72 | this.name = ItemName;
|
---|
| 73 | this.description = ItemDescription;
|
---|
| 74 | this.logitModel = logitModel;
|
---|
| 75 | this.targetVariable = targetVariable;
|
---|
| 76 | this.allowedInputVariables = allowedInputVariables.ToArray();
|
---|
| 77 | this.classValues = (double[])classValues.Clone();
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[6576] | 81 | return new MultinomialLogitModel(this, cloner);
|
---|
[6567] | 82 | }
|
---|
| 83 |
|
---|
| 84 | public IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) {
|
---|
| 85 | double[,] inputData = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables, rows);
|
---|
| 86 |
|
---|
| 87 | int n = inputData.GetLength(0);
|
---|
| 88 | int columns = inputData.GetLength(1);
|
---|
| 89 | double[] x = new double[columns];
|
---|
| 90 | double[] y = new double[classValues.Length];
|
---|
| 91 |
|
---|
| 92 | for (int row = 0; row < n; row++) {
|
---|
| 93 | for (int column = 0; column < columns; column++) {
|
---|
| 94 | x[column] = inputData[row, column];
|
---|
| 95 | }
|
---|
| 96 | alglib.mnlprocess(logitModel, x, ref y);
|
---|
| 97 | // find class for with the largest probability value
|
---|
| 98 | int maxProbClassIndex = 0;
|
---|
| 99 | double maxProb = y[0];
|
---|
| 100 | for (int i = 1; i < y.Length; i++) {
|
---|
| 101 | if (maxProb < y[i]) {
|
---|
| 102 | maxProb = y[i];
|
---|
| 103 | maxProbClassIndex = i;
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 | yield return classValues[maxProbClassIndex];
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[6604] | 110 | public MultinomialLogitClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
|
---|
[8528] | 111 | return new MultinomialLogitClassificationSolution(new ClassificationProblemData(problemData), this);
|
---|
[6604] | 112 | }
|
---|
| 113 | IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {
|
---|
| 114 | return CreateClassificationSolution(problemData);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[6567] | 117 | #region events
|
---|
| 118 | public event EventHandler Changed;
|
---|
| 119 | private void OnChanged(EventArgs e) {
|
---|
| 120 | var handlers = Changed;
|
---|
| 121 | if (handlers != null)
|
---|
| 122 | handlers(this, e);
|
---|
| 123 | }
|
---|
| 124 | #endregion
|
---|
| 125 |
|
---|
| 126 | #region persistence
|
---|
| 127 | [Storable]
|
---|
| 128 | private double[] LogitModelW {
|
---|
| 129 | get {
|
---|
| 130 | return logitModel.innerobj.w;
|
---|
| 131 | }
|
---|
| 132 | set {
|
---|
| 133 | logitModel.innerobj.w = value;
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 | #endregion
|
---|
| 137 | }
|
---|
| 138 | }
|
---|