View 0.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!pip install numpy pandas pyvespa lightgbm | |
import json | |
import lightgbm as lgb | |
import numpy as np | |
import pandas as pd |
View 14.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!rm -fr lightgbm | |
vespa_docker.container.stop(timeout=600) | |
vespa_docker.container.remove() |
View 13.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
assert features["model_prediction"].tolist() == features["vespa_relevance"].tolist() |
View 12.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
features["model_prediction"] = model.predict(features) | |
features |
View 9.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
feed_batch = [ | |
{ | |
"id": idx, | |
"fields": { | |
"id": idx, | |
"numeric": row["attribute(numeric)"], | |
"categorical": row["attribute(categorical)"] | |
} | |
} for idx, row in features.iterrows() | |
] |
View 8.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from vespa.deployment import VespaDocker | |
vespa_docker = VespaDocker() | |
app = vespa_docker.deploy_from_disk( | |
application_name="lightgbm", | |
application_root="lightgbm" | |
) |
View 7.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with open("lightgbm/models/lightgbm_model.json", "w") as f: | |
json.dump(model.dump_model(), f, indent=2) |
View 6.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
Path("lightgbm").mkdir(parents=True, exist_ok=True) | |
app_package.to_files("lightgbm") |
NewerOlder