fuzzyclust.large.OnlineFCM#
- class fuzzyclust.large.OnlineFCM(n_clusters=3, chunk_size=10000, m=2.0, tol=0.0001, max_iter=300, init='random', n_init=1, chunk_init='previous', shuffle=True, random_state=None, verbose=0)[source]#
Bases:
BaseFuzzyClustererOnline fuzzy c-means (oFCM) for very large and streaming data.
Every chunk is clustered on its own with FCM; the \(c\) prototypes of each chunk, weighted by the total membership they collected, are kept. When all chunks have been seen, the \(s\cdot c\) weighted prototypes are clustered into \(c\) clusters with weighted FCM (
WeightedFCM), and the partition of all samples is obtained by one extension step. In contrast toSinglePassFCMthe chunks do not influence each other, so the chunk clusterings can run in parallel or arrive from a stream in any order; the price is the storage of \(s\cdot c\) prototypes and the final clustering of them (Hore et al. 2009; Havens et al. 2012).- Parameters:
- n_clusters
int, default=3 Number of clusters \(c\).
- chunk_size
int, default=10000 Samples per chunk \(n_l\).
- m
float, default=2.0 Fuzziness exponent \(m>1\).
- tol
float, default=1e-4 - max_iter
int, default=300 - init{“random”, “kmeans++”}
orndarray, default=”random” Initialization of the FCM run on the first chunk.
- n_init
int, default=1 Random restarts of the first chunk.
- chunk_init{“previous”, “independent”}, default=”previous”
Start the FCM run on a chunk from the prototypes of the previous chunk (fewer iterations, aligned cluster order) or from a fresh initialization with
init/n_init(fully independent chunk clusterings, as in the original formulation).- shufflebool, default=True
Randomly permute the samples before chunking in
fit;partial_fitnever reorders.- random_state
int,RandomStateinstanceorNone, default=None - verbose
int, default=0
- n_clusters
- Attributes:
- cluster_centers_
ndarrayofshape(n_clusters,n_features) The prototypes of the final weighted clustering of the chunk prototypes.
- center_weights_
ndarrayofshape(n_clusters,) Weight (number of observations) collected by every final prototype.
- chunk_centers_
ndarrayofshape(n_chunks,n_clusters,n_features) Prototypes of every chunk.
- chunk_weights_
ndarrayofshape(n_chunks,n_clusters) Weights \(w_{li}=\sum_k\mu_{ik}\) of the chunk prototypes.
- chunk_objective_, chunk_n_iter_
ndarrayofshape(n_chunks,) - n_chunks_
int - centers_model_
WeightedFCMorNone The final weighted clusterer of the chunk prototypes (
Noneafter a single chunk, when there is nothing to merge).- membership_
ndarrayofshape(n_samples,n_clusters) Extended partition of the data passed to
fit(afterpartial_fit: of the last chunk).- labels_, distances_, objective_
Of the extended partition.
- objective_history_
ndarray \(J_w\) per iteration of the final weighted clustering.
- n_iter_
int Iterations of the final weighted clustering.
- cluster_centers_
Notes
\[\begin{split}(\mathbf{U}_l,\mathbf{V}_l)&=\text{FCM}(\mathbf{X}_l),\qquad w_{li}=\sum_{k}\mu_{l,ik},\qquad l=1,\dots,s,\\ \mathbf{V}&=\text{wFCM}\big([\mathbf{V}_1;\dots;\mathbf{V}_s],\ [\mathbf{w}_1;\dots;\mathbf{w}_s]\big),\\ \mathbf{U}&=\text{extend}(\mathbf{X},\mathbf{V}).\end{split}\]The final weighted clustering is initialized with the prototypes of the last chunk.
partial_fit()appends the prototypes of a new chunk and repeats the (cheap) final clustering, so the model is usable after every chunk of a stream.References
[1]T. C. Havens, J. C. Bezdek, C. Leckie, L. O. Hall, M. Palaniswami, “Fuzzy c-means algorithms for very large data”, IEEE Transactions on Fuzzy Systems 20(6), 1130–1146, 2012.
[2]P. Hore, L. O. Hall, D. B. Goldgof, Y. Gu, A. A. Maudsley, A. Darkazanli, “A scalable framework for segmenting magnetic resonance images”, Journal of Signal Processing Systems 54(1–3), 183–203, 2009.
Examples
>>> from fuzzyclust.large import OnlineFCM, make_large_blobs >>> X, y = make_large_blobs(n_samples=5000, n_clusters=3, random_state=0) >>> of = OnlineFCM(n_clusters=3, chunk_size=1000, random_state=0).fit(X) >>> of.n_chunks_, of.chunk_weights_.shape, of.membership_.shape (5, (5, 3), (5000, 3))
- fit(X, y=None)[source]#
Cluster every chunk, cluster the weighted chunk prototypes, extend to all samples.
- Parameters:
- Xarray_like
ofshape(n_samples,n_features) - y
Ignored
- Xarray_like
- Returns:
- self
object
- self
- partial_fit(X, y=None)[source]#
Add one chunk of a stream: cluster it, store its weighted prototypes, update the model.
membership_andlabels_refer to the chunk just processed; usepredict_proba()for other data.- Parameters:
- Xarray_like
ofshape(n_chunk,n_features) - y
Ignored
- Xarray_like
- Returns:
- self
object
- self
Examples using fuzzyclust.large.OnlineFCM#
Fuzzy c-means for very large data: rseFCM, spFCM and oFCM
Clustering a drifting stream chunk by chunk with partial_fit