17.6.8 Inferring Anomalies

You can use a trained model to infer anomaly scores or labels for unseen nodes and store them in the database as described in the following code:

opg4j>  var vertexScores = model.inferAnomalyScores(fullGraph, fullGraph.getVertices()).flattenAll()
opg4j> vertexScores.write().
    db().
    name("vertex scores").
    tablename("vertexScores").  
    overwrite(true).             
    store()
PgxFrame vertexScores = model.inferAnomalyScores(fullGraph,fullGraph.getVertices()).flattenAll();
vertexScores.write()
    .db()
    .name("vertex scores")
    .tablename("vertexScores") 
    .overwrite(true)            
    .store();
vertex_scores = model.infer_anomaly_scores(full_Graph,full_Graph.get_vertices()).flatten_all()
vertex_scores.write().db().table_name("table_name").name("vertex_scores").overwrite(True).store()

If you know the contamination factor of the data, you can use it to find a good threshold:

opg4j>  var vertexLabels = model.inferAnomalyScores(fullGraph, fullGraph.getVertices()).flattenAll()
opg4j> vertexLabels.write().
    db().
    name("vertex labels").
    tablename("vertexLabels").  
    overwrite(true).             
    store()
PgxFrame vertexLabels = model.inferAnomalyScores(fullGraph,fullGraph.getVertices()).flattenAll();
vertexLabels.write()
    .db()
    .name("vertex labels")
    .tablename("vertexLabels") 
    .overwrite(true)            
    .store();
vertex_labels = model.infer_anomaly_scores(full_Graph,full_Graph.get_vertices()).flatten_all()
vertex_labels.write().db().table_name("table_name").name("vertex_labels").overwrite(True).store()

All the preceding examples assume that you are inferring anomalies for the model in the current logged in database. If you must infer anomalies in a different database, then you must additionally provide the database credentials such as username, password, and jdbcUrl to the inferAnomalyScores method.

opg4j> vertexScores.write().
     db().
     name("vertex scores").
     tablename("vertexScores").         
     username("user").                   
     password("password").               
     jdbcUrl("jdbcUrl").                 
     overwrite(true).                    
     store()
vertexScores.write()
    .db()
    .name("vertex scores")
    .tablename("vertexScores")          
    .username("user")                   
    .password("password")              
    .jdbcUrl("jdbcUrl")                 
    .overwrite(true)                    
    .store();
vertex_scores.write().db().table_name("table_name") \
                           .name("vertex scores") \
                           .username("user") \      
                           .password("password") \  
                           .jdbc_url("jdbc_url") \
                           .overwrite(True) \
                           .store()