forked from jasonlaska/spherecluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
von_mises_fisher_mixture.py
1151 lines (926 loc) · 38.3 KB
/
von_mises_fisher_mixture.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import warnings
import numpy as np
import scipy.sparse as sp
from scipy.special import iv # modified Bessel function of first kind, I_v
from numpy import i0 # modified Bessel function of first kind order 0, I_0
from scipy.special import logsumexp
from sklearn.base import BaseEstimator, ClusterMixin, TransformerMixin
from sklearn.cluster._kmeans import (
_check_sample_weight,
# _labels_inertia,
_tolerance,
)
from sklearn.utils.validation import FLOAT_DTYPES
from sklearn.utils.validation import check_is_fitted
from sklearn.utils import check_array, check_random_state, as_float_array
from sklearn.preprocessing import normalize
from sklearn.utils.extmath import squared_norm
from sklearn.metrics.pairwise import cosine_distances
from joblib import Parallel, delayed
from . import spherical_kmeans
MAX_CONTENTRATION = 1e10
def _inertia_from_labels(X, centers, labels, v_weights = None):
"""Compute inertia with cosine distance using known labels.
"""
if v_weights is None:
v_weights = np.ones((X.shape[0],))
n_examples, n_features = X.shape
v_weights = v_weights / np.sum(v_weights) * n_examples
inertia = np.zeros((n_examples,))
for ee in range(n_examples):
inertia[ee] = 1 - np.abs(X[ee, :].dot(centers[int(labels[ee]), :]).T)
return np.sum(inertia*v_weights)
def _labels_inertia(X, centers, v_weights = None):
"""Compute labels and inertia with cosine distance.
"""
n_examples, n_features = X.shape
n_clusters, n_features = centers.shape
if v_weights is None:
v_weights = np.ones((n_examples,))
v_weights = v_weights / np.sum(v_weights) * n_examples
labels = np.zeros((n_examples,))
inertia = np.zeros((n_examples,))
for ee in range(n_examples):
dists = np.zeros((n_clusters,))
for cc in range(n_clusters):
dists[cc] = 1 - np.abs(X[ee, :].dot(centers[cc, :]).T)
labels[ee] = np.argmin(dists)
inertia[ee] = dists[int(labels[ee])]
return labels, np.sum(inertia*v_weights)
def _vmf_log(X, kappa, mu):
"""Computs log(vMF(X, kappa, mu)) using built-in numpy/scipy Bessel
approximations.
Works well on small kappa and mu.
"""
n_examples, n_features = X.shape
return np.log(_vmf_normalize(kappa, n_features) * np.exp(kappa * np.abs(X.dot(mu)).T))
def _vmf_normalize(kappa, dim):
"""Compute normalization constant using built-in numpy/scipy Bessel
approximations.
Works well on small kappa and mu.
"""
num = np.power(kappa, dim / 2. - 1.)
if dim / 2. - 1. < 1e-15:
denom = np.power(2. * np.pi, dim / 2.) * i0(kappa)
else:
denom = np.power(2. * np.pi, dim / 2.) * iv(dim / 2. - 1., kappa)
if np.isinf(num):
raise ValueError("VMF scaling numerator was inf.")
if np.isinf(denom):
raise ValueError("VMF scaling denominator was inf.")
if np.abs(denom) < 1e-15:
raise ValueError("VMF scaling denominator was 0.")
return num / denom
def _log_H_asymptotic(nu, kappa):
"""Compute the Amos-type upper bound asymptotic approximation on H where
log(H_\nu)(\kappa) = \int_0^\kappa R_\nu(t) dt.
See "lH_asymptotic <-" in movMF.R and utility function implementation notes
from https://cran.r-project.org/web/packages/movMF/index.html
"""
beta = np.sqrt((nu + 0.5) ** 2)
kappa_l = np.min([kappa, np.sqrt((3. * nu + 11. / 2.) * (nu + 3. / 2.))])
return _S(kappa, nu + 0.5, beta) + (
_S(kappa_l, nu, nu + 2.) - _S(kappa_l, nu + 0.5, beta)
)
def _S(kappa, alpha, beta):
"""Compute the antiderivative of the Amos-type bound G on the modified
Bessel function ratio.
Note: Handles scalar kappa, alpha, and beta only.
See "S <-" in movMF.R and utility function implementation notes from
https://cran.r-project.org/web/packages/movMF/index.html
"""
kappa = 1. * np.abs(kappa)
alpha = 1. * alpha
beta = 1. * np.abs(beta)
a_plus_b = alpha + beta
u = np.sqrt(kappa ** 2 + beta ** 2)
if alpha == 0:
alpha_scale = 0
else:
alpha_scale = alpha * np.log((alpha + u) / a_plus_b)
return u - beta - alpha_scale
def _vmf_log_asymptotic(X, kappa, mu):
"""Compute log(f(x|theta)) via Amos approximation
log(f(x|theta)) = theta' x - log(H_{d/2-1})(\|theta\|)
where theta = kappa * X, \|theta\| = kappa.
Computing _vmf_log helps with numerical stability / loss of precision for
for large values of kappa and n_features.
See utility function implementation notes in movMF.R from
https://cran.r-project.org/web/packages/movMF/index.html
"""
n_examples, n_features = X.shape
log_vfm = kappa * np.abs(X.dot(mu)).T + -_log_H_asymptotic(n_features / 2. - 1., kappa)
return log_vfm
def _log_likelihood(X, centers, weights, concentrations):
if len(np.shape(X)) != 2:
X = X.reshape((1, len(X)))
n_examples, n_features = np.shape(X)
n_clusters, _ = centers.shape
if n_features <= 50: # works up to about 50 before numrically unstable
vmf_f = _vmf_log
else:
vmf_f = _vmf_log_asymptotic
f_log = np.zeros((n_clusters, n_examples))
for cc in range(n_clusters):
f_log[cc, :] = vmf_f(X, concentrations[cc], centers[cc, :])
posterior = np.zeros((n_clusters, n_examples))
weights_log = np.log(weights)
posterior = np.tile(weights_log.T, (n_examples, 1)).T + f_log
for ee in range(n_examples):
posterior[:, ee] = np.exp(posterior[:, ee] - logsumexp(posterior[:, ee]))
return posterior
def _log_likelihood_total(X, centers, weights, concentrations):
if len(np.shape(X)) != 2:
X = X.reshape((1, len(X)))
n_examples, n_features = np.shape(X)
n_clusters, _ = centers.shape
if n_features <= 50: # works up to about 50 before numerically unstable
vmf_f = _vmf_log
else:
vmf_f = _vmf_log_asymptotic
f_log = np.zeros((n_clusters, n_examples))
for cc in range(n_clusters):
f_log[cc, :] = vmf_f(X, concentrations[cc], centers[cc, :])
weights_log = np.log(weights)
log_likelihoods = np.zeros(n_examples)
for ee in range(n_examples):
log_likelihoods[ee] = logsumexp(weights_log + f_log[:, ee])
# Sum the log likelihoods of all data points to get the total log likelihood
total_log_likelihood = np.sum(log_likelihoods)
return total_log_likelihood
def _init_unit_centers(X, n_clusters, random_state, init, add_uniform_cluster=False):
"""Initializes unit norm centers.
Parameters
----------
X : array-like or sparse matrix, shape=(n_samples, n_features)
n_clusters : int, optional, default: 8
The number of clusters to form as well as the number of
centroids to generate.
random_state : integer or numpy.RandomState, optional
The generator used to initialize the centers. If an integer is
given, it fixes the seed. Defaults to the global numpy random
number generator.
init: (string) one of
k-means++ : uses sklearn k-means++ initialization algorithm
spherical-k-means : use centroids from one pass of spherical k-means
random : random unit norm vectors
random-orthonormal : random orthonormal vectors
If an ndarray is passed, it should be of shape (n_clusters, n_features)
and gives the initial centers.
"""
n_examples, n_features = np.shape(X)
n_clusters = n_clusters + (1 if add_uniform_cluster else 0)
if isinstance(init, np.ndarray):
n_init_clusters, n_init_features = init.shape
assert n_init_clusters == n_clusters
assert n_init_features == n_features
# ensure unit normed centers
centers = init
for cc in range(n_clusters):
centers[cc, :] = centers[cc, :] / np.linalg.norm(centers[cc, :])
return centers
elif init == "spherical-k-means":
labels, inertia, centers, iters = spherical_kmeans._spherical_kmeans_single_lloyd(
X, n_clusters, x_squared_norms=np.ones((n_examples,)), init="k-means++"
)
return centers
elif init == "random":
centers = np.random.randn(n_clusters, n_features)
for cc in range(n_clusters):
centers[cc, :] = centers[cc, :] / np.linalg.norm(centers[cc, :])
return centers
elif init == "k-means++":
centers = _init_centroids(
X,
n_clusters,
"k-means++",
random_state=random_state,
x_squared_norms=np.ones((n_examples,)),
)
for cc in range(n_clusters):
centers[cc, :] = centers[cc, :] / np.linalg.norm(centers[cc, :])
return centers
elif init == "random-orthonormal":
centers = np.random.randn(n_clusters, n_features)
q, r = np.linalg.qr(centers.T, mode="reduced")
return q.T
elif init == "random-class":
centers = np.zeros((n_clusters, n_features))
for cc in range(n_clusters):
while np.linalg.norm(centers[cc, :]) == 0:
labels = np.random.randint(0, n_clusters, n_examples)
centers[cc, :] = X[labels == cc, :].sum(axis=0)
for cc in range(n_clusters):
centers[cc, :] = centers[cc, :] / np.linalg.norm(centers[cc, :])
# if add_uniform_cluster:
# # Add an extra cluster with specific initialization
# # Here, we initialize it as a random unit vector
# extra_center = np.random.randn(n_features)
# extra_center /= np.linalg.norm(extra_center)
# if isinstance(centers, np.ndarray):
# centers = np.vstack([centers, extra_center])
# else:
# centers = extra_center.reshape(1, -1)
return centers
def _expectation(X, centers, weights, concentrations, posterior_type="soft"):
"""Compute the log-likelihood of each datapoint being in each cluster.
Parameters
----------
centers (mu) : array, [n_centers x n_features]
weights (alpha) : array, [n_centers, ] (alpha)
concentrations (kappa) : array, [n_centers, ]
Returns
----------
posterior : array, [n_centers, n_examples]
"""
n_examples, n_features = np.shape(X)
n_clusters, _ = centers.shape
if n_features <= 50: # works up to about 50 before numrically unstable
vmf_f = _vmf_log
else:
vmf_f = _vmf_log_asymptotic
f_log = np.zeros((n_clusters, n_examples))
for cc in range(n_clusters):
f_log[cc, :] = vmf_f(X, concentrations[cc], centers[cc, :])
posterior = np.zeros((n_clusters, n_examples))
if posterior_type == "soft":
weights_log = np.log(weights)
posterior = np.tile(weights_log.T, (n_examples, 1)).T + f_log
for ee in range(n_examples):
posterior[:, ee] = np.exp(posterior[:, ee] - logsumexp(posterior[:, ee]))
elif posterior_type == "hard":
weights_log = np.log(weights)
weighted_f_log = np.tile(weights_log.T, (n_examples, 1)).T + f_log
for ee in range(n_examples):
posterior[np.argmax(weighted_f_log[:, ee]), ee] = 1.0
return posterior
# def _maximization(X, posterior, force_weights=None):
# """Estimate new centers, weights, and concentrations from
# Parameters
# ----------
# posterior : array, [n_centers, n_examples]
# The posterior matrix from the expectation step.
# force_weights : None or array, [n_centers, ]
# If None is passed, will estimate weights.
# If an array is passed, will use instead of estimating.
# Returns
# ----------
# centers (mu) : array, [n_centers x n_features]
# weights (alpha) : array, [n_centers, ] (alpha)
# concentrations (kappa) : array, [n_centers, ]
# """
# n_examples, n_features = X.shape
# n_clusters, n_examples = posterior.shape
# concentrations = np.zeros((n_clusters,))
# centers = np.zeros((n_clusters, n_features))
# if force_weights is None:
# weights = np.zeros((n_clusters,))
# for cc in range(n_clusters):
# # update weights (alpha)
# if force_weights is None:
# weights[cc] = np.mean(posterior[cc, :])
# else:
# weights = force_weights
# # update centers (mu)
# X_scaled = X.copy()
# if sp.issparse(X):
# X_scaled.data *= posterior[cc, :].repeat(np.diff(X_scaled.indptr))
# else:
# for ee in range(n_examples):
# X_scaled[ee, :] *= posterior[cc, ee]
# centers[cc, :] = X_scaled.sum(axis=0)
# # normalize centers
# center_norm = np.linalg.norm(centers[cc, :])
# if center_norm > 1e-8:
# centers[cc, :] = centers[cc, :] / center_norm
# # update concentration (kappa) [TODO: add other kappa approximations]
# rbar = center_norm / (n_examples * weights[cc])
# concentrations[cc] = rbar * n_features - np.power(rbar, 3.)
# if np.abs(rbar - 1.0) < 1e-10:
# concentrations[cc] = MAX_CONTENTRATION
# else:
# concentrations[cc] /= 1. - np.power(rbar, 2.)
# # let python know we can free this (good for large dense X)
# del X_scaled
# return centers, weights, concentrations
def _maximization(X, posterior, v_weights=None, force_weights=None):
"""Estimate new centers, weights, and concentrations from
Parameters
----------
posterior : array, [n_centers, n_examples]
The posterior matrix from the expectation step.
force_weights : None or array, [n_centers, ]
If None is passed, will estimate weights.
If an array is passed, will use instead of estimating.
Returns
----------
centers (mu) : array, [n_centers x n_features]
weights (alpha) : array, [n_centers, ] (alpha)
concentrations (kappa) : array, [n_centers, ]
"""
n_examples, n_features = X.shape
n_clusters, n_examples = posterior.shape
concentrations = np.zeros((n_clusters,))
std_devs = np.zeros((n_clusters,))
centers = np.zeros((n_clusters, n_features))
if force_weights is None:
weights = np.zeros((n_clusters,))
if v_weights is None:
v_weights = np.ones(n_examples)
for cc in range(n_clusters):
# update weights (alpha)
if force_weights is None:
weights[cc] = np.mean(posterior[cc, :]*v_weights)
else:
weights = force_weights
# update centers (mu) and concentration (kappa) using fisher_stats
weights_cluster = posterior[cc, :]
centers[cc, :], concentrations[cc], std_devs[cc] = fisher_stats(X.T, weights=weights_cluster*v_weights)
return centers, weights, concentrations, std_devs
def fisher_stats(xyz, weights=None, conf=68.27):
"""
Returns the resultant vector from a series of longitudes and latitudes. If
a confidence is set the function additionally returns the opening angle
of the confidence small circle (Fisher, 19..) and the dispersion factor
(kappa).
Parameters
----------
lons : array-like
A sequence of longitudes (in radians)
lats : array-like
A sequence of latitudes (in radians)
conf : confidence value
The confidence used for the calculation (float). Defaults to None.
Returns
-------
mean vector: tuple
The point that lies in the center of a set of vectors.
(Longitude, Latitude) in radians.
If 1 vector is passed to the function it returns two None-values. For
more than one vector the following 3 values are returned as a tuple:
r_value: float
The magnitude of the resultant vector (between 0 and 1) This represents
the degree of clustering in the data.
angle: float
The opening angle of the small circle that corresponds to confidence
of the calculated direction.
kappa: float
A measure for the amount of dispersion of a group of layers. For
one vector the factor is undefined. Approaches infinity for nearly
parallel vectors and zero for highly dispersed vectors.
"""
if weights is None:
weights = np.ones(len(xyz.T))
weights = weights.copy()
weights[np.isnan(weights)] = 1
# Calculate the weighted orientation tensor
orientation_tensor = np.zeros((3, 3))
total_weight = 0
for n, w in zip(xyz.T, weights):
orientation_tensor += w * np.outer(n, n)
total_weight += w
orientation_tensor /= total_weight
# Find eigenvectors and eigenvalues
eigenvalues, eigenvectors = np.linalg.eig(orientation_tensor)
# Select the eigenvector corresponding to the largest eigenvalue
max_index = np.argmax(eigenvalues)
mean_vector = eigenvectors[:, max_index]
if len(xyz.T) > 1:
# Calculate the weighted Fisher k value
R = eigenvalues[max_index]
k = (total_weight-1) / (total_weight - np.sqrt(R)*total_weight)
# Calculate the variability angle associated with the specified confidence level
p = (100-conf)/100
fract1 = (1 - np.sqrt(R)) / (np.sqrt(R))
fract3 = 1.0 / (total_weight - 1.0)
confidence_angle = np.arccos(1 - fract1 * ((1 / p) ** fract3 - 1))
confidence_angle = np.degrees(confidence_angle)
variability_angle = confidence_angle*(total_weight-1)**0.5
return mean_vector, k, variability_angle
else:
return None, None
def _movMF(
X,
n_clusters,
v_weights=None,
posterior_type="soft",
force_weights=None,
max_iter=300,
verbose=False,
init="random-class",
random_state=None,
tol=1e-6,
add_uniform_cluster=False,
random_weight_mod = 1.0
):
"""Mixture of von Mises Fisher clustering.
Implements the algorithms (i) and (ii) from
"Clustering on the Unit Hypersphere using von Mises-Fisher Distributions"
by Banerjee, Dhillon, Ghosh, and Sra.
TODO: Currently only supports Banerjee et al 2005 approximation of kappa,
however, there are numerous other approximations see _update_params.
Attribution
----------
Approximation of log-vmf distribution function from movMF R-package.
movMF: An R Package for Fitting Mixtures of von Mises-Fisher Distributions
by Kurt Hornik, Bettina Grun, 2014
Find more at:
https://cran.r-project.org/web/packages/movMF/vignettes/movMF.pdf
https://cran.r-project.org/web/packages/movMF/index.html
Parameters
----------
n_clusters : int, optional, default: 8
The number of clusters to form as well as the number of
centroids to generate.
posterior_type: 'soft' or 'hard'
Type of posterior computed in exepectation step.
See note about attribute: self.posterior_
force_weights : None or array [n_clusters, ]
If None, the algorithm will estimate the weights.
If an array of weights, algorithm will estimate concentrations and
centers with given weights.
max_iter : int, default: 300
Maximum number of iterations of the k-means algorithm for a
single run.
n_init : int, default: 10
Number of time the k-means algorithm will be run with different
centroid seeds. The final results will be the best output of
n_init consecutive runs in terms of inertia.
init: (string) one of
random-class [default]: random class assignment & centroid computation
k-means++ : uses sklearn k-means++ initialization algorithm
spherical-k-means : use centroids from one pass of spherical k-means
random : random unit norm vectors
random-orthonormal : random orthonormal vectors
If an ndarray is passed, it should be of shape (n_clusters, n_features)
and gives the initial centers.
tol : float, default: 1e-6
Relative tolerance with regards to inertia to declare convergence
n_jobs : int
The number of jobs to use for the computation. This works by computing
each of the n_init runs in parallel.
If -1 all CPUs are used. If 1 is given, no parallel computing code is
used at all, which is useful for debugging. For n_jobs below -1,
(n_cpus + 1 + n_jobs) are used. Thus for n_jobs = -2, all CPUs but one
are used.
random_state : integer or numpy.RandomState, optional
The generator used to initialize the centers. If an integer is
given, it fixes the seed. Defaults to the global numpy random
number generator.
verbose : int, default 0
Verbosity mode.
copy_x : boolean, default True
When pre-computing distances it is more numerically accurate to center
the data first. If copy_x is True, then the original data is not
modified. If False, the original data is modified, and put back before
the function returns, but small numerical differences may be introduced
by subtracting and then adding the data mean.
"""
random_state = check_random_state(random_state)
n_examples, n_features = np.shape(X)
# init centers (mus)
centers = _init_unit_centers(X, n_clusters, random_state, init, add_uniform_cluster=add_uniform_cluster)
if add_uniform_cluster:
n_clusters = n_clusters + 1
# init weights (alphas)
if force_weights is None:
weights = np.ones((n_clusters,))
weights = weights / np.sum(weights)
else:
weights = force_weights
# init concentrations (kappas)
concentrations = np.ones((n_clusters,))
if add_uniform_cluster:
concentrations[-1] = 0.1
weights[-1] = np.max([weights[-1], np.sum(weights[0:-1])*random_weight_mod])
weights = weights / np.sum(weights)
if verbose:
print("Initialization complete")
for iter in range(max_iter):
centers_prev = centers.copy()
# expectation step
posterior = _expectation(
X, centers, weights, concentrations, posterior_type=posterior_type
)
# maximization step
centers, weights, concentrations_updated, std_devs = _maximization(
X, posterior, v_weights=v_weights, force_weights=force_weights
)
if add_uniform_cluster:
concentrations[:-1] = concentrations_updated[:-1]
average_of_weights = np.mean(weights[:-1])
weights[-1] = np.max([weights[-1], np.sum(weights[0:-1])*random_weight_mod])
weights = weights / np.sum(weights)
else:
concentrations = concentrations_updated
# check convergence
tolcheck = squared_norm(centers_prev - centers)
if tolcheck <= tol:
if verbose:
print(
"Converged at iteration %d: "
"center shift %e within tolerance %e" % (iter, tolcheck, tol)
)
break
# labels come for free via posterior
labels = np.zeros((n_examples,))
for ee in range(n_examples):
labels[ee] = np.argmax(posterior[:, ee])
inertia = _inertia_from_labels(X, centers, labels, v_weights=v_weights)
return centers, weights, concentrations, std_devs, posterior, labels, inertia
def movMF(
X,
n_clusters,
posterior_type="soft",
force_weights=None,
n_init=10,
n_jobs=1,
max_iter=300,
verbose=False,
init="random-class",
random_state=None,
tol=1e-6,
copy_x=True,
v_weights=None,
add_uniform_cluster=False,
random_weight_mod = 1.0,
):
"""Wrapper for parallelization of _movMF and running n_init times.
"""
if n_init <= 0:
raise ValueError(
"Invalid number of initializations."
" n_init=%d must be bigger than zero." % n_init
)
random_state = check_random_state(random_state)
if max_iter <= 0:
raise ValueError(
"Number of iterations should be a positive number,"
" got %d instead" % max_iter
)
best_inertia = np.infty
X = as_float_array(X, copy=copy_x)
tol = _tolerance(X, tol)
if hasattr(init, "__array__"):
init = check_array(init, dtype=X.dtype.type, copy=True)
# _validate_center_shape(X, n_clusters, init)
if n_init != 1:
warnings.warn(
"Explicit initial center position passed: "
"performing only one init in k-means instead of n_init=%d" % n_init,
RuntimeWarning,
stacklevel=2,
)
n_init = 1
# defaults
best_centers = None
best_labels = None
best_weights = None
best_concentrations = None
best_std_devs = None
best_posterior = None
best_inertia = None
if n_jobs == 1:
# For a single thread, less memory is needed if we just store one set
# of the best results (as opposed to one set per run per thread).
for it in range(n_init):
# cluster on the sphere
(centers, weights, concentrations, std_devs, posterior, labels, inertia) = _movMF(
X,
n_clusters,
v_weights=v_weights,
posterior_type=posterior_type,
force_weights=force_weights,
max_iter=max_iter,
verbose=verbose,
init=init,
random_state=random_state,
tol=tol,
add_uniform_cluster=add_uniform_cluster,
random_weight_mod = random_weight_mod,
)
# determine if these results are the best so far
if best_inertia is None or inertia < best_inertia:
best_centers = centers.copy()
best_labels = labels.copy()
best_weights = weights.copy()
best_concentrations = concentrations.copy()
best_std_devs = std_devs.copy()
best_posterior = posterior.copy()
best_inertia = inertia
else:
# parallelisation of movMF runs
seeds = random_state.randint(np.iinfo(np.int32).max, size=n_init)
results = Parallel(n_jobs=n_jobs, verbose=0)(
delayed(_movMF)(
X,
n_clusters,
v_weights=v_weights,
posterior_type=posterior_type,
force_weights=force_weights,
max_iter=max_iter,
verbose=verbose,
init=init,
random_state=random_state,
tol=tol,
add_uniform_cluster=add_uniform_cluster,
random_weight_mod = random_weight_mod,
)
for seed in seeds
)
# Get results with the lowest inertia
centers, weights, concentrations, std_devs, posteriors, labels, inertia = zip(*results)
best = np.argmin(inertia)
best_labels = labels[best]
best_inertia = inertia[best]
best_centers = centers[best]
best_concentrations = concentrations[best]
best_std_devs = std_devs[best]
best_posterior = posteriors[best]
best_weights = weights[best]
return (
best_centers,
best_labels,
best_inertia,
best_weights,
best_concentrations,
best_std_devs,
best_posterior,
)
class VonMisesFisherMixture(BaseEstimator, ClusterMixin, TransformerMixin):
"""Estimator for Mixture of von Mises Fisher clustering on the unit sphere.
Implements the algorithms (i) and (ii) from
"Clustering on the Unit Hypersphere using von Mises-Fisher Distributions"
by Banerjee, Dhillon, Ghosh, and Sra.
TODO: Currently only supports Banerjee et al 2005 approximation of kappa,
however, there are numerous other approximations see _update_params.
Attribution
----------
Approximation of log-vmf distribution function from movMF R-package.
movMF: An R Package for Fitting Mixtures of von Mises-Fisher Distributions
by Kurt Hornik, Bettina Grun, 2014
Find more at:
https://cran.r-project.org/web/packages/movMF/vignettes/movMF.pdf
https://cran.r-project.org/web/packages/movMF/index.html
Basic sklearn scaffolding from sklearn.cluster.KMeans.
Parameters
----------
n_clusters : int, optional, default: 8
The number of clusters to form as well as the number of
centroids to generate.
posterior_type: 'soft' or 'hard'
Type of posterior computed in exepectation step.
See note about attribute: self.posterior_
force_weights : None or array [n_clusters, ]
If None, the algorithm will estimate the weights.
If an array of weights, algorithm will estimate concentrations and
centers with given weights.
max_iter : int, default: 300
Maximum number of iterations of the k-means algorithm for a
single run.
n_init : int, default: 10
Number of time the k-means algorithm will be run with different
centroid seeds. The final results will be the best output of
n_init consecutive runs in terms of inertia.
init: (string) one of
random-class [default]: random class assignment & centroid computation
k-means++ : uses sklearn k-means++ initialization algorithm
spherical-k-means : use centroids from one pass of spherical k-means
random : random unit norm vectors
random-orthonormal : random orthonormal vectors
If an ndarray is passed, it should be of shape (n_clusters, n_features)
and gives the initial centers.
tol : float, default: 1e-6
Relative tolerance with regards to inertia to declare convergence
n_jobs : int
The number of jobs to use for the computation. This works by computing
each of the n_init runs in parallel.
If -1 all CPUs are used. If 1 is given, no parallel computing code is
used at all, which is useful for debugging. For n_jobs below -1,
(n_cpus + 1 + n_jobs) are used. Thus for n_jobs = -2, all CPUs but one
are used.
random_state : integer or numpy.RandomState, optional
The generator used to initialize the centers. If an integer is
given, it fixes the seed. Defaults to the global numpy random
number generator.
verbose : int, default 0
Verbosity mode.
copy_x : boolean, default True
When pre-computing distances it is more numerically accurate to center
the data first. If copy_x is True, then the original data is not
modified. If False, the original data is modified, and put back before
the function returns, but small numerical differences may be introduced
by subtracting and then adding the data mean.
normalize : boolean, default True
Normalize the input to have unnit norm.
Attributes
----------
cluster_centers_ : array, [n_clusters, n_features]
Coordinates of cluster centers
labels_ :
Labels of each point
inertia_ : float
Sum of distances of samples to their closest cluster center.
weights_ : array, [n_clusters,]
Weights of each cluster in vMF distribution (alpha).
concentrations_ : array [n_clusters,]
Concentration parameter for each cluster (kappa).
Larger values correspond to more concentrated clusters.
posterior_ : array, [n_clusters, n_examples]
Each column corresponds to the posterio distribution for and example.
If posterior_type='hard' is used, there will only be one non-zero per
column, its index corresponding to the example's cluster label.
If posterior_type='soft' is used, this matrix will be dense and the
column values correspond to soft clustering weights.
"""
def __init__(
self,
n_clusters=5,
posterior_type="soft",
force_weights=None,
n_init=10,
n_jobs=1,
max_iter=300,
verbose=False,
init="random-class",
random_state=None,
tol=1e-6,
copy_x=True,
normalize=True,
):
self.n_clusters = n_clusters
self.posterior_type = posterior_type
self.force_weights = force_weights
self.n_init = n_init
self.n_jobs = n_jobs
self.max_iter = max_iter
self.verbose = verbose
self.init = init
self.random_state = random_state
self.tol = tol
self.copy_x = copy_x
self.normalize = normalize
def _check_force_weights(self):
if self.force_weights is None:
return
if len(self.force_weights) != self.n_clusters:
raise ValueError(
(
"len(force_weights)={} but must equal "
"n_clusters={}".format(len(self.force_weights), self.n_clusters)
)
)
def _check_fit_data(self, X):
"""Verify that the number of samples given is larger than k"""
X = check_array(X, accept_sparse="csr", dtype=[np.float64, np.float32])
n_samples, n_features = X.shape
if X.shape[0] < self.n_clusters:
raise ValueError(
"n_samples=%d should be >= n_clusters=%d"
% (X.shape[0], self.n_clusters)
)
for ee in range(n_samples):
if sp.issparse(X):
n = sp.linalg.norm(X[ee, :])
else:
n = np.linalg.norm(X[ee, :])
if np.abs(n - 1.) > 1e-4:
raise ValueError("Data l2-norm must be 1, found {}".format(n))
return X
def _check_test_data(self, X):
X = check_array(X, accept_sparse="csr", dtype=FLOAT_DTYPES)
n_samples, n_features = X.shape
expected_n_features = self.cluster_centers_.shape[1]
if not n_features == expected_n_features:
raise ValueError(
"Incorrect number of features. "
"Got %d features, expected %d" % (n_features, expected_n_features)
)
for ee in range(n_samples):
if sp.issparse(X):
n = sp.linalg.norm(X[ee, :])
else:
n = np.linalg.norm(X[ee, :])