Skip to content

Instantly share code, notes, and snippets.

Avatar

Jean-Michel Daignan jeanmidevacc

View GitHub Profile
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]
@jeanmidevacc
jeanmidevacc / neo4j_evaluation_songs.py
Last active May 11, 2020
neo4j_evaluation_songs.py
View 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())
View 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
neo4j_count_entities_bsaber_apoc.py
View 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
"""
View neo4j_count_entities_bsaber_raw.py
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)
View tensorflow_transfer_learning_example.py
import tensorflow as tf
import tensorflow_hub as hub
# colelct the feature extractor of mobile net
feature_extractor_url = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/2" #@param {type:"string"}
feature_extractor_layer = hub.KerasLayer(feature_extractor_url,
input_shape=(IMG_HEIGHT, IMG_WIDTH,3))
feature_extractor_layer.trainable = False
View tensorflow_model_design_cnn_pml.py
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(
32, (3, 3), padding='same', activation='relu'),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Dropout(rate=0.5),
tf.keras.layers.Conv2D(
64, (3, 3), padding='same', activation='relu'),
You can’t perform that action at this time.