我的代码是:
def load_data(datafile, categories=None, cat_columns=None):
ohe_categories = 'auto'
if categories and len(categories) > 0:
ohe_categories = categories
ohe = OneHotEncoder(handle_unknown='ignore', categories=ohe_categories)
当categories
为None
时,它可以正常工作。但是,如果我通过某些内容,则会收到错误消息:
ValueError: The truth value of a Index is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
我通过以下方式调用该函数
training_x, training_y, categories, cat_columns = loader.load_data(
'data/training.csv')
test_x, test_y = loader.load_data(
'data/test.csv', categories=categories, cat_columns=cat_columns)
如何正确检查?
答案 0 :(得分:4)
我建议这样做:
def load_data(datafile, categories=None, cat_columns=None):
ohe_categories = 'auto'
if categories is not None:
if len(categories) > 0:
ohe_categories = categories
ohe = OneHotEncoder(handle_unknown='ignore', categories=ohe_categories)
答案 1 :(得分:2)
您要传递的值不支持转换为bool
。在这种情况下,您需要明确检查该值是否不是None
:
if categories is not None: