fdgnhgdh
fdgnhgdh

Reputation: 77

Can I save the coordinates for a UMAP embedding?

I am running a UMAP model as follows:

    embedding = umap.UMAP(n_components=2,n_neighbors=30, min_dist=0.0, metric='euclidean').fit(data)

And plotting:

    f = umap.plot.points(embedding, labels=df['name'])

This generates a nice looking graph. I'd like to get the coordinates of the points plotted, to move to a different visualisation. I don't fully understand what is stored in the embedding object.

Is there a way I can export to something like:

    [{'name': name1, 'x-value': x1, 'y-value': y1}, {'name': name2, 'x-value': x2, 'y-value': y2 }...] 

Or similar?

Upvotes: 0

Views: 1650

Answers (2)

Stefan
Stefan

Reputation: 59

One option would be to create a DataFrame with coordinates, add a column with the names, and turn it into a list of dicts.

It seems like you already have a DataFrame with the column name. If that's the case, I'd go for something like:

import pandas as pd

# Add coordinates into a Df
df_c = pd.DataFrame(embedding.embedding_)

# Add column with names
df_c['name'] = df['name']

# Transform Df into list of dicts
df_c.to_dict('records') 

Upvotes: 0

fdgnhgdh
fdgnhgdh

Reputation: 77

You can get an array of the x,y coordinates by doing

    embedding.embedding_

Not sure how I missed that.

Upvotes: 1

Related Questions