{"id":12,"date":"2018-11-24T23:53:00","date_gmt":"2018-11-24T22:53:00","guid":{"rendered":""},"modified":"2020-08-22T08:46:56","modified_gmt":"2020-08-22T06:46:56","slug":"what-is-principal-component-analysis-pca-a-simple-tutorial","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/what-is-principal-component-analysis-pca-a-simple-tutorial\/","title":{"rendered":"What is Principal Component Analysis (PCA) &#8211; A Simple Tutorial"},"content":{"rendered":"<p>In this simple tutorial, I would explain the concept of Principal Components Analysis (PCA) in Machine Learning. I would try to be as simple and clear as possible.<br \/>\nThe we would use Python in Tutorial 2 to actually do some of the hands-on, performing principal components analysis.<\/p>\n<div style=\"clear: both; text-align: center;\"><a style=\"margin-left: 1em; margin-right: 1em;\" href=\"https:\/\/kindsonthegenius.com\/blog\/wp-content\/uploads\/2018\/11\/Principal-2BComponents-2BAnalysis-2BTutorial.jpg\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/kindsonthegenius.com\/blog\/wp-content\/uploads\/2018\/11\/Principal-2BComponents-2BAnalysis-2BTutorial-300x127.jpg\" width=\"400\" height=\"168\" border=\"0\" data-original-height=\"504\" data-original-width=\"1194\" \/><\/a><\/div>\n<p><b>What is Principal Components Analysis?<\/b><br \/>\nPrincipal Components Analysis is an unsupervised learning class of statistical techniques used to explain data in high dimension using smaller number of variables called the principal components.<br \/>\nIn PCA, we compute the principal component and used the to explain the data.<br \/>\n<ins style=\"display: block;\" data-ad-format=\"fluid\" data-ad-layout-key=\"-hk-o+30-4t+2k\" data-ad-client=\"ca-pub-7041870931346451\" data-ad-slot=\"6887528786\"><\/ins><br \/>\n<b>How PCA Work?<\/b><br \/>\nAssuming we have a set X made up of n measurements each represented by a set of <i><b>p<\/b><\/i> features, X<sub>1<\/sub>, X<sub>2<\/sub>, \u2026 , X<sub>p<\/sub>. If we want to plot this data in a 2-dimensional plane, we can plot n measurements using two features at a time. If the number of features are more than 3 or four then plotting this in two dimension will be a challenge as the number of plots would be p(p-1)\/2 which would be hard to plot.<br \/>\nWe would like to visualize this data in two dimension without losing information contained in the data. This is what PCA allows us to do.<\/p>\n<p>&nbsp;<\/p>\n<p><b>How to Computer Principal Components?<\/b><br \/>\nGiven a dataset X of dimension n x p, how do we compute the first principal components?<br \/>\nTo do this we look for linear combination of the feature values of the form:<\/p>\n<div style=\"clear: both; text-align: center;\"><a style=\"margin-left: 1em; margin-right: 1em;\" href=\"https:\/\/kindsonthegenius.com\/blog\/wp-content\/uploads\/2018\/11\/PCA10.2.jpg\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/kindsonthegenius.com\/blog\/wp-content\/uploads\/2018\/11\/PCA10.2-300x37.jpg\" width=\"320\" height=\"39\" border=\"0\" data-original-height=\"71\" data-original-width=\"570\" \/><\/a><\/div>\n<p>that has the largest sample variance subject to the constraint that:<\/p>\n<div style=\"clear: both; text-align: center;\"><a style=\"margin-left: 1em; margin-right: 1em;\" href=\"https:\/\/kindsonthegenius.com\/blog\/wp-content\/uploads\/2018\/11\/PCA-2BConstraint.jpg\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/kindsonthegenius.com\/blog\/wp-content\/uploads\/2018\/11\/PCA-2BConstraint.jpg\" width=\"200\" height=\"116\" border=\"0\" data-original-height=\"130\" data-original-width=\"224\" \/><\/a><\/div>\n<p>This means that\u00a0 the first principal component loading vector solves the optimization problem such that we need to maximize the objective function subject to some constraint.<br \/>\nThe objective function is given by:<\/p>\n<div style=\"clear: both; text-align: center;\"><a style=\"margin-left: 1em; margin-right: 1em;\" href=\"https:\/\/kindsonthegenius.com\/blog\/wp-content\/uploads\/2018\/11\/PCA10.3.jpg\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/kindsonthegenius.com\/blog\/wp-content\/uploads\/2018\/11\/PCA10.3-300x133.jpg\" width=\"200\" height=\"88\" border=\"0\" data-original-height=\"153\" data-original-width=\"344\" \/><\/a><\/div>\n<p>And this is subject to the constraint:<\/p>\n<div style=\"clear: both; text-align: center;\"><a style=\"margin-left: 1em; margin-right: 1em;\" href=\"https:\/\/kindsonthegenius.com\/blog\/wp-content\/uploads\/2018\/11\/PCA-2BConstraint-1.jpg\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/kindsonthegenius.com\/blog\/wp-content\/uploads\/2018\/11\/PCA-2BConstraint-1.jpg\" width=\"200\" height=\"115\" border=\"0\" data-original-height=\"130\" data-original-width=\"224\" \/><\/a><\/div>\n<p><ins style=\"display: block;\" data-ad-format=\"fluid\" data-ad-layout-key=\"-hk-o+30-4t+2k\" data-ad-client=\"ca-pub-7041870931346451\" data-ad-slot=\"6887528786\"><\/ins><br \/>\nThe objective function (function to maximize) can be rewritten as:<\/p>\n<div style=\"clear: both; text-align: center;\"><a style=\"margin-left: 1em; margin-right: 1em;\" href=\"https:\/\/kindsonthegenius.com\/blog\/wp-content\/uploads\/2018\/11\/PCA-2B-2Brewrite-2Bobjective-2Bfunction.jpg\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/kindsonthegenius.com\/blog\/wp-content\/uploads\/2018\/11\/PCA-2B-2Brewrite-2Bobjective-2Bfunction.jpg\" width=\"200\" height=\"103\" border=\"0\" data-original-height=\"125\" data-original-width=\"241\" \/><\/a><\/div>\n<p>Since this also holds:<\/p>\n<div style=\"clear: both; text-align: center;\"><a style=\"margin-left: 1em; margin-right: 1em;\" href=\"https:\/\/kindsonthegenius.com\/blog\/wp-content\/uploads\/2018\/11\/PCA-This-2Balso-2Bholds.jpg\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/kindsonthegenius.com\/blog\/wp-content\/uploads\/2018\/11\/PCA-This-2Balso-2Bholds.jpg\" width=\"200\" height=\"108\" border=\"0\" data-original-height=\"130\" data-original-width=\"239\" \/><\/a><\/div>\n<p>Therefore the average of z<sub>11<\/sub>,&#8230;, z<sub>n1<\/sub> will also be zero. Therefor the objective function that is being maximized is simply the sample variance of the n values of z<sub>i1<\/sub>.<br \/>\nz<sub>11<\/sub>, z<sub>2<\/sub>,&#8230;,z<sub>n1<\/sub> are referred to as the scores of the first principal component.<\/p>\n<p><ins style=\"display: block;\" data-ad-format=\"fluid\" data-ad-layout-key=\"-hk-o+30-4t+2k\" data-ad-client=\"ca-pub-7041870931346451\" data-ad-slot=\"6887528786\"><\/ins><br \/>\n<b>How then do we maximize the given objective function?\u00a0<\/b><br \/>\nWe do this by performing eigen decomposition of the covariance matrix. Details of how to perform eigen decomposition is explained here.<\/p>\n<p><b>Explaining the Principal Components<\/b><br \/>\nThe loading vector\u00a0<span style=\"background-color: transparent; color: #222222; display: inline; float: none; font-family: 'arial' , sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;\">\u0424<sub>1<\/sub><\/span> with elements <span style=\"background-color: transparent; color: #222222; display: inline; float: none; font-family: 'arial' , sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;\">\u0424<sub>11<\/sub><\/span>, <span style=\"background-color: transparent; color: #222222; display: inline; float: none; font-family: 'arial' , sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;\">\u0424<sub>21<\/sub><\/span>,&#8230;,<span style=\"background-color: transparent; color: #222222; display: inline; float: none; font-family: 'arial' , sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;\">\u0424<sub>p1<\/sub><\/span>\u00a0 defines a direction in the feature space along which there is maximum variance in the data.<br \/>\nThus, if we are to project the n data points x<sub>1<\/sub>, x<sub>2<\/sub>,&#8230;, x<sub>n<\/sub> onto this direction, then projected values are the actual principal component scores z<sub>11<\/sub>, z<sub>21<\/sub>, \u2026, z<sub>n1<\/sub>.<\/p>\n<p>After the first principal components, Z<sub>1<\/sub> of the features has been determined, then the second principal component is the linear combination of X<sub>1<\/sub>, ,X<sub>2<\/sub>,&#8230; X<sub>p<\/sub> that has the highest variance out of all the linear combinations that are uncorrelated with Z<sub>1<\/sub>. The second principal component scores z<sub>12<\/sub>, z<sub>22<\/sub>,&#8230;,z<sub>n2<\/sub> take the form<\/p>\n<p><a style=\"-webkit-text-stroke-width: 0px; background-color: transparent; clear: left; color: #0066cc; float: left; font-family: &amp;quot; font-size: 18px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 18px; margin-right: 18px; orphans: 2; text-align: justify; text-decoration: underline; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;\" href=\"https:\/\/kindsonthegenius.com\/blog\/wp-content\/uploads\/2018\/11\/PCA10.4.jpg\"><img decoding=\"async\" loading=\"lazy\" style=\"cursor: move;\" src=\"https:\/\/kindsonthegenius.com\/blog\/wp-content\/uploads\/2018\/11\/PCA10.4-300x37.jpg\" width=\"320\" height=\"39\" border=\"0\" data-original-height=\"71\" data-original-width=\"571\" \/><\/a><b><\/b><i><\/i><u><\/u><\/p>\n<div style=\"clear: both; text-align: center;\"><\/div>\n<p>where <span style=\"background-color: transparent; color: #222222; display: inline; float: none; font-family: 'arial' , sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;\">\u0424<sub>2<\/sub><\/span> is the second principal component loading vector, with elements <span style=\"background-color: transparent; color: #222222; display: inline; float: none; font-family: 'arial' , sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;\">\u0424<sub>11<\/sub><\/span>, <span style=\"background-color: transparent; color: #222222; display: inline; float: none; font-family: 'arial' , sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;\">\u0424<sub>12<\/sub><\/span>, \u2026 ,<span style=\"background-color: transparent; color: #222222; display: inline; float: none; font-family: 'arial' , sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;\">\u0424<sub>p2<\/sub><\/span> . It turns out that constraining Z<sub>2<\/sub> to be uncorrelated with Z<sub>1<\/sub> is the same as constraining the direction of\u00a0<span style=\"background-color: transparent; color: #222222; display: inline; float: none; font-family: 'arial' , sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;\">\u04242<\/span> to be orthogonal to the direction of\u00a0\u00a0<span style=\"background-color: transparent; color: #222222; display: inline; float: none; font-family: 'arial' , sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;\">\u0424<sub>1<\/sub><\/span><br \/>\nWe would now take an example to see how PCA works.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this simple tutorial, I would explain the concept of Principal Components Analysis (PCA) in Machine Learning. I would try to be as simple and &hellip; <\/p>\n","protected":false},"author":2,"featured_media":288,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[11,16],"tags":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/12"}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=12"}],"version-history":[{"count":2,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/12\/revisions"}],"predecessor-version":[{"id":396,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/12\/revisions\/396"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media\/288"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}