Handling nan values with KBinsDiscretizer

Viewed 683

I have a numpy array that looks like this,

A = np.random.randn(100, )
A.ravel()[np.random.choice(A.size, 10, replace=False)] = np.nan

Few of the values are missing indicated by np.nan

I'm trying to fit KBinsDiscretizer on it. I want to bin the non null values normally and mark null values as separate bin.

While trying to fit KBinsDiscretizer normally I got the following error due to the presence of np.nan in the data.

est = KBinsDiscretizer(n_bins=5, encode='ordinal', strategy='quantile')
est.fit(A.reshape(-1, 1))

ValueError: Input contains NaN, infinity or a value too large for dtype('float64').

Is there any work around for this? I want to bin non null values normally and create an additional bin for null values.

1 Answers

There's a PR to do this, that's stalled out in deciding how to treat the NaN bin for different values of encode; in the interim, you could use/adapt the code there locally.

Related