Search in sources :

Example 6 with Example

use of com.airbnb.aerosolve.core.Example in project aerosolve by airbnb.

the class FeaturesTest method toExampleMultiClass.

@Test
public void toExampleMultiClass() throws Exception {
    Example example = createMultiClassFeature().toExample(true);
    FeatureVector featureVector = example.getExample().get(0);
    final Map<String, Map<String, Double>> floatFeatures = featureVector.getFloatFeatures();
    assertEquals(1, floatFeatures.size());
    Map<String, Double> floatFeature = floatFeatures.get(Features.LABEL);
    assertEquals(2, floatFeature.size());
    assertEquals(1, floatFeature.get("a"), 0);
    assertEquals(2, floatFeature.get("b"), 0);
}
Also used : FeatureVector(com.airbnb.aerosolve.core.FeatureVector) Example(com.airbnb.aerosolve.core.Example) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 7 with Example

use of com.airbnb.aerosolve.core.Example in project aerosolve by airbnb.

the class FeaturesTest method toExample.

@Test
public void toExample() throws Exception {
    Example example = createFeature().toExample(false);
    FeatureVector featureVector = example.getExample().get(0);
    final Map<String, Set<String>> stringFeatures = featureVector.getStringFeatures();
    final Map<String, Map<String, Double>> floatFeatures = featureVector.getFloatFeatures();
    final Map<String, String> metadata = example.getMetadata();
    // we have default BIAS
    assertEquals(4, stringFeatures.size());
    Set<String> stringFeature = stringFeatures.get("f");
    assertEquals(1, stringFeature.size());
    assertTrue(stringFeature.contains("raw_feature"));
    stringFeature = stringFeatures.get("K");
    assertEquals(2, stringFeature.size());
    assertTrue(stringFeature.contains("star:monkey"));
    assertTrue(stringFeature.contains("good:F"));
    stringFeature = stringFeatures.get("X");
    assertNull(stringFeature);
    stringFeature = stringFeatures.get(Features.MISS);
    assertEquals(1, stringFeature.size());
    assertTrue(stringFeature.contains("X_jump"));
    assertEquals(2, floatFeatures.size());
    Map<String, Double> floatFeature = floatFeatures.get("S");
    assertEquals(1, floatFeature.size());
    assertEquals(10.0, floatFeature.get("speed"), 0);
    floatFeature = floatFeatures.get(Features.LABEL);
    assertEquals(1, floatFeature.size());
    assertEquals(5.0, floatFeature.get(Features.LABEL_FEATURE_NAME), 0);
    assertEquals(1, metadata.size());
    assertEquals("12345", metadata.get("id_listing"));
}
Also used : FeatureVector(com.airbnb.aerosolve.core.FeatureVector) Set(java.util.Set) Example(com.airbnb.aerosolve.core.Example) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 8 with Example

use of com.airbnb.aerosolve.core.Example in project aerosolve by airbnb.

the class Debug method loadExampleFromResource.

/*
  loadExampleFromResource read example from resources folder, i.e. test/resources
  use it on unit test to load example from disk
 */
public static Example loadExampleFromResource(String name) {
    URL url = Debug.class.getResource("/" + name);
    try {
        Path path = Paths.get(url.toURI());
        byte[] bytes = Files.readAllBytes(path);
        TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory());
        Example example = new Example();
        deserializer.deserialize(example, bytes);
        return example;
    } catch (Exception e) {
        e.printStackTrace();
    }
    assert (false);
    return null;
}
Also used : Path(java.nio.file.Path) TDeserializer(org.apache.thrift.TDeserializer) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) Example(com.airbnb.aerosolve.core.Example) URL(java.net.URL)

Example 9 with Example

use of com.airbnb.aerosolve.core.Example in project aerosolve by airbnb.

the class FeatureVectorGen method toSingleFeatureVectorExample.

public static Example toSingleFeatureVectorExample(Features features, List<StringFamily> stringFamilies, List<FloatFamily> floatFamilies) {
    Example example = new Example();
    FeatureVector featureVector = toFeatureVector(features, stringFamilies, floatFamilies);
    example.addToExample(featureVector);
    return example;
}
Also used : FeatureVector(com.airbnb.aerosolve.core.FeatureVector) Example(com.airbnb.aerosolve.core.Example)

Example 10 with Example

use of com.airbnb.aerosolve.core.Example in project aerosolve by airbnb.

the class Features method toExample.

// TODO  make it more generic, for example, taking care of dense feature
public Example toExample(boolean isMultiClass) {
    assert (names.length == values.length);
    Example example = new Example();
    FeatureVector featureVector = new FeatureVector();
    example.addToExample(featureVector);
    // Set string features.
    final Map<String, Set<String>> stringFeatures = new HashMap<>();
    featureVector.setStringFeatures(stringFeatures);
    final Map<String, Map<String, Double>> floatFeatures = new HashMap<>();
    featureVector.setFloatFeatures(floatFeatures);
    // create LABEL family
    floatFeatures.put(LABEL, new HashMap<>());
    final Set<String> bias = new HashSet<>();
    final Set<String> missing = new HashSet<>();
    bias.add("B");
    stringFeatures.put("BIAS", bias);
    stringFeatures.put(MISS, missing);
    // metadata map
    final Map<String, String> metadata = new HashMap<>();
    example.setMetadata(metadata);
    for (int i = 0; i < names.length; i++) {
        String name = names[i];
        Object value = values[i];
        if (isMetadata(name)) {
            metadata.put(name.substring(METADATA_PREFIX.length()), value == null ? null : value.toString());
        } else {
            if (value == null) {
                missing.add(name);
            } else {
                Pair<String, String> feature = getFamily(name);
                if (value instanceof String) {
                    String str = (String) value;
                    if (isMultiClass && isLabel(feature)) {
                        addMultiClassLabel(str, floatFeatures);
                    } else {
                        addStringFeature(str, feature, stringFeatures);
                    }
                } else if (value instanceof Boolean) {
                    Boolean b = (Boolean) value;
                    addBoolFeature(b, feature, stringFeatures);
                } else {
                    addNumberFeature((Number) value, feature, floatFeatures);
                }
            }
        }
    }
    return example;
}
Also used : FeatureVector(com.airbnb.aerosolve.core.FeatureVector) Example(com.airbnb.aerosolve.core.Example)

Aggregations

Example (com.airbnb.aerosolve.core.Example)11 FeatureVector (com.airbnb.aerosolve.core.FeatureVector)9 Test (org.junit.Test)5 Map (java.util.Map)4 HashMap (java.util.HashMap)3 Set (java.util.Set)3 ArrayList (java.util.ArrayList)2 TDeserializer (org.apache.thrift.TDeserializer)2 DebugScoreDiffRecord (com.airbnb.aerosolve.core.DebugScoreDiffRecord)1 DebugScoreRecord (com.airbnb.aerosolve.core.DebugScoreRecord)1 KDTreeNode (com.airbnb.aerosolve.core.KDTreeNode)1 ModelRecord (com.airbnb.aerosolve.core.ModelRecord)1 HashCode (com.google.common.hash.HashCode)1 Hasher (com.google.common.hash.Hasher)1 Hashing (com.google.common.hash.Hashing)1 Config (com.typesafe.config.Config)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1