How to impute missing class labels using nearest neighbours in Python?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

# How to impute missing class labels using nearest neighbours in Python?

This recipe helps you impute missing class labels using nearest neighbours in Python

0

## Recipe Objective

Have you ever tried to impute calss labels? We can impute class labels by K nearest neighbours by training it on known data and predicting the class labels.

So this is the recipe on how we can impute missing class labels using nearest neighbours in Python.

## Step 1 - Import the library

``` import numpy as np from sklearn.neighbors import KNeighborsClassifier ```

We have imported numpy and KNeighborsClassifier which is needed.

## Step 2 - Setting up the Data

We have created a feature matrix using array and we will use this to train the KNN model. ``` X = np.array([[0, 2.10, 1.45], [2, 1.18, 1.33], [0, 1.22, 1.27], [1, 1.32, 1.97], [1, -0.21, -1.19]]) ``` We have created a matrix with missing class labels. ``` X_with_nan = np.array([[np.nan, 0.87, 1.31], [np.nan, 0.37, 1.91], [np.nan, 0.54, 1.27], [np.nan, -0.67, -0.22]]) ```

## Step 3 - Predicting the Class Labels

We are training the KNeighborsClassifier with parameters K equals to 3 and weights equals to distance. We have used the matrix X to train the model. ``` clf = KNeighborsClassifier(3, weights="distance") trained_model = clf.fit(X[:,1:], X[:,0]) ``` We have predicted the class labels of matrix "X_with_nan". ``` imputed_values = trained_model.predict(X_with_nan[:,1:]) print(imputed_values) ``` So finally we have filled the null values with the predicted output of model. ``` X_with_imputed = np.hstack((imputed_values.reshape(-1,1), X_with_nan[:,1:])) print(); print(X_with_imputed) ``` So the output comes as

```[2. 1. 2. 1.]

[[ 2.    0.87  1.31]
[ 1.    0.37  1.91]
[ 2.    0.54  1.27]
[ 1.   -0.67 -0.22]]
```

#### Relevant Projects

##### Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

##### Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

##### Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

##### Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

##### Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

##### Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

##### Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

##### Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

##### Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

##### Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.