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
the cyclist 2020-3-18
You should learn how to use the debugger.
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.
phdcomputer Eng
phdcomputer Eng 2020-3-21
编辑:phdcomputer Eng 2020-3-21
@the cyclist great thanks for your useful comment, That's right, by using breakpoints in Matlab, all the variables tp_dt, tn_dt, fp_dt, and fn_dt are computed zero and ADT=0/0=NaN. why all tp, tn, fp, fn are zero? DTree function doesn't return values of 1 or 2, it returns floating point numbers in the order of magnitude 1e3. How can I convert the output of the function to 1 or 2?
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 Its result is NaN, I used this classifier function for other programs and It worked well.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile 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!

Translated by