fuzzyclust.large.SinglePassFCM#
- class fuzzyclust.large.SinglePassFCM(n_clusters=3, chunk_size=10000, m=2.0, tol=0.0001, max_iter=300, init='random', n_init=1, shuffle=True, forgetting_factor=1.0, random_state=None, verbose=0)[source]#
Bases:
BaseFuzzyClustererSingle-pass fuzzy c-means (spFCM) for very large and streaming data.
The data are processed in chunks that fit into memory. The first chunk is clustered with FCM; the resulting \(c\) prototypes, each weighted by the total membership it collected, are carried over as \(c\) extra weighted samples into the next chunk, which is clustered with weighted FCM (
WeightedFCM) starting from the previous prototypes, and so on. The prototypes of the last chunk summarize the whole data set; the partition of all samples is obtained by one extension step. Every chunk is seen once, the working memory is \(O(\text{chunk size}\cdot n)\) andpartial_fit()processes a stream chunk by chunk (Hore, Hall and Goldgof 2007; Havens et al. 2012).- Parameters:
- n_clusters
int, default=3 Number of clusters \(c\).
- chunk_size
int, default=10000 Samples per chunk \(n_l\) (
fitsplits the data into \(s=\lceil N/n_l\rceil\) chunks).- m
float, default=2.0 Fuzziness exponent \(m>1\).
- tol
float, default=1e-4 - max_iter
int, default=300 Termination of the (weighted) FCM run on every chunk.
- init{“random”, “kmeans++”}
orndarray, default=”random” Initialization of the first chunk; later chunks start from the carried-over prototypes.
- n_init
int, default=1 Random restarts of the first chunk.
- shufflebool, default=True
Randomly permute the samples before chunking in
fit(the chunks of the paper are random subsets). SetFalseto keep the order of the data, e.g. to simulate a stream;partial_fitnever reorders.- forgetting_factor
float, default=1.0 Factor \(\lambda\in[0,1]\) by which the weights of the carried prototypes are multiplied before they enter the next chunk.
1.0is the algorithm of the paper (all past observations count fully); a smaller value lets the model follow a drifting stream, the old data being down-weighted geometrically (an effective memory of about \(1/(1-\lambda)\) chunks).0.0clusters every chunk on its own, started from the previous prototypes.- random_state
int,RandomStateinstanceorNone, default=None - verbose
int, default=0
- n_clusters
- Attributes:
- cluster_centers_
ndarrayofshape(n_clusters,n_features) Prototypes after the last chunk.
- center_weights_
ndarrayofshape(n_clusters,) Weight \(w_i=\sum_k w_k\mu_{ik}\) of every prototype after the last chunk (the number of observations it stands for).
- chunk_centers_
ndarrayofshape(n_chunks,n_clusters,n_features) Prototypes after every chunk (the chunk timeline).
- chunk_weights_
ndarrayofshape(n_chunks,n_clusters) - chunk_objective_
ndarrayofshape(n_chunks,) Final \(J_w\) of every chunk run.
- chunk_n_iter_
ndarrayofshape(n_chunks,) - n_chunks_
int - membership_
ndarrayofshape(n_samples,n_clusters) Extended partition of the data passed to
fit(afterpartial_fit: of the last chunk).- labels_
ndarrayofshape(n_samples,) - distances_
ndarrayofshape(n_samples,n_clusters) - objective_
float \(J\) of the extended partition.
- objective_history_
ndarrayofshape(n_chunks,) Same as
chunk_objective_.- n_iter_
int Total number of (weighted) FCM iterations over all chunks.
- cluster_centers_
Notes
With chunks \(\mathbf{X}_1,\dots,\mathbf{X}_s\) the algorithm is
\[\begin{split}(\mathbf{U}_1,\mathbf{V}_1)&=\text{wFCM}(\mathbf{X}_1,\ \mathbf{w}=\mathbf{1}),\\ w'_{i}&=\sum_{k} w_k\,\mu_{ik}\quad\text{(weight of prototype } \mathbf{v}_i\text{)},\\ (\mathbf{U}_l,\mathbf{V}_l)&=\text{wFCM}\big([\mathbf{X}_l;\mathbf{V}_{l-1}],\ [\mathbf{1};\mathbf{w}']\big)\ \text{initialized with } \mathbf{V}_{l-1},\quad l=2,\dots,s,\end{split}\]and the result is \(\mathbf{V}=\mathbf{V}_s\) with the extension \(\mathbf{U}=\text{extend}(\mathbf{X},\mathbf{V})\). The weights make a carried prototype count as the observations it represents, so the final prototypes approximate those of FCM on the complete data; the approximation is good when every chunk contains all clusters and deteriorates for tiny chunks.
The
forgetting_factor\(\lambda\) is an addition of this implementation for streams whose clusters drift: the carried weights become \(\lambda\,w'_i\). It is not part of the published algorithm, which corresponds to \(\lambda=1\).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, “Single pass fuzzy c means”, Proc. IEEE International Conference on Fuzzy Systems, 1–7, 2007.
Examples
>>> from fuzzyclust.large import SinglePassFCM, make_large_blobs >>> X, y = make_large_blobs(n_samples=5000, n_clusters=3, random_state=0) >>> sp = SinglePassFCM(n_clusters=3, chunk_size=1000, random_state=0).fit(X) >>> sp.n_chunks_, sp.chunk_centers_.shape, sp.membership_.shape (5, (5, 3, 2), (5000, 3))
- fit(X, y=None)[source]#
Process the data chunk by chunk and extend the final prototypes 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]#
Process one more chunk of a stream (the first call initializes the model).
The prototypes and their weights are carried over from the previous call;
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.SinglePassFCM#
Fuzzy c-means for very large data: rseFCM, spFCM and oFCM
Clustering a drifting stream chunk by chunk with partial_fit