import hypertools as hyp
from collections import Counter

%matplotlib inline

"""
Explanation: Clustering with Hypertools
The cluster feature performs clustering analysis on the data (an arrray, dataframe, or list) and returns a list of cluster labels. 
The default clustering method is K-Means (argument 'KMeans') with MiniBatchKMeans, AgglomerativeClustering, Birch, FeatureAgglomeration, SpectralClustering and HDBSCAN also supported.
Note that, if a list is passed, the arrays will be stacked and clustering will be performed across all lists (not within each list).
Import Packages
End of explanation
"""


geo = hyp.load('mushrooms')
mushrooms = geo.get_data()

"""
Explanation: Load your data
We will load one of the sample datasets. This dataset consists of 8,124 samples of mushrooms with various text features.
End of explanation
"""


mushrooms.head()

"""
Explanation: We can peek at the first few rows of the dataframe using the pandas function head()
End of explanation
"""


labels = hyp.cluster(mushrooms)
set(labels)

"""
Explanation: Obtain cluster labels
To obtain cluster labels, simply pass the data to hyp.cluster.  Since we have not specified a desired number of cluster, the default of 3 clusters is used (labels 0, 1, and 2). Additionally, since we have note specified a desired clustering algorithm, K-Means is used by default.
End of explanation
"""


Counter(labels)

"""
Explanation: We can further examine the number of datapoints assigned each label.
End of explanation
"""


labels_10 = hyp.cluster(mushrooms, n_clusters = 10)
set(labels_10)

"""
Explanation: Specify number of cluster labels
You can also specify the number of desired clusters by setting the n_clusters argument to an integer number of clusters, as below. We can see that when we pass the int 10 to n_clusters, 10 cluster labels are assigned. 
Since we have note specified a desired clustering algorithm, K-Means is used by default.
End of explanation
"""


labels_HDBSCAN = hyp.cluster(mushrooms, cluster='HDBSCAN')

geo = hyp.plot(mushrooms, '.', hue=labels_10, title='K-means clustering')
geo = hyp.plot(mushrooms, '.', hue=labels_HDBSCAN, title='HCBSCAN clustering')

"""
Explanation: Different clustering models
You may prefer to use a clustering model other than K-Means. To do so, simply pass a string to the cluster argument specifying the desired clustering algorithm.
In this case, we specify both the clustering model (HDBSCAN) and the number of clusters (10).
End of explanation
"""


