predict function in Tree package

I have classification tree in R, I tried cross validation by:

cv.tree1<-cv.tree(tree1)

Then I tried

tree3 = prune.tree(tree1, best=15)

Then I am trying to predict all my labels from the current tree:

predict(tree3, data.train[1,])

The output is:

            0         1          2          3           4           5           6          7          8
1 0.0006247397 0.8531862 0.03706789 0.02207414 0.003123698 0.008746356 0.009371095 0.00728863 0.05310287
            9
1 0.005414411

As I understand it gives me the probability of each label since I have 10 labels here 0:9 So I am trying to get max of last statement for prediction of all labels

predict.list <-matrix(0,nrow=nrow(data.train),ncol=10)
for (index in c(1:nrow(digits.train)))
{
  predict.list[index]<-predict(tree3, data.train[index,])
}

and then I tried to get max of each row in predict.list, but actually this doesn't work So I tried to see the structure of str(predict(tree3, data.train[index,])) I found it's

   num [1, 1:10] 0.00656 0.00583 0.00947 0.07479 0.14813 ...
  - attr(*, "dimnames")=List of 2
  ..$ : chr "8184"
  ..$ : chr [1:10] "0" "1" "2" "3" ...

So the question is, Am I right in getting max. of each one so by that way I got the prediction, and how I can get the max with corresponding label

I can get the max by

max(predict(tree3, digits.train[1,]))

but I can't get the corresponding label

Answers


The predict.tree() function has an argument called type. Its default value is "vector", which in case of a classification tree will return a vector containing the class probabilities for each of your observation row. You can change it to "class" and it will only return the class with the highest probability. In your case using

predict.list <- predict(tree3, data.train, type="class")

will return a factor vector of length nrow(data.train) with each value being the factor level that was predicted for the corresponding row.


Need Your Help

Silverlight - working with files

c# silverlight visual-studio-2010

I want to create a Silverlight (Framework 4.0, C# and Visual Studio 2010) application that loads some XML configuration files from the local directory of the application. I need that the code loads...

Any other attributes other than layout_weight that will make one element take more space than the others?

android tablerow android-layout-weight

This question is based off my other one. Why is each element in the table row still taking the same amount of space

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.