Comments (2)
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")
from node2vec.
Oh, that's the reason. 😂 Thank you!
from node2vec.
Related Issues (20)
- How can node2vec use GPU HOT 1
- too slow and stuck for some reason HOT 9
- reproducability HOT 2
- Error in model creation node2vec HOT 1
- Cannot import Node2Vec HOT 5
- A faster implement HOT 1
- Train vs Inference methods HOT 6
- [Question] about directed graph and weight HOT 2
- There may be a small problem with the walking length. HOT 1
- Interpretation of weights HOT 2
- model save and loading examples HOT 2
- Are there any ways to initialize node vector using pretrained embeddings? HOT 1
- Crash when edges are not initialised with a weight attribute HOT 8
- can same temp directory be used for different graphs in for loop training? HOT 4
- _precompute_probabilities parallel execution HOT 2
- import error HOT 3
- segmentation fault HOT 5
- Error installing gensim in poetry env on mac m1 HOT 3
- How to set parameters for a collection small disconnected graphs HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node2vec.