This file contains hidden or 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 |
This file contains hidden or 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() |
This file contains hidden or 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() |
This file contains hidden or 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 |
This file contains hidden or 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"] |
This file contains hidden or 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 |
This file contains hidden or 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() | |
| ] |
This file contains hidden or 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" | |
| ) |
This file contains hidden or 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) |
This file contains hidden or 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