运行以下代码时出现错误:
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
答案 0 :(得分:0)
函数classify_data
中的行
unique_classes, counts_unique_classes = np.unique(label_column, return_counts=True)
counts_unique_classes
返回一个空的numpy数组。重现此内容的简单方法如下。
import numpy as np
a = np.array([])
a.argmax()
错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: attempt to get argmax of an empty sequence
因此,在运行.argmax()
之前,请添加一些条件,例如
if counts_unique_classes.size == 0:
# Do something
如果您不应该获取空序列/数组,请尝试添加日志以查看传递给classify_data
函数的数据。
答案 1 :(得分:0)
我尝试了此操作,但返回了相同的错误:
def classify_data(data):
label_column = data.values[:, -1]
unique_classes, counts_unique_classes = np.unique(label_column, return_counts=True)
index = counts_unique_classes.argmax()
classification = unique_classes[index]
return classification