Skip to content

Instantly share code, notes, and snippets.

View thigm85's full-sized avatar

Thiago G. Martins thigm85

View GitHub Profile
@thigm85
thigm85 / multi_class_neural_netwrok_diagram
Created June 11, 2013 20:04
Graphviz script to draw a multi-class neural network diagram. Copy this to a `file.txt` file and then run `dot -Tpng -O file.txt` from command-line to get a .png figure with the diagram.
digraph G {
rankdir=LR
splines=line
nodesep=.05;
node [label=""];
subgraph cluster_0 {
color=white;
@thigm85
thigm85 / loading_plot.R
Created November 28, 2013 09:45
Sample code to plot PCs coefficients associated with variables in the dataset. See http://tgmstat.wordpress.com/2013/11/28/computing-and-visualizing-pca-in-r/ for more info.
require(ggplot2)
theta <- seq(0,2*pi,length.out = 100)
circle <- data.frame(x = cos(theta), y = sin(theta))
p <- ggplot(circle,aes(x,y)) + geom_path()
loadings <- data.frame(ir.pca$rotation,
.names = row.names(ir.pca$rotation))
p + geom_text(data=loadings,
mapping=aes(x = PC1, y = PC2, label = .names, colour = .names)) +
!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"
)