首页 > 解决方案 > TypeError: unhashable type: 'list' 添加交互功能时

问题描述

在 Kaggle 完成特征工程课程后,我现在正尝试将其应用于房价竞赛。我为分类变量生成了交互特征:

interactions = pd.DataFrame(index=X_full.index)

# Iterate through each pair of features, combine them into interaction features
for col1, col2 in itertools.combinations(categorical_cols, 2):
        new_col_name = '_'.join([col1, col2])

        # Convert to strings and combine
        new_values = X_full[col1].map(str) + "_" + X_full[col2].map(str)

        encoder = preprocessing.LabelEncoder()
        interactions[new_col_name] = encoder.fit_transform(new_values)

interactions_list = [interactions.columns.values.tolist()] + interactions.values.tolist()

我将数据框转换为列表,因为我想将它们添加到我已经拥有的分类和数字特征中,这些特征是列表格式:

# Keep selected columns only
my_cols = categorical_cols + numerical_cols + interactions_list
X_train = X_train_full[my_cols].copy()
#X_valid = X_valid_full[my_cols].copy()
#X_test = X_test_full[my_cols].copy()

但是当我尝试将这些与我得到的其他功能结合起来时TypeError: unhashable type: 'list'。当我从 my_cols 中注释掉交互列表时,没有问题,一切运行正常。谁能帮我理解这一点?

标签: pythoninteractionkagglefeature-engineering

解决方案


推荐阅读