How to convert floating-point numbers to integer values for comparison with each other?
1 次查看(过去 30 天)
显示 更早的评论
I want to classify a dataset by using Decision Tree(DT) to compute the accuracy, for accuracy computation , we compare the result of DTree with the class labels 1 or 2, but the problem is that DTree function returns floating point numbers in the order of magnitude 1e3. the result of DT classifier was obtained:
DT =
1.0e+03 *
1.5311
1.2482
3.0774
1.2482
1.0627
1.5311
2.6613
3.3919
1.3951
1.2482
3.3919
1.2482
I compared DT(i) (the result of a decision tree) with ytest(i) (the last column of test data that are class labels) to identify where the real result is equal to ideal result for computing the accuracy of the classifier by using TP, TN, FN, FP.
For example, TP(True Positive) when we correctly identified the instance belongs to the class 1 (this is our observation from the result of the classifier) and also the instance's label in the test data is 1 (the ideal result) so one unit is added to TP. By computing the TP, TN, FP, FN we use them in the accuracy formula accuracy=(TP+TN)/(TP+TN+FN+FP) that It's range is [0 1] , but ADT=0/0=NaN because the variables tp_dt, tn_dt, fp_dt, and fn_dt are computed zero.
How can I convert the output of the DTree function to integer values 1 or 2?
I'll be vary gratefull to have your opinions how to solve this problem. Thanks so much.
clc;
clear;
close all;
load colon.mat
data=colon;
[n,m]=size(data);
for a=1:n
if data(a,m)==0
data(a,m)=2;
end
end
S=[1630,1,878,1810,22,1955,2,2000,6,306,1901,9,1921,26,807,1905,16,1895,357,4,1714,10,1973,119,3,1928,11,1951,317,7];
data0=data(:,S);
rows=(1:n);
test_count=floor((0.2)*n);
[n,m]=size(data0);
test_rows=randsample(rows,test_count);
train_rows=setdiff(rows,test_rows);
test=data0(test_rows,:);
train=data0(train_rows,:);
xtest=test(:,1:m-1);
ytest=test(:,m);
xtrain=train(:,1:m-1);
ytrain=train(:,m);
DT=DTree(xtest,xtrain,ytrain);
tp_dt=0;tn_dt=0;fp_dt=0;fn_dt=0;
for i=1:test_count
if(DT(i)==1 && ytest(i)==1)
tp_dt=tp_dt+1;
end
if(DT(i)==2 && ytest(i)==2)
tn_dt=tn_dt+1;
end
if(DT(i)==2 && ytest(i)==1)
fp_dt=fp_dt+1;
end
if(DT(i)==1 && ytest(i)==2)
fn_dt=fn_dt+1;
end
end
ADT=(tp_dt+tn_dt)/(tp_dt+tn_dt+fp_dt+fn_dt);
disp('Accuracy'); disp(ADT);
DTree classifier function is:
function ppred=DTree (xtest,xtrain,ytrain)
DTreeModel=ClassificationTree.fit(xtrain,ytrain);
ppred=DTreeModel.predict(xtest);
end
2 个评论
the cyclist
2020-3-18
Using it, I can tell you that all of your variables like
tp_dt
are still 0 at the end of the program.
None of your if statements are entered. This is because none of the values in DT are exactly equal to integers, which is what you are checking.
Beyond that, I am not sure what you intended or expected.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Get Started with Statistics and Machine Learning Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!