# randomForest chooses regression instead of classification

I'm using the randomForest package in R and using the iris data, the random forest generated is a classification but when I use a dataset with around 700 features (the features are each pixel in a 28x28 pixel image) and the label column is named label, the randomForest generated is regression. I'm using the following line:

rf <- randomForest(label ~ ., data=train)


How come regression is used instead of classification? The data is read in through read.csv().

-
add comment

## 2 Answers

randomForest will default to classification or regression depending on the class of the variable. So if you type

class(iris\$Species)


you will see that it is a factor. 'label' in your code, is most likely numeric, so randomForest defaults to regression. You will need to convert it to a factor for classification. You can convert it or read it in as a factor by setting colClasses in read.table.

-
add comment

Most often this is because you did not tell R that label is a categorical variable. The read.csv function tries to guess what type to use for each column and if it looks like a numeric column then that is what it will use. You can check how R stores the variable using the str function. You can force read.csv to read the variable as a factor (or numeric, or ...) using the colClasses argument. Or you can change labels to a factor after reading it in using the factor function.

If this is not the case then we need more information about your data. The results from running str on your data frame would probably be useful.

-
add comment