# Algorithm/Library which applies classification then use classification results to apply (conditional) regression

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)