Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5.5 Entity Visualization #20

Open
789456123fcf opened this issue Feb 20, 2023 · 2 comments
Open

5.5 Entity Visualization #20

789456123fcf opened this issue Feb 20, 2023 · 2 comments

Comments

@789456123fcf
Copy link

Hello, is there any reference code for the visualization part of 5.5 Entity Visualization? I would appreciate it if there were any,thanks.

@intfloat
Copy link
Owner

intfloat commented Feb 20, 2023

I use the following code snippet, please change accordingly based on your data format.

def plot_entity_embeddings(path='./vectors.json'):
    import json
    import pandas as pd

    import seaborn as sns
    import matplotlib.pyplot as plt

    from sklearn.manifold import TSNE

    tail_id_to_str = {'Q5': 'Human',
                      'Q16521': 'Taxon',
                      'Q482994': 'Album',
                      'Q11424': 'Movie',
                      'Q486972': 'Community',
                      'Q4830453': 'Company',
                      'Q571': 'Book',
                      'Q532': 'Village'}

    examples = json.load(open(path, 'r', encoding='utf-8'))
    x, y = [], []
    for ex in examples:
        x.append([float(n) for n in ex['head_vector'].split(',')])
        y.append(tail_id_to_str[ex['tail_id']])

    tsne = TSNE(n_components=2, verbose=1, random_state=12)
    z = tsne.fit_transform(x)
    df = pd.DataFrame()
    df["entity type"] = y
    df["x"] = z[:, 0]
    df["y"] = z[:, 1]

    sns.scatterplot(x="x", y="y", hue='entity type',
                    palette=sns.color_palette("hls", len(tail_id_to_str)),
                    data=df).set(title="Entity embedding visualization")


    plt.show()

The input path contains pre-computed vectors in the following format:

[{"head_vector": "0.2,0.3...", "tail_id": "Q571"}]

@789456123fcf
Copy link
Author

Thank you very much for your reply and help! I wish you have good health.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants