Skip to content

Instantly share code, notes, and snippets.

View thigm85's full-sized avatar

Thiago G. Martins thigm85

View GitHub Profile
!pip install numpy pandas pyvespa lightgbm
import json
import lightgbm as lgb
import numpy as np
import pandas as pd
!rm -fr lightgbm
vespa_docker.container.stop(timeout=600)
vespa_docker.container.remove()
assert features["model_prediction"].tolist() == features["vespa_relevance"].tolist()
vespa_relevance = []
for idx, row in features.iterrows():
vespa_relevance.append(
compute_vespa_relevance(
id_value=idx,
query_value = row["query(value)"]
)
)
features["vespa_relevance"] = vespa_relevance
features
def compute_vespa_relevance(id_value, query_value):
hits = app.query(
body={
"yql": "select * from sources * where id = {}".format(str(id_value)),
"ranking": "classify",
"ranking.features.query(value)": features.loc[id_value, "query(value)"],
"hits": 1
}
).hits
return hits[0]["relevance"]
features["model_prediction"] = model.predict(features)
features
feed_batch = [
{
"id": idx,
"fields": {
"id": idx,
"numeric": row["attribute(numeric)"],
"categorical": row["attribute(categorical)"]
}
} for idx, row in features.iterrows()
]
from vespa.deployment import VespaDocker
vespa_docker = VespaDocker()
app = vespa_docker.deploy_from_disk(
application_name="lightgbm",
application_root="lightgbm"
)
with open("lightgbm/models/lightgbm_model.json", "w") as f:
json.dump(model.dump_model(), f, indent=2)
from pathlib import Path
Path("lightgbm").mkdir(parents=True, exist_ok=True)
app_package.to_files("lightgbm")