# what is criterion in flcuster of scipy package?

Could some one explain what does criterion of fcluster indicate? I tried to read the documentation but I am unable to understand. What does maxclust criterion indicate?

Welcome to the community!

You may want to refer to a tutorial on Agglomerative Hierarchical Clustering before reading this answer. My explanation is more practical.

Assume the data below:

from scipy.cluster.hierarchy import ward, fcluster
from scipy.spatial.distance import pdist
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.text import TextPath

X = [[0, 0], [0, 1], [1, 0],
[0, 4], [0, 3], [1, 4],
[4, 0], [3, 0], [4, 1],
[4, 4], [3, 4], [4, 3]]
X = np.array(X)
Z = ward(pdist(X))

plt.plot(X[:,0],X[:,1],'o')


We have 12 points in 2 dimensions. This is how they are distributed: Now let's see how maxclust affects the clustering. Please note that I named clusters by numbers and plot the name of cluster each data belongs to, instead of its point.

for tt in [1, 3, 5, 9]:
plt.figure(figsize=(8,8))
plt.title('t={}'.format(str(tt)))
plt.xlim((-1,5))
plt.ylim((-1,5))
memberships = fcluster(Z, t=tt, criterion='maxclust')
for ii in range(len(memberships)):
path = TextPath((X[ii,0],X[ii,1]), str(memberships[ii]))
plt.plot(X[ii,0],X[ii,1],marker=path,c='b',markersize=50)

plt.show()


Results are shown below. I explain each:

t is the parameter which limits number of clusters i.e. you can have maximum t clusters in output. If t=1, any other criteria will be ignored as all data points have to be in the same cluster. So far so good. Let's say t=3. Now the fun starts. maxclust finds an optimal distance between each pair of points which are going to be in the same cluster. If t=3, without that distance, you get unbalanced clusters. In the figure below we have 4 obvious clusters and if I want 3 clusters out, two of them will merge and cause a cluster which is "wrong" (let's say wrong for simplicity). The maxclust distance threshold, prevents the algorithm to do so as it rejects far points being in the same cluster. So you see the data is clustered to 2 clusters instead of 3 which makes perfect sense. Please note that, yes, we have bigger clusters. But they are "right". It is like we zoomed out and see the clusters from a higher perspective. Let's go with t=5. Now the algorithm is allowed to find 4 clusters. Topology tells you that finding "right clusters" i.e. clusters in which each pair of points have a relatively small distance (in compare with distances to other clusters), is possible. This means that maxclust can find an optimal distance threshold and t lets us have 4 clusters. So it works! The last but not least is t=9. No we can have clusters more than the actual number needed (which was 4). Now maxclust tries to find its minimum distance between pairs so that points in same clusters are closer to each other than to other points in other clusters. As you see in each real cluster, The distance between center points to other two is smaller than the distance between those two diagonal points (as this distance is identical, clustering algorithms choose members randomly). So you see that each real cluster is divided to two, due to the effect of combination of maxclust and t Hope it helped. Good Luck!