1 | /*
|
---|
2 | * This program is free software; you can redistribute it and/or modify
|
---|
3 | * it under the terms of the GNU General Public License as published by
|
---|
4 | * the Free Software Foundation; either version 2 of the License, or
|
---|
5 | * (at your option) any later version.
|
---|
6 | *
|
---|
7 | * This program is distributed in the hope that it will be useful,
|
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
10 | * GNU General Public License for more details.
|
---|
11 | *
|
---|
12 | * You should have received a copy of the GNU General Public License
|
---|
13 | * along with this program; if not, write to the Free Software
|
---|
14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
15 | */
|
---|
16 |
|
---|
17 | /*
|
---|
18 | * DistributionClassifier.java
|
---|
19 | * Copyright (C) 1999 Eibe Frank, Len Trigg
|
---|
20 | *
|
---|
21 | */
|
---|
22 |
|
---|
23 | package weka.classifiers;
|
---|
24 |
|
---|
25 | import weka.core.*;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Abstract classification model that produces (for each test instance)
|
---|
29 | * an estimate of the membership in each class
|
---|
30 | * (ie. a probability distribution).
|
---|
31 | *
|
---|
32 | * @author Eibe Frank ([email protected])
|
---|
33 | * @author Len Trigg ([email protected])
|
---|
34 | * @version $Revision: 8815 $
|
---|
35 | */
|
---|
36 | public abstract class DistributionClassifier extends Classifier {
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Predicts the class memberships for a given instance. If
|
---|
40 | * an instance is unclassified, the returned array elements
|
---|
41 | * must be all zero. If the class is numeric, the array
|
---|
42 | * must consist of only one element, which contains the
|
---|
43 | * predicted value.
|
---|
44 | *
|
---|
45 | * @param instance the instance to be classified
|
---|
46 | * @return an array containing the estimated membership
|
---|
47 | * probabilities of the test instance in each class (this
|
---|
48 | * should sum to at most 1)
|
---|
49 | * @exception Exception if distribution could not be
|
---|
50 | * computed successfully
|
---|
51 | */
|
---|
52 | public abstract double[] distributionForInstance(Instance instance)
|
---|
53 | throws Exception;
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Classifies the given test instance. The instance has to belong to a
|
---|
57 | * dataset when it's being classified.
|
---|
58 | *
|
---|
59 | * @param instance the instance to be classified
|
---|
60 | * @return the predicted most likely class for the instance or
|
---|
61 | * Instance.missingValue() if no prediction is made
|
---|
62 | * @exception Exception if an error occurred during the prediction
|
---|
63 | */
|
---|
64 | public double classifyInstance(Instance instance) throws Exception {
|
---|
65 |
|
---|
66 | double [] dist = distributionForInstance(instance);
|
---|
67 | if (dist == null) {
|
---|
68 | throw new Exception("Null distribution predicted");
|
---|
69 | }
|
---|
70 | switch (instance.classAttribute().type()) {
|
---|
71 | case Attribute.NOMINAL:
|
---|
72 | double max = 0;
|
---|
73 | int maxIndex = 0;
|
---|
74 |
|
---|
75 | for (int i = 0; i < dist.length; i++) {
|
---|
76 | if (dist[i] > max) {
|
---|
77 | maxIndex = i;
|
---|
78 | max = dist[i];
|
---|
79 | }
|
---|
80 | }
|
---|
81 | if (max > 0) {
|
---|
82 | return maxIndex;
|
---|
83 | } else {
|
---|
84 | return Instance.missingValue();
|
---|
85 | }
|
---|
86 | case Attribute.NUMERIC:
|
---|
87 | return dist[0];
|
---|
88 | default:
|
---|
89 | return Instance.missingValue();
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|