Can someone explain the error? Function with duplicate name dtree cannot be defined
3 views (last 30 days)
Show older comments
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 = dtree(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;
dtree(vertcat(tposExample,tnegExample),list1,dominant);
dtree(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
0 Comments
Answers (1)
Walter Roberson
on 26 Sep 2016
If all of those are saved into the same .m file, then the combination of script and functions would only be permitted in R2016b or later.
In R2016b it would be permitted, but you would need to name the script something other than dtree.m : when you use a function in a script in R2016b or later, you cannot name the function the same thing as the script.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!