OrdinalEncoder の挙動についてのメモ
sklearn.preprocessing.OrdinalEncoder - scikit-learn 0.24.2 documentation
The input to this transformer should be an array-like of integers or strings, denoting the values taken on by categorical (discrete) features. The features are converted to ordinal integers. This results in a single column of integers (0 to n_categories - 1) per feature.
scikit-learn.org
カテゴリカル feature を int にします
>>> df = pd.DataFrame([1,2,3,2,1])
>>> oe = OrdinalEncoder(categories='auto', dtype=np.int64)
>>> oe.fit(df)
>>> oe.transform(df)
array([[0],
[1],
[2],
[1],
[0]])
>>> oe.categories_
[array([1, 2, 3])]カテゴリーの型は全て同じである必要がある
必要があれば value を str に変換するなど必要
df = pd.DataFrame([1,2,3,2,1]).astype('str') # astype(str) すれば問題ない
categories = [['1', '2', '3', 'na']]
oe = OrdinalEncoder(categories=categories, dtype=np.int64)
oe.fit(df)
oe.transform(df)