Salut a tous ,
Bon voila un programme en java qui décrit l’algorithme Prism de datamining , je veux créer un autre programme ayant le même principe de création de règles de décision et comme je suis débutante je vais m inspirer de ce code la (au fait je n'arive ps a bien comprendre la représentation de la regle et de PrismRule et comment l'ensemble des regles est representer ) est ce que quelqu'un peut m'aider a le bien comprendre ,
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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
 
 
 
package weka.classifiers.rules;
 
import weka.classifiers.Classifier;
import weka.classifiers.AbstractClassifier;
import weka.core.Attribute;
import weka.core.Capabilities;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.RevisionHandler;
import weka.core.RevisionUtils;
import weka.core.TechnicalInformation;
import weka.core.TechnicalInformationHandler;
import weka.core.Capabilities.Capability;
import weka.core.TechnicalInformation.Field;
import weka.core.TechnicalInformation.Type;
import weka.core.Utils;
 
import java.io.Serializable;
import java.util.Enumeration;
 
 
public class Prism extends AbstractClassifier implements TechnicalInformationHandler {
 
  /** for serialization */
  static final long serialVersionUID = 1310258880025902106L;
 
  /**
   * Returns a string describing classifier
   * @return a description suitable for
   * displaying in the explorer/experimenter gui
   */
  public String globalInfo() {
    return "Class for building and using a PRISM rule set for classification. "
      + "Can only deal with nominal attributes. Can't deal with missing values. "
      + "Doesn't do any pruning.\n\n"
      + "For more information, see \n\n"
      + getTechnicalInformation().toString();
  }
 
  /**
   * Returns an instance of a TechnicalInformation object, containing 
   * detailed information about the technical background of this class,
   * e.g., paper reference or book this class is based on.
   * 
   * @return the technical information about this class
   */
  public TechnicalInformation getTechnicalInformation() {
    TechnicalInformation 	result;
 
    result = new TechnicalInformation(Type.ARTICLE);
    result.setValue(Field.AUTHOR, "J. Cendrowska");
    result.setValue(Field.YEAR, "1987");
    result.setValue(Field.TITLE, "PRISM: An algorithm for inducing modular rules");
    result.setValue(Field.JOURNAL, "International Journal of Man-Machine Studies");
    result.setValue(Field.VOLUME, "27");
    result.setValue(Field.NUMBER, "4");
    result.setValue(Field.PAGES, "349-370");
 
    return result;
  }
 
  /**
   * Class for storing a PRISM ruleset, i.e. a list of rules
   */
  private class PrismRule 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 PrismRule m_next;
 
