Skip to content

Instantly share code, notes, and snippets.

Avatar

Thiago G. Martins thigm85

View GitHub Profile
View 0.py
!pip install numpy pandas pyvespa lightgbm
import json
import lightgbm as lgb
import numpy as np
import pandas as pd
View 14.py
!rm -fr lightgbm
vespa_docker.container.stop(timeout=600)
vespa_docker.container.remove()
View 13.py
assert features["model_prediction"].tolist() == features["vespa_relevance"].tolist()
View 12.py
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
View 11.py
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"]
View 10.py
features["model_prediction"] = model.predict(features)
features
View 9.py
feed_batch = [
{
"id": idx,
"fields": {
"id": idx,
"numeric": row["attribute(numeric)"],
"categorical": row["attribute(categorical)"]
}
} for idx, row in features.iterrows()
]
View 8.py
from vespa.deployment import VespaDocker
vespa_docker = VespaDocker()
app = vespa_docker.deploy_from_disk(
application_name="lightgbm",
application_root="lightgbm"
)
View 7.py
with open("lightgbm/models/lightgbm_model.json", "w") as f:
json.dump(model.dump_model(), f, indent=2)
View 6.py
from pathlib import Path
Path("lightgbm").mkdir(parents=True, exist_ok=True)
app_package.to_files("lightgbm")