Giter VIP home page Giter VIP logo

Comments (2)

eliorc avatar eliorc commented on June 18, 2024

I can totally see why you are puzzled by this :)

It is written in the README, but apparently not clear enough 😅 - Output node names are ALWAYS string.
When you write m_node.wv[0] you actually get the vector in the first index, and it is not guaranteed (and most likely won't be) the vector that is marked as 0 in the networkx graph.

In the line vectors = {node: m_node.wv[node] for node in G.nodes()}, cast to string by switching it to vectors = {node: m_node.wv[str(node)] for node in G.nodes()} and it will work as expected

Here is the code with this small change and the result, hope this makes sense :)

import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.manifold import TSNE
from node2vec import Node2Vec

# create the synthetic network
sizes = [300, 300, 300]
probs = [[0.2, 0.01, 0.01], [0.01, 0.2, 0.01], [0.01, 0.01, 0.2]]
G = nx.stochastic_block_model(sizes, probs, seed=0)
node_colors =  [0] * 300 + [1] * 300 + [2] * 300

# train the model
m_node = Node2Vec(G, dimensions = 128, p = 1, q = 1, walk_length = 80, num_walks = 10).fit()

# get the vectors
vectors = {node: m_node.wv[str(node)] for node in G.nodes()}
vectors = pd.DataFrame.from_dict(vectors, orient = 'index')

# prepare dataframe for visualisation
model = TSNE(n_components=2)
df = model.fit_transform(vectors)
df = pd.DataFrame(df, index = G.nodes())
df = df.rename(columns = {0: 'dim1', 1: 'dim2'})
df = df.assign(color = node_colors)

# plot
fig, ax = plt.subplots()
sns.scatterplot(data = df, x = 'dim1', y = 'dim2', hue = 'color', palette = 'Set1', alpha = 0.6)
ax.set_xlabel("dim1")
ax.set_ylabel("dim2")

image

from node2vec.

yuanmohe avatar yuanmohe commented on June 18, 2024

Oh, that's the reason. 😂 Thank you!

from node2vec.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.