0
$\begingroup$

I have a project where I'd like to predict two labels and due to the amount of data available (300~), I'd like to stay away from Neural Networks. Let's take the iris dataset for example. The labels that I want to train with and predict are Species (Classification problem) then Petal.Length (Regression problem). I say then because I'd like to be more precise in predicting Petal.Length. So, if the classifier predicted 'setosa', I'd only run a regression model independently trained on 'setosa' class. Is that reasoning sound, or should I be using 'Species' as a regressor?

Either way, I am trying to find an algorithm/package/method for me in either R or Python, without having to build classification and regression models separately. Below is the R-code on what I'm trying to do:

library(caret)
df <- iris
index = createDataPartition(y=df$Species, p=0.75, list=FALSE)
train = df[index,]
test = df[-index,]
rf_model = train(Species ~ Sepal.Length + Sepal.Width + Petal.Width , data = train, method = 'rf', trControl = trainControl(method="cv", number=5))
lr_model = list(train(Petal.Length ~ Sepal.Length + Sepal.Width + Petal.Width + (Species == 'setosa') , data = train, method = 'lm', trControl = trainControl(method="cv", number=5)),
                train(Petal.Length ~ Sepal.Length + Sepal.Width + Petal.Width + (Species == 'versicolor') , data = train, method = 'lm', trControl = trainControl(method="cv", number=5)),
                train(Petal.Length ~ Sepal.Length + Sepal.Width + Petal.Width + (Species == 'virginica') , data = train, method = 'lm', trControl = trainControl(method="cv", number=5)))
                
regress_results <- function(test, model, model2){
  test <- test[,c(-3,-5)]
  x <- as.character(predict(model, test))
  test$Species <- x
  y <- c()
  for (i in 1:nrow(test)){
    if (test[i,4] == 'setosa') {y[i] <- predict(model2[1],test[i,])}
    if (test[i,4] == 'versicolor') {y[i] <- predict(model2[2],test[i,])}
    if (test[i,4] == 'virginica') {y[i] <- predict(model2[3],test[i,])}
  }
  names(y) <- x
  return(unlist(y))
}
regress_results(test, rf_model, lr_model)
$\endgroup$

1 Answer 1

1
$\begingroup$

It sounds like you don’t actually want to predict the class membership.

You can run a regression that uses the species as a feature; this is completely routine. Note that you might want to interact your continuous variables with the categorical variable.

$\endgroup$
2
  • $\begingroup$ I do want to predict class membership. I'd like to predict class then choose which regression model depending on that class. $\endgroup$
    – purple1437
    Nov 2, 2022 at 16:47
  • 1
    $\begingroup$ Why? Your ultimate goal is to predict something using a regression. Why the step in the middle? $\endgroup$
    – Dave
    Nov 2, 2022 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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