{"id":844,"date":"2023-08-29T11:01:45","date_gmt":"2023-08-29T11:01:45","guid":{"rendered":"https:\/\/savvaspanagi.com\/?p=844"},"modified":"2023-11-19T16:11:00","modified_gmt":"2023-11-19T16:11:00","slug":"feature-scaling","status":"publish","type":"post","link":"https:\/\/savvaspanagi.com\/?p=844","title":{"rendered":"Feature Scaling (Normalisation and Standardisation) \/ Python implementation"},"content":{"rendered":"\n<p class=\"has-ast-global-color-2-color has-text-color\">following the previous article about <a href=\"https:\/\/savvaspanagi.com\/?p=803\"><span style=\"text-decoration: underline;\">handle cyclical features<\/span><\/a>, feature scaling is also important when we are going to use a machine learning algorithms. Is a method to scale the features in a comparative form. But, how can we scaling it ? what is the difference between normalization and standardization ? and when we can use it ? All this questions is gone solve at the current article\/blog post. <\/p>\n\n\n\n<p class=\"has-ast-global-color-2-color has-text-color\"><strong>Equations<\/strong><\/p>\n\n\n<p class=\"ql-center-displayed-equation\" style=\"line-height: 169px;\"><span class=\"ql-right-eqno\"> &nbsp; <\/span><span class=\"ql-left-eqno\"> &nbsp; <\/span><img decoding=\"async\" src=\"https:\/\/savvaspanagi.com\/wp-content\/ql-cache\/quicklatex.com-379cf12134d3869d1cbcb49c1e3e6c99_l3.png\" height=\"169\" width=\"291\" class=\"ql-img-displayed-equation quicklatex-auto-format\" alt=\" &#92;&#98;&#101;&#103;&#105;&#110;&#123;&#99;&#101;&#110;&#116;&#101;&#114;&#125; &#32;&#32;&#32;&#32;&#78;&#111;&#114;&#109;&#97;&#108;&#105;&#122;&#97;&#116;&#105;&#111;&#110;&#40;&#77;&#105;&#110;&#45;&#77;&#97;&#120;&#32;&#84;&#114;&#97;&#110;&#115;&#102;&#111;&#114;&#109;&#41;&#58; &#32;&#32;&#32;&#32;&#92;&#91;&#32;&#88;&#39;&#32;&#61;&#32;&#92;&#102;&#114;&#97;&#99;&#123;&#88;&#32;&#45;&#32;&#88;&#95;&#123;&#92;&#116;&#101;&#120;&#116;&#123;&#109;&#105;&#110;&#125;&#125;&#125;&#123;&#88;&#95;&#123;&#92;&#116;&#101;&#120;&#116;&#123;&#109;&#97;&#120;&#125;&#125;&#32;&#45;&#32;&#88;&#95;&#123;&#92;&#116;&#101;&#120;&#116;&#123;&#109;&#105;&#110;&#125;&#125;&#125;&#32;&#92;&#93; &#32;&#32;&#32;&#32;&#83;&#116;&#97;&#110;&#100;&#97;&#114;&#100;&#105;&#122;&#97;&#116;&#105;&#111;&#110;&#32;&#40;&#90;&#45;&#83;&#99;&#111;&#114;&#101;&#32;&#84;&#114;&#97;&#110;&#115;&#102;&#111;&#114;&#109;&#41;&#58; &#32;&#32;&#32;&#32;&#92;&#91;&#32;&#88;&#39;&#32;&#61;&#32;&#92;&#102;&#114;&#97;&#99;&#123;&#88;&#32;&#45;&#32;&#92;&#109;&#117;&#125;&#123;&#92;&#115;&#105;&#103;&#109;&#97;&#125;&#32;&#92;&#93; &#92;&#101;&#110;&#100;&#123;&#99;&#101;&#110;&#116;&#101;&#114;&#125; \" title=\"Rendered by QuickLaTeX.com\"\/><\/p>\n\n\n\n<p class=\"has-ast-global-color-2-color has-text-color\">Base on the very helpful Udemy courses that i have take (Machine learning A-Z) i will try to provide what&#8217;s the problem if we are not scaling the features and how scaling can help be giving a simple example. Let&#8217;s imagine that we have 2 columns of <strong>income<\/strong> and <strong>age<\/strong>, also we have 3 different persons as the following table:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-ast-global-color-2-color has-text-color\"><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">Person<\/td><td class=\"has-text-align-center\" data-align=\"center\">Income<\/td><td class=\"has-text-align-center\" data-align=\"center\">Age (Years)<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">1<\/td><td class=\"has-text-align-center\" data-align=\"center\">70.000\u20ac<\/td><td class=\"has-text-align-center\" data-align=\"center\">45<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">2<\/td><td class=\"has-text-align-center\" data-align=\"center\">60.000\u20ac<\/td><td class=\"has-text-align-center\" data-align=\"center\">44<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">3<\/td><td class=\"has-text-align-center\" data-align=\"center\">52.000\u20ac<\/td><td class=\"has-text-align-center\" data-align=\"center\">40<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"has-ast-global-color-2-color has-text-color\">So which 2 of this 3 persons is more similar together?  We are clearly sure that the person 1-3 is the worst similar couple that we can take so ignore this pattern. If we try to calculate the difference between the remain combinations, person 1 from 2 has salary difference of 10.000\u20ac and age different of 1 year, but person 2 from 3 has salary different of 8.000\u20ac and age different of 4 years. So numerically the best pair is the person 2-3 which has less totally &#8220;distance&#8221; (8004&lt;10001). Is this true? No, because is not acceptable to compare income with age. If we want to compare different units of measurement like income with age we have to change the units to a common comparable form. <\/p>\n\n\n\n<p class=\"has-ast-global-color-2-color has-text-color\"><span style=\"text-decoration: underline;\">What is the different of each transform (Normalisation and Standardisation) ? <\/span><\/p>\n\n\n\n<p class=\"has-ast-global-color-2-color has-text-color\">Normalization, transform the data&#8217;s is a scale of [0-1], in contrast the standardisation is not predetermined scale from before and as larger is the standard deviation (\u03c3) as closest to the zero was the standardisation data&#8217;s. <\/p>\n\n\n\n<p class=\"has-ast-global-color-2-color has-text-color\"><span style=\"text-decoration: underline;\">When we use each transform ? <\/span><\/p>\n\n\n\n<p class=\"has-ast-global-color-2-color has-text-color\">Normalisation is recommended when the features follows normal distribution, in contrast the standardisation can be used in the most of situations without any problem. But this universal usage of standardisation does not mean that is always the best choice to scaling the features.<\/p>\n\n\n\n<p class=\"has-ast-global-color-2-color has-text-color\">Going back to the problem we have described and try to normalisation the values ,we will have the following table:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-ast-global-color-2-color has-text-color\"><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">Person<\/td><td class=\"has-text-align-center\" data-align=\"center\">Income<\/td><td class=\"has-text-align-center\" data-align=\"center\">Age <\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">1<\/td><td class=\"has-text-align-center\" data-align=\"center\">1<\/td><td class=\"has-text-align-center\" data-align=\"center\">1<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">2<\/td><td class=\"has-text-align-center\" data-align=\"center\">0.444<\/td><td class=\"has-text-align-center\" data-align=\"center\">0.8<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">3<\/td><td class=\"has-text-align-center\" data-align=\"center\">0<\/td><td class=\"has-text-align-center\" data-align=\"center\">0<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"has-ast-global-color-2-color has-text-color\">What about the most common pair now? <\/p>\n\n\n\n<p class=\"has-ast-global-color-2-color has-text-color\">Person [1-2] = (1-0.444)+(1-0.8) = 0.756<\/p>\n\n\n\n<p class=\"has-ast-global-color-2-color has-text-color\">Person [2-3] = (0.444-0)+(0.8-0) = 1.244<\/p>\n\n\n\n<p class=\"has-ast-global-color-2-color has-text-color\">So is obvious that the most similar couple is the person 1 with person 2 and not the above results he had calculated before normalization.<\/p>\n\n\n\n<p class=\"has-ast-global-color-2-color has-text-color\">To implement this 2 methodologies with python, i will propose to use sklearn.preprocessing library. <\/p>\n\n\n\n<p><strong>Standardisation:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport numpy as np\nfrom sklearn.preprocessing import StandardScaler\n\n# Create a sample dataset with two features (columns)\ndata = np.array(&#x5B;&#x5B;10, 2],\n                 &#x5B;20, 4],\n                 &#x5B;30, 6],\n                 &#x5B;40, 8]])\n\n# Create a StandardScaler object\nscaler = StandardScaler()\n\n# Fit the scaler to the data and transform the data\nnormalized_data = scaler.fit_transform(data)\n\nprint(&quot;Original Data:\\n&quot;, data)\nprint(&quot;\\nNormalized Data:\\n&quot;, normalized_data)\n<\/pre><\/div>\n\n\n<p><strong>Normalisation:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport numpy as np\nfrom sklearn.preprocessing import MinMaxScaler\n\n# Create a sample dataset with two features (columns)\ndata = np.array(&#x5B;&#x5B;10, 2],\n                 &#x5B;20, 4],\n                 &#x5B;30, 6],\n                 &#x5B;40, 8]])\n\n# Create a MinMaxScaler object\nscaler = MinMaxScaler()\n\n# Fit the scaler to the data and transform the data\nnormalized_data = scaler.fit_transform(data)\n\nprint(&quot;Original Data:\\n&quot;, data)\nprint(&quot;\\nNormalized Data:\\n&quot;, normalized_data)\n\n<\/pre><\/div>\n\n\n<p class=\"has-ast-global-color-2-color has-text-color\">Your thoughts and questions are important for me. Feel free to share your insights or inquire about anything in the comments section below. Let&#8217;s keep the conversation going!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>following the previous article about handle cyclical features, feature scaling is also important when we are going to use a machine learning algorithms. Is a method to scale the features in a comparative form. But, how can we scaling it ? what is the difference between normalization and standardization ? and when we can use &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"https:\/\/savvaspanagi.com\/?p=844\"> <span class=\"screen-reader-text\">Feature Scaling (Normalisation and Standardisation) \/ Python implementation<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"footnotes":""},"categories":[7],"tags":[],"jetpack_featured_media_url":"","rttpg_featured_image_url":null,"rttpg_author":{"display_name":"Savvas Panagi","author_link":"https:\/\/savvaspanagi.com\/?author=1"},"rttpg_comment":0,"rttpg_category":"<a href=\"https:\/\/savvaspanagi.com\/?cat=7\" rel=\"category\">Machine Learning<\/a>","rttpg_excerpt":"following the previous article about handle cyclical features, feature scaling is also important when we are going to use a machine learning algorithms. Is a method to scale the features in a comparative form. But, how can we scaling it ? what is the difference between normalization and standardization ? and when we can use&hellip;","_links":{"self":[{"href":"https:\/\/savvaspanagi.com\/index.php?rest_route=\/wp\/v2\/posts\/844"}],"collection":[{"href":"https:\/\/savvaspanagi.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savvaspanagi.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savvaspanagi.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savvaspanagi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=844"}],"version-history":[{"count":28,"href":"https:\/\/savvaspanagi.com\/index.php?rest_route=\/wp\/v2\/posts\/844\/revisions"}],"predecessor-version":[{"id":1152,"href":"https:\/\/savvaspanagi.com\/index.php?rest_route=\/wp\/v2\/posts\/844\/revisions\/1152"}],"wp:attachment":[{"href":"https:\/\/savvaspanagi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savvaspanagi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savvaspanagi.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}