|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 | 15 | import math
|
| 16 | +from typing import cast |
16 | 17 |
|
17 | 18 | import pandas as pd
|
18 | 19 | import pytest
|
19 | 20 |
|
20 | 21 | from bigframes.ml import model_selection
|
21 | 22 | import bigframes.pandas as bpd
|
| 23 | +import bigframes.session |
22 | 24 |
|
23 | 25 |
|
24 | 26 | @pytest.mark.parametrize(
|
@@ -219,6 +221,78 @@ def test_train_test_split_seeded_correct_rows(
|
219 | 221 | )
|
220 | 222 |
|
221 | 223 |
|
| 224 | +def test_train_test_split_no_shuffle_correct_shape( |
| 225 | + penguins_df_default_index: bpd.DataFrame, |
| 226 | +): |
| 227 | + X = penguins_df_default_index[["species"]] |
| 228 | + y = penguins_df_default_index["body_mass_g"] |
| 229 | + X_train, X_test, y_train, y_test = model_selection.train_test_split( |
| 230 | + X, y, shuffle=False |
| 231 | + ) |
| 232 | + assert isinstance(X_train, bpd.DataFrame) |
| 233 | + assert isinstance(X_test, bpd.DataFrame) |
| 234 | + assert isinstance(y_train, bpd.Series) |
| 235 | + assert isinstance(y_test, bpd.Series) |
| 236 | + |
| 237 | + assert X_train.shape == (258, 1) |
| 238 | + assert X_test.shape == (86, 1) |
| 239 | + assert y_train.shape == (258,) |
| 240 | + assert y_test.shape == (86,) |
| 241 | + |
| 242 | + |
| 243 | +def test_train_test_split_no_shuffle_correct_rows( |
| 244 | + session: bigframes.session.Session, penguins_pandas_df_default_index: bpd.DataFrame |
| 245 | +): |
| 246 | + # Note that we're using `penguins_pandas_df_default_index` as this test depends |
| 247 | + # on a stable row order being present end to end |
| 248 | + # filter down to the chunkiest penguins, to keep our test code a reasonable size |
| 249 | + all_data = penguins_pandas_df_default_index[ |
| 250 | + penguins_pandas_df_default_index.body_mass_g > 5500 |
| 251 | + ].sort_index() |
| 252 | + |
| 253 | + # Note that bigframes loses the index if it doesn't have a name |
| 254 | + all_data.index.name = "rowindex" |
| 255 | + |
| 256 | + df = session.read_pandas(all_data) |
| 257 | + |
| 258 | + X = df[ |
| 259 | + [ |
| 260 | + "species", |
| 261 | + "island", |
| 262 | + "culmen_length_mm", |
| 263 | + ] |
| 264 | + ] |
| 265 | + y = df["body_mass_g"] |
| 266 | + X_train, X_test, y_train, y_test = model_selection.train_test_split( |
| 267 | + X, y, shuffle=False |
| 268 | + ) |
| 269 | + |
| 270 | + X_train_pd = cast(bpd.DataFrame, X_train).to_pandas() |
| 271 | + X_test_pd = cast(bpd.DataFrame, X_test).to_pandas() |
| 272 | + y_train_pd = cast(bpd.Series, y_train).to_pandas() |
| 273 | + y_test_pd = cast(bpd.Series, y_test).to_pandas() |
| 274 | + |
| 275 | + total_rows = len(all_data) |
| 276 | + train_size = 0.75 |
| 277 | + train_rows = int(total_rows * train_size) |
| 278 | + test_rows = total_rows - train_rows |
| 279 | + |
| 280 | + expected_X_train = all_data.head(train_rows)[ |
| 281 | + ["species", "island", "culmen_length_mm"] |
| 282 | + ] |
| 283 | + expected_y_train = all_data.head(train_rows)["body_mass_g"] |
| 284 | + |
| 285 | + expected_X_test = all_data.tail(test_rows)[ |
| 286 | + ["species", "island", "culmen_length_mm"] |
| 287 | + ] |
| 288 | + expected_y_test = all_data.tail(test_rows)["body_mass_g"] |
| 289 | + |
| 290 | + pd.testing.assert_frame_equal(X_train_pd, expected_X_train) |
| 291 | + pd.testing.assert_frame_equal(X_test_pd, expected_X_test) |
| 292 | + pd.testing.assert_series_equal(y_train_pd, expected_y_train) |
| 293 | + pd.testing.assert_series_equal(y_test_pd, expected_y_test) |
| 294 | + |
| 295 | + |
222 | 296 | @pytest.mark.parametrize(
|
223 | 297 | ("train_size", "test_size"),
|
224 | 298 | [
|
|
0 commit comments