Search in sources :

Example 11 with FeatureVector

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;
}
Also used : FeatureVector(com.airbnb.aerosolve.core.FeatureVector) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 12 with 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;
}
Also used : FeatureVector(com.airbnb.aerosolve.core.FeatureVector) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 13 with 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);
    }
}
Also used : FeatureVector(com.airbnb.aerosolve.core.FeatureVector) ModelHeader(com.airbnb.aerosolve.core.ModelHeader) ModelRecord(com.airbnb.aerosolve.core.ModelRecord) Test(org.junit.Test)

Example 14 with FeatureVector

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);
}
Also used : FeatureVector(com.airbnb.aerosolve.core.FeatureVector) Test(org.junit.Test)

Example 15 with FeatureVector

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);
}
Also used : FeatureVector(com.airbnb.aerosolve.core.FeatureVector) Config(com.typesafe.config.Config)

Aggregations

FeatureVector (com.airbnb.aerosolve.core.FeatureVector)225 Test (org.junit.Test)154 Config (com.typesafe.config.Config)117 Set (java.util.Set)59 HashMap (java.util.HashMap)58 HashSet (java.util.HashSet)50 Map (java.util.Map)46 ArrayList (java.util.ArrayList)11 Example (com.airbnb.aerosolve.core.Example)9 List (java.util.List)9 ModelRecord (com.airbnb.aerosolve.core.ModelRecord)5 ModelHeader (com.airbnb.aerosolve.core.ModelHeader)4 BufferedReader (java.io.BufferedReader)4 IOException (java.io.IOException)4 MulticlassScoringResult (com.airbnb.aerosolve.core.MulticlassScoringResult)3 BufferedWriter (java.io.BufferedWriter)3 CharArrayWriter (java.io.CharArrayWriter)3 StringReader (java.io.StringReader)3 DebugScoreRecord (com.airbnb.aerosolve.core.DebugScoreRecord)2 SimpleEntry (java.util.AbstractMap.SimpleEntry)2