use of com.airbnb.aerosolve.core.FeatureVector in project aerosolve by airbnb.
the class AdditiveModelTest method makeFeatureVector.
public FeatureVector makeFeatureVector(float a, float c) {
FeatureVector featureVector = new FeatureVector();
HashMap stringFeatures = new HashMap<String, HashSet<String>>();
featureVector.setStringFeatures(stringFeatures);
HashMap floatFeatures = new HashMap<String, HashMap<String, Double>>();
featureVector.setFloatFeatures(floatFeatures);
// prepare string features
Set list1 = new HashSet<String>();
// weight = 5.0f
list1.add("bbb");
// this feature is missing in the model
list1.add("ggg");
stringFeatures.put("spline_string", list1);
Set list2 = new HashSet<String>();
// weight = 3.0f
list2.add("ddd");
// this feature is missing in the model
list2.add("ggg");
stringFeatures.put("linear_string", list2);
featureVector.setStringFeatures(stringFeatures);
// prepare float features
HashMap splineFloat = new HashMap<String, Double>();
HashMap linearFloat = new HashMap<String, Double>();
floatFeatures.put("spline_float", splineFloat);
floatFeatures.put("linear_float", linearFloat);
// corresponds to Spline(1.0f, 3.0f, {5, 10, -20})
splineFloat.put("aaa", (double) a);
// missing features
splineFloat.put("ggg", 1.0);
// weight = 1+2*c
linearFloat.put("ccc", (double) c);
// missing features
linearFloat.put("ggg", 10.0);
return featureVector;
}
use of com.airbnb.aerosolve.core.FeatureVector in project aerosolve by airbnb.
the class LinearModelTest method makeFeatureVector.
public FeatureVector makeFeatureVector() {
Set list = new HashSet<String>();
list.add("aaa");
list.add("bbb");
// add a feature that is missing in the model
list.add("ccc");
HashMap stringFeatures = new HashMap<String, ArrayList<String>>();
stringFeatures.put("string_feature", list);
FeatureVector featureVector = new FeatureVector();
featureVector.setStringFeatures(stringFeatures);
return featureVector;
}
use of com.airbnb.aerosolve.core.FeatureVector in project aerosolve by airbnb.
the class LinearModelTest method testLoad.
@Test
public void testLoad() {
CharArrayWriter charWriter = new CharArrayWriter();
BufferedWriter writer = new BufferedWriter(charWriter);
ModelHeader header = new ModelHeader();
header.setModelType("linear");
header.setNumRecords(1);
ModelRecord record1 = new ModelRecord();
record1.setModelHeader(header);
ModelRecord record2 = new ModelRecord();
record2.setFeatureFamily("string_feature");
record2.setFeatureName("bbb");
record2.setFeatureWeight(0.9f);
try {
writer.write(Util.encode(record1) + "\n");
writer.write(Util.encode(record2) + "\n");
writer.close();
} catch (IOException e) {
assertTrue("Could not write", false);
}
String serialized = charWriter.toString();
assertTrue(serialized.length() > 0);
StringReader strReader = new StringReader(serialized);
BufferedReader reader = new BufferedReader(strReader);
FeatureVector featureVector = makeFeatureVector();
try {
Optional<AbstractModel> model = ModelFactory.createFromReader(reader);
assertTrue(model.isPresent());
float score = model.get().scoreItem(featureVector);
assertTrue(score > 0.89f);
assertTrue(score < 0.91f);
} catch (IOException e) {
assertTrue("Could not read", false);
}
}
use of com.airbnb.aerosolve.core.FeatureVector in project aerosolve by airbnb.
the class LinearModelTest method testScoreEmptyFeature.
@Test
public void testScoreEmptyFeature() {
FeatureVector featureVector = new FeatureVector();
LinearModel model = new LinearModel();
float score = model.scoreItem(featureVector);
assertTrue(score < 1e-10f);
assertTrue(score > -1e-10f);
}
use of com.airbnb.aerosolve.core.FeatureVector in project aerosolve by airbnb.
the class DecisionTreeTransformTest method testTransformAt.
public void testTransformAt(double x, double y, String expectedLeaf, double expectedOutput) {
Config config = ConfigFactory.parseString(makeConfig());
Transform transform = TransformFactory.createTransform(config, "test_tree");
FeatureVector featureVector;
featureVector = makeFeatureVector(x, y);
transform.doTransform(featureVector);
Map<String, Set<String>> stringFeatures = featureVector.getStringFeatures();
assertEquals(2, stringFeatures.size());
Set<String> out = featureVector.stringFeatures.get("LEAF");
for (String entry : out) {
log.info(entry);
}
assertTrue(out.contains(expectedLeaf));
Map<String, Double> treeOutput = featureVector.floatFeatures.get("SCORE");
assertTrue(treeOutput.containsKey("TREE0"));
assertEquals(expectedOutput, treeOutput.get("TREE0"), 0.1);
}
Aggregations