Skip to content

Commit

Permalink
feat: Add ml.metrics.pairwise.manhattan_distance (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleyxuu authored Feb 27, 2024
1 parent 81ece46 commit 9d31865
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
16 changes: 16 additions & 0 deletions bigframes/ml/metrics/pairwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,19 @@ def paired_cosine_distances(
paired_cosine_distances.__doc__ = inspect.getdoc(
vendored_metrics_pairwise.paired_cosine_distances
)


def paired_manhattan_distance(
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="MANHATTAN", name="manhattan_distance")


paired_manhattan_distance.__doc__ = inspect.getdoc(
vendored_metrics_pairwise.paired_manhattan_distance
)
14 changes: 14 additions & 0 deletions tests/system/small/ml/test_metrics_pairwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,17 @@ def test_paired_cosine_distances():
pd.testing.assert_frame_equal(
result.to_pandas(), expected_pd_df, check_dtype=False, check_index_type=False
)


def test_paired_manhattan_distance():
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.paired_manhattan_distance(X, Y)
expected_pd_df = pd.DataFrame({"X": x_col, "Y": y_col, "manhattan_distance": [3.1]})

pd.testing.assert_frame_equal(
result.to_pandas(), expected_pd_df, check_dtype=False, check_index_type=False
)
15 changes: 15 additions & 0 deletions third_party/bigframes_vendored/sklearn/metrics/pairwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,18 @@ def paired_cosine_distances(X, Y) -> bpd.DataFrame:
bigframes.dataframe.DataFrame: DataFrame with columns of X, Y and cosine_distance
"""
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)


def paired_manhattan_distance(X, Y) -> bpd.DataFrame:
"""Compute the L1 distances between the vectors in X and Y.
Args:
X (Series or single column DataFrame of array of numeric type):
Input data.
Y (Series or single column DataFrame of array of numeric type):
Input data. X and Y are mapped by indexes, must have the same index.
Returns:
bigframes.dataframe.DataFrame: DataFrame with columns of X, Y and manhattan_distance
"""
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)

0 comments on commit 9d31865

Please sign in to comment.