Recipe: How to impute missing class labels in Python?
DATA MUNGING

How to impute missing class labels in Python?

This recipe helps you impute missing class labels in Python
In [2]:
## How to impute missing class labels in Python 
def Kickstarter_Example_27():
    print()
    print(format('How to impute missing class labels in Python', '*^82'))
    import warnings
    warnings.filterwarnings("ignore")

    # Load libraries
    import numpy as np
    from sklearn.preprocessing import Imputer

    # Create Feature Matrix With Missing Values
    X = np.array([[2,       2.10, 1.45],
                  [1,       1.18, 1.33],
                  [2,       1.22, 1.27],
                  [0,       -0.21, -1.19],
                  [np.nan,  0.87, 1.31],
                  [np.nan, -0.67, -0.22]])

    # Create Imputer object
    imputer = Imputer(strategy='most_frequent', axis=0)

    # Fill missing values with most frequent class
    print(); print(X)
    print(); print(imputer.fit_transform(X))

Kickstarter_Example_27()
*******************How to impute missing class labels in Python*******************

[[ 2.    2.1   1.45]
 [ 1.    1.18  1.33]
 [ 2.    1.22  1.27]
 [ 0.   -0.21 -1.19]
 [  nan  0.87  1.31]
 [  nan -0.67 -0.22]]

[[ 2.    2.1   1.45]
 [ 1.    1.18  1.33]
 [ 2.    1.22  1.27]
 [ 0.   -0.21 -1.19]
 [ 2.    0.87  1.31]
 [ 2.   -0.67 -0.22]]


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 KPMG
1 developer from Vodafone
1 developer from Altimetrik
1 developer from LTI
1 developer from YASH Technologies
1 developer from ANAC
1 developer from MudraCircle
1 developer from HvH
1 developer from Verisk