Search code examples
machine-learningclassificationr-caret

Use tested machine learning model on new unlabeled single observation or dataset?


How can I use a trained and tested algorithm (eg. machine learning classifier) after being saved, on a new observation/dataset, whose I do not know the class (eg. ill vs healthy) based on predictors used for model training? I use caret but can't find any lines of code for this. many thanks


Solution

  • After training and testing any machine learning model you can save the model as .rds file and call it as

    #Save the fitted model as .rds file
    saveRDS(model_fit, "model.rds")
    my_model <- readRDS("model.rds")
    

    Creating a new observation from the same dataset or you can use a new dataset also

    new_obs <- iris[100,] #I am using default iris dataset, 100 no sample
    

    Prediction on the new observation

    predicted_new <- predict(my_model, new_obs)
    confusionMatrix(reference = new_obs$Species, data = predicted_new)
    table(new_obs$Species, predicted_new)