Skip to content

Commit

Permalink
Fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonlaska committed Aug 20, 2017
1 parent 1f88474 commit 3c1d867
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 37 deletions.
10 changes: 9 additions & 1 deletion examples/make_sphere_graphic.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D # NOQA
Expand Down Expand Up @@ -36,4 +37,11 @@
plt.axis('off')
plt.show()

# raw_input()
def r_input(val=None):
val = val or ''
if sys.version_info[0] >= 3:
return eval(input(val))

return raw_input(val)

r_input()
12 changes: 11 additions & 1 deletion examples/small_mix.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import numpy as np
from matplotlib import pyplot as plt
from sklearn.cluster import KMeans
Expand All @@ -16,6 +17,15 @@
Provides a basic smell test that the algoriths are performing as intended.
'''


def r_input(val=None):
val = val or ''
if sys.version_info[0] >= 3:
return eval(input(val))

return raw_input(val)


###############################################################################
# Generate small-mix dataset
mu_0 = np.array([-0.251, -0.968])
Expand Down Expand Up @@ -222,4 +232,4 @@
print("V-measure: %0.3f (vmf-soft)" % metrics.v_measure_score(labels, vmf_soft.labels_))
print("V-measure: %0.3f (vmf-hard)" % metrics.v_measure_score(labels, vmf_hard.labels_))

# raw_input()
r_input()
56 changes: 32 additions & 24 deletions examples/small_mix_3d.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
Expand All @@ -11,6 +12,13 @@
plt.ion()


def r_input(val=None):
val = val or ''
if sys.version_info[0] >= 3:
return eval(input(val))

return raw_input(val)


###############################################################################
# Generate small-mix dataset
Expand All @@ -19,8 +27,8 @@
mu_1 = np.array([0.399, 0.917, 0.713])
mu_1 = mu_1 / np.linalg.norm(mu_1)
mus = [mu_0, mu_1]
kappa_0 = 8 # concentration parameter
kappa_1 = 2 # concentration parameter
kappa_0 = 8 # concentration parameter
kappa_1 = 2 # concentration parameter
kappas = [kappa_0, kappa_1]
num_points_per_class = 300

Expand Down Expand Up @@ -163,45 +171,45 @@
plt.show()


print 'mu 0: {}'.format(mu_0)
print 'mu 0: {} (kmeans), error={} ({})'.format(km.cluster_centers_[km_mu_0_idx], km_mu_0_error, km_mu_0_error_norm)
print 'mu 0: {} (spherical kmeans), error={}'.format(skm.cluster_centers_[skm_mu_0_idx], skm_mu_0_error)
print 'mu 0: {} (vmf-soft), error={}'.format(vmf_soft.cluster_centers_[vmf_soft_mu_0_idx], vmf_soft_mu_0_error)
print 'mu 0: {} (vmf-hard), error={}'.format(vmf_hard.cluster_centers_[vmf_hard_mu_0_idx], vmf_hard_mu_0_error)
print('mu 0: {}'.format(mu_0))
print('mu 0: {} (kmeans), error={} ({})'.format(km.cluster_centers_[km_mu_0_idx], km_mu_0_error, km_mu_0_error_norm))
print('mu 0: {} (spherical kmeans), error={}'.format(skm.cluster_centers_[skm_mu_0_idx], skm_mu_0_error))
print('mu 0: {} (vmf-soft), error={}'.format(vmf_soft.cluster_centers_[vmf_soft_mu_0_idx], vmf_soft_mu_0_error))
print('mu 0: {} (vmf-hard), error={}'.format(vmf_hard.cluster_centers_[vmf_hard_mu_0_idx], vmf_hard_mu_0_error))

print '---'
print 'mu 1: {}'.format(mu_1)
print 'mu 1: {} (kmeans), error={} ({})'.format(km.cluster_centers_[km_mu_1_idx], km_mu_1_error, km_mu_1_error_norm)
print 'mu 1: {} (spherical kmeans), error={}'.format(skm.cluster_centers_[skm_mu_1_idx], skm_mu_1_error)
print 'mu 1: {} (vmf-soft), error={}'.format(vmf_soft.cluster_centers_[vmf_soft_mu_1_idx], vmf_soft_mu_1_error)
print 'mu 1: {} (vmf-hard), error={}'.format(vmf_hard.cluster_centers_[vmf_hard_mu_1_idx], vmf_hard_mu_1_error)
print('---')
print('mu 1: {}'.format(mu_1))
print('mu 1: {} (kmeans), error={} ({})'.format(km.cluster_centers_[km_mu_1_idx], km_mu_1_error, km_mu_1_error_norm))
print('mu 1: {} (spherical kmeans), error={}'.format(skm.cluster_centers_[skm_mu_1_idx], skm_mu_1_error))
print('mu 1: {} (vmf-soft), error={}'.format(vmf_soft.cluster_centers_[vmf_soft_mu_1_idx], vmf_soft_mu_1_error))
print('mu 1: {} (vmf-hard), error={}'.format(vmf_hard.cluster_centers_[vmf_hard_mu_1_idx], vmf_hard_mu_1_error))


print '---'
print 'true kappas {}'.format(kappas)
print 'vmf-soft kappas {}'.format(vmf_soft.concentrations_[[vmf_soft_mu_0_idx, vmf_soft_mu_1_idx]])
print 'vmf-hard kappas {}'.format(vmf_hard.concentrations_[[vmf_hard_mu_0_idx, vmf_hard_mu_1_idx]])
print('---')
print('true kappas {}'.format(kappas))
print('vmf-soft kappas {}'.format(vmf_soft.concentrations_[[vmf_soft_mu_0_idx, vmf_soft_mu_1_idx]]))
print('vmf-hard kappas {}'.format(vmf_hard.concentrations_[[vmf_hard_mu_0_idx, vmf_hard_mu_1_idx]]))

print '---'
print 'vmf-soft weights {}'.format(vmf_soft.weights_[[vmf_soft_mu_0_idx, vmf_soft_mu_1_idx]])
print 'vmf-hard weights {}'.format(vmf_hard.weights_[[vmf_hard_mu_0_idx, vmf_hard_mu_1_idx]])
print('---')
print('vmf-soft weights {}'.format(vmf_soft.weights_[[vmf_soft_mu_0_idx, vmf_soft_mu_1_idx]]))
print('vmf-hard weights {}'.format(vmf_hard.weights_[[vmf_hard_mu_0_idx, vmf_hard_mu_1_idx]]))

print '---'
print('---')
print("Homogeneity: %0.3f (k-means)" % metrics.homogeneity_score(labels, km.labels_))
print("Homogeneity: %0.3f (spherical k-means)" % metrics.homogeneity_score(labels, skm.labels_))
print("Homogeneity: %0.3f (vmf-soft)" % metrics.homogeneity_score(labels, vmf_soft.labels_))
print("Homogeneity: %0.3f (vmf-hard)" % metrics.homogeneity_score(labels, vmf_hard.labels_))

print '---'
print('---')
print("Completeness: %0.3f (k-means)" % metrics.completeness_score(labels, km.labels_))
print("Completeness: %0.3f (spherical k-means)" % metrics.completeness_score(labels, skm.labels_))
print("Completeness: %0.3f" % metrics.completeness_score(labels, vmf_soft.labels_))
print("Completeness: %0.3f" % metrics.completeness_score(labels, vmf_hard.labels_))

print '---'
print('---')
print("V-measure: %0.3f (k-means)" % metrics.v_measure_score(labels, km.labels_))
print("V-measure: %0.3f (spherical k-means)" % metrics.v_measure_score(labels, skm.labels_))
print("V-measure: %0.3f (vmf-soft)" % metrics.v_measure_score(labels, vmf_soft.labels_))
print("V-measure: %0.3f (vmf-hard)" % metrics.v_measure_score(labels, vmf_hard.labels_))

# raw_input()
r_input()
5 changes: 2 additions & 3 deletions spherecluster/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
from spherecluster import SphericalKMeans


class TestCommon(object):
def test_estimator_spherical_k_means(self):
return check_estimator(SphericalKMeans)
def test_estimator_spherical_k_means():
return check_estimator(SphericalKMeans)
24 changes: 16 additions & 8 deletions spherecluster/von_mises_fisher_mixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ def _check_fit_data(self, X):
else:
n = np.linalg.norm(X[ee, :])

if np.abs(n - 1.) > 1e-3:
if np.abs(n - 1.) > 1e-4:
raise ValueError("Data l2-norm must be 1, found {}".format(n))

return X
Expand All @@ -791,7 +791,7 @@ def _check_test_data(self, X):
n = np.linalg.norm(X[ee, :])

if np.abs(n - 1.) > 1e-4:
raise ValueError("Data l2-norm must be 1")
raise ValueError("Data l2-norm must be 1, found {}".format(n))

return X

Expand Down Expand Up @@ -836,7 +836,6 @@ def fit_transform(self, X, y=None):
# np.array or CSR format already.
# XXX This skips _check_test_data, which may change the dtype;
# we should refactor the input validation.
X = self._check_fit_data(X)
return self.fit(X)._transform(X)

def transform(self, X, y=None):
Expand All @@ -855,6 +854,9 @@ def transform(self, X, y=None):
X_new : array, shape [n_samples, k]
X transformed in the new space.
"""
if self.normalize:
X = normalize(X)

check_is_fitted(self, 'cluster_centers_')
X = self._check_test_data(X)
return self._transform(X)
Expand All @@ -881,6 +883,9 @@ def predict(self, X):
labels : array, shape [n_samples,]
Index of the cluster each sample belongs to.
"""
if self.normalize:
X = normalize(X)

check_is_fitted(self, 'cluster_centers_')

X = self._check_test_data(X)
Expand All @@ -899,16 +904,19 @@ def score(self, X, y=None):
score : float
Larger score is better.
"""
check_is_fitted(self, 'cluster_centers_')
if self.normalize:
X = normalize(X)

check_is_fitted(self, 'cluster_centers_')
X = self._check_test_data(X)
return -_labels_inertia(X, self.cluster_centers_)[1]

def log_likelihood(self, X):
check_is_fitted(self, 'cluster_centers_')

return _log_likelihood(
X,
self.cluster_centers_,
self.weights_,
self.concentrations_)
X,
self.cluster_centers_,
self.weights_,
self.concentrations_
)

0 comments on commit 3c1d867

Please sign in to comment.