@Override public void doTransform(FeatureVector featureVector) { Map<String, Map<String, Double>> floatFeatures = featureVector.getFloatFeatures(); if (floatFeatures == null) { return; } Map<String, Double> feature1 = floatFeatures.get(fieldName1); if (feature1 == null || feature1.isEmpty()) { return; } Util.optionallyCreateStringFeatures(featureVector); Map<String, Set<String>> stringFeatures = featureVector.getStringFeatures(); Set<String> output = Util.getOrCreateStringFeature(outputName, stringFeatures); for (Entry<String, Double> feature : feature1.entrySet()) { output.add(logQuantize(feature.getKey(), feature.getValue())); } }
@Override public void doTransform(FeatureVector featureVector) { Map<String, Map<String, Double>> floatFeatures = featureVector.getFloatFeatures(); if (floatFeatures == null) { return; } Map<String, Double> feature1 = floatFeatures.get(fieldName1); if (feature1 == null || feature1.isEmpty()) { return; } Map<String, Double> output = Util.getOrCreateFloatFeature(outputName, floatFeatures); for (Entry<String, Double> feature : feature1.entrySet()) { Double dbl = LinearLogQuantizeTransform.quantize(feature.getValue(), bucket); Double newVal = feature.getValue() - dbl; String name = feature.getKey() + '[' + bucket + "]=" + dbl; output.put(name, newVal); } }
@Override public void doTransform(FeatureVector featureVector) { Map<String, Map<String, Double>> floatFeatures = featureVector.getFloatFeatures(); if (floatFeatures == null) { return; } Map<String, Double> feature1 = floatFeatures.get(fieldName1); if (feature1 == null) { return; } Map<String, Double> feature2 = floatFeatures.get(fieldName2); if (feature2 == null) { return; } Double div = feature2.get(key2); if (div == null) { return; } Double scale = 1.0 / (constant + div); Map<String, Double> output = Util.getOrCreateFloatFeature(outputName, floatFeatures); Set<String> allKeys = feature1.keySet(); for (String key : allKeys) { if (keys == null || keys.contains(key)) { Double val = feature1.get(key); if (val != null) { output.put(key + "-d-" + key2, val * scale); } } } }
@Test public void testLoad() { CharArrayWriter charWriter = new CharArrayWriter(); BufferedWriter writer = new BufferedWriter(charWriter); ModelHeader header = new ModelHeader(); header.setModelType("additive"); header.setNumRecords(4); ArrayList<Double> ws = new ArrayList<Double>(); ws.add(5.0); ws.add(10.0); ws.add(-20.0); ArrayList<Double> wl = new ArrayList<Double>(); wl.add(1.0); wl.add(2.0); ModelRecord record1 = new ModelRecord(); record1.setModelHeader(header); ModelRecord record2 = new ModelRecord(); record2.setFunctionForm(FunctionForm.Spline); record2.setFeatureFamily("spline_float"); record2.setFeatureName("aaa"); record2.setWeightVector(ws); record2.setMinVal(1.0); record2.setMaxVal(3.0); ModelRecord record3 = new ModelRecord(); record3.setFunctionForm(FunctionForm.Spline); record3.setFeatureFamily("spline_string"); record3.setFeatureName("bbb"); record3.setWeightVector(ws); record3.setMinVal(1.0); record3.setMaxVal(2.0); ModelRecord record4 = new ModelRecord(); record4.setFunctionForm(FunctionForm.Linear); record4.setFeatureFamily("linear_float"); record4.setFeatureName("ccc"); record4.setWeightVector(wl); ModelRecord record5 = new ModelRecord(); record5.setFunctionForm(FunctionForm.Linear); record5.setFeatureFamily("linear_string"); record5.setFeatureName("ddd"); record5.setWeightVector(wl); try { writer.write(Util.encode(record1) + "\n"); writer.write(Util.encode(record2) + "\n"); writer.write(Util.encode(record3) + "\n"); writer.write(Util.encode(record4) + "\n"); writer.write(Util.encode(record5) + "\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(2.0f, 7.0f); try { Optional<AbstractModel> model = ModelFactory.createFromReader(reader); assertTrue(model.isPresent()); float score = model.get().scoreItem(featureVector); assertEquals(8.0f + 10.0f + 15.0f, score, 0.001f); } catch (IOException e) { assertTrue("Could not read", false); } }