{"id":80082,"date":"2020-10-23T09:00:10","date_gmt":"2020-10-23T03:30:10","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=80082"},"modified":"2020-10-23T09:00:10","modified_gmt":"2020-10-23T03:30:10","slug":"clustering-in-machine-learning","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/clustering-in-machine-learning\/","title":{"rendered":"Clustering in Machine Learning"},"content":{"rendered":"<p>In this article, we will study about an unsupervised learning-based technique known as <strong>clustering in machine learning.<\/strong>Here, we will discuss what clustering really is, What are its types? And we will look at the algorithms involved in the clustering technique.<\/p>\n<h3>Clustering in Machine Learning<\/h3>\n<p>Let\u2019s try to understand what clustering exactly is. Examples make the job a lot more easier.<\/p>\n<p>So, as we know, there are two types of learning: <strong>active<\/strong> and <strong>passive<\/strong>.<\/p>\n<p>Passive means that the model follows a certain pre-written path and is also done under supervision. This doesn\u2019t mean something like we will take unsupervised learning on the active side. It is just that the human intervention in unsupervised learning is quite minimal as compared to supervised learning.<\/p>\n<p>Also, we have unlabeled data in unsupervised learning. So, here, the algorithm has to completely analyze the data, find patterns, and cluster the data depicting similar features.<\/p>\n<p>For example, if we provide a dataset consisting of images of two different objects. The model will scan the images for certain features. If some images have matching features, it will form a cluster.<\/p>\n<p><em><strong>Note:-<\/strong><\/em>Active learning is a different concept. It\u2019s applicable for semi-supervised and reinforcement learning techniques.<\/p>\n<h4>Examples of Clustering in Machine Learning<\/h4>\n<p>A real-life example would be: -Trying to solve a hard problem in chess. The possibilities to checkmate the king are endless. There is no predefined or pre-set solution in chess. You have to analyze the positions, your pieces, the opponent\u2019s pieces and find a solution.<\/p>\n<p>These traits to find the solution are the dataset, you are the model that has to analyze and find the answer. This is what <strong>unsupervised<\/strong> <strong>learning<\/strong> is.<\/p>\n<p>Now let\u2019s understand clustering. In clustering, we classify data points into clusters based on similar features rather than labels. The labelling part in clustering comes at the end when clustering is over.<\/p>\n<p>Also, we should add a lot of data to the dataset, to increase the accuracy of the results. The algorithm will learn various patterns in the dataset.<\/p>\n<p>Like, it will look for certain traits and features and compare the <strong>similarities<\/strong> between the data.<\/p>\n<p>There is a difference here between classification and clustering. The labeling part involves a lot of human intervention that proves both costly and time-consuming. Although labelling gets fairly simple in unsupervised learning, the model would have to process more due to more analysis.<\/p>\n<p>Let\u2019s take an example. If we have a dataset consisting of images of tigers, zebras, leopards, cheetahs.<\/p>\n<p>The clustering algorithm would analyze this dataset and then divide the data based on some specific characteristics. The characteristics would include fur color, patterns (spots, stripes), face shape, etc.<\/p>\n<p>The model would remember the pattern in which it classified the data. This knowledge will come in handy for future unknown data.<\/p>\n<p>We also have other applications of clustering like fake-news detection, fraud detection, spam mail segregation, etc. Now, let\u2019s dive a bit deeper into clustering now that we have seen and understood it.<\/p>\n<p>There are various types of clustering that we should know about. These will help us to further classify and understand the various algorithms that unsupervised learning has.<\/p>\n<p>These include:<\/p>\n<ul>\n<li>Centroid-based clustering<\/li>\n<li>Density-based clustering<\/li>\n<li>Distribution-based clustering<\/li>\n<li>Hierarchical clustering<\/li>\n<li>Grid clustering<\/li>\n<\/ul>\n<p>Now let\u2019s understand these one-by-one.<\/p>\n<h3>Types of Clustering in Machine Learning<\/h3>\n<h4>1. Centroid-Based Clustering in Machine Learning<\/h4>\n<p>In centroid-based clustering, we form clusters around several points that act as the centroids. The k-means clustering algorithm is the perfect example of the Centroid-based clustering method. Here, we form k number of clusters that have k number of centroids.<\/p>\n<p>The algorithm assigns the datapoints to certain cluster centres (centroids) based on their proximity to certain centroids.<\/p>\n<p>The algorithm measures the <strong>Euclidean<\/strong> distances between the datapoints and all k centroids. The one that is the nearest will get clustered with that particular centroid.<\/p>\n<p>Also, k-means is the most widely used centroid-based clustering algorithm. The primary aim of the algorithm is to simplify an N-dimensional dataset into smaller K clusters.<\/p>\n<p><strong>K-means Clustering in Machine Learning<\/strong><\/p>\n<p>Let\u2019s try to understand more about k-means clustering. It is an iterative clustering type of algorithm. This means that it compares each datapoint\u2019s proximity with the centroids one-by-one in an <strong>iterative<\/strong> fashion. The premise of this algorithm is that it has to find the maximum (local maxima) or the best possible value for each iteration.<\/p>\n<p>Let\u2019s understand the working of the algorithm with the help of some images:<\/p>\n<p>Let\u2019s say we have two different types of datapoints. Red colour and blue colour.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/ml-image9.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-80096\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/ml-image9.jpg\" alt=\"Machine learning\" width=\"750\" height=\"628\" \/><\/a><\/p>\n<p>Now, for the next step, let\u2019s assign the value of k. Let\u2019s say k=2 as we have two types of data. K=2 means we will have two centroids far from each other<\/p>\n<p><em><strong>Note:-<\/strong><\/em> The centroids should always remain distant from each other to avoid any confusion and error.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/ml-image10.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-80097\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/ml-image10.jpg\" alt=\"centroids in Machine learning\" width=\"700\" height=\"580\" \/><\/a><\/p>\n<p>The two-colored crosses are the centroids.<\/p>\n<p>Now the algorithm will compare the distance of each point with the centroids. The points will join the cluster whose centroid is nearer to them. This algorithm makes nice use of the distance metrics for this purpose. The points are now assigned to their respective clusters.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/ml-image11.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-80098\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/ml-image11.jpg\" alt=\"cluster in machine learning\" width=\"700\" height=\"580\" \/><\/a><\/p>\n<p>After this, again centroids are assigned for the clusters and again they re-adjust themselves.<\/p>\n<p>The last two steps will keep on continuing until the algorithm achieves maximum accuracy (maxima) or you can say, until the clusters stop moving and everything becomes stable.<\/p>\n<p>So, let\u2019s see the coding part of it:<\/p>\n<p><strong>Step 1:<\/strong> First, we will import all the necessary libraries. We have the basic import libraries:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">from sklearn.datasets import make_classification\nfrom sklearn.cluster import KMeans\nfrom matplotlib import pyplot\nfrom numpy import unique\nfrom numpy import where\n<\/pre>\n<p>Here, make_classification is for the dataset. <strong>KMeans<\/strong> is to import the model for the KMeans algorithm.<\/p>\n<p>If there are any special or unique elements in the <strong>NumPy<\/strong> array, we can extract them with the help of the unique function provided by the unique library. Also, in the case of the where function, you can relate it with the conditional operator in mathematics.<\/p>\n<p>You have to give a condition to the where function in order to get the output according to it. Remember that these two functions belong to the Numpy group.<\/p>\n<p>Here, we have used <strong>sklearn.datasets<\/strong> in order to include more parameters related to the datasets.<\/p>\n<p><strong>Step-2:<\/strong> Let\u2019s try to define the dataset. We will use the <strong>make_classification<\/strong> function to define our dataset and to include certain parameters with it.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">A, _ = make_classification(n_samples=1000, n_features=4, n_informative=2, n_redundant=0,  n_repeated=0, n_clusters_per_class=1, random_state=4)\n<\/pre>\n<p>Remember that, whatever parameter you choose to have, the output would look different for each case.<\/p>\n<p>Also, these parameters have specific meanings.<\/p>\n<p><strong>n_samples<\/strong> is the number of samples.<\/p>\n<p><strong>n_features<\/strong> is the number of features that you would want to have in the dataset.<\/p>\n<p>Now, these next three are a bit important to know. As they are responsible for how your data would look.<\/p>\n<p><strong>n_informative<\/strong> tells the number of important and informative features of your data.<\/p>\n<p><strong>n_redundant<\/strong> gives you random combinations of informative features.<\/p>\n<p><strong>n_repeated<\/strong> helps to draw out the duplicate features from the above two features.<\/p>\n<p>The <strong>Numpy<\/strong> array stores the features in the same order as mentioned and <strong>Random_state<\/strong> helps in giving random numbers in the same order.<\/p>\n<p><strong>Step-3:<\/strong> Now, we will bring in our model. In the parameter of our model or the function name (KMeans), we will give the number of clusters we need:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">model = KMeans(n_clusters=3)<\/pre>\n<p><strong>Step-4:<\/strong> Now, here we run the model. We try to fit in the data that we have defined above.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">model.fit(A)\n<\/pre>\n<p>After execution, it will also show a certain output message (depends on the framework that you use)<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/image1-15.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-80099\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/image1-15.png\" alt=\"\" width=\"586\" height=\"59\" \/><\/a><\/p>\n<p><strong>Step-5:<\/strong> The function predict() will help to analyze and predict the dataset to make clusters.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">B = model.predict(A)<\/pre>\n<p><strong>Step-6:<\/strong> Here the unique function comes into play. This will help to segregate certain features for the cluster in a NumPy array.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">clust = unique(B)<\/pre>\n<p><strong>Step-7:<\/strong> Now, we will run the entire system in a loop so as to keep taking the next set of data for creating the clusters. Here, the clust variable already has the segregated data for the cluster.<\/p>\n<p>We will use it in the where function and create a condition.<\/p>\n<p>In the end, we will see a graph plotted as a result (because pyplot and matplotlib come into play here).<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">for cluster in clust:\n    C = where(B == cluster)\n    pyplot.scatter(A[C, 0], A[C, 1])\npyplot.show()\n<\/pre>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/image2-1-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-80100\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/image2-1-1.png\" alt=\"Machine learning Clustering\" width=\"368\" height=\"249\" \/><\/a><\/p>\n<h4>2. Density-Based Clustering in Machine Learning<\/h4>\n<p>In this type of clustering, the clustering doesn\u2019t happen around centroid or central points, but the cluster forms where the density looks higher. Also, the advantage is that unlike centroid-based clustering it doesn\u2019t include all the points.<\/p>\n<p>Including all the points can create unnecessary noise in the data and that is where density-based clustering has an edge over centroid-based clustering.<\/p>\n<p>The sparse\/noise datapoints are included in order to define the border between clusters. These points are always outside the main cluster.<\/p>\n<p>Also, one negative thing is that in density-based clustering, the cluster borders are defined by decreasing density. So, the borders might be of varied shapes that make it difficult to draw a perfect border for clusters. However, there are many methods out there to improve these kinds of problems, but we look at them some other time.<\/p>\n<p>We have three main algorithms in Density-based clustering &#8211; <strong>DBSCAN<\/strong>, <strong>HDBSCAN<\/strong>, and <strong>OPTICS<\/strong>.<\/p>\n<p>DBSCAN uses a fixed distance for separating the dense clusters from the noise datapoints. It is the fastest among clustering algorithms.<\/p>\n<p>HDBSCAN uses a range of distances to separate itself from the noise. It requires the least amount of user-input.<\/p>\n<p>OPTICS measures the distance between neighboring features and it draws a reachability plot to separate itself from the noise datapoints.<\/p>\n<p>Now, we will use the same code but some different functions to understand density-based clustering. So, we will only mention the different functions here as the rest is the same.<\/p>\n<p>So, let\u2019s have a look at DBSCAN: We just need to import DBSCAN:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">from sklearn.cluster import DBSCAN<\/pre>\n<p>This will import the DBSCAN model into your program. Now to define your model and to provide it with the arguments:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">model = DBSCAN(eps=0.20, min_samples=5)<\/pre>\n<p>Here, eps is the max. distance between two data points. This is the prime parameter for the DBSCAN as the values we enter in eps would change the plot every time.<\/p>\n<p>The reason is that cluster density would look different for different values.<\/p>\n<p>Also, <strong>min_samples<\/strong> helps to set the minimum number of samples we want within a neighborhood collection of features. So, the output for the given value set would be:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/image3-1-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-80101\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/image3-1-1.png\" alt=\"density based clustering\" width=\"367\" height=\"251\" \/><\/a><\/p>\n<h4>3. Distribution-Based Clustering in Machine Learning<\/h4>\n<p>In distribution-based clustering, if the distance between the point and the central distribution of points increases, then the probability of the point being included in the distribution decreases.<\/p>\n<p>For problems like these, we use the Gaussian distribution model. The model works like, there will be a fixed number of distributors for the Gaussian model. These distributors are concentric figures with decreasing color intensity from inside to the outside.<\/p>\n<p>The central part tends to be denser and it tends to decrease as we go outside as we have bigger distributors now. So, even though distributors might contain the same number of points, their densities may still differ due to the size of distributors. Overfitting can be a bit of a problem for this type of clustering.<\/p>\n<p>As long as we don\u2019t set any strong criteria for points, we can avoid overfitting.<\/p>\n<p>The Gaussian mixture model is generally used with the expectation-maximization model. An important point to remember that we cannot use this technique for density-based clustering as it would not properly fit among the distributions.<\/p>\n<p>Only expectation-maximization algorithms can work well with Gaussian mixture models.<\/p>\n<p>So let\u2019s see how the Gaussian distribution looks in general:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/ml-image2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-80102\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/ml-image2.jpg\" alt=\"Gaussian Distribution\" width=\"800\" height=\"570\" \/><\/a><\/p>\n<p>The concentric figures may not be this perfect, but it\u2019s a similar depiction of what the real thing looks like.<\/p>\n<p>Now, let\u2019s understand the code involved in this:<\/p>\n<p>The code has numerous similarities just like the previous case. So, we will only mention the main components involved as the rest gets pretty much the same. These are all easier examples, just to understand how the clustering works.<\/p>\n<p>The coding has an enormous depth with numerous parameters for the functions that we use. But, here we restrict to very few functions to make the explanation simpler.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">From sklearn.cluster import GaussianMixture<\/pre>\n<p>Firstly, we will import the model.<\/p>\n<p><em><strong>Note:<\/strong><\/em> All the models and algorithms that are a part of <strong>sklearn<\/strong>, we can import them directly from sklearn and just use their function to define the models.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">model = GaussianMixture(n_components=4)\n<\/pre>\n<p>Here, <strong>n_components<\/strong> is the number of mixture components ( the number of different colored components that we will use in the plot).<\/p>\n<p>So, the result would look like:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/image4-1-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-80103\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/image4-1-1.png\" alt=\"Distribution Based clustering\" width=\"384\" height=\"258\" \/><\/a><\/p>\n<h4>4. Hierarchical Clustering in Machine Learning<\/h4>\n<p>Well, in hierarchical clustering we deal with either merging of clusters or division of a big cluster.<\/p>\n<p>So, we should know that hierarchical clustering has two types: Agglomerative hierarchical clustering and divisive hierarchical clustering.<\/p>\n<p>In agglomerative clustering, we tend to merge smaller clusters into bigger clusters. But this also has some process. If the two clusters that we compare have similarities between them and if they are near to each other, then we merge them.<\/p>\n<p>In the case of divisive hierarchical clustering, we divide one big cluster into n-smaller clusters. The clusters here are divided if some datapoints are not similar to the larger cluster; we separate them and make an individual cluster for them.<\/p>\n<p>For solving the hierarchical clustering problem, we use the proximity (distance) matrix in which we store the distances of each individual cluster among each other.<\/p>\n<p>For every merge or divide, the matrix would change because at every next step we get a new cluster.<\/p>\n<p>Like, if we have 5 clusters, we merge two of them, then the matrix will then have 4 clusters and the distances stored in the matrix will eventually change (Same case for divisive hierarchical clustering as well).<\/p>\n<p>Also, for visualizing hierarchical clustering, we use a dendrogram, which is a type of graph. In hierarchical clustering, the most important job is to calculate the similarity between clusters.<\/p>\n<p>It\u2019s the most important part as it helps us to understand whether to merge or divide the cluster. We have several methods like:<\/p>\n<p><strong>Single Linkage Algorithm (MIN)<\/strong><\/p>\n<ul>\n<li>In this, we take two points from two individual clusters and these points have to be the closest to each.<\/li>\n<li>We then calculate the distance and similarity between them.<\/li>\n<li>It works well for separating non-elliptical clusters.<\/li>\n<li>Noise in the data can create problems.<\/li>\n<\/ul>\n<p><strong>Complete Linkage Algorithm (MAX)<\/strong><\/p>\n<ul>\n<li>This is the exact opposite of the MIN algorithm.<\/li>\n<li>We take the distance between the two farthest points in two clusters and measure the distance and similarities.<\/li>\n<li>It works well even in the presence of noise.<\/li>\n<li>However, it can accidentally break bigger clusters as well.<\/li>\n<\/ul>\n<p><strong>Group Average<\/strong><\/p>\n<ul>\n<li>Here, we take all the points and then measure them.<\/li>\n<li>It can accurately separate clusters even if there remains noise in between clusters.<\/li>\n<\/ul>\n<p><strong>Distance Between the Centroids<\/strong><\/p>\n<ul>\n<li>We can measure the distance between the centroids of both the clusters.<\/li>\n<li>Although it\u2019s not used that much as you have to find the centroid after every merge or divide.<\/li>\n<\/ul>\n<p><strong>Ward\u2019s Method<\/strong><\/p>\n<ul>\n<li>This one is the last method to calculate the similarity.<\/li>\n<li>Here, instead of taking the average, we take the sum of squares of distances.<\/li>\n<\/ul>\n<p><em><strong>Note:-<\/strong><\/em> The complexity of space in hierarchical clustering is O(n2). We require lots and lots of space to hold the matrix. For time complexity it is O(n3). The iterations we perform can get complex if there is a large dataset.<\/p>\n<p>We have some visual representations of how the clustering happens in hierarchical clustering:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/ml-image8.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-80107\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/ml-image8.jpg\" alt=\"clustering in ML\" width=\"700\" height=\"628\" \/><\/a><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/ml-image6.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-80108\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/ml-image6.jpg\" alt=\"ML clustering\" width=\"700\" height=\"628\" \/><\/a><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/ml-image4.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-80109\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/ml-image4.jpg\" alt=\"ML clustering\" width=\"750\" height=\"628\" \/><\/a><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/ml-image7.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-80110\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/ml-image7.jpg\" alt=\"clustering in ML\" width=\"700\" height=\"628\" \/><\/a><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/ml-image3.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-80106\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/ml-image3.jpg\" alt=\"Hierarchical clustering \" width=\"800\" height=\"628\" \/><\/a><\/p>\n<p>There is also the proximity matrix and the dendrogram:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/ml-image5.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-80105\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/ml-image5.jpg\" alt=\"Proximity matrix in Machine learning\" width=\"700\" height=\"628\" \/><\/a><\/p>\n<p>This is the proximity matrix. Now the dendrogram:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/ml-image1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-80104\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/ml-image1.jpg\" alt=\"dendogram in Machine learning\" width=\"800\" height=\"628\" \/><\/a><\/p>\n<h4>5. Grid Clustering in Machine Learning<\/h4>\n<p>This technique is very useful for multidimensional spaces. Here, we can divide the entire data space into a finite number of small cells. This helps a lot in reducing the complexity of the problem and this is what separates grid clustering from all conventional clustering.<\/p>\n<p>We can recognize denser areas as the places that have clusters. When we divide the plane into cells, we can then calculate the density of each cell and then sort them.<\/p>\n<p>At first, we will select one cell and calculate its density. If it is more than the normal density it will become a cluster. We will apply the same process for the cell\u2019s neighbors until there are no neighbors left. All the neighboring cells would have grouped into a cluster. The process will continue until all the cells are traversed.<\/p>\n<p>Here, we focus on the cells rather than the data. This helps in reducing complexity. The algorithms that fall under the grid-based clustering are the <strong>STING<\/strong> and <strong>CLIQUE<\/strong> algorithms.<\/p>\n<h3>Applications of Clustering in Machine Learning<\/h3>\n<p>So, finally, let\u2019s have a look at the specific areas where this concept is applied. Here are the top applications of the clustering concept:<\/p>\n<ul>\n<li>It\u2019s useful in various image recognition platforms and also various image segregation tasks. One such example is the biological field.<\/li>\n<li>Here, this can help the researchers to categorize and classify certain unknown species.<\/li>\n<li>Clustering can come in handy for the city\u2019s crime branch for classifying different parts of a city based on the crime-rate.<\/li>\n<li>The algorithm can classify and tell based on the frequency and number of cases that, which part has more criminal activities.<\/li>\n<li>It\u2019s quite useful in data-mining as well because clustering can help in understanding the data.<\/li>\n<li>It can be useful for banks as a fraud detection algorithm. The bank sector faces a problem of credit card scams and frauds.<\/li>\n<li>Using clustering we can uncover several hidden patterns that may tell us which scheme is a fraudulent scheme.<\/li>\n<li>We can understand various seismic zones based on the data and the clustering algorithm can create clusters of various seismic activities and active regions.<\/li>\n<li>In the health industry as well, we can use this algorithm to detect various ailments, especially tumors and cancers.<\/li>\n<li>The search engine is a prime example of the clustering technique.<\/li>\n<li>It categorizes search results on the basis of billions of searches and has the results ready at the go every time for every search.<\/li>\n<li>It can be of great use in analyzing data of soil for agriculture.<\/li>\n<li>With the data of the soil it can classify whether the soil has the necessary nutrient content for growing crops.<\/li>\n<li>It is also useful in the case of advertising the product based on customer interest and feedback.<\/li>\n<\/ul>\n<h3>Conclusion<\/h3>\n<p>So, for this article, we have studied all the necessary aspects of clustering in machine learning that especially a student or someone who wants to pursue ML should know about.<\/p>\n<p>We covered everything from what clustering is to what are its types. We even have a few coding examples, more importantly, what function and library we should use in ML is more important than to understand the whole code. You can understand the whole code if you know python language.<\/p>\n<p>We even saw the algorithms that fall under various categories. Also, several diagrams have been used for better understanding.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will study about an unsupervised learning-based technique known as clustering in machine learning.Here, we will discuss what clustering really is, What are its types? And we will look at the&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":80095,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[210],"tags":[3324,3325,3326,3327,3328,3329],"class_list":["post-80082","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-machine-learning","tag-centroid-based-clustering","tag-clustering-in-machine-learning","tag-density-based-clustering","tag-distribution-based-clustering","tag-grid-clustering","tag-hierarchical-clustering"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Clustering in Machine Learning - TechVidvan<\/title>\n<meta name=\"description\" content=\"Clustering is a Machine Learning technique that involves the grouping of data points. Learn about Clustering in machine learning with types .\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/clustering-in-machine-learning\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Clustering in Machine Learning - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Clustering is a Machine Learning technique that involves the grouping of data points. Learn about Clustering in machine learning with types .\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/clustering-in-machine-learning\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-10-23T03:30:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/10\/Clustering-in-Machine-Learning.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"18 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Clustering in Machine Learning - TechVidvan","description":"Clustering is a Machine Learning technique that involves the grouping of data points. Learn about Clustering in machine learning with types .","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/clustering-in-machine-learning\/","og_locale":"en_US","og_type":"article","og_title":"Clustering in Machine Learning - TechVidvan","og_description":"Clustering is a Machine Learning technique that involves the grouping of data points. Learn about Clustering in machine learning with types .","og_url":"https:\/\/techvidvan.com\/tutorials\/clustering-in-machine-learning\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-10-23T03:30:10+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/10\/Clustering-in-Machine-Learning.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"18 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/clustering-in-machine-learning\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/clustering-in-machine-learning\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Clustering in Machine Learning","datePublished":"2020-10-23T03:30:10+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/clustering-in-machine-learning\/"},"wordCount":3148,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/clustering-in-machine-learning\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/10\/Clustering-in-Machine-Learning.jpg","keywords":["Centroid-based clustering","Clustering in Machine Learning","Density-based clustering","Distribution-based clustering","Grid clustering","Hierarchical clustering"],"articleSection":["Machine Learning Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/clustering-in-machine-learning\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/clustering-in-machine-learning\/","url":"https:\/\/techvidvan.com\/tutorials\/clustering-in-machine-learning\/","name":"Clustering in Machine Learning - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/clustering-in-machine-learning\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/clustering-in-machine-learning\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/10\/Clustering-in-Machine-Learning.jpg","datePublished":"2020-10-23T03:30:10+00:00","description":"Clustering is a Machine Learning technique that involves the grouping of data points. Learn about Clustering in machine learning with types .","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/clustering-in-machine-learning\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/clustering-in-machine-learning\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/clustering-in-machine-learning\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/10\/Clustering-in-Machine-Learning.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/10\/Clustering-in-Machine-Learning.jpg","width":1200,"height":628,"caption":"Clustering in Machine Learning"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/clustering-in-machine-learning\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Clustering in Machine Learning"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/80082","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=80082"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/80082\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/80095"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=80082"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=80082"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=80082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}