This file contains 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 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") |
This file contains 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 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" |
This file contains 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 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" |
This file contains 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 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', |
This file contains 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
%%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, |
This file contains 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 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] |
This file contains 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
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()) |
This file contains 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
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()) |
This file contains 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
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 | |
""" |
This file contains 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
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) |