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 | * Values.java |
---|
19 | * Copyright (C) 1999 University of Waikato, Hamilton, New Zealand |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | |
---|
24 | package weka.classifiers.trees.m5; |
---|
25 | |
---|
26 | import weka.core.Instances; |
---|
27 | import weka.core.RevisionHandler; |
---|
28 | import weka.core.RevisionUtils; |
---|
29 | |
---|
30 | /** |
---|
31 | * Stores some statistics. |
---|
32 | * @author Yong Wang (yongwang@cs.waikato.ac.nz) |
---|
33 | * @version $Revision: 1.7 $ |
---|
34 | */ |
---|
35 | public final class Values |
---|
36 | implements RevisionHandler { |
---|
37 | |
---|
38 | int numInstances; // number of the instances |
---|
39 | int missingInstances; // number of the instances with missing values |
---|
40 | int first; // index of the first instance |
---|
41 | int last; // index of the last instance |
---|
42 | int attr; // attribute |
---|
43 | double sum; // sum of the instances for attribute |
---|
44 | double sqrSum; // squared sum of the instances for attribute |
---|
45 | double va; // variance |
---|
46 | double sd; // standard deviation |
---|
47 | |
---|
48 | |
---|
49 | /** |
---|
50 | * Constructs an object which stores some statistics of the instances such |
---|
51 | * as sum, squared sum, variance, standard deviation |
---|
52 | * @param low the index of the first instance |
---|
53 | * @param high the index of the last instance |
---|
54 | * @param attribute the attribute |
---|
55 | * @param inst the instances |
---|
56 | */ |
---|
57 | |
---|
58 | public Values(int low,int high,int attribute,Instances inst){ |
---|
59 | int i,count=0; |
---|
60 | double value; |
---|
61 | |
---|
62 | numInstances = high-low+1; |
---|
63 | missingInstances = 0; |
---|
64 | first = low; |
---|
65 | last = high; |
---|
66 | attr = attribute; |
---|
67 | sum=0.0; |
---|
68 | sqrSum=0.0; |
---|
69 | for(i=first;i<=last;i++){ |
---|
70 | if(inst.instance(i).isMissing(attr)==false){ |
---|
71 | count++; |
---|
72 | value = inst.instance(i).value(attr); |
---|
73 | sum += value; |
---|
74 | sqrSum += value * value; |
---|
75 | } |
---|
76 | |
---|
77 | if(count >1){ |
---|
78 | va = (sqrSum - sum * sum/count)/count; |
---|
79 | va = Math.abs(va); |
---|
80 | sd = Math.sqrt(va); |
---|
81 | } |
---|
82 | else {va = 0.0; sd = 0.0;} |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * Converts the stats to a string |
---|
88 | * @return the converted string |
---|
89 | */ |
---|
90 | public final String toString(){ |
---|
91 | |
---|
92 | StringBuffer text = new StringBuffer(); |
---|
93 | |
---|
94 | text.append("Print statistic values of instances (" + first + "-" + last + |
---|
95 | "\n"); |
---|
96 | text.append(" Number of instances:\t" + numInstances + "\n"); |
---|
97 | text.append(" NUmber of instances with unknowns:\t" + missingInstances + |
---|
98 | "\n"); |
---|
99 | text.append(" Attribute:\t\t\t:" + attr + "\n"); |
---|
100 | text.append(" Sum:\t\t\t" + sum + "\n"); |
---|
101 | text.append(" Squared sum:\t\t" + sqrSum + "\n"); |
---|
102 | text.append(" Stanard Deviation:\t\t" + sd + "\n"); |
---|
103 | |
---|
104 | return text.toString(); |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * Returns the revision string. |
---|
109 | * |
---|
110 | * @return the revision |
---|
111 | */ |
---|
112 | public String getRevision() { |
---|
113 | return RevisionUtils.extract("$Revision: 1.7 $"); |
---|
114 | } |
---|
115 | } |
---|