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

Improve estimation of gamma for k-prototypes #186

Merged
merged 1 commit into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion kmodes/kprototypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ def k_prototypes(X, categorical, n_clusters, max_iter, num_dissim, cat_dissim,
# Estimate a good value for gamma, which determines the weighing of
# categorical values in clusters (see Huang [1997]).
if gamma is None:
gamma = 0.5 * Xnum.std()
gamma = 0.5 * np.mean(Xnum.std(axis=0))

results = []
seeds = random_state.randint(np.iinfo(np.int32).max, size=n_init)
Expand Down
20 changes: 20 additions & 0 deletions kmodes/tests/test_kprototypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,23 @@ def test_pandas_numpy_equality(self):
result_np = kproto.fit_predict(STOCKS, categorical=[1, 2])
result_pd = kproto.fit_predict(pd.DataFrame(STOCKS), categorical=[1, 2])
np.testing.assert_array_equal(result_np, result_pd)

def test_gamma_estimation(self):
data = np.hstack([
np.array([
[0.0],
[0.0],
[0.0],
[1.0],
[1.0],
[1.0],
[2.0],
[2.0],
[2.0],
[3.0],
[4.0],
[5.0],
]), STOCKS])
kproto = kprototypes.KPrototypes(n_clusters=4, init='Cao', random_state=42)
kproto_fitted = kproto.fit(data, categorical=[2, 3])
self.assertEqual(kproto_fitted.gamma, 35.33525036439546)