<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by avish arora on Medium]]></title>
        <description><![CDATA[Stories by avish arora on Medium]]></description>
        <link>https://medium.com/@aavisharora?source=rss-decf107ec083------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*U1vhPjvrBWOKwvVr</url>
            <title>Stories by avish arora on Medium</title>
            <link>https://medium.com/@aavisharora?source=rss-decf107ec083------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 24 May 2026 02:25:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aavisharora/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Data Scaling Techniques in Python]]></title>
            <link>https://medium.com/@aavisharora/data-scaling-techniques-in-python-932c20eb5bed?source=rss-decf107ec083------2</link>
            <guid isPermaLink="false">https://medium.com/p/932c20eb5bed</guid>
            <dc:creator><![CDATA[avish arora]]></dc:creator>
            <pubDate>Mon, 07 Jul 2025 16:58:27 GMT</pubDate>
            <atom:updated>2025-07-07T16:58:27.172Z</atom:updated>
            <content:encoded><![CDATA[<p>When you’re building a machine learning model, one crucial step is preparing your data. Features in a dataset often have many different scales. When comparing features with different scales, like height in centimeters versus salary in dollars, algorithms can get confused because they treat larger numbers as more important, regardless of the actual meaning. This can cause algorithms that use distance or gradient descent to perform poorly, as they become biased towards features with bigger values. Scaling helps by putting all features on a similar scale, so each one contributes equally to the model’s decisions and improves performance with faster convergence.</p><p>Feature scaling adjusts the scale of features to a similar range, ensuring that no single feature dominates the learning process, it also helps in gradient descent by making the optimization process more efficient and stable.</p><p>Here’s how:</p><ol><li>Faster Convergence: When features are on very different scales, the cost function’s contour can be elongated. Gradient descent updates weights based on gradients, and with varying scales, the steps can be uneven. This causes a zig-zagging path towards the minimum, slowing down convergence. Feature scaling helps to round out the cost function, allowing gradient descent to take a more direct path to the minimum, resulting in faster convergence.</li><li>Balanced Weight Updates: Feature scaling ensures that no feature dominates the weight updates just because it has a larger numerical range. This leads to more stable and balanced adjustments to the model’s parameters.</li><li>Avoiding Getting Stuck: Without scaling, the elongated cost function contours can cause the gradient descent algorithm to get stuck in inefficient loops or potentially converge to a suboptimal solution. Scaling helps create a more spherical cost function, enabling the algorithm to find the optimal minimum more effectively.</li><li>Helps with Regularization: Scaling is also beneficial when using regularization techniques like Ridge or Lasso, which penalize large weights. Scaling ensures that this penalty is applied fairly to all features, preventing features with larger scales from being penalized disproportionately.</li></ol><p>Several feature scaling techniques are explained in this post:</p><ul><li>MinMax Scaler</li><li>Standardization (Z-score scaling)</li><li>Normalization</li><li>Robust Scaler</li><li>MaxAbs Scaler</li></ul><p>Here is the explanation of each technique in simple terms with implementations using Python’s Scikit-learn library.</p><p>1. <strong>MinMax Scaler (Normalization)</strong></p><p>MinMax Scaler, also known as normalization, transforms each feature to a specific range, usually between 0 and 1.</p><p><strong>How it works:</strong></p><p>Each feature is scaled and translated individually based on its minimum and maximum values in the training set. Here is the formula:</p><p>X_scaled = (X — X_min) / (X_max — X_min)</p><p>Where X is the feature value, X_min is the minimum value in the feature column, and X_max is the maximum value.</p><p><strong>When to use it:</strong></p><ul><li>When values are needed within a bounded interval, like in image processing.</li><li>When the data has few or no outliers.</li><li>For algorithms sensitive to the scale of features, like K-Means clustering or algorithms using distance calculations.</li></ul><p><strong>Python code example:</strong></p><p>from sklearn.preprocessing import MinMaxScaler</p><p>Import numpy as np</p><p>data = np.array([[1, 2], [3, 4], [5, 6]])</p><p>scaler = MinMaxScaler()</p><p>scaled_data = scaler.fit_transform(data)</p><p>print(scaled_data)</p><p><strong>Output</strong>:</p><p>[[0. 0. ]<br> [0.5 0.5 ]<br> [1. 1. ]]</p><p>The data is scaled to the range [0, 1].</p><p>2. <strong>Standardization (Z-score scaling)</strong></p><p>Standardization or Z-score scaling transforms data into a mean of 0 and a standard deviation of 1.</p><p><strong>How it works:</strong></p><p>The mean of each feature is subtracted, and then divided by its standard deviation. The formula is:</p><p>X_scaled = (X — mean) / standard_deviation</p><p><strong>When to use it:</strong></p><ul><li>When the data has an approximately Gaussian (normal) distribution.</li><li>For algorithms that assume normally distributed data, like linear regression or logistic regression.</li><li>Standardization maintains useful information about outliers and makes algorithms less sensitive to them compared to MinMax scaling.</li></ul><p><strong>Python code example:</strong></p><p>from sklearn.preprocessing import StandardScaler</p><p>import numpy as np</p><p>data = np.array([[1, 2], [3, 4], [5, 6]])</p><p>scaler = StandardScaler()</p><p>scaled_data = scaler.fit_transform(data)</p><p>print(scaled_data)</p><p><strong>Output:</strong></p><p>[[-1.22474487 -1.22474487]<br> [ 0. 0. ]<br> [ 1.22474487 1.22474487]]</p><p>Here, the data is transformed to have a mean of 0 and a standard deviation of 1.</p><p><strong>3. Normalization (L1 or L2)</strong></p><p>Normalization, in the context of scaling, often refers to scaling each sample (row) to have unit norm. This is different from the feature scaling discussed earlier. There are two common types:</p><ul><li>L1 normalization: Scales the values so that the sum of the absolute values of the components is 1.</li><li>L2 normalization: Scales the values so that the sum of the squares of the components is 1.</li></ul><p><strong>How it works:</strong></p><p>Each data vector (sample) is rescaled independently of the distribution of the samples.</p><p><strong>When to use it:</strong></p><ul><li>In scenarios where you want to compare similarities between samples based on distance measures.</li><li>For text mining, MaxAbs scaling is often used.</li></ul><p><strong>Python code example (using L2 normalization):</strong></p><p>from sklearn.preprocessing import Normalizer</p><p>import numpy as np</p><p>data = np.array([[1, 2], [3, 4], [5, 6]])</p><p>scaler = Normalizer(norm=’l2&#39;)</p><p>scaled_data = scaler.fit_transform(data)</p><p>print(scaled_data)</p><p><strong>Output:</strong></p><p>[[0.4472136 0.89442719]<br> [0.6 0.8 ]<br> [0.6401844 0.76980036]]</p><p>Each row is now a unit vector.</p><p><strong>4. Robust Scaler</strong></p><p>Robust Scaler is designed to handle datasets with outliers.</p><p><strong>How it works:</strong></p><p>The median is removed, and the data is scaled based on the interquartile range (IQR), which is the range between the 25th and 75th percentiles.</p><p><strong>When to use it:</strong></p><ul><li>When the data contains outliers.</li><li>To maintain the relative distances between non-outlier data points.</li></ul><p><strong>Python code example:</strong></p><p>from sklearn.preprocessing import RobustScaler</p><p>import numpy as np</p><p>data = np.array([[1, 2], [3, 4], [1000, 6]])</p><p>scaler = RobustScaler()</p><p>scaled_data = scaler.fit_transform(data)</p><p>print(scaled_data)</p><p><strong>Output:</strong></p><p>[[ 0. -1. ]<br> [ 1. 0. ]<br> [498.5 1. ]]</p><p>The outlier (1000) doesn’t drastically skew the scaling of other values, compared to if you used StandardScaler.</p><p><strong>5. MaxAbs Scaler</strong></p><p>MaxAbs Scaler scales each feature by its maximum absolute value.</p><p><strong>How it works:</strong></p><p>Each feature is scaled and translated individually such that the maximal absolute value of each feature in the training set will be 1.0.</p><p><strong>When to use it:</strong></p><ul><li>For sparse data matrices, such as in text mining.</li><li>When you want to keep the zero values as zeros.</li></ul><p><strong>Python code example:</strong></p><p>from sklearn.preprocessing import MaxAbsScaler</p><p>import numpy as np</p><p>data = np.array([[1, 2], [3, 4], [-5, 6]])</p><p>scaler = MaxAbsScaler()</p><p>scaled_data = scaler.fit_transform(data)</p><p>print(scaled_data)</p><p><strong>Output:</strong></p><p>[[ 0.2 0.33333333]<br> [ 0.6 0.66666667]<br> [-1. 1. ]]</p><p>Each feature is scaled such that its maximum absolute value is 1.</p><p><strong>Conclusion: Choosing the Right Scaler</strong></p><p>The best scaling method depends on the data and the machine learning algorithm being used.</p><ul><li>If the data has a normal distribution, StandardScaler might be a good choice.</li><li>If the data has outliers, RobustScaler is a better option.</li><li>If the data needs to be in a specific range and doesn’t have many outliers, MinMaxScaler might be appropriate.</li><li>MaxAbsScaler is useful for sparse data.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=932c20eb5bed" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>