Skip to content

Instantly share code, notes, and snippets.

Avatar

Jean-Michel Daignan jeanmidevacc

View GitHub Profile
View surprise_build_recommendations_two_stage.py
for idx, row in dfp_archetypes.iterrows():
print("ARCHETYPE:", row["userid"])
inventory_positive = dfp_inventory_positive.loc[row["userid"]]
# Get the candidates
buffer = []
for contentid in inventory_positive:
closest_contentids = get_closest_neighbors(model_retriever_items, contentid, 10, type_="item")
buffer.extend(closest_contentids)
sp_count_contentids = pd.Series(dict(Counter(buffer))).sort_values(ascending=False)
View surprise_get_closest_neighbours.py
def get_closest_neighbors(model, entityid, k, type_="item"):
if type_ == "item":
inner_entity_id = model.trainset.to_inner_iid(entityid)
else:
inner_entity_id = model.trainset.to_inner_uid(entityid)
closest_entity_id = model.get_neighbors(inner_entity_id, k)
if type_ == "item":
return [model.trainset.to_raw_iid(id_) for id_ in closest_entity_id]
View surprise_build_recos.py
def build_recommendations(model, userid, inventory, dfp_items, k=5):
dfp_recommendations = dfp_items[["title", "category", "year", "contentid"]]
dfp_recommendations["contentid"] = dfp_recommendations["contentid"].astype(str)
dfp_recommendations["rating_predicted"] = dfp_recommendations["contentid"].apply(lambda contentid: compute_ranking(model, str(userid), str(contentid)))
dfp_recommendations.sort_values("rating_predicted", ascending=False, inplace=True)
dfp_recommendations = dfp_recommendations.loc[dfp_recommendations["contentid"].isin(inventory) == False]
return dfp_recommendations.head(k).reset_index(drop=True)
View surprise_search.py
from time import time
from hyperopt import fmin, tpe, hp, anneal, Trials
import mlflow
from sklearn.metrics import mean_squared_error
import surprise
def evaluate_model(model, dfp_ratings_test):
dfp_evaluation = dfp_ratings_test.copy()
dfp_evaluation["rating_predicted"] = dfp_evaluation.apply(lambda row: compute_ranking(model, str(row["userid"]), str(row["contentid"])), axis=1)
return mean_squared_error(dfp_evaluation["rating"].tolist(), dfp_evaluation["rating_predicted"].tolist(), squared=False)
View build_first_forecasts.py
import pandas as pd
from kats import models
# Selection your base model
def build_model(model, kts):
if model == "prophet":
return models.prophet.ProphetModel(kts, params=models.prophet.ProphetParams())
elif model == "theta":
return models.theta.ThetaModel(kts, params=models.theta.ThetaParams())
elif model == "holtwinters":
View init_kats_timeseries.py
from kats.consts import TimeSeriesData
def build_kats_timeserie(dfp, column_time = "time", column_value = "value"):
return TimeSeriesData(time=dfp[column_time], value=dfp[column_value])
kts_test = build_kats_timeserie(dfp_test,"date","value")
View neo4j_set_relationships.py
from neo4j import GraphDatabase
# Declare the connector to the graph database
uri = "bolt://localhost:7687"
driver = GraphDatabase.driver(uri, auth=("neo4j", "pwd"), encrypted=False)
# Build the query to build the relationship
query = f'''
MATCH (u:User), (s:Song)
WHERE u.id = "id of the user" AND s.id = "id of the song"
View neo4j_set_nodes.py
from neo4j import GraphDatabase
# Declare the connector to the graph database
uri = "bolt://localhost:7687"
driver = GraphDatabase.driver(uri, auth=("neo4j", "pwd"), encrypted=False)
# Define the node properties
node = {
'url': 'https://bsaber.com/members/jeanmidev/',
'picture': 'https://bsaber.com/wp-content/uploads/avatars/89910/5e99a50d31e27-bpfull.png',
View neo4j_recommender_jaccard.py
%%time
def get_recommendations_jaccard(user_id):
# Based on the code in https://neo4j.com/docs/graph-data-science/current/alpha-algorithms/jaccard/
query = f"""
MATCH (u1:User)-[:BOOKMARKED]->(song1)
WITH u1, collect(id(song1)) AS u1Song
WHERE u1.id = '{user_id}'
MATCH (u2:User)-[:BOOKMARKED]->(song2) WHERE u1 <> u2
WITH u1, u1Song, u2, collect(id(song2)) AS u2Song
RETURN u2.id AS user_id,
View neo4j_recommendations_fof.py
def get_recommendations_fof(user_id, relationship = 'IS_FRIEND_WITH'):
query = f"""
MATCH (u:User)-[:{relationship}]->(u1:User)-[:{relationship}]->(u2:User)
WHERE u.id = '{user_id}'
RETURN collect(DISTINCT u2.id) AS recommendations
"""
with driver.session() as session:
result = session.run(query)
return result.values()[0][0]