问题描述
我有一个 .csv 格式的数据集。它有 136 列和 15036 行。我想计算每列数据集的熵和信息增益。这是我的代码:
def calc_entropy(column):
counts = np.bincount(column)
prob = counts/(len(column))
entropy = 0
for prob in prob:
if prob > 0:
entropy += prob * math.log(prob,2)
return -entropy
def information_gain(data,split,target):
ori_entropy = calc_entropy(data[target])
values = data[split].unique()
left_split = data[data[split]==values[0]]
right_split = data[data[split]==values[1]]
subract = 0
for subset in [left_split,right_split]:
prob = (subset.shape[0])/data.shape[0]
subract += prob * calc_entropy(subset[target])
return ori_entropy - subract
print(calc_entropy(dre[dre.iloc[:,0:136]]))
print(information_gain(dre,dre.iloc[:,0:136],"type"))
但是,我收到错误消息:
File "D:\informatics\FinalProject\Features_Ranking.py",line 59,in <module> print(calc_entropy(dre[dre.iloc[:,0:136]]))
File "C:\Users\ana\anaconda3\lib\site-packages\pandas\core\frame.py",line 2889,in __getitem__ return self.where(key)
File "C:\Users\ana\anaconda3\lib\site-packages\pandas\core\generic.py",line 9004,in where return self.where(
File "C:\Users\ana\anaconda3\lib\site-packages\pandas\core\generic.py",line 8766,in _where raise ValueError(msg.format(dtype=dt))
ValueError: Boolean array expected for the condition,not int64
谢谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)