【问题标题】:Matlab function for entropy in decision trees决策树中熵的Matlab函数
【发布时间】:2015-04-05 19:26:22
【问题描述】:

你好,Matlab 大师!

一个月前我开始在某个地方学习 MATLAB(在我的试用许可证过期后,我切换到了 octave)。我正在编写一个函数(仅用于教育需要)来计算熵(例如在决策树的叶子中),但我被卡住了。我收到以下错误:

>> my_entropy(cell3, false)
f = -0
f =

  -0  -0

f =

  -0  -0   3

error: my_entropy: A(I,J): column index out of bounds; value 2 out of bound 1
error: called from:
error:   C:\big data\octave\my_entropy.m at line 29, column 13

将 5.04.15 更新为 @Daniel 建议

# The main difference between MATLAB bundled entropy function
# and this custom function is that they use a transformation to uint8
# and the bundled entropy() function is used mostly for signal processing
# while I simply use a straightforward solution usefull e.g. for learning trees

function f = my_entropy(data, weighted)
  # function accepts only cell arrays;
  # weighted tells whether return one weighed average entropy
  # or return a vector of entropies per bucket
  # moreover, I find vectors as the only representation of "buckets"
  # in other words, vector = bucket (leaf of decision tree)
  if nargin < 2
    weighted = true;
  end;

  rows = @(x) size(x,1);
  cols = @(x) size(x,2);

  if weighted
    f = 0;
  else
    f = [];
  end;

  for r = 1:rows(data)

    for c = 1:cols(data{r}) # in most cases this will be 1:1

      omega = sum(data{r,c});
      epsilon = 0;

      for b = 1:cols(data{r,c})
        epsilon = epsilon + ( (data{r,c}(b) / omega) * (log2(data{r,c}(b) / omega)) );
      end;

      entropy = -epsilon;

      if weighted
        f = f + entropy
      else
        f = [f entropy]
      end;

    end;

  end;

end;

# test cases

cell1 = { [16];[16];[2 2 2 2 2 2 2 2];[12];[16] }
cell2 = { [16],[12];[16],[2];[2 2 2 2 2 2 2 2],[8 8];[12],[8 8];[16],[8 8] }
cell3 = { [16];[16];[2 2 2 2 2 2 2 2];[12];[16] }

输入

c = { [16];[16];[2 2 2 2 2 2 2 2];[12];[16] }

my_entropy(c, false) 的答案 应该是

[0, 0, 3, 0, 0]

这张图片可以帮助形象化

一个桶是一个matlab向量,整个调色板是一个matlab单元表, 数字是不同的各种数据的计数。因此,在这张图片中,中间单元格 {2,2} 的熵为 3,而其他桶(单元格)的熵为 0。

感谢您提供建议如何解决此问题的帮助, 此致! :)

【问题讨论】:

    标签: matlab octave


    【解决方案1】:

    错误在这里for c = 1:cols(cell{r})

    你想要单元格的列数,这是cols(cell)。你写的返回单元格的第 r 个元素的列数。

    您应该避免使用与 cell 等内置函数相同的变量名称

    【讨论】:

    • 嗯。这就是我正在做的,对于工作表的每个 i;j 单元格处理此单元格中的数据(1 个向量)。至于命名约定,我相应地更新了代码。
    • 试试for c = 1:cols(cell),我得到了预期的结果。
    猜你喜欢
    • 2021-05-27
    • 2021-11-14
    • 2010-12-29
    • 2013-02-07
    • 2016-05-11
    • 2018-05-04
    • 2010-11-11
    • 2014-05-27
    • 2013-06-03
    相关资源
    最近更新 更多