source: src/test/java/weka/filters/unsupervised/attribute/EMImputationTest.java @ 4

Last change on this file since 4 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 3.8 KB
Line 
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 * Copyright (C) 2009 Amri Napolitano
19 */
20
21package weka.filters.unsupervised.attribute;
22
23import weka.core.Attribute;
24import weka.core.AttributeStats;
25import weka.core.Instances;
26import weka.core.TestInstances;
27import weka.filters.AbstractFilterTest;
28import weka.filters.Filter;
29import weka.filters.unsupervised.attribute.StringToNominal;
30import weka.filters.unsupervised.attribute.NominalToBinary;
31import weka.filters.unsupervised.attribute.Remove;
32
33import junit.framework.Test;
34import junit.framework.TestSuite;
35
36/**
37 * Tests EMImputation. Run from the command line with:<p>
38 * java weka.filters.unsupervised.attribute.EMImputationTest
39 *
40 * @author Amri Napolitano
41 * @version $Revision: 5374 $
42 */
43public class EMImputationTest extends AbstractFilterTest {
44 
45  public EMImputationTest(String name) { super(name);  }
46
47  /** Creates a default EMImputation */
48  public Filter getFilter() {
49    return new EMImputation();
50  }
51
52  protected void setUp() throws Exception {
53    super.setUp();
54
55    Instances temp = new Instances(m_Instances);
56    for (int j = 0; j < 2; j++) {
57      for (int i = 0; i < temp.numInstances(); i++) {
58        m_Instances.add(temp.instance(i));
59      }
60    }
61
62    // now just filter the instances to convert String attributes
63    // and binarize nominal attributes
64    StringToNominal stn = new StringToNominal();
65    stn.setAttributeRange("first-last");
66    stn.setInputFormat(m_Instances);
67    m_Instances = Filter.useFilter(m_Instances, stn);
68    NominalToBinary ntb = new NominalToBinary();
69    ntb.setInputFormat(m_Instances);
70    m_Instances = Filter.useFilter(m_Instances, ntb);
71
72    // remove the last column (date attribute)
73    Remove r = new Remove();
74    r.setAttributeIndices("last");
75    r.setInputFormat(m_Instances);
76    m_Instances = Filter.useFilter(m_Instances, r);
77  }
78
79  protected Instances getFilteredClassifierData() throws Exception {
80    TestInstances test;
81    Instances result;
82   
83    test = TestInstances.forCapabilities(getFilter().getCapabilities());
84    test.setClassIndex(TestInstances.CLASS_IS_LAST);
85    test.setNumNumeric(3);
86
87    result = test.generate();
88
89    return result;
90  }
91
92  public void testTypical() {
93    Instances result = useFilter();
94    // Number of attributes shouldn't change
95    assertEquals(m_Instances.numAttributes(), result.numAttributes());
96    // Number of instances may change (if an instance has all missing values)
97    // assertEquals(m_Instances.numInstances(), result.numInstances());
98    for (int j = 0; j < result.numAttributes(); j++) {
99      if (j == m_Instances.classIndex() && m_Instances.attribute(j).isNumeric() == false) {
100        continue;
101      }
102      AttributeStats currentStats = m_Instances.attributeStats(j);
103      if (currentStats.distinctCount < 2) {
104        continue;
105      }
106      assertTrue("All missing values except for those in nonnumeric class " +
107                  "attributes should be replaced.", 
108                  result.attributeStats(j).missingCount == 0);
109    }
110  }
111
112  public static Test suite() {
113    return new TestSuite(EMImputationTest.class);
114  }
115
116  public static void main(String[] args){
117    junit.textui.TestRunner.run(suite());
118  }
119
120}
Note: See TracBrowser for help on using the repository browser.