private void checkClassification( String target, List<Attribute> attributes, DecisionTreeBuilder dtBuilder, String... values) { List<Value> newValues = getValueListForTestInstance(attributes, values); Instance newInstance = new Instance(newValues); Value targetClassValue = dtBuilder.classify(newInstance); Assert.assertTrue(targetClassValue.getName().equals(target)); }
@Test public void testID3Simple2() throws Exception { String fileName = "ID3Simple2.arff"; DecisionTreeBuilder dt = new DecisionTreeBuilder(); ArffElements arffElements = ParserUtils.getArffElements(ROOT, fileName); List<Attribute> attributes = arffElements.getAttributes(); String csvFilePath = getCreatedCSVFilePath(fileName, arffElements.getData(), APP_TEMP_FOLDER); IInstances instances = InstancesFactory.getInstance() .createInstances("STAND_ALONE", attributes, attributes.size() - 1, csvFilePath); dt.train(instances); List<Value> newValues = getValueListForTestInstance( attributes, "Y", "N", "Y", "N", "None", "Mod", "N", "Y", "Thai", "0-10"); Instance newInstance = new Instance(newValues); Value targetClassValue = dt.classify(newInstance); Assert.assertTrue(targetClassValue.getName().equals("N")); }
@Test public void testID3Simple1() throws Exception { String fileName = "ID3Simple1.arff"; DecisionTreeBuilder dtBuilder = new DecisionTreeBuilder(); ArffElements arffElements = ParserUtils.getArffElements(ROOT, fileName); List<Attribute> attributes = arffElements.getAttributes(); String csvFilePath = getCreatedCSVFilePath(fileName, arffElements.getData(), APP_TEMP_FOLDER); IInstances instances = InstancesFactory.getInstance() .createInstances("STAND_ALONE", attributes, attributes.size() - 1, csvFilePath); dtBuilder.train(instances); DecisionTreeNode rootNode = dtBuilder.getRootNode(); Assert.assertTrue(rootNode.toString().equals("Outlook")); checkClassification("No", attributes, dtBuilder, "Sunny", "Hot", "High", "Strong"); checkClassification("No", attributes, dtBuilder, "Sunny", "Cool", "High", "Strong"); checkClassification("No", attributes, dtBuilder, "Sunny", "Mild", "High", "Weak"); checkClassification("Yes", attributes, dtBuilder, "Sunny", "Mild", "Normal", "Weak"); checkClassification("Yes", attributes, dtBuilder, "Sunny", "Hot", "Normal", "Strong"); checkClassification("Yes", attributes, dtBuilder, "Sunny", "Cool", "Normal", "Weak"); checkClassification("Yes", attributes, dtBuilder, "Overcast", "Mild", "High", "Weak"); checkClassification("Yes", attributes, dtBuilder, "Overcast", "Hot", "Normal", "Strong"); checkClassification("Yes", attributes, dtBuilder, "Overcast", "Cool", "High", "Weak"); checkClassification("Yes", attributes, dtBuilder, "Overcast", "Mild", "Normal", "Strong"); checkClassification("Yes", attributes, dtBuilder, "Rain", "Mild", "High", "Weak"); checkClassification("Yes", attributes, dtBuilder, "Rain", "Cool", "Normal", "Weak"); checkClassification("Yes", attributes, dtBuilder, "Rain", "Hot", "High", "Weak"); checkClassification("No", attributes, dtBuilder, "Rain", "Hot", "Normal", "Strong"); checkClassification("No", attributes, dtBuilder, "Rain", "Cool", "High", "Strong"); checkClassification("No", attributes, dtBuilder, "Rain", "Mild", "Normal", "Strong"); }