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

Allow absolute cosine similarity #55

Closed
shntnu opened this issue Jan 2, 2024 · 1 comment · Fixed by #68
Closed

Allow absolute cosine similarity #55

shntnu opened this issue Jan 2, 2024 · 1 comment · Fixed by #68

Comments

@shntnu
Copy link
Member

shntnu commented Jan 2, 2024

Sometimes both, strong positive and strong negative correlations are meaningful, so we should allow for that.

One way to do this is to add a boolean absolute_value argument to pairwise_cosine

def pairwise_cosine(x_sample: np.ndarray, y_sample: np.ndarray) -> np.ndarray:
x_norm = x_sample / np.linalg.norm(x_sample, axis=1)[:, np.newaxis]
y_norm = y_sample / np.linalg.norm(y_sample, axis=1)[:, np.newaxis]
c_sim = np.sum(x_norm * y_norm, axis=1)
return c_sim

def pairwise_cosine(x_sample: np.ndarray, y_sample: np.ndarray, absolute_value: bool = False) -> np.ndarray:
    x_norm = x_sample / np.linalg.norm(x_sample, axis=1)[:, np.newaxis]
    y_norm = y_sample / np.linalg.norm(y_sample, axis=1)[:, np.newaxis]
    c_sim = np.sum(x_norm * y_norm, axis=1)
    if absolute_value:
        c_sim = np.abs(c_sim)
    return c_sim
@shntnu
Copy link
Member Author

shntnu commented Jan 2, 2024

One way to do this is to add a boolean absolute_value argument to pairwise_cosine

Is that reasonable?

@johnarevalo johnarevalo linked a pull request Oct 22, 2024 that will close this issue
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

Successfully merging a pull request may close this issue.

2 participants