### Question:
FrequencyEncoder extends AbstractDatasetFeatureVectorTransformer { @Override public FeatureVector apply(FeatureVector featureVector) { InstanceBuilder builder = new InstanceBuilder(); for (String valueName : nominalValueNames) { Value value = featureVector.get(valueName); NominalValueStatistics valueStats = (NominalValueStatistics) statistics.getValueStatistics(valueName); if (!value.isNull()) { int count = valueStats.getCount(((NominalValue) value).getString()); float frequency = (float) count / totalCount; builder.set(valueName + FEATURE_SUFFIX, frequency); } else if (nullValueStrategy == NullValueStrategy.ASSIGN_FREQUENCY) { int count = valueStats.getNumNullValues(); float frequency = (float) count / totalCount; builder.set(valueName + FEATURE_SUFFIX, frequency); } else if (nullValueStrategy == NullValueStrategy.KEEP_NULL) { builder.setNull(valueName + FEATURE_SUFFIX); } } return new AppendedVector(featureVector, builder.create()); } FrequencyEncoder(Dataset dataset);  FrequencyEncoder(Dataset dataset, NullValueStrategy nullValueStrategy); @Override FeatureInformation getFeatureInformation(FeatureInformation featureInformation); @Override FeatureVector apply(FeatureVector featureVector);  }### Answer:
@Test public void testWithRegularValues() { FrequencyEncoder frequencyEncoder = new FrequencyEncoder(dataset); Instance testInstance = new InstanceBuilder().set("f1", "a").set("f2", "a").create(true); FeatureVector transformedVector = frequencyEncoder.apply(testInstance).getVector(); assertEquals(3. / 8, transformedVector.getNumeric("f1_frequency").getDouble(), DELTA); assertEquals(5. / 8, transformedVector.getNumeric("f2_frequency").getDouble(), DELTA); }
@Test public void testWithNullValues_frequency() { FrequencyEncoder frequencyEncoder = new FrequencyEncoder(dataset, NullValueStrategy.ASSIGN_FREQUENCY); Instance testInstance = new InstanceBuilder().setNull("f1").setNull("f2").create(true); FeatureVector transformedVector = frequencyEncoder.apply(testInstance).getVector(); assertEquals(0, transformedVector.getNumeric("f1_frequency").getDouble(), DELTA); assertEquals(3. / 8, transformedVector.getNumeric("f2_frequency").getDouble(), DELTA); }
@Test public void testWithNullValues_null() { FrequencyEncoder frequencyEncoder = new FrequencyEncoder(dataset, NullValueStrategy.KEEP_NULL); Instance testInstance = new InstanceBuilder().setNull("f1").setNull("f2").create(true); FeatureVector transformedVector = frequencyEncoder.apply(testInstance).getVector(); assertTrue(transformedVector.get("f1_frequency").isNull()); assertTrue(transformedVector.get("f2_frequency").isNull()); }