Skip to content

Instantly share code, notes, and snippets.


Jean-Michel Daignan jeanmidevacc

View GitHub Profile
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)
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")
sp_count_contentids = pd.Series(dict(Counter(buffer))).sort_values(ascending=False)
def get_closest_neighbors(model, entityid, k, type_="item"):
if type_ == "item":
inner_entity_id = model.trainset.to_inner_iid(entityid)
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]
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)
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":
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")
from sklearn.neighbors import KNeighborsRegressor
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score, explained_variance_score
import mlflow
import mlflow.sklearn
import numpy as np
# Launch the experiment on mlflow
experiment_name = "electricityconsumption-forecast"
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 = "id of the user" AND = "id of the song"
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': '',
'picture': '',
def get_recommendations_jaccard(user_id):
# Based on the code in
query = f"""
MATCH (u1:User)-[:BOOKMARKED]->(song1)
WITH u1, collect(id(song1)) AS u1Song
WHERE = '{user_id}'
MATCH (u2:User)-[:BOOKMARKED]->(song2) WHERE u1 <> u2
WITH u1, u1Song, u2, collect(id(song2)) AS u2Song
RETURN AS user_id,