<?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 Aditya Totla on Medium]]></title>
        <description><![CDATA[Stories by Aditya Totla on Medium]]></description>
        <link>https://medium.com/@adityatotla?source=rss-b2d6c9219963------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ix-zBbvqDtC38qnlHSK1wA.jpeg</url>
            <title>Stories by Aditya Totla on Medium</title>
            <link>https://medium.com/@adityatotla?source=rss-b2d6c9219963------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 19:46:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@adityatotla/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[KNNImputer for Missing Value Imputation in Python using scikit-learn]]></title>
            <link>https://medium.com/@adityatotla/knnimputer-for-missing-value-imputation-in-python-using-scikit-learn-67f659ca8520?source=rss-b2d6c9219963------2</link>
            <guid isPermaLink="false">https://medium.com/p/67f659ca8520</guid>
            <category><![CDATA[knn]]></category>
            <category><![CDATA[scikit-learn]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Aditya Totla]]></dc:creator>
            <pubDate>Thu, 24 Sep 2020 06:53:15 GMT</pubDate>
            <atom:updated>2020-09-24T07:58:11.636Z</atom:updated>
            <content:encoded><![CDATA[<h3>A Guide To KNN Imputation For Handling Missing Values</h3><p><strong><em>How to handle missing data in your dataset with Scikit-Learn’s KNN Imputer</em></strong></p><p><em>Missing Values in the dataset is one heck of a problem before we could get into Modelling. A lot of machine learning algorithms demand those missing values to be imputed before proceeding further.</em></p><h3>What are Missing Values?</h3><p>A missing value can be defined as the data value that is not captured nor stored for a variable in the observation of interest. There are 3 types of missing values -</p><p><strong>Missing Completely at Random (MCAR)</strong></p><p>MCAR occurs when the missing on the variable is completely unsystematic. When our dataset is missing values completely at random, the probability of missing data is unrelated to any other variable and unrelated to the variable with missing values itself. For example, MCAR would occur when data is missing because the responses to a research survey about depression are lost in the mail.</p><p><strong>Missing at Random (MAR)</strong></p><p>MAR occurs when the probability of the missing data on a variable is related to some other measured variable but unrelated to the variable with missing values itself. For example, the data values are missing because males are less likely to respond to a depression survey. In this case, the missing data is related to the gender of the respondents. However, the missing data is not related to the level of depression itself.</p><p><strong>Missing Not at Random (MNAR)</strong></p><p>MNAR occurs when the missing values on a variable are related to the variable with the missing values itself. In this case, the data values are missing because the respondents failed to fill in the survey due to their level of depression.</p><h3>Effects of Missing Values</h3><p>Having missing values in our datasets can have various detrimental effects. Here are a few examples -</p><ol><li>Missing data can limit our ability to perform important data science tasks such as converting data types or visualizing data</li><li>Missing data can reduce the statistical power of our models which in turn increases the probability of Type II error. Type II error is the failure to reject a false null hypothesis.</li><li>Missing data can reduce the representativeness of the samples in the dataset.</li><li>Missing data can distort the validity of the scientific trials and can lead to invalid conclusions.</li></ol><h3>KNN Imputer</h3><p>The popular (computationally least expensive) way that a lot of Data scientists try is to use mean/median/mode or if it’s a Time Series, then lead or lag record.</p><p>There must be a better way — that’s also easier to do — which is what the widely preferred KNN-based Missing Value Imputation.</p><p><em>scikit-learn</em>‘s v0.22 natively supports KNN Imputer — which is now officially the easiest + best (computationally least expensive) way of Imputing Missing Value. It’s a 3-step process to impute/fill NaN (Missing Values). This post is a very short tutorial of explaining how to impute missing values using KNNImputer</p><p>Make sure you update your scikit-learn</p><pre>pip3 install -U scikit-learn</pre><h3>1. Load KNNImputer</h3><pre>from sklearn.impute import KNNImputer</pre><h3>How does it work?</h3><p>According scikit-learn docs:</p><blockquote>Each sample’s missing values are imputed using the mean value from n_neighbors nearest neighbors found in the training set. Two samples are close if the features that neither is missing are close. By default, a euclidean distance metric that supports missing values, nan_euclidean_distances, is used to find the nearest neighbors.</blockquote><h4>Creating Dataframe with Missing Values</h4><pre>import numpy as np<br>import pandas as pd<br>dict = {‘First’:[100, 90, np.nan, 95], <br> ‘Second’: [30, 45, 56, np.nan], <br> ‘Third’:[np.nan, 40, 80, 98]} <br> <br># creating a dataframe from list <br>df = pd.DataFrame(dict)</pre><figure><img alt="At this point, You’ve got the dataframe df with missing values." src="https://cdn-images-1.medium.com/max/1024/1*jKlI-I_9LxT7yEFgJqQRVA.png" /><figcaption>At this point, You’ve got the dataframe df with missing values.</figcaption></figure><h3>2. Initialize KNNImputer</h3><p>You can define your own n_neighbors value (as its typical of KNN algorithm).</p><pre>imputer = KNNImputer(n_neighbors=2)</pre><h3>3. Impute/Fill Missing Values</h3><pre>df_filled = imputer.fit_transform(df)</pre><h4>Display the filled-in data</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3xgWnjS3gXi0b9ylBGmj7w.png" /></figure><h4>Conclusion</h4><p>As you can see above, that’s the entire missing value imputation process is. It’s as simple as just using mean or median but more effective and accurate than using a simple average. Thanks to the new native support in scikit-learn, This imputation fit well in our pre-processing pipeline.</p><h4>References:</h4><p><a href="https://scikit-learn.org/stable/modules/impute.html#knnimpute"><em>https://scikit-learn.org/stable/modules/impute.html#knnimpute</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=67f659ca8520" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>