如何解决我正在尝试在python中实现决策树id3代码不知道这个错误是什么意思
这是我的代码。
def train(x_train,tree=None):
features = x_train.keys()[:-1]
info_gains=[]
for ix in features:
i_gain=info_gain(x_train,ix)
info_gains.append(i_gain)
fkey=features[np.argmax(info_gain)]
print(fkey +" has been selected as root")
if tree is None:
tree={}
tree[fkey] = {}
elt=divide_Dataset(x_train,fkey)
for i in elt:
clValue,counts = np.unique(i['salary_more_then_100k'],return_counts=True)
if len(counts)==1:
tree[fkey][i[fkey].values[0]] = clValue[0]
else:
tree[fkey][i[fkey].values[0]] = train(i)
return tree
这是错误:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/base.py in get_loc(self,key,method,tolerance)
2645 try:
-> 2646 return self._engine.get_loc(key)
2647 except KeyError:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
KeyError: 0
During handling of the above exception,another exception occurred:
KeyError Traceback (most recent call last)
9 frames
/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/base.py in get_loc(self,tolerance)
2646 return self._engine.get_loc(key)
2647 except KeyError:
-> 2648 return self._engine.get_loc(self._maybe_cast_indexer(key))
2649 indexer = self.get_indexer([key],method=method,tolerance=tolerance)
2650 if indexer.ndim > 1 or indexer.size > 1:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
KeyError: 0
我正在尝试在python中实现决策树id3代码。不知道此错误意味着什么。 我认为train(i)的递归步骤中存在一些问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。