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

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

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

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.

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

We have imported numpy and KNeighborsClassifier which is needed.

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]])
```

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]]

In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

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.

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

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

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.

In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

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.