Exercise

KNN imputation of categorical values

Once all the categorical columns in the DataFrame have been converted to ordinal values, the DataFrame is ready to be imputed. Imputing using statistical models like K-Nearest Neighbors (KNN) provides better imputations.

In this exercise, you'll

  1. Use the KNN() function from fancyimpute to impute the missing values in the ordinally encoded DataFrame users.
  2. Convert the ordinal values back to their respective categories using the ordinal encoder's .inverse_transform() method.

Remember, ordinal_enc_dict stores sklearn's OrdinalEncoder() for each column. The users DataFrame stores the encoded values (ordinal values) for each column.

The KNN() function, the dictionary of OrdinalEncoder()s ordinal_enc_dict and the users DataFrame have already been loaded for you.

Instructions

100 XP
  • Impute the users DataFrame using KNN_imputer's fit_transform() method. These transformed values are rounded to get integers.
  • Iterate over columns in users.
  • Select the column's OrdinalEncoder() from ordinal_enc_dict and perform .inverse_transform() on the reshaped array reshaped.