from sklearn import imputer code example

Example 1: sciket learn imputer code

from sklearn.preprocessing import Imputerimputer = Imputer(missing_values = 'NaN', strategy = 'mean', axis = 0)imputer = imputer.fit(X[:, 1:3])X[:, 1:3] = imputer.transform(X[:, 1:3])

Example 2: Multivariate feature imputation

# Multivariate feature imputation

import numpy as np
from sklearn.experimental import enable_iterative_imputer
from sklearn.impute import IterativeImputer
imp = IterativeImputer(max_iter=10, random_state=0)
imp.fit([[1, 2], [3, 6], [4, 8], [np.nan, 3], [7, np.nan]])
# IterativeImputer(random_state=0)
X_test = [[np.nan, 2], [6, np.nan], [np.nan, 6]]
# the model learns that the second feature is double the first
print(np.round(imp.transform(X_test)))
# [[ 1.  2.]
#  [ 6. 12.]
#  [ 3.  6.]]

Tags:

Misc Example