NaiveBayes extends AbstractClassifier<Data,String> { @SuppressWarnings("unchecked") @Override public void learn(Data item) { if( labelAttribute == null ){ for( String name : item.keySet() ) if( name.startsWith( "_class" ) ){ labelAttribute = name; break; } } if( item.get( labelAttribute ) == null ){ log.warn( "Not processing unlabeled data item {}", item ); return; } String clazz = item.get( labelAttribute ).toString(); log.debug( "Learning from example with label={}", clazz ); if( this.classDistribution == null ) this.classDistribution = new NominalDistributionModel<String>(); if( log.isDebugEnabled() ){ log.debug( "Classes: {}", classDistribution.getElements() ); for( String t : classDistribution.getElements() ) log.debug( " {}: {}", t, classDistribution.getCount( t ) ); } for( String attribute : item.keySet() ){ if( attribute.equalsIgnoreCase( labelAttribute ) ){ classDistribution.update( clazz ); } else { Object obj = item.get( attribute ); if( obj.getClass().equals( Double.class ) ){ Double value = (Double) obj; log.debug( "Handling numerical case ({}) with value {}", obj, value ); Distribution<Double> numDist = (Distribution<Double>) distributions.get( attribute ); if( numDist == null ){ numDist = this.createNumericalDistribution(); log.debug( "Creating new numerical distribution model for attribute {}", attribute ); distributions.put( attribute, numDist ); } numDist.update( value ); } else { String value = this.getNominalCondition( attribute, item ); log.debug( "Handling nominal case for [ {} | {} ]", value, "class=" + clazz ); Distribution<String> nomDist = (Distribution<String>) distributions.get( clazz ); if( nomDist == null ){ nomDist = this.createNominalDistribution(); log.debug( "Creating new nominal distribution model for attribute {}, {}", attribute, "class=" + clazz ); distributions.put( clazz, nomDist ); } nomDist.update( value ); } } } } NaiveBayes();  NaiveBayes( String labelAttribute ); String getLabelAttribute(); void setLabelAttribute(String labelAttribute); Double getLaplaceCorrection(); void setLaplaceCorrection(Double laplaceCorrection); Double getConfidenceGap(); void setConfidenceGap(Double confidenceGap); Boolean getWop(); void setWop(Boolean wop); @SuppressWarnings("unchecked") Map<String,Double> vote(Data item); @Override String predict( Data item ); String getNominalCondition( String attribute, Data item ); @SuppressWarnings("unchecked") @Override void learn(Data item); Distribution<String> getClassDistribution(); @SuppressWarnings("unchecked") List<Distribution<Double>> getNumericalDistributions(); Distribution<String> createNominalDistribution(); Distribution<Double> createNumericalDistribution();  }
@Test public void testLearn() throws Exception { List<Data> dataset = new ArrayList<Data>(); ICsvListReader listReader = new CsvListReader( new FileReader("src/test/resources/golf.csv"), CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE); listReader.getHeader(true); List<String> list; while( (list = listReader.read()) != null ) { Data data = new DataImpl(); data.put("outlook", list.get(0)); data.put("temperature", Integer.parseInt(list.get(1))); data.put("humidity", Integer.parseInt(list.get(2))); data.put("wind", Boolean.parseBoolean(list.get(3))); data.put("play", list.get(4)); dataset.add(data); } NaiveBayes nb = new NaiveBayes(); nb.setLabelAttribute( "play" ); for (Data data : dataset) { nb.learn( data ); } Data test = new DataImpl(); test.put( "outlook", "sunny" ); test.put( "temperature", "cool" ); test.put( "humidity", "high" ); test.put( "windy", "TRUE" ); String prediction = nb.predict( test ); log.info( "item is: {}", test ); log.info( "Prediction is: {}", prediction ); test.put( "outlook", "overcast" ); test.put( "temperature", "mild" ); test.put( "humidity", "normal" ); test.put( "windy", "FALSE" ); prediction = nb.predict( test ); log.info( "item is: {}", test ); log.info( "Prediction is: {}", prediction ); test.put( "outlook", "overcast" ); test.put( "temperature", "hot" ); test.put( "humidity", "normal" ); test.put( "windy", "FALSE" ); prediction = nb.predict( test ); log.info( "item is: {}", test ); log.info( "Prediction is: {}", prediction ); Assert.assertEquals( "yes", prediction ); }