By default, the classification template reads 4 properties of a user entity: "attr0", "attr1", "attr2" and "plan". You can modify the default DataSource to read your custom properties or different Entity Type.
In this example, we modify DataSource to read properties "featureA", "featureB", "featureC", "featureD" and "label" for entity type "item". You can find the complete modified source code here.
Note: you also need import events with these properties accordingly.
Modify the readTraining()
and readEval()
in DataSource.scala:
- modify the
entityType
parameter - modify the list of properties names in the
required
parameter - modify how to create the
LabeledPoint
object using the entity properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | def readTraining(sc: SparkContext): TrainingData = { ... val labeledPoints: RDD[LabeledPoint] = PEventStore.aggregateProperties( appName = dsp.appName, entityType = "item", // MODIFIED // only keep entities with these required properties defined required = Some(List( // MODIFIED "featureA", "featureB", "featureC", "featureD", "label")))(sc) // aggregateProperties() returns RDD pair of // entity ID and its aggregated properties .map { case (entityId, properties) => try { // MODIFIED LabeledPoint(properties.get[Double]("label"), Vectors.dense(Array( properties.get[Double]("featureA"), properties.get[Double]("featureB"), properties.get[Double]("featureC"), properties.get[Double]("featureD") )) ) } catch { case e: Exception => { logger.error(s"Failed to get properties ${properties} of" + s" ${entityId}. Exception: ${e}.") throw e } } }.cache() ... } |
Lastly, redefine the Query class parameters to take in four double values: featureA, featureB, featureC, and featureD. Now, to send a query, the field names must be changed accordingly:
1 | $ curl -H "Content-Type: application/json" -d '{ "featureA":2, "featureB":0, "featureC":0, "featureD":0 }' http://localhost:8000/queries.json |
That's it! Now your classification engine is using different properties as training data.