Skip to content

feat: add ml.metrics.pairwise.cosine_similarity function #374

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

Merged
merged 5 commits into from
Feb 13, 2024

Conversation

GarrettWu
Copy link
Contributor

Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Fixes #<issue_number_goes_here> 🦕

@product-auto-label product-auto-label bot added size: l Pull request size is large. api: bigquery Issues related to the googleapis/python-bigquery-dataframes API. labels Feb 7, 2024
@GarrettWu GarrettWu requested review from shobsi and ashleyxuu February 8, 2024 18:09
@GarrettWu GarrettWu marked this pull request as ready for review February 8, 2024 18:09
@GarrettWu GarrettWu requested review from a team as code owners February 8, 2024 18:09
Input data. X and Y are mapped by indexes, must have the same index.

Returns:
DataFrame with columns of X, Y and cosine_similarity
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the type hint as well: bigframes.dataframe.DataFrame: DataFrame with columns of X, Y and cosine_similarity

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -84,6 +62,59 @@ def _apply_sql(

return df

def distance(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add docstring here too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

import bigframes.pandas as bpd


def test_cosine_similarity():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this ML.DISTANCE COSINE in BQML is different from sklearn package sklearn.metrics.pairwise import cosine_similarity, are these two comparable?

from sklearn.metrics.pairwise import cosine_similarity
X = [[4.1, 0.5, 1.0]]
Y = [[3.0, 0, 2.5]]
cosine_similarity(X, Y)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally different, sklearn takes matrixes and returns matrixes, which we will be hard to support.

@GarrettWu GarrettWu requested a review from ashleyxuu February 13, 2024 00:36
@GarrettWu GarrettWu added the automerge Merge the pull request once unit tests and other checks pass. label Feb 13, 2024
@gcf-merge-on-green gcf-merge-on-green bot merged commit 126f566 into main Feb 13, 2024
@gcf-merge-on-green gcf-merge-on-green bot deleted the garrettwu-distance branch February 13, 2024 19:00
@gcf-merge-on-green gcf-merge-on-green bot removed the automerge Merge the pull request once unit tests and other checks pass. label Feb 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: bigquery Issues related to the googleapis/python-bigquery-dataframes API. size: l Pull request size is large.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants