-
Notifications
You must be signed in to change notification settings - Fork 49
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Copyright 2023 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from bigframes.ml.metrics import pairwise | ||
from bigframes.ml.metrics._metrics import ( | ||
accuracy_score, | ||
auc, | ||
confusion_matrix, | ||
f1_score, | ||
precision_score, | ||
r2_score, | ||
recall_score, | ||
roc_auc_score, | ||
roc_curve, | ||
) | ||
|
||
__all__ = [ | ||
"r2_score", | ||
"recall_score", | ||
"accuracy_score", | ||
"roc_curve", | ||
"roc_auc_score", | ||
"auc", | ||
"confusion_matrix", | ||
"precision_score", | ||
"f1_score", | ||
"pairwise", | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Copyright 2023 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import inspect | ||
from typing import Union | ||
|
||
from bigframes.ml import core, utils | ||
import bigframes.pandas as bpd | ||
import third_party.bigframes_vendored.sklearn.metrics.pairwise as vendored_metrics_pairwise | ||
|
||
|
||
def cosine_similarity( | ||
X: Union[bpd.DataFrame, bpd.Series], Y: Union[bpd.DataFrame, bpd.Series] | ||
) -> bpd.DataFrame: | ||
X, Y = utils.convert_to_dataframe(X, Y) | ||
if len(X.columns) != 1 or len(Y.columns) != 1: | ||
raise ValueError("Inputs X and Y can only contain 1 column.") | ||
|
||
base_bqml = core.BaseBqml(session=X._session) | ||
return base_bqml.distance(X, Y, type="COSINE", name="cosine_similarity") | ||
|
||
|
||
cosine_similarity.__doc__ = inspect.getdoc(vendored_metrics_pairwise.cosine_similarity) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Copyright 2023 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import numpy as np | ||
import pandas as pd | ||
|
||
from bigframes.ml import metrics | ||
import bigframes.pandas as bpd | ||
|
||
|
||
def test_cosine_similarity(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
x_col = [np.array([4.1, 0.5, 1.0])] | ||
y_col = [np.array([3.0, 0.0, 2.5])] | ||
X = bpd.read_pandas(pd.DataFrame({"X": x_col})) | ||
Y = bpd.read_pandas(pd.DataFrame({"Y": y_col})) | ||
|
||
result = metrics.pairwise.cosine_similarity(X, Y) | ||
expected_pd_df = pd.DataFrame( | ||
{"X": x_col, "Y": y_col, "cosine_similarity": [0.108199]} | ||
) | ||
|
||
pd.testing.assert_frame_equal( | ||
result.to_pandas(), expected_pd_df, check_dtype=False, check_index_type=False | ||
) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.