How to impute missing class labels in Python?
0

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