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

customize metrics to .fit #267

Open
2533245542 opened this issue Dec 5, 2024 · 2 comments
Open

customize metrics to .fit #267

2533245542 opened this issue Dec 5, 2024 · 2 comments

Comments

@2533245542
Copy link

is there a way to provide customized metrics to .fit function?

import pandas as pd
from sklearn import datasets
from openTSNE import TSNE
from scipy.spatial.distance import euclidean
import numpy as np
import matplotlib.pyplot as plt

# a. use the iris dataset
iris = datasets.load_iris()
x, y = iris["data"], iris["target"]
year = np.arange(x.shape[0]).reshape(-1, 1)
data_with_time = np.hstack([x, year])


# b. use my dataset
# embeddings = np.load("asdf.npy")
# metadata = pd.read_csv("asdf.tsv", sep="\t")
# year = metadata['year'].values.reshape(-1, 1)
# data_with_time = np.hstack([embeddings, year])

def custom_metric(v1, v2):
    feature_dist = euclidean(v1[:-1], v2[:-1])  # non-time dis
    time_dist = abs(v1[-1] - v2[-1])  # time dis
    return feature_dist + time_dist

embedding = TSNE(
    metric=custom_metric, 
    perplexity=30, 
    n_iter=500, 
    random_state=42
).fit(data_with_time)

this script seems to be work with sklearn's iris dataset but after switching to my dataset, it says error

  File "anaconda3/envs/tsne/lib/python3.10/site-packages/openTSNE/nearest_neighbors.py", line 247, in build
    self.index = AnnoyIndex(data.shape[1], annoy_metric)
TypeError: argument 2 must be str, not function
@dkobak
Copy link
Contributor

dkobak commented Dec 6, 2024

I think it is possible to use custom metric with PyNNDescent, but you will probably need to do it outside of openTSNE, and then pass the kNN graph into openTSNE as PrecomputedNeighbors.

@2533245542
Copy link
Author

I am able to install opentsne in a editable state with pip install -e ..

Do you think if there is a way to modify the source code directly so I can customize the metrics in a more straightforward way?

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