{"id":55,"date":"2026-03-03T09:51:10","date_gmt":"2026-03-03T09:51:10","guid":{"rendered":"https:\/\/gigz.pk\/r\/?post_type=lesson&#038;p=55"},"modified":"2026-04-01T12:13:49","modified_gmt":"2026-04-01T12:13:49","slug":"clustering-and-unsupervised-learning","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/r\/lesson\/clustering-and-unsupervised-learning\/","title":{"rendered":"Clustering and Unsupervised Learning"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Clustering and unsupervised learning are used to explore patterns in data without predefined labels. These techniques help identify natural groupings, reduce dimensionality, and gain insights from unlabeled datasets.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>1. What is Unsupervised Learning?<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Unsupervised learning analyzes datasets without target labels. The goal is to discover hidden patterns or structures. Common techniques include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Clustering:<\/strong> Group similar observations together. Examples: K-Means, Hierarchical Clustering<\/li>\n\n\n\n<li><strong>Dimensionality Reduction:<\/strong> Reduce the number of variables while retaining important information. Examples: PCA (Principal Component Analysis)<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>2. K-Means Clustering<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">K-Means partitions data into K clusters based on similarity.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>a) Load Data<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"># Sample dataset<br>data &lt;- read.csv(\"customer_data.csv\")# Select numeric variables for clustering<br>customer_data &lt;- data[, c(\"Age\", \"PurchaseAmt\")]<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>b) Apply K-Means<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">set.seed(123)<br>kmeans_model &lt;- kmeans(customer_data, centers = 3)# View cluster assignments<br>kmeans_model$cluster<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>c) Add Cluster Labels to Data<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">customer_data$Cluster &lt;- factor(kmeans_model$cluster)<br>head(customer_data)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>d) Visualize Clusters<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">library(ggplot2)ggplot(customer_data, aes(x = Age, y = PurchaseAmt, color = Cluster)) +<br>  geom_point(size = 3) +<br>  ggtitle(\"K-Means Clustering of Customers\")<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>3. Hierarchical Clustering<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Hierarchical clustering builds a tree of clusters called a dendrogram.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Compute distance matrix<br>dist_matrix &lt;- dist(customer_data[, c(\"Age\", \"PurchaseAmt\")], method = \"euclidean\")# Perform hierarchical clustering<br>hc_model &lt;- hclust(dist_matrix, method = \"ward.D2\")# Plot dendrogram<br>plot(hc_model, main = \"Hierarchical Clustering Dendrogram\")<br>rect.hclust(hc_model, k = 3, border = \"red\")  # Highlight 3 clusters<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>4. Principal Component Analysis (PCA)<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">PCA reduces dimensionality while preserving variability in data.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Standardize data<br>customer_scaled &lt;- scale(customer_data[, c(\"Age\", \"PurchaseAmt\")])# Apply PCA<br>pca_model &lt;- prcomp(customer_scaled)<br>summary(pca_model)# Plot PCA<br>biplot(pca_model, main = \"PCA Biplot\")<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>5. Advantages of Clustering and Unsupervised Learning<\/strong><\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Identify natural groupings in data without labels<\/li>\n\n\n\n<li>Discover patterns for marketing, segmentation, and research<\/li>\n\n\n\n<li>Reduce dimensionality for visualization and modeling<\/li>\n\n\n\n<li>Inform decision-making through data-driven insights<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>6. Conclusion<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Clustering and unsupervised learning in R allow analysts to uncover hidden structures in data. Techniques like K-Means, hierarchical clustering, and PCA help segment customers, reduce complexity, and reveal insights that guide business strategy. Mastering these tools enables effective exploratory analysis and data-driven decision-making.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/r\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">R Programming (R Lang) > R for Data Science &#038; Machine Learning > Clustering and Unsupervised Learning<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1775045610742\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":32,"template":"","class_list":["post-55","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Clustering and Unsupervised Learning - Analyze Deep. Visualize Better. Build with R.<\/title>\n<meta name=\"description\" content=\"Learn how to perform K-means clustering and PCA in R for customer segmentation. Discover patterns, reduce dimensions, and visualize clusters.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Clustering and Unsupervised Learning - Analyze Deep. Visualize Better. Build with R.\" \/>\n<meta property=\"og:description\" content=\"Learn how to perform K-means clustering and PCA in R for customer segmentation. Discover patterns, reduce dimensions, and visualize clusters.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:site_name\" content=\"Analyze Deep. Visualize Better. Build with R.\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-01T12:13:49+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/r\\\/lesson\\\/clustering-and-unsupervised-learning\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"Clustering and Unsupervised Learning - Analyze Deep. Visualize Better. Build with R.\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/r\\\/#website\"},\"datePublished\":\"2026-03-03T09:51:10+00:00\",\"dateModified\":\"2026-04-01T12:13:49+00:00\",\"description\":\"Learn how to perform K-means clustering and PCA in R for customer segmentation. Discover patterns, reduce dimensions, and visualize clusters.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/r\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"R Programming (R Lang) > R for Data Science & Machine Learning > Clustering and Unsupervised Learning\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/r\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/r\\\/\",\"name\":\"Analyze Deep. Visualize Better. Build with R.\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/r\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Clustering and Unsupervised Learning - Analyze Deep. Visualize Better. Build with R.","description":"Learn how to perform K-means clustering and PCA in R for customer segmentation. Discover patterns, reduce dimensions, and visualize clusters.","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:\/\/gigz.pk\/","og_locale":"en_US","og_type":"article","og_title":"Clustering and Unsupervised Learning - Analyze Deep. Visualize Better. Build with R.","og_description":"Learn how to perform K-means clustering and PCA in R for customer segmentation. Discover patterns, reduce dimensions, and visualize clusters.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Analyze Deep. Visualize Better. Build with R.","article_modified_time":"2026-04-01T12:13:49+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/r\/lesson\/clustering-and-unsupervised-learning\/","url":"https:\/\/gigz.pk\/","name":"Clustering and Unsupervised Learning - Analyze Deep. Visualize Better. Build with R.","isPartOf":{"@id":"https:\/\/gigz.pk\/r\/#website"},"datePublished":"2026-03-03T09:51:10+00:00","dateModified":"2026-04-01T12:13:49+00:00","description":"Learn how to perform K-means clustering and PCA in R for customer segmentation. Discover patterns, reduce dimensions, and visualize clusters.","breadcrumb":{"@id":"https:\/\/gigz.pk\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/r\/"},{"@type":"ListItem","position":2,"name":"R Programming (R Lang) > R for Data Science & Machine Learning > Clustering and Unsupervised Learning"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/r\/#website","url":"https:\/\/gigz.pk\/r\/","name":"Analyze Deep. Visualize Better. Build with R.","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/r\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/r\/wp-json\/wp\/v2\/lesson\/55","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/r\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/r\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/r\/wp-json\/wp\/v2\/media?parent=55"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}