This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
/
DecisionSet.cs
261 lines (232 loc) · 7.76 KB
/
DecisionSet.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Accord Machine Learning Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © César Souza, 2009-2015
// cesarsouza at gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
namespace Accord.MachineLearning.DecisionTrees.Rules
{
using System.Collections.Generic;
using System.Text;
using Accord.Statistics.Filters;
using System;
using System.Globalization;
/// <summary>
/// Decision rule set.
/// </summary>
///
public class DecisionSet : IEnumerable<DecisionRule>
{
HashSet<DecisionRule> rules;
/// <summary>
/// Gets the number of possible output
/// classes covered by this decision set.
/// </summary>
///
public int OutputClasses { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="DecisionSet"/> class.
/// </summary>
///
public DecisionSet()
{
this.rules = new HashSet<DecisionRule>();
}
/// <summary>
/// Initializes a new instance of the <see cref="DecisionSet"/> class.
/// </summary>
///
/// <param name="rules">A set of decision rules.</param>
///
public DecisionSet(IEnumerable<DecisionRule> rules)
{
this.rules = new HashSet<DecisionRule>(rules);
}
/// <summary>
/// Creates a new <see cref="DecisionSet"/> from a <see cref="DecisionTree"/>.
/// </summary>
///
/// <param name="tree">A <see cref="DecisionTree"/>.</param>
///
/// <returns>A <see cref="DecisionSet"/> that is completely
/// equivalent to the given <paramref name="tree"/></returns>
///
public static DecisionSet FromDecisionTree(DecisionTree tree)
{
var rules = new List<DecisionRule>();
foreach (var node in tree)
{
if (node.IsLeaf && !node.IsRoot && node.Output.HasValue)
{
rules.Add(DecisionRule.FromNode(node));
}
}
return new DecisionSet(rules)
{
OutputClasses = tree.OutputClasses
};
}
/// <summary>
/// Computes the decision output for a given input.
/// </summary>
///
/// <param name="input">An input vector.</param>
///
/// <returns>The decision output for the given
/// <paramref name="input"/>.</returns>
///
public double? Compute(double[] input)
{
foreach (DecisionRule rule in rules)
{
if (rule.Match(input))
return rule.Output;
}
return null;
}
/// <summary>
/// Adds a new <see cref="DecisionRule"/> to the set.
/// </summary>
///
/// <param name="item">The <see cref="DecisionRule"/> to be added.</param>
///
public void Add(DecisionRule item)
{
rules.Add(item);
}
/// <summary>
/// Adds a collection of new <see cref="DecisionRule"/>s to the set.
/// </summary>
///
/// <param name="items">The collection of <see cref="DecisionRule"/>s to be added.</param>
///
public void AddRange(IEnumerable<DecisionRule> items)
{
foreach (var rule in items)
rules.Add(rule);
}
/// <summary>
/// Removes all rules from this set.
/// </summary>
///
public void Clear()
{
rules.Clear();
}
/// <summary>
/// Gets the number of rules in this set.
/// </summary>
///
public int Count
{
get { return rules.Count; }
}
/// <summary>
/// Removes a given rule from the set.
/// </summary>
///
/// <param name="item">The <see cref="DecisionRule"/> to be removed.</param>
///
/// <returns>True if the rule was removed; false otherwise.</returns>
///
public bool Remove(DecisionRule item)
{
return rules.Remove(item);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
///
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
///
public override string ToString()
{
return toString(null, CultureInfo.CurrentUICulture);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
///
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
///
public string ToString(Codification codebook, CultureInfo cultureInfo)
{
return toString(codebook, cultureInfo);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
///
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
///
public string ToString(Codification codebook)
{
return ToString(codebook, CultureInfo.CurrentUICulture);
}
private string toString(Codification codebook, CultureInfo cultureInfo)
{
var rulesArray = new DecisionRule[this.rules.Count];
rules.CopyTo(rulesArray);
Array.Sort(rulesArray);
StringBuilder sb = new StringBuilder();
if (codebook != null)
{
foreach (DecisionRule rule in rulesArray)
sb.AppendLine(rule.ToString(codebook, cultureInfo));
}
else
{
foreach (DecisionRule rule in rulesArray)
sb.AppendLine(rule.ToString(cultureInfo));
}
return sb.ToString();
}
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
///
/// <returns>
/// An <see cref="T:System.Collections.IEnumerator"/> object
/// that can be used to iterate through the collection.
/// </returns>
///
public IEnumerator<DecisionRule> GetEnumerator()
{
return rules.GetEnumerator();
}
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
///
/// <returns>
/// An <see cref="T:System.Collections.IEnumerator"/> object
/// that can be used to iterate through the collection.
/// </returns>
///
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return rules.GetEnumerator();
}
}
}