gpt4 book ai didi

python - Python 中不常见功能级别的一种热门编码

转载 作者:行者123 更新时间:2023-11-30 09:49:02 25 4
gpt4 key购买 nike

我有一个带有分类因素的模型。我使用 pandas.get_dummies 将其编码为 One Hot Encoding。

不过,分类因素有许多不常见的级别。如果我使用 pandas.get_dummies 重新编码新数据,新列可能会“关闭”,因为新级别不会出现在新数据中。

我正在考虑执行以下操作:

dummies_df = pd.get_dummies(list_of_all_possible_levels)
dummies_df[:] = 0

dummies_df.drop(dummies_df.index[1:], inplace=True)
# If there are 10 levels this becomes a 10x10 Dataframe. I only need
# one 'empty' row and drop everything after the first.


# Let's say the DataFrame looks like this:
df['categorical_factor', 'numeric_factor', 'other_numeric_factor']

# I want to do something where I flag the column of the feature as 1
# and append the one-row dummies_df to each row of df

for cat in df.categorical_factor:
dummies_df[cat] = 1
df['numeric_factor', 'other_numeric_factor'] + dummies_df

我只是很困惑是否应该像这样循环遍历行,或者有更好的“笛卡尔积”类型的答案。如果这是 R,我只会执行 cbind(df, dummies_df),因为 R 知道回收 dummies_df 的值。

或者也许我应该在新数据上使用 pandas.get_dummies 并将缺少的级别加入为新列,如下所示:

new_dat['missing_level_1'] = [0 for _ in new_dat.index]
new_dat['missing_level_2'] = [0 for _ in new_dat.index]

编辑:示例数据

levels=['level_1', 'level_2', 'level_3']

A = [0,1,2]
B = [3,4,5]

df = pd.DataFrame({'levels': levels, 'A': A, 'B': B})

df = df.drop('levels', axis=1).join(pd.get_dummies(df.levels))



new_levels=['level_1', 'level_2', 'level_2']

new_A = [5,6,7]
new_B = [8,9,7]

new_df = pd.DataFrame({'levels': new_levels, 'A': new_A, 'B': new_B})

new_df = new_df.drop('levels', axis=1).join(pd.get_dummies(new_df.levels))

df 现在是

+---------+---+---+---------+---------+---------+
| (index) | A | B | level_1 | level_2 | level_3 |
+---------+---+---+---------+---------+---------+
| 0 | 0 | 3 | 1 | 0 | 0 |
| 1 | 1 | 4 | 0 | 1 | 0 |
| 2 | 2 | 5 | 0 | 0 | 1 |
+---------+---+---+---------+---------+---------+

并且new_df现在是

+---------+---+---+---------+---------+
| (index) | A | B | level_1 | level_2 |
+---------+---+---+---------+---------+
| 0 | 5 | 8 | 1 | 0 |
| 1 | 6 | 9 | 0 | 1 |
| 2 | 7 | 7 | 0 | 1 |
+---------+---+---+---------+---------+

(缺少 level_3 列。)

我希望 new_df 成为

+---------+---+---+---------+---------+---------+
| (index) | A | B | level_1 | level_2 | level_3 |
+---------+---+---+---------+---------+---------+
| 0 | 5 | 8 | 1 | 0 | 0 |
| 1 | 6 | 9 | 0 | 1 | 0 |
| 2 | 7 | 7 | 0 | 1 | 0 |
+---------+---+---+---------+---------+---------+

最佳答案

最稳定的解决方案是重新索引虚拟数据帧。

当您对第一个(原型(prototype))数据帧进行编码时,您会记住虚拟列的列表:

# the initial encoding
levels=['level_1', 'level_2', 'level_3']
df_original = pd.DataFrame({'levels': levels, 'A': [0,1,2], 'B': [3,4,5]})
dummies = pd.get_dummies(df_original.levels)
df = df_original.drop('levels', axis=1).join(dummies)
# remember the levels and their order
dummy_columns = list(dummies.columns)

之后,您强制新的虚拟数据框具有相同的列:

# encoding another dataframe
new_levels=['level_1', 'level_2', 'level_2']
new_df_original = pd.DataFrame({'levels': new_levels, 'A': [5,6,7], 'B': [8,9,7]})
# this is where I use the remembered information
new_dummies = pd.get_dummies(new_df_original.levels). \
reindex(columns=dummy_columns).fillna(0).astype(int)
new_df = new_df_original.drop('levels', axis=1).join(new_dummies)
print(new_df)

它给出了您想要的结果:

   A  B  level_1  level_2  level_3
0 5 8 1 0 0
1 6 9 0 1 0
2 7 7 0 1 0

关于python - Python 中不常见功能级别的一种热门编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48310058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com