Mathematica:零件分配 [英] Mathematica: part assignment
问题描述
我正在尝试实施一种算法,以根据数据集构建决策树。
我编写了一个函数来计算子集和特定分区之间的信息增益,然后尝试所有可能的分区,并希望选择最佳分区,因为它具有最低的熵。
此过程必须是递归的,因此,在第一次迭代后,它需要对上一步中获得的分区的每个子集都起作用。
I'm trying to implement an algorithm to build a decision tree from a dataset. I wrote a function to calculate the information gain between a subset and a particular partition, then I try all the possible partition and want to choose the "best" partition, in the sense that it's got the lowest entropy. This procedure must be recursive, hence, after the first iteration, it needs to work for every subset of the partition you got in the previous step.
这些是数据:
X = {{1, 0, 1, 1}, {1, 1, 1, 1}, {0, 1, 1, 1}, {1, 1, 1, 0}, {1, 1, 0, 0}}
Xfin[0]=X
此功能:对于分区的每个子集,它将尝试所有可能的分区并计算 IG
。然后使用 IGMAX
选择分区:
This is the function: for every subset of the partition, it tries all the possible partitions and calculate the IG
. Then it selects the partition with IGMAX
:
Partizioneottimale[X_, n_] :=
For[l = 1, l <= Length[Flatten[X[n], n - 1]], l++,
For[v = 1, v <= m, v++,
If[IG[X[n][[l]], Partizione[X[n][[l]], v]] == IGMAX[X[n][[l]]],
X[n + 1][[l]] := Partizione[X[n][[l]], v]]]]
然后我称之为:
Partizioneottimale[Xfin, 0]
,第一个也可以正常工作:
and it works fine for the first one:
Xfin[1]
{{{1, 0, 1, 1}, {1, 1, 1, 1}, {0, 1, 1, 1}, {1, 1, 1, 0}}, {{1, 0, 0, 0}}}
这是熵最低的分区。
但不适用于下一个:
Partizioneottimale[Xfin, 1]
Set delayed::steps : Xfin[1+1] in the part assignment is not a symbol
有人对如何解决这个问题有任何想法吗?
谢谢
Has anybody any idea about how to solve this? Thanks
推荐答案
而无需弄清所有逻辑,一个简单的解决方法是:
without unraveling all your logic a simple fix is this:
Partizioneottimale[X_, n_] := (
xnp1 = Table[Null, {Length[Flatten[X[n], n - 1]]}] ;
For[l = 1, l <= Length[Flatten[X[n], n - 1]], l++,
For[v = 1, v <= m, v++,
If[IG[X[n][[l]], Partizione[X[n][[l]], v]] == IGMAX[X[n][[l]]],
xnp1[[l]] = Partizione[X[n][[l]], v]]]] ;
X[n+1] = xnp1 ; )
这篇关于Mathematica:零件分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!