<jupyter_start><jupyter_text>Py2neoPy2neo is a client library and toolkit for working with Neo4j from within Python applications and from the command line.The library supports both Bolt and HTTP and provides a high level API. [Documentation](https://py2neo.org/2.0/cypher.html)  Create py2neo connection to our neo4j database<jupyter_code>USER = "neo4j"
PASSWORD = "<PASSWORD>"
DATABASE_URL = "bolt://127.0.0.1:7687"
graph = Graph(DATABASE_URL, auth=(USER, PASSWORD))<jupyter_output><empty_output><jupyter_text>Execute simple query via py2neo<jupyter_code>for record in graph.run("MATCH (c:Character) RETURN count(c) AS num"):
    print(record[0])<jupyter_output>796<jupyter_text>ipython-cypherConnect to a graph database, using neo4jrestclient_ driver, then issue Cypher commands within IPython or IPython Notebook.The default DATABASE URL used by ipython-chiper is `NEO4J_URI`. Make sure the variable is set and the username and password are correct.<jupyter_code>#!export NEO4J_URI='http://neo4j:neo@localhost:7474/db/data'

def check_neo_uri():
    try:
        os.environ['NEO4J_URI']
    except KeyError:
        print('''
        NEO4J_URI env var is not configured. 
        Use "export NEO4J_URI='http://neo4j:neo@localhost:7474/db/data" 
        Restart jupyter!
        ''')
        
check_neo_uri()<jupyter_output><empty_output><jupyter_text>Execute simple query via cyper magic<jupyter_code>result =  %cypher MATCH p=(:Character)-[:INTERACTS]-(:Character) RETURN p LIMIT 50
result[:2]<jupyter_output><empty_output><jupyter_text>Load the result into networkx object and plot the graph<jupyter_code>g = result.get_graph()
nx.draw(g)<jupyter_output><empty_output><jupyter_text>Execute simple query and create 3d plot of the resulting graphGood tutorail about using jgraph to draw 3d graphs is available [here](http://patrickfuller.github.io/jgraph/examples/ipython.html)<jupyter_code>data = graph.run("MATCH (n:Character)-[:INTERACTS]-(m:Character) RETURN ID(n), ID(m) LIMIT 50")
data = [tuple(x) for x in data]

jgraph.draw(data,  size=(900, 600))<jupyter_output><empty_output><jupyter_text>Load preview of the 3d graph  Cypher Query LanguageIs a query language for Neo4j Graph Database.Is a declarative pattern-matching language.Follows SQL like syntax.Syntax is very simple and in human readable format.  Task 1Write a query that selects all neighbours of "<NAME>" at distance at most 2 hops.This [link](http://patrickfuller.github.io/jgraph/examples/ipython.html) contains short tutorial about CQL.<jupyter_code>##Insert your code here<jupyter_output><empty_output><jupyter_text>Random walks example<jupyter_code>walks = []
SIZE_OF_THE_RANDOM_WALK = 4
NUMBER_OF_WALKS = 1
NUMBER_OF_REPETITIONS = 10

for i in range(NUMBER_OF_REPETITIONS):
    results = %cypher MATCH (c:Character {name: "Jon-Snow"}) \
                  CALL algo.randomWalk.stream(id(c), {SIZE_OF_THE_RANDOM_WALK}, {NUMBER_OF_WALKS}) \
                  YIELD nodeIds \
                  UNWIND nodeIds AS nodeId \
                  RETURN algo.asNode(nodeId).name AS character, nodeId AS nodeid;
    walks.append(results.get_dataframe())
walks[0]<jupyter_output><empty_output><jupyter_text>GensimIs a framework for natural language processing. Most of its algorithms are implemented in C and it provides pythonic interface. Word2Vec documentation can be found [here](https://radimrehurek.com/gensim/models/word2vec.html)  Memory efficient sentence Iterator<jupyter_code>class WalksIterator:
    def __init__(self, dirname):
        self.dirname = dirname

    def __iter__(self):
        files = os.listdir(self.dirname)
        files = [f for f in files if ".txt" in f]
        files = shuffle(files)
        for fname in files:
            for line in open(os.path.join(self.dirname, fname)):
                walk = line.split()
                if len(walk) == 1:
                    continue
                yield line.split()

walks = WalksIterator(f"./")
for sentence in walks:
    print(sentence)<jupyter_output>['human', 'interface', 'computer']
['survey', 'user', 'computer', 'system', 'response', 'time']
['eps', 'user', 'interface', 'system']
['system', 'human', 'system', 'eps']
['user', 'response', 'time']
['graph', 'trees']
['graph', 'minors', 'trees']
['graph', 'minors', 'survey']<jupyter_text>Create Word2Vec model<jupyter_code>model = Word2Vec(size=60, window=3, min_count=1, workers=os.cpu_count(), sg=0, negative=20, compute_loss=True, seed=42, sample=0)
model.epochs = 3<jupyter_output><empty_output><jupyter_text>Initialize the vocabulary<jupyter_code>model.build_vocab(walks, progress_per=5)<jupyter_output>2020-01-27 01:11:14,667 : INFO : collecting all words and their counts
2020-01-27 01:11:14,676 : INFO : PROGRESS: at sentence #0, processed 0 words, keeping 0 word types
2020-01-27 01:11:14,682 : INFO : PROGRESS: at sentence #5, processed 20 words, keeping 9 word types
2020-01-27 01:11:14,688 : INFO : collected 12 word types from a corpus of 28 raw words and 8 sentences
2020-01-27 01:11:14,695 : INFO : Loading a fresh vocabulary
2020-01-27 01:11:14,698 : INFO : effective_min_count=1 retains 12 unique words (100% of original 12, drops 0)
2020-01-27 01:11:14,700 : INFO : effective_min_count=1 leaves 28 word corpus (100% of original 28, drops 0)
2020-01-27 01:11:14,704 : INFO : deleting the raw counts dictionary of 12 items
2020-01-27 01:11:14,708 : INFO : sample=0 downsamples 0 most-common words
2020-01-27 01:11:14,710 : INFO : downsampling leaves estimated 28 word corpus (100.0% of prior 28)
2020-01-27 01:11:14,712 : INFO : estimated required memory for 12 words and 60 dimensions: 11760[...]<jupyter_text>Train the model<jupyter_code>model.train(walks, total_examples=model.corpus_count, epochs=model.epochs, report_delay=1)<jupyter_output>2020-01-27 01:11:16,219 : INFO : training model with 4 workers on 12 vocabulary and 60 features, using sg=0 hs=0 sample=0 negative=20 window=3
2020-01-27 01:11:16,283 : INFO : worker thread finished; awaiting finish of 3 more threads
2020-01-27 01:11:16,284 : INFO : worker thread finished; awaiting finish of 2 more threads
2020-01-27 01:11:16,287 : INFO : worker thread finished; awaiting finish of 1 more threads
2020-01-27 01:11:16,296 : INFO : worker thread finished; awaiting finish of 0 more threads
2020-01-27 01:11:16,298 : INFO : EPOCH - 1 : training on 28 raw words (28 effective words) took 0.0s, 1028 effective words/s
2020-01-27 01:11:16,319 : INFO : worker thread finished; awaiting finish of 3 more threads
2020-01-27 01:11:16,322 : INFO : worker thread finished; awaiting finish of 2 more threads
2020-01-27 01:11:16,324 : INFO : worker thread finished; awaiting finish of 1 more threads
2020-01-27 01:11:16,325 : INFO : worker thread finished; awaiting finish of 0 more threads
2020[...]<jupyter_text>Find the most similar words to `user`<jupyter_code>model.wv.most_similar('user', topn=10)<jupyter_output>2020-01-27 01:11:19,182 : INFO : precomputing L2-norms of word weight vectors<jupyter_text>Task 2Generate random walks from each node in the graph.Store the walks in a text file. Each line in the file should be single random walk.For example, single line in the file should be something like:`Jon-Snow Ebben Jon-Snow Robb-Stark`Use the provided `WalksIterator` to train `Word2Vec` model.Find top 10 most similar nodes to `<NAME>`<jupyter_code>#Insert your code here<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10927555856465809, 'fraction_numerical': 0.07393364928909953, 'mean_word_length': 4.8479809976247035, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 55, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '326866', 'license': 'MIT', 'n_tokens_mistral': 2817, 'n_tokens_neox': 2391, 'n_words': 732, 'path': 'machine-learning-in-networks/notebooks/[0]-ML-in-Graphs.ipynb', 'repo_name': 'AI-Cluster-Bulgaria/workshops', 'size': '7382'}