Skip to content

fix: stop ignoring arguments to MatrixFactorization.score(X, y) #1726

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 2 commits into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions bigframes/ml/decomposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,5 +360,12 @@ def score(
if not self._bqml_model:
raise RuntimeError("A model must be fitted before score")

# TODO(b/291973741): X param is ignored. Update BQML supports input in ML.EVALUATE.
return self._bqml_model.evaluate()
if X is not None and y is not None:
X, y = utils.batch_convert_to_dataframe(
X, y, session=self._bqml_model.session
)
input_data = X.join(y, how="outer")
else:
input_data = X

return self._bqml_model.evaluate(input_data)
12 changes: 11 additions & 1 deletion tests/system/large/ml/test_decomposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import pandas as pd
import pandas.testing

from bigframes.ml import decomposition
from tests.system import utils
Expand Down Expand Up @@ -193,7 +194,16 @@ def test_decomposition_mf_configure_fit_load(
)
)

reloaded_model.score(new_ratings)
# Make sure the input to score is not ignored.
scores_training_data = reloaded_model.score().to_pandas()
scores_new_ratings = reloaded_model.score(new_ratings).to_pandas()
pandas.testing.assert_index_equal(
scores_training_data.columns, scores_new_ratings.columns
)
assert (
scores_training_data["mean_squared_error"].iloc[0]
!= scores_new_ratings["mean_squared_error"].iloc[0]
)

result = reloaded_model.predict(new_ratings).to_pandas()

Expand Down
22 changes: 19 additions & 3 deletions tests/unit/ml/test_golden_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def mock_X(mock_y, mock_session):
["index_column_id"],
["index_column_label"],
)
type(mock_X).sql = mock.PropertyMock(return_value="input_X_sql_property")
mock_X.reset_index(drop=True).cache().sql = "input_X_no_index_sql"
mock_X.join(mock_y).sql = "input_X_y_sql"
mock_X.join(mock_y).cache.return_value = mock_X.join(mock_y)
Expand Down Expand Up @@ -248,7 +249,7 @@ def test_decomposition_mf_predict(mock_session, bqml_model, mock_X):
)


def test_decomposition_mf_score(mock_session, bqml_model, mock_X):
def test_decomposition_mf_score(mock_session, bqml_model):
model = decomposition.MatrixFactorization(
num_factors=34,
feedback_type="explicit",
Expand All @@ -258,8 +259,23 @@ def test_decomposition_mf_score(mock_session, bqml_model, mock_X):
l2_reg=9.83,
)
model._bqml_model = bqml_model
model.score(mock_X)

model.score()
mock_session.read_gbq.assert_called_once_with(
"SELECT * FROM ML.EVALUATE(MODEL `model_project`.`model_dataset`.`model_id`)"
)


def test_decomposition_mf_score_with_x(mock_session, bqml_model, mock_X):
model = decomposition.MatrixFactorization(
num_factors=34,
feedback_type="explicit",
user_col="user_id",
item_col="item_col",
rating_col="rating_col",
l2_reg=9.83,
)
model._bqml_model = bqml_model
model.score(mock_X)
mock_session.read_gbq.assert_called_once_with(
"SELECT * FROM ML.EVALUATE(MODEL `model_project`.`model_dataset`.`model_id`,\n (input_X_sql_property))"
)
10 changes: 6 additions & 4 deletions third_party/bigframes_vendored/sklearn/decomposition/_mf.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ def score(self, X=None, y=None):
for the outputs relevant to this model type.

Args:
X (default None):
Ignored.
X (bigframes.dataframe.DataFrame | bigframes.series.Series | None):
DataFrame of shape (n_samples, n_features). Test samples.

y (bigframes.dataframe.DataFrame | bigframes.series.Series | None):
DataFrame of shape (n_samples,) or (n_samples, n_outputs). True
labels for `X`.

y (default None):
Ignored.
Returns:
bigframes.dataframe.DataFrame: DataFrame that represents model metrics.
"""
Expand Down