运行以下代码时出现错误:
def isolation_tree(data, counter=0, max_depth=50, random_subspace=False):
# End Loop
if (counter == max_depth) or data.shape[0]<=1:
classification = classify_data(data)
return classification
else:
# Counter
counter +=1
# Select feature
split_column = select_feature(data)
# Select value
split_value = select_value(data, split_column)
# Split data
data_below, data_above = split_data(data, split_column, split_value)
# instantiate sub-tree
question = "{} <= {}".format(split_column, split_value)
sub_tree = {question: []}
# Recursive part
below_answer = isolation_tree(data_below, counter, max_depth=max_depth)
above_answer = isolation_tree(data_above, counter, max_depth=max_depth)
if below_answer == above_answer:
sub_tree = below_answer
else:
sub_tree[question].append(below_answer)
sub_tree[question].append(above_answer)
return sub_tree
tree = isolation_tree(df.head(6), max_depth=1)
pprint(tree)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-25-487c502563a5> in <module>
----> 1 tree = isolation_tree(df.head(256), max_depth=1)
2 pprint(tree)
<ipython-input-24-d71e3aed4b2a> in isolation_tree(data, counter, max_depth, random_subspace)
25 # Recursive part
26 below_answer = isolation_tree(data_below, counter,max_depth=max_depth)
---> 27 above_answer = isolation_tree(data_above, counter,max_depth=max_depth)
28
29 if below_answer == above_answer:
<ipython-input-24-d71e3aed4b2a> in isolation_tree(data, counter, max_depth, random_subspace)
3 # End Loop
4 if (counter == max_depth) or data.shape[0]<=1:
----> 5 classification = classify_data(data)
6 return classification
7
<ipython-input-15-26b1d48eb27a> in classify_data(data)
4 unique_classes, counts_unique_classes = np.unique(label_column, return_counts=True)
5
----> 6 index = counts_unique_classes.argmax()
7 classification = unique_classes[index]
8
ValueError: attempt to get argmax of an empty sequence
我尝试了此操作,但返回了相同的错误:
在函数
classify_data
中,在行中counts_unique_classes
正在返回一个空的numpy数组。简单的复制方法如下错误
因此,在运行
.argmax()
之前,添加一些条件,如如果不应该获取空序列/数组,请尝试添加日志以查看传递给
classify_data
函数的数据是什么相关问题 更多 >
编程相关推荐