12
$\begingroup$

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().

$\endgroup$
10
$\begingroup$

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.

$\endgroup$
4
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.