{"id":9108,"date":"2020-10-07T10:06:09","date_gmt":"2020-10-07T10:06:09","guid":{"rendered":"https:\/\/www.askpython.com\/?p=9108"},"modified":"2020-10-07T10:06:12","modified_gmt":"2020-10-07T10:06:12","slug":"normalize-data-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/normalize-data-in-python","title":{"rendered":"How to Normalize Data in Python &#8211; All You Need to Know"},"content":{"rendered":"\n<p>Hello readers! In this article. we will be focusing on how we can normalize data in Python. So, let us get started.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What is Normalization?<\/h2>\n\n\n\n<p>Before diving into normalization, let us first understand the need of it!!<\/p>\n\n\n\n<p><strong>Feature Scaling<\/strong> is an essential step in the data analysis and preparation of data for modeling. Wherein, we make the data scale-free for easy analysis.<\/p>\n\n\n\n<p>Normalization is one of the feature scaling techniques. We particularly apply normalization when the data is <strong>skewed <\/strong>on the either axis i.e. when the data does not follow the <strong>gaussian distribution<\/strong>.<\/p>\n\n\n\n<p>In <strong>normalization<\/strong>, we <strong>convert the data features of different scales to a common scale<\/strong> which further makes it easy for the data to be processed for modeling. Thus, all the data features(variables) tend to have a similar impact on the modeling portion.<\/p>\n\n\n\n<p>According to the below formula, we normalize each feature by subtracting the minimum data value from the data variable and then divide it by the range of the variable as shown&#8211;<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"240\" height=\"87\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/Normalization.png\" alt=\"Normalization\" class=\"wp-image-9114\"\/><figcaption>Normalization<\/figcaption><\/figure><\/div>\n\n\n\n<p>Thus, we transform the values to a range between <strong>[0,1]<\/strong>. Let us now try to implement the concept of Normalization in Python in the upcoming section.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Normalize Data in Python <\/h2>\n\n\n\n<p>There are various approaches in Python through which we can perform Normalization.<\/p>\n\n\n\n<p>Today, we will be using one of the most popular way&#8211; <strong>MinMaxScaler<\/strong>.<\/p>\n\n\n\n<p>Let us first have a look at the dataset which we would be scaling ahead.<\/p>\n\n\n\n<p><strong>Dataset:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"567\" height=\"407\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/Dataset-for-Normalization.png\" alt=\"Dataset For Normalization\" class=\"wp-image-9115\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/Dataset-for-Normalization.png 567w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/Dataset-for-Normalization-300x215.png 300w\" sizes=\"auto, (max-width: 567px) 100vw, 567px\" \/><figcaption><strong>Dataset For Normalization<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>Further, we will be using <strong>min and max scaling in sklearn<\/strong> to perform normalization. <\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pandas as pd\nimport os\nfrom sklearn.preprocessing import MinMaxScaler\n#Changing the working directory to the specified path--\nos.chdir(&quot;D:\/Normalize - Loan_Defaulter&quot;)\n\ndata = pd.read_csv(&quot;bank-loan.csv&quot;) # dataset\nscaler = MinMaxScaler()\n\nloan=pd.DataFrame(scaler.fit_transform(data),\n            columns=data.columns, index=data.index) \nprint(loan)\n<\/pre><\/div>\n\n\n<p>Here, we have created an object of <code>MinMaxScaler()<\/code> <a href=\"https:\/\/www.askpython.com\/python\/oops\/python-classes-objects\" class=\"rank-math-link\">class<\/a>. Further, we have used <code>fit_transform() method<\/code> to normalize the data values.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p>So, as clearly visible, we have transformed and normalized the data values in the range of 0 and 1.<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/Dataset-after-Normalization-using-MinMaxScaler.png\" alt=\"Dataset After Normalization Using MinMaxScaler - How to normalize data in Python\" class=\"wp-image-9113\" width=\"580\" height=\"370\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/Dataset-after-Normalization-using-MinMaxScaler.png 632w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/Dataset-after-Normalization-using-MinMaxScaler-300x192.png 300w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><figcaption><strong>Dataset After Normalization Using MinMaxScaler<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>Thus, from the above explanation, the following insights can be drawn&#8211;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Normalization is used when the data values are <strong>skewed <\/strong>and <strong>do not follow gaussian distribution<\/strong>.<\/li><li>The data values get converted between a <strong>range of 0 and 1<\/strong>.<\/li><li>Normalization makes the data <strong>scale free<\/strong>.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>By this, we have come to the end of this article. Feel free to comment below in case you come across any question.<\/p>\n\n\n\n<p>Till then, Stay tuned @ <a class=\"rank-math-link\" href=\"https:\/\/www.askpython.com\/\">Python with AskPython<\/a> and Keep Learning!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello readers! In this article. we will be focusing on how we can normalize data in Python. So, let us get started. What is Normalization? Before diving into normalization, let us first understand the need of it!! Feature Scaling is an essential step in the data analysis and preparation of data for modeling. Wherein, we [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":9141,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-9108","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/9108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=9108"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/9108\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/9141"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=9108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=9108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=9108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}