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);
}
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"));
}
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;
}
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;
}
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;
}
Aggregations