Salut a tous ,
étant donné une classe appelé Regle définie comme suiset une autre classe Test () qui est utiliser par Regle()
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 private class Regle implements Serializable, RevisionHandler { /** for serialization */ static final long serialVersionUID = 4248784350656508583L; /** The classification */ private int m_classification; /** The instance */ private Instances m_instances; /** First test of this rule */ private Test m_test; /** Number of errors made by this rule (will end up 0) */ private int m_errors; /** The next rule in the list */ private Regle m_next; /** * Constructor that takes instances and the classification. * * @param data the instances * @param cl the class * @throws Exception if something goes wrong */ public Regle(Instances data, int cl) throws Exception { m_instances = data; m_classification = cl; m_test = null; m_next = null; m_errors = 0; Enumeration enu = data.enumerateInstances(); while (enu.hasMoreElements()) { if ((int) ((Instance) enu.nextElement()).classValue() != cl) { m_errors++; } } m_instances = new Instances(m_instances, 0); } /** * Returns the result assigned by this rule to a given instance. * * @param inst the instance to be classified * @return the classification */ public int resultRule(Instance inst) { if (m_test == null || m_test.satisfies(inst)) { return m_classification; } else { return -1; } } public boolean coveres(Instance data) { boolean s=false; if (resultRule(data) != -1) { return s; } return s; } /** * Returns the result assigned by these rules to a given instance. * * @param inst the instance to be classified * @return the classification */ public int resultRules(Instance inst) { if (resultRule(inst) != -1) { return m_classification; } else if (m_next != null) { return m_next.resultRules(inst); } else { return -1; } } /** * Returns the set of instances that are covered by this rule. * * @param data the instances to be checked * @return the instances covered */ public Instances coveredBy(Instances data) { Instances r = new Instances(data, data.numInstances()); Enumeration enu = data.enumerateInstances(); while (enu.hasMoreElements()) { Instance i = (Instance) enu.nextElement(); if (resultRule(i) != -1) { r.add(i); } } r.compactify(); return r; } /** * Returns the set of instances that are not covered by this rule. * * @param data the instances to be checked * @return the instances not covered */ public Instances notCoveredBy(Instances data) { Instances r = new Instances(data, data.numInstances()); Enumeration enu = data.enumerateInstances(); while (enu.hasMoreElements()) { Instance i = (Instance) enu.nextElement(); if (resultRule(i) == -1) { r.add(i); } } r.compactify(); return r; } /** * Prints the set of rules. * * @return a description of the rules as a string */ public String toString() { try { StringBuffer text = new StringBuffer(); if (m_test != null) { text.append("If "); for (Test t = m_test; t != null; t = t.m_next) { if (t.m_attr == -1) { text.append("?"); } else { text.append(m_instances.attribute(t.m_attr).name() + " = " + m_instances.attribute(t.m_attr).value(t.m_val)); } if (t.m_next != null) { text.append("\n and "); } } text.append(" then "); } text.append(m_instances.classAttribute().value(m_classification) + "\n"); if (m_next != null) { text.append(m_next.toString()); } return text.toString(); } catch (Exception e) { return "Can't print Prism classifier!"; } } }
j'ai un training set data et je veux creer une regle vide et puis pour chaque attribut et pour chaque valeur d'attribut je creer une noouvelle regle et je l 'ajoute au ensemble de regles j'ai essayer avec ça
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 private class Test implements Serializable, RevisionHandler { /** for serialization */ static final long serialVersionUID = -8925333011350280799L; /** Attribute to test */ private int m_attr = -1; /** The attribute's value */ private int m_val; /** The next test in the rule */ private Test m_next = null; /** * Returns whether a given instance satisfies this test. * * @param inst the instance to be tested * @return true if the instance satisfies the test */ private boolean satisfies(Instance inst) { if ((int) inst.value(m_attr) == m_val) { if (m_next == null) { return true; } else { return m_next.satisfies(inst); } } return false; } }j'ai toujours 1 seule regles ,aparement je n'utilise pas les deux classes precedente correctement !
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 public void buildClassifier(Instances data) throws Exception { System.out.println("55555555555555555555555555Start build------------------"); int cl=0; // possible value of theClass Instances E, ruleE; PrismRule rule = null; Test test = null, oldTest = null; int bestCorrect, bestCovers, attUsed; Enumeration enumAtt; // can classifier handle the data? getCapabilities().testWithFail(data); // remove instances with missing class data = new Instances(data); data.deleteWithMissingClass(); E = data; // initialize E to the instance set CR_rules = addRule(CR_rules , new PrismRule(E, cl)); System.out.println("====first rule ========"+CR_rules.toString()+"]"); ruleE = E; // examples covered by this rule test = new Test(); // for every attribute not mentioned in the rule enumAtt = ruleE.enumerateAttributes(); while (enumAtt.hasMoreElements()) { Attribute attr = (Attribute) enumAtt.nextElement(); System.out.println("attribute"+attr.name()); if (isMentionedIn(attr, CR_rules.m_test)) { continue; } int M = attr.numValues(); // ... for each value of this attribute, see if this test is better for (int val = 0; val < M; val ++) { System.out.println("value"+val); test.m_attr = attr.index(); test.m_val = val; rule.m_test= CR_rules.m_test; System.out.println("the test"+rule.toString()); CR_rules.m_next=rule; System.out.println("rule"+rule); } //------------------------------------------------------------------------ oldTest = addTest(CR_rules, oldTest, test); ruleE = CR_rules.coveredBy(ruleE); E = CR_rules.notCoveredBy(E); System.out.println("=============CR_rules ========"+CR_rules); System.out.println("********************End build***************************"); System.out.println("-------------------------------------------"); } }
Partager