Skip to content

Instantly share code, notes, and snippets.

@benwtrent
Created March 7, 2019 21:50
Show Gist options
  • Save benwtrent/945ca9fc42d010c1cfac93969771b048 to your computer and use it in GitHub Desktop.
Save benwtrent/945ca9fc42d010c1cfac93969771b048 to your computer and use it in GitHub Desktop.
Linear Regression Model inference with Painless in ElasticSearch
PUT _scripts/linear_regression_inference
{
"script": {
"lang": "painless",
"source": """
double total = params.intercept;
for (int i = 0; i < params.coefs.length; ++i) {
total += params.coefs.get(i) * doc[params['x'+i]].value;
}
return total;
"""
}
}
Example:
GET diabetes_test/_search
{
"script_fields": {
"regression_score": {
"script": {
"id": "linear_regression_inference",
"params": {
"coefs": [-35.55683674, -243.1692265, 562.75404632, 305.47203008, -662.78772128, 324.27527477, 24.78193291, 170.33056502, 731.67810787, 43.02846824],
"intercept": 152.53813351954059,
"x0": "age",
"x1": "sex",
"x2": "bmi",
"x3": "bp",
"x4": "s1",
"x5": "s2",
"x6": "s3",
"x7": "s4",
"x8": "s5",
"x9": "s6"
}
}
}
}
}
>{
"took" : 12,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 89,
"max_score" : 1.0,
"hits" : [
{
"_index" : "diabetes_test",
"_type" : "_doc",
"_id" : "CbwAWmkBea2J_i2W7BYc",
"_score" : 1.0,
"fields" : {
"regression_score" : [
248.93170645607083
]
}
},...
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment