Matlab Nested Classes, Reference to object of the same class from within -
i trying create tree class in matlab creation of decision tree. when try run script, first time runs through code, can see arrived @ favorable value of f, , v. however, after part of initialization of new nodes left , right, current object empty. how correctly nest reference same class within class such not interfere each other
classdef dtree properties (access = public) maxdepth; currentdepth; features; left; right; f; v; end methods function this=dtree(md, cd, nf, xtrain, ytrain) % real initialization begins this.maxdepth = md this.currentdepth = cd; this.features = nf; this.train(xtrain, ytrain); end function s = gini(dt, labels) s = 1; s = s - (sum(labels > 0.0) ./ numel(labels)) ^ 2; s = s - (sum(labels < 0.0) ./ numel(labels)) ^ 2; end function train(dt, xtrain, ytrain) if (size(unique(ytrain)) == 1 | dt.currentdepth > dt.maxdepth) return; end mingini = inf; minf = 0; minv = inf; = dt.features n = 1:size(xtrain, 1) idx = xtrain(:, i) > xtrain(n, i); gini = dt.gini(ytrain(idx)) + dt.gini(ytrain(~idx)); if gini < mingini mingini = gini; minf = i; minv = xtrain(n, i); end end end dt.f = minf dt.v = minv lidx = xtrain(:, dt.f) > xtrain(dt.v, dt.f); dt.left = dtree(dt.maxdepth, dt.currentdepth + 1, dt.features,xtrain(lidx, :), ytrain(lidx)) dt.right = dtree(dt.maxdepth, dt.currentdepth + 1, dt.features, xtrain(~lidx, :), ytrain(~lidx)); end end
end
ans = dtree(1, 1, [2, 5 ,6], xtrain, ytrain);
during execution, dtree properties:
maxdepth: 1 currentdepth: 1 features: [4 5 2] left: [] right: [] f: 5 v: 7
after execution, when type ans ans =
dtree properties:
maxdepth: 1 currentdepth: 1 features: [4 5 2] left: [] right: [] f: [] v: []
which empty object prior running train.
when deal class methods, need explicitly pass , return object
function = dtree(...) = this.train(...) end function dt = train(dt, xtrain, ytrain) ... end
if don't this, reference object have not updated. need update reference in matlab!
Comments
Post a Comment