def test_stateful_transform(): data_train = patsy.demo_data("x1", "x2", "y") data_train['x1'][:] = 1 # mean of x1 is 1 data_test = patsy.demo_data("x1", "x2", "y") data_test['x1'][:] = 0 # center x1 est = PatsyTransformer("center(x1) + x2") est.fit(data_train) data_trans = est.transform(data_test) # make sure that mean of training, not test data was removed assert_array_equal(data_trans[:, 0], -1)
def test_intercept_transformer(): data = patsy.demo_data("x1", "x2", "x3", "y") # check wether X contains only the two features, no intercept est = PatsyTransformer("x1 + x2") est.fit(data) assert_equal(est.transform(data).shape[1], 2) # check wether X does contain intercept est = PatsyTransformer("x1 + x2", add_intercept=True) est.fit(data) data_transformed = est.transform(data) assert_array_equal(data_transformed[:, 0], 1) assert_equal(est.transform(data).shape[1], 3)
def test_scope_transformer(): data = patsy.demo_data("x1", "x2", "x3", "y") def myfunc(x): tmp = np.ones_like(x) tmp.fill(42) return tmp est = PatsyTransformer("x1 + myfunc(x2)") est.fit(data) data_trans = est.transform(data) assert_array_equal(data_trans[:, 1], 42) est = PatsyTransformer("x1 + myfunc(x2)") data_trans = est.fit_transform(data) assert_array_equal(data_trans[:, 1], 42)
def test_stateful_transform_dataframe(): data_train = pd.DataFrame(patsy.demo_data("x1", "x2", "y")) data_train['x1'][:] = 1 # mean of x1 is 1 data_test = pd.DataFrame(patsy.demo_data("x1", "x2", "y")) data_test['x1'][:] = 0 # center x1 est = PatsyTransformer("center(x1) + x2", return_type='dataframe') est.fit(data_train) data_trans = est.transform(data_test) # make sure result is pandas dataframe assert type(data_trans) is pd.DataFrame # make sure that mean of training, not test data was removed assert_array_equal(data_trans['center(x1)'][:],-1)
def test_stateful_transform_dataframe(): data_train = pd.DataFrame(patsy.demo_data("x1", "x2", "y")) data_train['x1'][:] = 1 # mean of x1 is 1 data_test = pd.DataFrame(patsy.demo_data("x1", "x2", "y")) data_test['x1'][:] = 0 # center x1 est = PatsyTransformer("center(x1) + x2", return_type='dataframe') est.fit(data_train) data_trans = est.transform(data_test) # make sure result is pandas dataframe assert type(data_trans) is pd.DataFrame # make sure that mean of training, not test data was removed assert_array_equal(data_trans['center(x1)'][:], -1)
def test_scope_transformer(): data = patsy.demo_data("x1", "x2", "x3", "y") def myfunc(x): tmp = np.ones_like(x) tmp.fill(42) return tmp est = PatsyTransformer("x1 + myfunc(x2)") est.fit(data) data_trans = est.transform(data) assert_array_equal(data_trans[:, 1], 42) est = PatsyTransformer("x1 + myfunc(x2)") data_trans = est.fit_transform(data) assert_array_equal(data_trans[:, 1], 42) # test feature names assert_equal(est.feature_names_, ["x1", "myfunc(x2)"])