|
| 1 | +import ibis |
| 2 | +import numpy as np |
| 3 | +import pandas as pd |
| 4 | +import pandas.testing as tm |
| 5 | +import pytest |
| 6 | + |
| 7 | +import ibis_ml as ml |
| 8 | + |
| 9 | + |
| 10 | +def test_scalestandard(): |
| 11 | + cols = np.arange(0, 100) |
| 12 | + mean = np.mean(cols) |
| 13 | + std = np.std(cols) |
| 14 | + table = ibis.memtable({"col": cols}) |
| 15 | + step = ml.ScaleStandard("col") |
| 16 | + step.fit_table(table, ml.core.Metadata()) |
| 17 | + result = step.transform_table(table) |
| 18 | + expected = pd.DataFrame({"col": (cols - mean) / std}) |
| 19 | + tm.assert_frame_equal(result.execute(), expected, check_exact=False) |
| 20 | + |
| 21 | + |
| 22 | +def test_scaleminmax(): |
| 23 | + cols = np.arange(0, 100) |
| 24 | + min_val = np.min(cols) |
| 25 | + max_val = np.max(cols) |
| 26 | + table = ibis.memtable({"col": cols}) |
| 27 | + step = ml.ScaleMinMax("col") |
| 28 | + step.fit_table(table, ml.core.Metadata()) |
| 29 | + result = step.transform_table(table) |
| 30 | + expected = pd.DataFrame({"col": (cols - min_val) / (max_val - min_val)}) |
| 31 | + tm.assert_frame_equal(result.execute(), expected, check_exact=False) |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.parametrize( |
| 35 | + ("model", "msg"), |
| 36 | + [ |
| 37 | + ("ScaleStandard", "Cannot standardize 'col' - the standard deviation is zero"), |
| 38 | + ( |
| 39 | + "ScaleMinMax", |
| 40 | + "Cannot standardize 'col' - the maximum and minimum values are equal", |
| 41 | + ), |
| 42 | + ], |
| 43 | +) |
| 44 | +def test_scale_unique_col(model, msg): |
| 45 | + table = ibis.memtable({"col": [1]}) |
| 46 | + scale_class = getattr(ml, model) |
| 47 | + step = scale_class("col") |
| 48 | + with pytest.raises(ValueError, match=msg): |
| 49 | + step.fit_table(table, ml.core.Metadata()) |
0 commit comments