Skip to content

Instantly share code, notes, and snippets.

View jeanmidevacc's full-sized avatar

Jean-Michel Daignan jeanmidevacc

View GitHub Profile
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"
@jeanmidevacc
jeanmidevacc / neo4j_set_relationships.py
Created May 12, 2020 02:48
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"
@jeanmidevacc
jeanmidevacc / neo4j_set_nodes.py
Last active May 12, 2020 02:48
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',
@jeanmidevacc
jeanmidevacc / neo4j_recommender_jaccard.py
Created May 12, 2020 01:52
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,
@jeanmidevacc
jeanmidevacc / neo4j_recommendations_fof.py
Created May 11, 2020 21:47
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]
@jeanmidevacc
jeanmidevacc / neo4j_evaluation_songs.py
Last active May 11, 2020 15:25
neo4j_evaluation_songs.py
query = """
MATCH (n:Song)
RETURN n.title AS title, size((n)<-[:BOOKMARKED]-()) AS count_bookmarked, size((n)<-[:REVIEWED]-()) AS count_reviewed, toInteger(n.upvotes) AS upvotes, toInteger(n.downvotes) AS downvotes, toInteger(n.upvotes) + toInteger(n.downvotes) AS votes
"""
with driver.session() as session:
result = session.run(query)
df_evaluation_songs = pd.DataFrame([r.values() for r in result], columns=result.keys())
@jeanmidevacc
jeanmidevacc / neo4j_evaluation_users.py
Created May 11, 2020 01:14
neo4j_evaluation_users.py
query = """
MATCH (n:User)
RETURN n.id AS user_id, size((n)-[:BOOKMARKED]->()) AS count_bookmarked, size((n)-[:REVIEWED]->()) AS count_reviewed, size((n)-[:IS_FRIEND_WITH]->()) AS count_friends, size((n)-[:IS_FOLLOWED_BY]->()) AS count_followed, size((n)-[:FOLLOWS]->()) AS count_follow
"""
with driver.session() as session:
result = session.run(query)
df_evaluation_users = pd.DataFrame([r.values() for r in result], columns=result.keys())
@jeanmidevacc
jeanmidevacc / neo4j_count_entities_bsaber_apoc.py
Created May 10, 2020 23:17
neo4j_count_entities_bsaber_apoc.py
query = """
CALL db.labels() YIELD label
CALL apoc.cypher.run('MATCH (:`'+label+'`) RETURN count(*) as count',{}) YIELD value
RETURN label AS entities, value.count AS count
UNION ALL
CALL db.relationshipTypes() YIELD relationshipType as label
CALL apoc.cypher.run('MATCH ()-[:`'+label+'`]->() RETURN count(*) as count',{}) YIELD value
RETURN label AS entities, value.count AS count
"""
@jeanmidevacc
jeanmidevacc / neo4j_count_entities_bsaber_raw.py
Created May 10, 2020 23:01
neo4j_count_entities_bsaber_raw
query = """
MATCH (n:User)
WITH count(n) as count
RETURN 'User' as entities, count
UNION ALL
MATCH (n:Song)
WITH count(n) as count
RETURN 'Song' as entities, count
UNION ALL
MATCH (n1:User)-[r:BUILT]->(n2:Song)