Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

I execute the following program and none of the variables in the decision tree function and below show up in the workspace? Am I doing something wrong with how I'm defining and using the functions?

1 view (last 30 days)
load iris.dat
%discretizeData
discretizedData = zeros(150,5);
for i = 1:size(iris,2)
currentColumn = iris(:,i);
bins = length(currentColumn)/4;
discretizedData(:,i) = round(currentColumn/bins)+1;
if(i == 5)
for j = 1: size(iris,1)
if( iris(j,5) == 2 || iris(j,5) == 3)
discretizedData(j,5) = -1;
end
end
end
end
%generate test and train data
posIndex = randperm(50,25);
negIndex = randperm(150,50);
trainData = zeros(75,5);
trainData(1:25,:) = discretizedData(posIndex,:);
trainData(26:75,:) = discretizedData(negIndex,:);
list1=[];
list2=[];
%start of the decision tree algorithm
function e = dctree(trainData,list,dominant)
if size(trainData,1) == 0
list(length(list)+1) = strcat('=>',num2str(dominant));
disp(list); %print the rule at the leaf
e = 1;
end
dominant = dominantLabel(trainData);
nFeatures = size(trainData,2)-1;
levelEntropy = 999;
for i = 1:nFeatures
uniqueValues = unique(trainData(:,i));
nUnique = length(uniqueValues);
for u = 1:nUnique
tposExample = trainData((find(trainData(:,i) == uniqueValues(u) & trainData(:,5) == 1)),:);
tnegExample = trainData((find(trainData(:,i) == uniqueValues(u) & trainData(:,5) == -1)),:);
fposExample = trainData((find(trainData(:,i) ~= uniqueValues(u) & trainData(:,5) == 1)),:);
fnegExample = trainData((find(trainData(:,i) ~= uniqueValues(u) & trainData(:,5) == -1)),:);
tEntropy = entropy(tposExample,tnegExample);
fEntropy = entropy(fposExample,fnegExample);
thisLevelEntropy = (size(tposExample,1)+size(tnegExample,1))*tEntropy + (size(fposExample,1)+size(fnegExample,1))*fEntropy;
if thisLevelEntropy < levelEntropy
levelEntropy = thisLevelEntropy;
bestFeature1 = strcat('f',num2str(i),'=',num2str(u)+'^');
bestFeature2 = strcat('!f',num2str(i),'=',num2str(u)+'^');
end
end
end
list1(length(list)+1) = bestFeature1;
list2(length(list)+1) = bestFeature2;
dctree(vertcat(tposExample,tnegExample),list1,dominant);
dctree(vertcat(fposExample,fnegExample),list2,dominant);
end
%dominant label function
function dm = dominantLabel(trainingData)
posIndices = find(trainingData(:,5)==1);
negIndices = find(trainingData(:,5)==-1);
posCount = size(posIndices,1);
negCount = size(negIndices,1);
if posCount > negCount
dm = 1;
else
dm = -1;
end
end
%entropy function
function en = entropy(tExamples,fExamples)
posCount = size(tExamples,1);
negCount = size(fExamples,1);
pPos = posCount/(posCount+negCount);
pNeg = 1 - pPos;
en = -1*(pPos*log(pPos) + pNeg*log(pNeg));
end
  4 Comments
Stephen23
Stephen23 on 25 Sep 2016
"I read somewhere that there can only be one function in program"
Instead of reading random advice on the internet of unknown accuracy, why not just read the MATLAB documentation, which tells everyone exactly how MATLAB works:
As the documentation clearly states: "Files can include multiple local functions or nested functions"
Mudambi Srivatsa
Mudambi Srivatsa on 27 Sep 2016
I understand that you are trying to execute MATLAB code and the variables in the decision tree function and others below that do not show up in the workspace. I see that you have one MATLAB file "dtree.m" and all the functions reside in it. Prior to MATLAB R2016b, you cannot have functions in your script file. Executing the script would result in the error with the message "Function definitions are not permitted in this context.". However, starting MATLAB R2016b, you can have functions in your script file. If you are using previous releases of MATLAB, you should create separate files for the functions.
I was able to execute the code "dtree.m" in MATLAB R2016b and all the global variables in the script are loaded. I notice that there are no calls to "dctree" function in your script. Did you happen to remove the function calls when you posted the code here, or you do not have them at all? However, note that even with the proper function calls, the local variables will not be seen in the workspace after the function finishes execution.

Answers (0)

This question is closed.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!