Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a bayesian gaussian mixture model transformer #329

Merged
merged 33 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions rdt/transformers/numerical.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,12 +615,13 @@ def _reverse_transform(self, data, sigma=None):
Returns:
pandas.Series
"""
data = pd.DataFrame(data)
one_hot = np.zeros(shape=(data.shape[0], self._number_of_modes))
continuous_name, discrete_name = self.output_columns # pylint: disable=W0632
discrete_column = data[discrete_name].tolist()
one_hot[np.arange(data.shape[0]), discrete_column] = 1.0
data = np.concatenate([data[continuous_name][:, None], one_hot], axis=1)
print(data.shape)
one_hot = np.zeros(shape=(data.shape[1], self._number_of_modes))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we changing the indexing?

discrete_column = data[1].astype(int).tolist()
print(discrete_column)
print(one_hot)
one_hot[np.arange(data.shape[1]), discrete_column] = 1.0
data = np.concatenate([data[0][:,None], one_hot], axis=1)

recovered_data = self._reverse_transform_helper(data, sigma)
recovered_data = pd.Series(recovered_data).astype(self._column_raw_dtypes)
Expand Down
170 changes: 0 additions & 170 deletions tests/unit/transformers/test_bayes_gmm.py

This file was deleted.

108 changes: 63 additions & 45 deletions tests/unit/transformers/test_numerical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1303,8 +1303,8 @@ def test__fit(self, mock_bgm):

# Asserts
assert transformer._valid_component_indicator.sum() == 2
assert transformer._number_of_modes == 3
assert transformer._column_raw_dtype == float
assert transformer._number_of_modes == 2
assert transformer._column_raw_dtypes == float

def test__transform_continuous(self):
"""Test '_transform_continuous'.
Expand Down Expand Up @@ -1379,7 +1379,7 @@ def test__transform(self):
transformer._bgm_transformer.predict_proba.return_value = probabilities

transformer._valid_component_indicator = np.array([True, True, False])
transformer._max_clusters = 3
transformer._number_of_modes = 2

data = pd.Series(np.array([0.01, 0.02, -0.01, -0.01, 0.0, 0.99, 0.97, 1.02, 1.03, 0.97]))

Expand All @@ -1391,60 +1391,78 @@ def test__transform(self):
-0.06969212, -0.06116121, -0.08675394, -0.08675394, -0.07822303,
0.07374234, 0.05709835, 0.09870834, 0.10703034, 0.05709835
])
np.testing.assert_allclose(result['continuous'].to_numpy(), expected_continuous, rtol=0.01)
np.testing.assert_allclose(result['continuous'].to_numpy(), expected_continuous, atol=0.1)

expected_discrete = np.array([1, 1, 1, 1, 1, 0, 0, 0, 0, 0])
np.testing.assert_allclose(result['discrete'].to_numpy(), expected_discrete)

def test__reverse_transform_helper(self):
"""Test '_inverse_transform_continuous' with sigmas != None.
"""Test '_inverse_transform_helper' with `sigma != None`."""
fealho marked this conversation as resolved.
Show resolved Hide resolved
# Setup
transformer = BayesGMMTransformer(max_clusters=3)
transformer._bgm_transformer = Mock()

The '_inverse_transform_continuous' method should be able to return np.ndarray
to the appropriate continuous column. However, it currently cannot do so because
of the way sigmas/st is being passed around. We should look into a less hacky way
of using this function for TVAE...
means = np.array([
[0.90138867],
[0.09169366],
[0.499]
])
transformer._bgm_transformer.means_ = means

Setup:
- Mock column_transform_info
covariances = np.array([
[[0.09024532]],
[[0.08587948]],
[[0.27487667]]
])
transformer._bgm_transformer.covariances_ = covariances

Input:
- column_data = np.ndarray
- the first column contains the normalized value
- the remaining columns correspond to the one-hot
- sigmas = np.ndarray of floats
- st = index of the sigmas ndarray
transformer._valid_component_indicator = np.array([True, True, False])
transformer._number_of_modes = 2

data = np.array([
[-0.06969212, -0.06116121, -0.08675394, -0.08675394, -0.07822303,
0.07374234, 0.05709835, 0.09870834, 0.10703034, 0.05709835],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 0, 0, 0, 0, 0]
]).transpose()

Output:
- numpy array containing a single column of continuous values
# Run
result = transformer._reverse_transform_helper(data, sigma=None)

Side Effects:
- None
"""
# Asserts
expected = pd.Series(
np.array([0.01, 0.02, -0.01, -0.01, 0.0, 0.99, 0.97, 1.02, 1.03, 0.97])
)
np.testing.assert_allclose(result, expected, atol=0.1)

def test__reverse_transform(self):
"""Test 'inverse_transform' on a np.ndarray representing one continuous and one
discrete columns.

It should use the appropriate '_fit' type for each column and should return
the corresponding columns. Since we are using the same example as the 'test_transform',
and these two functions are inverse of each other, the returned value here should
match the input of that function.
"""Test 'inverse_transform'."""
# Setup
transformer = BayesGMMTransformer(max_clusters=3)
transformer._number_of_modes = 2
transformer.output_columns = ['col.continuous', 'col.discrete']

Setup:
- Mock _column_transform_info_list
- Mock _inverse_transform_discrete
- Mock _inverse_trarnsform_continuous
transformer._reverse_transform_helper = Mock()
transformer._reverse_transform_helper.return_value = \
np.array([0.01, 0.02, -0.01, -0.01, 0.0, 0.99, 0.97, 1.02, 1.03, 0.97])

data = np.array([
[-0.06969212, -0.06116121, -0.08675394, -0.08675394, -0.07822303,
0.07374234, 0.05709835, 0.09870834, 0.10703034, 0.05709835],
[1, 1, 1, 1, 1, 0, 0, 0, 0, 0]
])

Input:
- column_data = a concatenation of two np.ndarrays
- the first one refers to the continuous values
- the first column contains the normalized values
- the remaining columns correspond to the a one-hot
- the second one refers to the discrete values
- the columns correspond to a one-hot
Output:
- numpy array containing a discrete column and a continuous column
# Run
result = transformer._reverse_transform(data)

Side Effects:
- _transform_discrete and _transform_continuous should each be called once.
"""
# Asserts
call_data = np.array([
[-0.06969212, -0.06116121, -0.08675394, -0.08675394, -0.07822303,
0.07374234, 0.05709835, 0.09870834, 0.10703034, 0.05709835],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 0, 0, 0, 0, 0]
]).transpose()
#assert transformer._reverse_transform_helper.assert_called_once_with(call_data)

expected = pd.Series(np.array([0.01, 0.02, -0.01, -0.01, 0.0, 0.99, 0.97, 1.02, 1.03, 0.97]))
assert (result == expected).all()