def test_wrap_polynomial_features(): # load data X = fetch_california_housing(as_frame=True).frame # prepare selectors tr = PolynomialFeatures() tr_wrap = SklearnTransformerWrapper(transformer=PolynomialFeatures()) # Test: # When passing variable list varlist = ["MedInc", "HouseAge", "AveRooms", "AveBedrms"] tr_wrap.set_params(variables=varlist) Xt = pd.DataFrame(tr.fit_transform(X[varlist]), columns=tr.get_feature_names_out(varlist)) Xw = tr_wrap.fit_transform(X) pd.testing.assert_frame_equal(Xw, pd.concat([X, Xt], axis=1)) assert Xw.shape[1] == len(X.columns) + len( tr.get_feature_names_out(varlist)) # when variable list is None tr_wrap.set_params(variables=None) Xt = pd.DataFrame(tr.fit_transform(X), columns=tr.get_feature_names_out()) Xw = tr_wrap.fit_transform(X) pd.testing.assert_frame_equal(Xw, pd.concat([X, Xt], axis=1)) assert Xw.shape[1] == len(X.columns) + len( tr.get_feature_names_out(X.columns))
def test_wrap_transformers(transformer): # load data X = fetch_california_housing(as_frame=True).frame # prepare selectors tr = clone(transformer) tr_wrap = SklearnTransformerWrapper(transformer=transformer) # Test: # When passing variable list varlist = ["MedInc", "HouseAge", "AveRooms", "AveBedrms"] tr_wrap.set_params(variables=varlist) Xt = pd.DataFrame(tr.fit_transform(X[varlist]), columns=X[varlist].columns) Xw = tr_wrap.fit_transform(X) remaining = [f for f in X.columns if f not in varlist] assert Xt.shape[1] == 4 assert Xw.shape[1] == 9 pd.testing.assert_frame_equal(Xt, Xw[varlist]) pd.testing.assert_frame_equal(X[remaining], Xw[remaining]) # when variable list is None tr_wrap.set_params(variables=None) Xt = pd.DataFrame(tr.fit_transform(X), columns=X.columns) Xw = tr_wrap.fit_transform(X) pd.testing.assert_frame_equal(Xt, Xw)
def test_inverse_transform(transformer): X = fetch_california_housing(as_frame=True).frame X = X.drop(["Longitude"], axis=1) tr_wrap = SklearnTransformerWrapper(transformer=transformer) # When passing variable list varlist = ["MedInc", "HouseAge", "AveRooms", "AveBedrms"] tr_wrap.set_params(variables=varlist) X_tr = tr_wrap.fit_transform(X) X_inv = tr_wrap.inverse_transform(X_tr) pd.testing.assert_frame_equal(X_inv, X) # when variable list is None tr_wrap.set_params(variables=None) X_tr = tr_wrap.fit_transform(X) X_inv = tr_wrap.inverse_transform(X_tr) pd.testing.assert_frame_equal(X_inv, X)
def test_wrap_selectors(transformer): # load data X = fetch_california_housing(as_frame=True).frame y = X["MedHouseVal"] X = X.drop(["MedHouseVal"], axis=1) # prepare selectors sel = clone(transformer) sel_wrap = SklearnTransformerWrapper(transformer=transformer) # Test: # When passing variable list varlist = ["MedInc", "HouseAge", "AveRooms", "AveBedrms"] sel_wrap.set_params(variables=varlist) Xt = pd.DataFrame( sel.fit_transform(X[varlist], y), columns=X[varlist].columns[(sel.get_support())], ) Xw = sel_wrap.fit_transform(X, y) selected = X[varlist].columns[(sel.get_support())] remaining = [f for f in X.columns if f not in varlist] pd.testing.assert_frame_equal(Xt, Xw[selected]) pd.testing.assert_frame_equal(X[remaining], Xw[remaining]) assert Xw.shape[1] == len(remaining) + len(selected) # when variable list is None sel_wrap.set_params(variables=None) Xt = pd.DataFrame(sel.fit_transform(X, y), columns=X.columns[(sel.get_support())]) Xw = sel_wrap.fit_transform(X, y) pd.testing.assert_frame_equal(Xt, Xw)