问题描述
这是我的代码。
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 (将#修改为@)