pyclustering  0.10.1
pyclustring is a Python, C++ data mining library.
pyclustering.cluster.fcm.fcm Class Reference

Class represents Fuzzy C-means (FCM) clustering algorithm. More...

Public Member Functions

def __init__ (self, data, initial_centers, **kwargs)
 Initialize Fuzzy C-Means algorithm. More...
 
def process (self)
 Performs cluster analysis in line with Fuzzy C-Means algorithm. More...
 
def get_clusters (self)
 Returns allocated clusters that consists of points that most likely (in line with membership) belong to these clusters. More...
 
def get_centers (self)
 Returns list of centers of allocated clusters. More...
 
def get_membership (self)
 Returns cluster membership (probability) for each point in data. More...
 

Detailed Description

Class represents Fuzzy C-means (FCM) clustering algorithm.

Fuzzy clustering is a form of clustering in which each data point can belong to more than one cluster.

Fuzzy C-Means algorithm uses two general formulas for cluster analysis. The first is to updated membership of each point:

\[w_{ij}=\frac{1}{\sum_{k=0}^{c}\left ( \frac{\left \| x_{i}-c_{j} \right \|}{\left \| x_{i}-c_{k} \right \|} \right )^{\frac{2}{m-1}}}\]

The second formula is used to update centers in line with obtained centers:

\[c_{k}=\frac{\sum_{i=0}^{N}w_{k}\left ( x_{i} \right )^{m}x_{i}}{\sum_{i=0}^{N}w_{k}\left ( x_{i} \right )^{m}}\]

Fuzzy C-Means clustering results depend on initial centers. Algorithm K-Means++ can used for center initialization from module 'pyclustering.cluster.center_initializer'.

CCORE implementation of the algorithm uses thread pool to parallelize the clustering process.

Here is an example how to perform cluster analysis using Fuzzy C-Means algorithm:

from pyclustering.samples.definitions import FAMOUS_SAMPLES
from pyclustering.cluster import cluster_visualizer
from pyclustering.cluster.center_initializer import kmeans_plusplus_initializer
from pyclustering.cluster.fcm import fcm
from pyclustering.utils import read_sample
# load list of points for cluster analysis
sample = read_sample(FAMOUS_SAMPLES.SAMPLE_OLD_FAITHFUL)
# initialize
initial_centers = kmeans_plusplus_initializer(sample, 2, kmeans_plusplus_initializer.FARTHEST_CENTER_CANDIDATE).initialize()
# create instance of Fuzzy C-Means algorithm
fcm_instance = fcm(sample, initial_centers)
# run cluster analysis and obtain results
fcm_instance.process()
clusters = fcm_instance.get_clusters()
centers = fcm_instance.get_centers()
# visualize clustering results
visualizer = cluster_visualizer()
visualizer.append_clusters(clusters, sample)
visualizer.append_cluster(centers, marker='*', markersize=10)
visualizer.show()

The next example shows how to perform image segmentation using Fuzzy C-Means algorithm:

from pyclustering.cluster.center_initializer import kmeans_plusplus_initializer
from pyclustering.cluster.fcm import fcm
from pyclustering.utils import read_image, draw_image_mask_segments
# load list of points for cluster analysis
data = read_image("stpetersburg_admiral.jpg")
# initialize
initial_centers = kmeans_plusplus_initializer(data, 3, kmeans_plusplus_initializer.FARTHEST_CENTER_CANDIDATE).initialize()
# create instance of Fuzzy C-Means algorithm
fcm_instance = fcm(data, initial_centers)
# run cluster analysis and obtain results
fcm_instance.process()
clusters = fcm_instance.get_clusters()
# visualize segmentation results
draw_image_mask_segments("stpetersburg_admiral.jpg", clusters)
Image segmentation using Fuzzy C-Means algorithm.

Definition at line 20 of file fcm.py.

Constructor & Destructor Documentation

◆ __init__()

def pyclustering.cluster.fcm.fcm.__init__ (   self,
  data,
  initial_centers,
**  kwargs 
)

Initialize Fuzzy C-Means algorithm.

Parameters
[in]data(array_like): Input data that is presented as array of points (objects), each point should be represented by array_like data structure.
[in]initial_centers(array_like): Initial coordinates of centers of clusters that are represented by array_like data structure: [center1, center2, ...].
[in]**kwargsArbitrary keyword arguments (available arguments: 'tolerance', 'itermax', 'm').

Keyword Args:

  • ccore (bool): Defines should be CCORE library (C++ pyclustering library) used instead of Python code or not.
  • tolerance (float): Stop condition: if maximum value of change of centers of clusters is less than tolerance then algorithm stops processing.
  • itermax (uint): Maximum number of iterations that is used for clustering process (by default: 200).
  • m (float): Hyper-parameter that controls how fuzzy the cluster will be. The higher it is, the fuzzier the cluster will be in the end. This parameter should be greater than 1 (by default: 2).

Definition at line 93 of file fcm.py.

Member Function Documentation

◆ get_centers()

def pyclustering.cluster.fcm.fcm.get_centers (   self)

Returns list of centers of allocated clusters.

Returns
(array_like) Cluster centers.
See also
process()
get_clusters()
get_membership()

Definition at line 164 of file fcm.py.

◆ get_clusters()

def pyclustering.cluster.fcm.fcm.get_clusters (   self)

Returns allocated clusters that consists of points that most likely (in line with membership) belong to these clusters.

Remarks
Allocated clusters can be returned only after data processing (use method process()). Otherwise empty list is returned.
Returns
(list) List of allocated clusters, each cluster contains indexes from input data.
See also
process()
get_centers()
get_membership()

Definition at line 147 of file fcm.py.

Referenced by pyclustering.samples.answer_reader.get_cluster_lengths(), and pyclustering.cluster.optics.optics.process().

◆ get_membership()

def pyclustering.cluster.fcm.fcm.get_membership (   self)

Returns cluster membership (probability) for each point in data.

Returns
(array_like) Membership for each point in format [[Px1(c1), Px1(c2), ...], [Px2(c1), Px2(c2), ...], ...], where [Px1(c1), Px1(c2), ...] membership for point x1.
See also
process()
get_clusters()
get_centers()

Definition at line 178 of file fcm.py.

◆ process()

def pyclustering.cluster.fcm.fcm.process (   self)

Performs cluster analysis in line with Fuzzy C-Means algorithm.

Returns
(fcm) Returns itself (Fuzzy C-Means instance).
See also
get_clusters()
get_centers()
get_membership()

Definition at line 128 of file fcm.py.


The documentation for this class was generated from the following file:
pyclustering.cluster.center_initializer
Collection of center initializers for algorithm that uses initial centers, for example,...
Definition: center_initializer.py:1
pyclustering.utils.draw_image_mask_segments
def draw_image_mask_segments(source, clusters, hide_axes=True)
Shows image segments using black masks.
Definition: __init__.py:1054
pyclustering.utils.read_image
def read_image(filename)
Returns image as N-dimension (depends on the input image) matrix, where one element of list describes...
Definition: __init__.py:69
pyclustering.cluster
pyclustering module for cluster analysis.
Definition: __init__.py:1
pyclustering.cluster.fcm
Cluster analysis algorithm: Fuzzy C-Means.
Definition: fcm.py:1
pyclustering.utils
Utils that are used by modules of pyclustering.
Definition: __init__.py:1
pyclustering.utils.read_sample
def read_sample(filename)
Returns data sample from simple text file.
Definition: __init__.py:30