属于同一类别的多个列上的 OneHotEncoder
OneHotEncoder on multiple columns belonging to same categories
我有多个由分类变量组成的列,这些变量的形式是 0-4 之间的整数值。但是,所有列都属于同一类别。我尝试使用 scikit learn 中的 OneHotEncoder,但它不会处理列中缺失的类别,这会在我测试我的神经网络模型上看不见的数据时导致问题。下面的代码显示了我需要编码的数据类型
>>> df = pd.DataFrame(np.random.randint(low=0, high=4, size=(5, 5)),
columns=['color1', 'color2', 'color3', 'color4', 'color5'])
>>> df
color1 color2 color3 color4 color5
0 0 1 2 3 1
1 3 1 0 1 1
2 0 1 0 3 0
3 0 2 0 1 2
4 0 2 0 3 2
>>> df_onehotencoder = OneHotEncoder(sparse=False)
>>> df2 = df_onehotencoder.fit_transform(df)
>>> df2
array([[1., 0., 1., 0., 0., 1., 0., 1., 0., 1., 0.],
[0., 1., 1., 0., 1., 0., 1., 0., 0., 1., 0.],
[1., 0., 1., 0., 1., 0., 0., 1., 1., 0., 0.],
[1., 0., 0., 1., 1., 0., 1., 0., 0., 0., 1.],
[1., 0., 0., 1., 1., 0., 0., 1., 0., 0., 1.]])
这仅为每一列中存在的类别生成数组,而不是为缺失的类别生成数组。我需要为每一列设置相同数量的编码列,即缺少的类别将全为零。另外,解码这个 OneHotEncoded 数组的最佳选择是什么,这样我就可以轻松地将预测输出解码为实际整数值。
从 sklearn==0.20
开始,OneHotEncoder 具有 categories
参数,您可以在其中提供包含给定列的所有可能值的列表列表。
import pandas as pd
df = pd.DataFrame([[0, 1, 2, 3, 1],
[3, 1, 0, 1, 1],
[0, 1, 0, 3, 0],
[0, 2, 0, 1, 2],
[0, 2, 0, 3, 2]], columns=['color1', 'color2', 'color3', 'color4', 'color5'])
from sklearn.preprocessing import OrdinalEncoder, OneHotEncoder
# Get all the unique values if we don't have them
unique_values = pd.unique(df.values.ravel())
ohe = OneHotEncoder(categories=[unique_values]*df.shape[1], sparse=False)
encoded = pd.DataFrame(ohe.fit_transform(
df), columns=ohe.get_feature_names(df.columns))
>>> encoded
color1_0 color1_1 color1_2 color1_3 color2_0 color2_1 0
0 1.0 0.0 0.0 0.0 0.0 1.0 ...
1 0.0 0.0 0.0 1.0 0.0 1.0 ...
2 1.0 0.0 0.0 0.0 0.0 1.0 ...
3 1.0 0.0 0.0 0.0 0.0 0.0 ...
4 1.0 0.0 0.0 0.0 0.0 0.0 ...
要找回原来的 类 你可以 inverse_transform
:
>>> ohe.inverse_transform(encoded)
array([[0, 1, 2, 3, 1],
[3, 1, 0, 1, 1],
[0, 1, 0, 3, 0],
[0, 2, 0, 1, 2],
[0, 2, 0, 3, 2]], dtype=int64)
我有多个由分类变量组成的列,这些变量的形式是 0-4 之间的整数值。但是,所有列都属于同一类别。我尝试使用 scikit learn 中的 OneHotEncoder,但它不会处理列中缺失的类别,这会在我测试我的神经网络模型上看不见的数据时导致问题。下面的代码显示了我需要编码的数据类型
>>> df = pd.DataFrame(np.random.randint(low=0, high=4, size=(5, 5)),
columns=['color1', 'color2', 'color3', 'color4', 'color5'])
>>> df
color1 color2 color3 color4 color5
0 0 1 2 3 1
1 3 1 0 1 1
2 0 1 0 3 0
3 0 2 0 1 2
4 0 2 0 3 2
>>> df_onehotencoder = OneHotEncoder(sparse=False)
>>> df2 = df_onehotencoder.fit_transform(df)
>>> df2
array([[1., 0., 1., 0., 0., 1., 0., 1., 0., 1., 0.],
[0., 1., 1., 0., 1., 0., 1., 0., 0., 1., 0.],
[1., 0., 1., 0., 1., 0., 0., 1., 1., 0., 0.],
[1., 0., 0., 1., 1., 0., 1., 0., 0., 0., 1.],
[1., 0., 0., 1., 1., 0., 0., 1., 0., 0., 1.]])
这仅为每一列中存在的类别生成数组,而不是为缺失的类别生成数组。我需要为每一列设置相同数量的编码列,即缺少的类别将全为零。另外,解码这个 OneHotEncoded 数组的最佳选择是什么,这样我就可以轻松地将预测输出解码为实际整数值。
从 sklearn==0.20
开始,OneHotEncoder 具有 categories
参数,您可以在其中提供包含给定列的所有可能值的列表列表。
import pandas as pd
df = pd.DataFrame([[0, 1, 2, 3, 1],
[3, 1, 0, 1, 1],
[0, 1, 0, 3, 0],
[0, 2, 0, 1, 2],
[0, 2, 0, 3, 2]], columns=['color1', 'color2', 'color3', 'color4', 'color5'])
from sklearn.preprocessing import OrdinalEncoder, OneHotEncoder
# Get all the unique values if we don't have them
unique_values = pd.unique(df.values.ravel())
ohe = OneHotEncoder(categories=[unique_values]*df.shape[1], sparse=False)
encoded = pd.DataFrame(ohe.fit_transform(
df), columns=ohe.get_feature_names(df.columns))
>>> encoded
color1_0 color1_1 color1_2 color1_3 color2_0 color2_1 0
0 1.0 0.0 0.0 0.0 0.0 1.0 ...
1 0.0 0.0 0.0 1.0 0.0 1.0 ...
2 1.0 0.0 0.0 0.0 0.0 1.0 ...
3 1.0 0.0 0.0 0.0 0.0 0.0 ...
4 1.0 0.0 0.0 0.0 0.0 0.0 ...
要找回原来的 类 你可以 inverse_transform
:
>>> ohe.inverse_transform(encoded)
array([[0, 1, 2, 3, 1],
[3, 1, 0, 1, 1],
[0, 1, 0, 3, 0],
[0, 2, 0, 1, 2],
[0, 2, 0, 3, 2]], dtype=int64)