答案 0 :(得分:4)
您可以编写自己的Java procedures in Neo4j并从Cypher中调用它们。
对于回归函数,我们可以使用apache commons SimpleRegression class。
以下代码将创建名为regr
的过程,它将返回给定X和Y属性的基本回归值。
您可以将此程序从Cypher称为:regr('MY_LABEL', 'Y', 'X')
public class Regression {
@Context public GraphDatabaseService db;
// Result class
public static class Output {
public double r2;
public double avg_x;
public double avg_y;
public double slope;
public Output(double r2, double avg_x, double avg_y, double slope){
this.r2 = r2;
this.avg_x = avg_x;
this.avg_y = avg_y;
this.slope = slope;
}
}
@Procedure("regr")
public Stream<Output> regr(@Name("label") String label,
@Name("property_y") String y, @Name("property_x") String x ) {
SimpleRegression regr = new SimpleRegression(false);
double regr_avgx = 0;
double regr_avgy = 0;
int count = 0;
try (ResourceIterator it = db.findNodes(Label.label(label))) {
while (it.hasNext()) {
Node node = (Node)it.next();
if(node.hasProperty(x) && node.hasProperty(y))
{
Object prop_x = node.getProperty(x);
Object prop_y = node.getProperty(y);
regr_avgx += (double) ((Long)prop_x);
regr_avgy += (double) ((Long)prop_y);
regr.addData((double) ((Long)prop_x), (double) ((Long)prop_y));
count++;
}
}
}
regr_avgx /= count;
regr_avgy /= count;
return Stream.of(new Output(regr.getRSquare(), regr_avgx, regr_avgy, regr.getSlope()));
}
}