![]() |
To predict the cluster of new data after training a clustering model in R, you generally need to use the centroids (for k-means) or the hierarchical structure (for hierarchical clustering) obtained from the trained model. Here are steps and examples for predicting new data clusters using k-means and hierarchical clustering. What is Clustering?Clustering is a method of partitioning a set of data points into subsets (clusters), such that points in the same cluster are more similar to each other than to those in other clusters. It’s widely used in various fields, such as customer segmentation, image recognition, and biological data analysis. Types of ClusteringHere we discuss the main Types of Clustering in R Programming Language.
Predicting New Data Clusters with K-means ClusteringAfter fitting a k-means model to your training data, you can predict the cluster of new data points by finding the nearest centroid. Here’s how to do it in R. Step 1: Train the K-means ModelFirst we will create and train the K-means Model.
Output: x y Step 2: Predict Clusters for New DataNow we will Predict Clusters for New Data for our model.
Output: [1] 3 1 2 1 2 2 3 1 2 1 After fitting the k-means model, use the centroids to assign new data points to the nearest centroid. The predict_kmeans function calculates the Euclidean distance from each new data point to each centroid and assigns the point to the nearest centroid. Predicting New Data Clusters with Hierarchical ClusteringHierarchical clustering does not naturally extend to new data, but you can use a workaround by training a classification model based on the hierarchical clustering results. Step 1: Train the Hierarchical Clustering ModelNow we will Train the Hierarchical Clustering Model.
Output: ![]() Predict new data’s cluster after clustering training data Step 2: Cut the Dendrogram to Form ClustersNow we will Cut the Dendrogram to Form Clusters.
Output: [1] 1 1 2 1 2 2 3 1 1 1 2 2 2 3 1 2 2 1 2 1 1 3 1 1 1 1 2 3 1 2 2 1 2 2 2 2 2 1 1 1 1 1 Step 3: Train a Classification ModelIn this step we will Train a Classification Model.
Step 4: Predict Clusters for New DataNow at last we will Predict Clusters for New Data.
Output: [1] 2 1 1 1 1 1 1 1 1 2
ConclusionPredicting the cluster of new data points is crucial in practical applications of clustering. Using k-means clustering, you can directly use the centroids for prediction. For hierarchical clustering, you can leverage a classification model to extend the clustering to new data points. This process allows for the continuous application of clustering models in dynamic environments, enhancing their utility in real-world scenarios. |
Reffered: https://www.geeksforgeeks.org
AI ML DS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |