from evalml.pipelines.components import Imputer
df = pd.DataFrame({"a": [1, 2, 3], "b": ["1", "2", None]})
imputer = Imputer()
imputer.fit(df)
from evalml.pipelines.components import Imputer
df_with_bool = pd.DataFrame({"a": [1, 2, 3], "b": [True, False, None]})
imputer = Imputer()
imputer.fit(df_with_bool)
どちらも同じスタックトレースを持っています:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-69-9af4cfc17aec> in <module>
1 df_with_bool = pd.DataFrame({"a": [1, 2, 3], "b": [True, False, None]})
2 imputer = Imputer()
----> 3 imputer.fit(df_with_bool)
~/sources/evalml/evalml/utils/base_meta.py in _set_fit(self, X, y)
12 @wraps(method)
13 def _set_fit(self, X, y=None):
---> 14 return_value = method(self, X, y)
15 self._is_fitted = True
16 return return_value
~/sources/evalml/evalml/pipelines/components/transformers/imputers/imputer.py in fit(self, X, y)
76 X_categorical = X_null_dropped.select_dtypes(include=categorical_dtypes + boolean)
77 if len(X_categorical.columns) > 0:
---> 78 self._categorical_imputer.fit(X_categorical, y)
79 self._categorical_cols = X_categorical.columns
80 return self
~/sources/evalml/evalml/utils/base_meta.py in _set_fit(self, X, y)
12 @wraps(method)
13 def _set_fit(self, X, y=None):
---> 14 return_value = method(self, X, y)
15 self._is_fitted = True
16 return return_value
~/sources/evalml/evalml/pipelines/components/transformers/imputers/simple_imputer.py in fit(self, X, y)
42 if not isinstance(X, pd.DataFrame):
43 X = pd.DataFrame(X)
---> 44 self._component_obj.fit(X, y)
45 self._all_null_cols = set(X.columns) - set(X.dropna(axis=1, how='all').columns)
46 return self
~/miniconda3/envs/evalml/lib/python3.8/site-packages/sklearn/impute/_base.py in fit(self, X, y)
300 fill_value)
301 else:
--> 302 self.statistics_ = self._dense_fit(X,
303 self.strategy,
304 self.missing_values,
~/miniconda3/envs/evalml/lib/python3.8/site-packages/sklearn/impute/_base.py in _dense_fit(self, X, strategy, missing_values, fill_value)
384 row_mask = np.logical_not(row_mask).astype(np.bool)
385 row = row[row_mask]
--> 386 most_frequent[i] = _most_frequent(row, np.nan, 0)
387
388 return most_frequent
~/miniconda3/envs/evalml/lib/python3.8/site-packages/sklearn/impute/_base.py in _most_frequent(array, extra_value, n_repeat)
40 # has already been NaN-masked.
41 warnings.simplefilter("ignore", RuntimeWarning)
---> 42 mode = stats.mode(array)
43
44 most_frequent_value = mode[0][0]
~/miniconda3/envs/evalml/lib/python3.8/site-packages/scipy/stats/stats.py in mode(a, axis, nan_policy)
498 counts = np.zeros(a_view.shape[:-1], dtype=np.int)
499 for ind in inds:
--> 500 modes[ind], counts[ind] = _mode1D(a_view[ind])
501 newshape = list(a.shape)
502 newshape[axis] = 1
~/miniconda3/envs/evalml/lib/python3.8/site-packages/scipy/stats/stats.py in _mode1D(a)
485
486 def _mode1D(a):
--> 487 vals, cnts = np.unique(a, return_counts=True)
488 return vals[cnts.argmax()], cnts.max()
489
<__array_function__ internals> in unique(*args, **kwargs)
~/miniconda3/envs/evalml/lib/python3.8/site-packages/numpy/lib/arraysetops.py in unique(ar, return_index, return_inverse, return_counts, axis)
259 ar = np.asanyarray(ar)
260 if axis is None:
--> 261 ret = _unique1d(ar, return_index, return_inverse, return_counts)
262 return _unpack_tuple(ret)
263
~/miniconda3/envs/evalml/lib/python3.8/site-packages/numpy/lib/arraysetops.py in _unique1d(ar, return_index, return_inverse, return_counts)
320 aux = ar[perm]
321 else:
--> 322 ar.sort()
323 aux = ar
324 mask = np.empty(aux.shape, dtype=np.bool_)
TypeError: '<' not supported between instances of 'NoneType' and 'bool'
それはこの作品np.nan
の代わりにNone
@freddyaboulton明確な再現者に感謝します! これは別のバグ#1092も説明しているようです。
問題
pandasデータフレームのいずれかの機能にobject
タイプがあり、 None
値が含まれている場合、 Imputer
失敗します。
X = pd.DataFrame({'feature1': [False, True, None, np.nan]})
は、 object
タイプの機能を作成します。 Imputer.fit
失敗します。X = pd.DataFrame({'feature1': [False, True, np.nan]})
は、 object
タイプの機能を作成します。 Imputer.fit
機能します。X = pd.DataFrame({'feature1': [False, True]})
は、 bool
タイプの機能を作成します。 Imputer.fit
機能します。category
タイプについても同じことが言えます。 最後のケースは当てはまりませんが、文字列型についても同様の状況が発生します。
ノート
ここで紛らわしいのは、 None
はさまざまな意味を持つ可能性があるということです。 nan
と同じ場合もあれば、独自のカテゴリとして意図されている場合もあります。
その慣習を文書化して説明する限り、それをnan
として扱うのは問題ないと思います。
回避策
bool / category / string機能からNone
を削除します: df = df.fillna(value=np.nan)
修正
短期:
Imputer
を更新して、 None
をnp.nan
に置き換えますImputer
APIドキュメントとautomlユーザーガイドを更新して、これについて言及します。None
を含めて、 Imputer
テストカバレッジを追加します。代わりに、データにNone
がある場合にエラーとなる、 DataCheck
を追加できます。 しかし、 None
は簡単に変換できるため、これは不要だと感じます。
長期:
新しいDataTable
データ構造を使用するようにevalmlを更新すると、ユーザーは各機能のタイプを事前に構成できるようになります。 これは、標準化によってこの種のエラーが無関係になることを意味することを願っています。
これは#540に関連していますか?
@angela97lin🤦100 %関連しています...実際にはその重複です。 はは。 そこで、代入子にNone
をnp.nan
変換させることも決定しました。
ここでの記事はより最新のものであるため、これを支持して#540を閉じます。
ありがとうございました!