|
| 1 | +import numpy as np |
| 2 | +import pandas as pd |
| 3 | +from sklearn.linear_model import LinearRegression, LogisticRegression |
| 4 | + |
| 5 | +from poniard import PoniardClassifier, PoniardRegressor |
| 6 | + |
| 7 | + |
| 8 | +def _clf_data(): |
| 9 | + n = 60 |
| 10 | + X = pd.DataFrame( |
| 11 | + { |
| 12 | + "a": np.random.normal(size=n), |
| 13 | + "b": np.random.normal(size=n), |
| 14 | + "c": np.random.choice(["x", "y", "z"], size=n), |
| 15 | + } |
| 16 | + ) |
| 17 | + y = pd.Series(np.random.choice([0, 1], size=n)) |
| 18 | + return X, y |
| 19 | + |
| 20 | + |
| 21 | +def _reg_data(): |
| 22 | + X = pd.DataFrame(np.random.normal(size=(60, 3)), columns=["a", "b", "c"]) |
| 23 | + y = pd.Series(np.random.normal(size=60)) |
| 24 | + return X, y |
| 25 | + |
| 26 | + |
| 27 | +def test_save_load_round_trip_classifier(tmp_path): |
| 28 | + X, y = _clf_data() |
| 29 | + clf = PoniardClassifier( |
| 30 | + estimators=[LogisticRegression()], cv=2, random_state=0 |
| 31 | + ) |
| 32 | + clf.setup(X, y) |
| 33 | + clf.fit(X, y) |
| 34 | + results_before = clf.get_results() |
| 35 | + |
| 36 | + path = tmp_path / "clf.joblib" |
| 37 | + clf.save(path) |
| 38 | + loaded = PoniardClassifier.load(path) |
| 39 | + |
| 40 | + assert isinstance(loaded, PoniardClassifier) |
| 41 | + pd.testing.assert_frame_equal(loaded.get_results(), results_before) |
| 42 | + assert loaded._experiment_results.keys() == clf._experiment_results.keys() |
| 43 | + |
| 44 | + |
| 45 | +def test_save_load_round_trip_regressor(tmp_path): |
| 46 | + X, y = _reg_data() |
| 47 | + reg = PoniardRegressor( |
| 48 | + estimators=[LinearRegression()], cv=2, random_state=0 |
| 49 | + ) |
| 50 | + reg.setup(X, y) |
| 51 | + reg.fit(X, y) |
| 52 | + results_before = reg.get_results() |
| 53 | + |
| 54 | + path = tmp_path / "reg.joblib" |
| 55 | + reg.save(path) |
| 56 | + loaded = PoniardRegressor.load(path) |
| 57 | + |
| 58 | + assert isinstance(loaded, PoniardRegressor) |
| 59 | + pd.testing.assert_frame_equal(loaded.get_results(), results_before) |
| 60 | + |
| 61 | + |
| 62 | +def test_loaded_estimator_can_export_pipeline(tmp_path): |
| 63 | + X, y = _clf_data() |
| 64 | + clf = PoniardClassifier( |
| 65 | + estimators=[LogisticRegression()], cv=2, random_state=0 |
| 66 | + ) |
| 67 | + clf.setup(X, y) |
| 68 | + clf.fit(X, y) |
| 69 | + path = tmp_path / "clf.joblib" |
| 70 | + clf.save(path) |
| 71 | + loaded = PoniardClassifier.load(path) |
| 72 | + model = loaded.get_estimator( |
| 73 | + "LogisticRegression", retrain=True, X=X, y=y |
| 74 | + ) |
| 75 | + assert len(model.predict(X)) == len(X) |
| 76 | + |
| 77 | + |
| 78 | +def test_save_load_after_tuning(tmp_path): |
| 79 | + X, y = _clf_data() |
| 80 | + clf = PoniardClassifier( |
| 81 | + estimators=[LogisticRegression()], cv=2, random_state=0 |
| 82 | + ) |
| 83 | + clf.setup(X, y) |
| 84 | + clf.fit(X, y) |
| 85 | + clf.tune_estimator( |
| 86 | + "LogisticRegression", |
| 87 | + X, |
| 88 | + y, |
| 89 | + grid={"LogisticRegression__C": [0.1, 1.0]}, |
| 90 | + ) |
| 91 | + clf.fit(X, y) |
| 92 | + path = tmp_path / "tuned.joblib" |
| 93 | + clf.save(path) |
| 94 | + loaded = PoniardClassifier.load(path) |
| 95 | + assert "LogisticRegression_tuned" in loaded.pipelines |
| 96 | + assert loaded.get_results().shape[0] == 3 |
0 commit comments