    /**
     * Constructor that takes instances and the classification.
     *
     * @param data the instances
     * @param cl the class
     * @exception Exception if something goes wrong
     */
    public PrismRule(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;
      }
    }
 
    /**
     * 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!";
      }
    }
 
    /**
     * Returns the revision string.
     * 
     * @return          the revision
     */
    public String getRevision() {
      return RevisionUtils.extract("$Revision: 8109 $");
    }
  }
 
  /**
   * Class for storing a list of attribute-value tests
   */
  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;    
    }
 
    /**
     * Returns the revision string.
     * 
     * @return          the revision
     */
    public String getRevision() {
      return RevisionUtils.extract("$Revision: 8109 $");
    }
  }
 
  /** The first rule in the list of rules */
  private PrismRule m_rules;
 
  /**
   * Classifies a given instance.
   *
   * @param inst the instance to be classified
   * @return the classification
   */
  public double classifyInstance(Instance inst) {
 
    int result = m_rules.resultRules(inst);
    if (result == -1) {
      return Utils.missingValue();
    } else {
      return (double)result;
    }
  }
 
  /**
   * Returns default capabilities of the classifier.
   *
   * @return      the capabilities of this classifier
   */
  public Capabilities getCapabilities() {
    Capabilities result = super.getCapabilities();
    result.disableAll();
 
    // attributes
    result.enable(Capability.NOMINAL_ATTRIBUTES);
 
    // class
    result.enable(Capability.NOMINAL_CLASS);
    result.enable(Capability.MISSING_CLASS_VALUES);
 
    return result;
  }
 
  /**
   * Generates the classifier.
   *
   * @param data the data to be used
   * @exception Exception if the classifier can't built successfully
   */
  public void buildClassifier(Instances data) throws Exception {
 
    int cl; // 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();
 
    for (cl = 0; cl < data.numClasses(); cl++) { // for each class cl
      E = data; // initialize E to the instance set
      while (contains(E, cl)) { // while E contains examples in class cl
        rule = addRule(rule, new PrismRule(E, cl)); // make a new rule
        ruleE = E; // examples covered by this rule
        while (rule.m_errors != 0) { // until the rule is perfect
          test = new Test(); // make a new test
          bestCorrect = bestCovers = attUsed = 0;
 
          // for every attribute not mentioned in the rule
          enumAtt = ruleE.enumerateAttributes();
          while (enumAtt.hasMoreElements()) {
            Attribute attr = (Attribute) enumAtt.nextElement();
            if (isMentionedIn(attr, rule.m_test)) {
	      attUsed++; 
	      continue;
	    }
            int M = attr.numValues();
            int[] covers = new int [M];
            int[] correct = new int [M];
            for (int j = 0; j < M; j++) {
	      covers[j] = correct[j] = 0;
	    }
 
            // ... calculate the counts for this class
            Enumeration enu = ruleE.enumerateInstances();
            while (enu.hasMoreElements()) {
              Instance i = (Instance) enu.nextElement();
              covers[(int) i.value(attr)]++;
              if ((int) i.classValue() == cl) {
                correct[(int) i.value(attr)]++;
	      }
            }
 
            // ... for each value of this attribute, see if this test is better
            for (int val = 0; val < M; val ++) {
              int diff = correct[val] * bestCovers - bestCorrect * covers[val];
 
              // this is a ratio test, correct/covers vs best correct/covers
              if (test.m_attr == -1
                  || diff > 0 || (diff == 0 && correct[val] > bestCorrect)) {
 
                // update the rule to use this test
                bestCorrect = correct[val];
                bestCovers = covers[val];
                test.m_attr = attr.index();
                test.m_val = val;
                rule.m_errors = bestCovers - bestCorrect;
              }
            }
          }
	  if (test.m_attr == -1) { // Couldn't find any sensible test
	    break;
	  }
	  oldTest = addTest(rule, oldTest, test);
	  ruleE = rule.coveredBy(ruleE);
	  if (attUsed == (data.numAttributes() - 1)) { // Used all attributes.
	    break;
	  }
        }
        E = rule.notCoveredBy(E);
      }
    }
  }
 
  /**
   * Add a rule to the ruleset.
   *
   * @param lastRule the last rule in the rule set
   * @param newRule the rule to be added
   * @return the new last rule in the rule set
   */
  private PrismRule addRule(PrismRule lastRule, PrismRule newRule) {
 
    if (lastRule == null) {
      m_rules = newRule;
    } else {
      lastRule.m_next = newRule;
    }
    return newRule;
  }
 
  /**
   * Add a test to this rule.
   *
   * @param rule the rule to which test is to be added
   * @param lastTest the rule's last test
   * @param newTest the test to be added
   * @return the new last test of the rule
   */
  private Test addTest(PrismRule rule, Test lastTest, Test newTest) {
 
    if (rule.m_test == null) {
      rule.m_test = newTest;
    } else {
      lastTest.m_next = newTest;
    }
    return newTest;
  }
 
  /**
   * Does E contain any examples in the class C?
   *
   * @param E the instances to be checked
   * @param C the class
   * @return true if there are any instances of class C
   * @throws Exception if something goes wrong
   */
  private static boolean contains(Instances E, int C) throws Exception {
 
    Enumeration enu = E.enumerateInstances();
    while (enu.hasMoreElements()) {
      if ((int) ((Instance) enu.nextElement()).classValue() == C) {
	return true;
      }
    }
    return false;
  }
 
  /**
   * Is this attribute mentioned in the rule?
   *
   * @param attr the attribute to be checked for
   * @param t test contained by rule
   * @return true if the attribute is mentioned in the rule
   */
  private static boolean isMentionedIn(Attribute attr, Test t) {
 
    if (t == null) { 
      return false;
    }
    if (t.m_attr == attr.index()) {
      return true;
    }
    return isMentionedIn(attr, t.m_next);
  }    
 
  /**
   * Prints a description of the classifier.
   *
   * @return a description of the classifier as a string
   */
  public String toString() {
 
    if (m_rules == null) {
      return "Prism: No model built yet.";
    }
    return "Prism rules\n----------\n" + m_rules.toString();
  }
 
  /**
   * Returns the revision string.
   * 
   * @return            the revision
   */
  public String getRevision() {
    return RevisionUtils.extract("$Revision: 8109 $");
  }
 
  /**
   * Main method for testing this class
   * 
   * @param args the commandline parameters
   */
  public static void main(String[] args) {
    runClassifier(new Prism(), args);
  }
}
surtout la class PrismRule et Test !!!