Recipe: How to impute missing class labels using nearest neighbours in Python?
DATA MUNGING DATA CLEANING 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
In [1]:
## How to impute missing class labels using nearest neighbours in Python 
def Kickstarter_Example_28():
    print()
    print(format('How to impute missing class labels using nearest neighbours in Python', '*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # Load libraries
    import numpy as np
    from sklearn.neighbors import KNeighborsClassifier

    # Create Feature Matrix
    # Create feature matrix with categorical feature
    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]])

    # Create Feature Matrix With Missing Values
    # Create feature matrix with missing values in the categorical feature
    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]])

    # Train k-Nearest Neighbor Classifier
    clf = KNeighborsClassifier(3, weights='distance')
    trained_model = clf.fit(X[:,1:], X[:,0])

    # Predict missing values' class
    imputed_values = trained_model.predict(X_with_nan[:,1:])
    print(); print(imputed_values)

    # Join column of predicted class with their other features
    X_with_imputed = np.hstack((imputed_values.reshape(-1,1), X_with_nan[:,1:]))
    print(); print(X_with_imputed)

    # Join two feature matrices
    print(); print(np.vstack((X_with_imputed, X)))

Kickstarter_Example_28()
******How to impute missing class labels using nearest neighbours in Python*******

[2. 1. 2. 1.]

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

[[ 2.    0.87  1.31]
 [ 1.    0.37  1.91]
 [ 2.    0.54  1.27]
 [ 1.   -0.67 -0.22]
 [ 0.    2.1   1.45]
 [ 2.    1.18  1.33]
 [ 0.    1.22  1.27]
 [ 1.    1.32  1.97]
 [ 1.   -0.21 -1.19]]


Stuck at work?
Can't find the recipe you are looking for. Let us know and we will find an expert to create the recipe for you. Click here
Companies using this Recipe
1 developer from HvH
1 developer from Vodafone
1 developer from Altimetrik
1 developer from KPMG
1 developer from YASH Technologies
1 developer from ANAC
1 developer from LTI
1 developer from Cognizant
1 developer from MudraCircle