Skip to content

Commit

Permalink
Merge pull request #5 from clara-labs/feature/python3_cleanup
Browse files Browse the repository at this point in the history
Feature/python3 cleanup
  • Loading branch information
jasonlaska authored Aug 20, 2017
2 parents 3e20dec + 3c1d867 commit 7d7697c
Show file tree
Hide file tree
Showing 13 changed files with 429 additions and 374 deletions.
4 changes: 4 additions & 0 deletions example-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-r requirements.txt
tabulate==0.7.7
matplotlib==2.0.2
seaborn==0.8
3 changes: 0 additions & 3 deletions examples/document_clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
import logging
from sklearn.cluster import KMeans

import sys
sys.path.append('../spherecluster')

from spherecluster import SphericalKMeans
from spherecluster import VonMisesFisherMixture

Expand Down
33 changes: 20 additions & 13 deletions examples/make_sphere_graphic.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import sys
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import seaborn

import sys
sys.path.append('../spherecluster')
from mpl_toolkits.mplot3d import Axes3D # NOQA
import seaborn # NOQA

from spherecluster import sample_vMF

Expand All @@ -19,22 +17,31 @@

Xs = []
for nn in range(n_clusters):
new_X = sample_vMF(mus[nn], kappas[nn], num_points_per_class)
Xs.append(new_X.T)
new_X = sample_vMF(mus[nn], kappas[nn], num_points_per_class)
Xs.append(new_X.T)


fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(1, 1, 1, aspect='equal', projection='3d',
adjustable='box-forced', xlim=[-1.1, 1.1], ylim=[-1.1, 1.1],
zlim=[-1.1, 1.1])
ax = fig.add_subplot(
1, 1, 1, aspect='equal', projection='3d',
adjustable='box-forced', xlim=[-1.1, 1.1], ylim=[-1.1, 1.1],
zlim=[-1.1, 1.1]
)

colors = ['b', 'r', 'g']
for nn in range(n_clusters):
ax.scatter(Xs[nn][0, :], Xs[nn][1, :], Xs[nn][2, :], c=colors[nn])
ax.hold(True)
ax.scatter(Xs[nn][0, :], Xs[nn][1, :], Xs[nn][2, :], c=colors[nn])
ax.hold(True)

ax.set_aspect('equal')
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()
62 changes: 34 additions & 28 deletions examples/small_mix.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import sys
import numpy as np
from matplotlib import pyplot as plt
from sklearn.cluster import KMeans
from sklearn import metrics

import sys
sys.path.append('../spherecluster')

from spherecluster import SphericalKMeans
from spherecluster import VonMisesFisherMixture
from spherecluster import sample_vMF
Expand All @@ -19,13 +17,22 @@
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])
mu_1 = np.array([0.399, 0.917])
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 = 100

Expand Down Expand Up @@ -184,46 +191,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()
60 changes: 32 additions & 28 deletions examples/small_mix_3d.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import sys
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn.cluster import KMeans
from sklearn import metrics

import sys
sys.path.append('../spherecluster')

from spherecluster import SphericalKMeans
from spherecluster import VonMisesFisherMixture
from spherecluster import sample_vMF

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 @@ -22,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 @@ -166,46 +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()
9 changes: 3 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
scikit-learn==0.17.1
tabulate==0.7.5
matplotlib==1.4.3
seaborn==0.6.0
pytest==2.9.2
scikit-learn==0.19.0
pytest==3.2.1
numpy
scipy
scipy
25 changes: 13 additions & 12 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,25 @@


try:
import numpy
import numpy # NOQA
except ImportError:
print('numpy is required during installation')
sys.exit(1)

try:
import scipy
import scipy # NOQA
except ImportError:
print('scipy is required during installation')
sys.exit(1)

setup(name='spherecluster',
version='0.1.2',
description='Clustering on the unit hypersphere in scikit-learn.',
author='Jason Laska',
author_email='[email protected]',
packages=find_packages(),
install_requires=INSTALL_REQUIRES,
url='https://github.com/clara-labs/spherecluster',
license='MIT',
)
setup(
name='spherecluster',
version='0.1.5',
description='Clustering on the unit hypersphere in scikit-learn.',
author='Jason Laska',
author_email='[email protected]',
packages=find_packages(),
install_requires=INSTALL_REQUIRES,
url='https://github.com/clara-labs/spherecluster',
license='MIT',
)
3 changes: 2 additions & 1 deletion spherecluster/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import absolute_import
from .spherical_kmeans import SphericalKMeans
from .von_mises_fisher_mixture import VonMisesFisherMixture
from .util import sample_vMF
Expand All @@ -6,4 +7,4 @@
'SphericalKMeans',
'VonMisesFisherMixture',
'sample_vMF',
]
]
Loading

0 comments on commit 7d7697c

Please sign in to comment.