-
Notifications
You must be signed in to change notification settings - Fork 43
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
feat: add ml.model_selection.cross_validate support #1020
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Copyright 2024 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 pytest | ||
|
||
from bigframes.ml import linear_model, model_selection | ||
from tests.system import utils | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("cv", "n_fold"), | ||
( | ||
pytest.param( | ||
None, | ||
5, | ||
), | ||
pytest.param( | ||
4, | ||
4, | ||
), | ||
pytest.param( | ||
model_selection.KFold(3), | ||
3, | ||
), | ||
), | ||
) | ||
def test_cross_validate(penguins_df_default_index, cv, n_fold): | ||
model = linear_model.LinearRegression() | ||
df = penguins_df_default_index.dropna() | ||
X = df[ | ||
[ | ||
"species", | ||
"island", | ||
"culmen_length_mm", | ||
] | ||
] | ||
y = df["body_mass_g"] | ||
|
||
cv_results = model_selection.cross_validate(model, X, y, cv=cv) | ||
|
||
assert "test_score" in cv_results | ||
assert "fit_time" in cv_results | ||
assert "score_time" in cv_results | ||
|
||
assert len(cv_results["test_score"]) == n_fold | ||
assert len(cv_results["fit_time"]) == n_fold | ||
assert len(cv_results["score_time"]) == n_fold | ||
|
||
utils.check_pandas_df_schema_and_index( | ||
cv_results["test_score"][0].to_pandas(), | ||
columns=utils.ML_REGRESSION_METRICS, | ||
index=1, | ||
) |
46 changes: 46 additions & 0 deletions
46
third_party/bigframes_vendored/sklearn/model_selection/_validation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
""" | ||
The :mod:`sklearn.model_selection._validation` module includes classes and | ||
functions to validate the model. | ||
""" | ||
|
||
# Author: Alexandre Gramfort <[email protected]> | ||
# Gael Varoquaux <[email protected]> | ||
# Olivier Grisel <[email protected]> | ||
# Raghav RV <[email protected]> | ||
# Michal Karbownik <[email protected]> | ||
# License: BSD 3 clause | ||
|
||
|
||
def cross_validate(estimator, X, y=None, *, cv=None): | ||
"""Evaluate metric(s) by cross-validation and also record fit/score times. | ||
|
||
Args: | ||
estimator: | ||
bigframes.ml model that implements fit(). | ||
The object to use to fit the data. | ||
|
||
X (bigframes.dataframe.DataFrame or bigframes.series.Series): | ||
The data to fit. | ||
|
||
y (bigframes.dataframe.DataFrame, bigframes.series.Series or None): | ||
The target variable to try to predict in the case of supe()rvised learning. Default to None. | ||
|
||
cv (int, bigframes.ml.model_selection.KFold or None): | ||
Determines the cross-validation splitting strategy. | ||
Possible inputs for cv are: | ||
|
||
- None, to use the default 5-fold cross validation, | ||
- int, to specify the number of folds in a `KFold`, | ||
- bigframes.ml.model_selection.KFold instance. | ||
|
||
Returns: | ||
Dict[str, List]: A dict of arrays containing the score/time arrays for each scorer is returned. The keys for this ``dict`` are: | ||
|
||
``test_score`` | ||
The score array for test scores on each cv split. | ||
``fit_time`` | ||
The time for fitting the estimator on the train | ||
set for each cv split. | ||
``score_time`` | ||
The time for scoring the estimator on the test set for each | ||
cv split.""" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe use perf_counter. From chatgpt
time.time(): It is subject to system clock adjustments or skew. The system time can be adjusted backward and forward by the operating system, which can lead to inaccurate or unreliable results when measuring short durations or intervals. This makes it less suitable for performance testing where precise and stable measurements are critical.
time.perf_counter(): It provides a monotonic clock (i.e., always increasing) that is not affected by changes in the system clock. This makes it highly reliable for measuring precise time intervals, essential for benchmarking and profiling applications.
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.