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: BaseFuzzyClusterer

Single-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)\) and partial_fit() processes a stream chunk by chunk (Hore, Hall and Goldgof 2007; Havens et al. 2012).

Parameters:
n_clustersint, default=3

Number of clusters \(c\).

chunk_sizeint, default=10000

Samples per chunk \(n_l\) (fit splits the data into \(s=\lceil N/n_l\rceil\) chunks).

mfloat, default=2.0

Fuzziness exponent \(m>1\).

tolfloat, default=1e-4
max_iterint, default=300

Termination of the (weighted) FCM run on every chunk.

init{“random”, “kmeans++”} or ndarray, default=”random”

Initialization of the first chunk; later chunks start from the carried-over prototypes.

n_initint, 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). Set False to keep the order of the data, e.g. to simulate a stream; partial_fit never reorders.

forgetting_factorfloat, 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.0 is 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.0 clusters every chunk on its own, started from the previous prototypes.

random_stateint, RandomState instance or None, default=None
verboseint, default=0
Attributes:
cluster_centers_ndarray of shape (n_clusters, n_features)

Prototypes after the last chunk.

center_weights_ndarray of shape (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_ndarray of shape (n_chunks, n_clusters, n_features)

Prototypes after every chunk (the chunk timeline).

chunk_weights_ndarray of shape (n_chunks, n_clusters)
chunk_objective_ndarray of shape (n_chunks,)

Final \(J_w\) of every chunk run.

chunk_n_iter_ndarray of shape (n_chunks,)
n_chunks_int
membership_ndarray of shape (n_samples, n_clusters)

Extended partition of the data passed to fit (after partial_fit: of the last chunk).

labels_ndarray of shape (n_samples,)
distances_ndarray of shape (n_samples, n_clusters)
objective_float

\(J\) of the extended partition.

objective_history_ndarray of shape (n_chunks,)

Same as chunk_objective_.

n_iter_int

Total number of (weighted) FCM iterations over all chunks.

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 of shape (n_samples, n_features)
yIgnored
Returns:
selfobject
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_ and labels_ refer to the chunk just processed. Use predict_proba() for other data.

Parameters:
Xarray_like of shape (n_chunk, n_features)
yIgnored
Returns:
selfobject

Examples using fuzzyclust.large.SinglePassFCM#

Fuzzy c-means for very large data: rseFCM, spFCM and oFCM

Fuzzy c-means for very large data: rseFCM, spFCM and oFCM

Clustering a drifting stream chunk by chunk with partial_fit

Clustering a drifting stream chunk by chunk with partial_fit