Your First Machine Learning Project in R Step-By-Step (tutorial and template for future projects)

Do you want to do machine learning using R, but you’re having trouble getting started?

In this post you will complete your first machine learning project using R.

In this step-by-step tutorial you will:

  1. Download and install R and get the most useful package for machine learning in R.
  2. Load a dataset and understand it’s structure using statistical summaries and data visualization.
  3. Create 5 machine learning models, pick the best and build confidence that the accuracy is reliable.

If you are a machine learning beginner and looking to finally get started using R, this tutorial was designed for you.

Let’s get started!

Your First Machine Learning Project in R Step-by-Step

Your First Machine Learning Project in R Step-by-Step
Photo by Henry Burrows, some rights reserved.

How Do You Start Machine Learning in R?

The best way to learn machine learning is by designing and completing small projects.

R Can Be Intimidating When Getting Started

R provides a scripting language with an odd syntax. There are also hundreds of packages and thousands of functions to choose from, providing multiple ways to do each task. It can feel overwhelming.

The best way to get started using R for machine learning is to complete a project.

  • It will force you to install and start R (at the very least).
  • It will given you a bird’s eye view of how to step through a small project.
  • It will give you confidence, maybe to go on to your own small projects.

Beginners Need A Small End-to-End Project

Books and courses are frustrating. They give you lots of recipes and snippets, but you never get to see how they all fit together.

When you are applying machine learning to your own datasets, you are working on a project.

The process of a machine learning project may not be linear, but there are a number of well-known steps:

  1. Define Problem.
  2. Prepare Data.
  3. Evaluate Algorithms.
  4. Improve Results.
  5. Present Results.

For more information on the steps in a machine learning project see this checklist and more on the process.

The best way to really come to terms with a new platform or tool is to work through a machine learning project end-to-end and cover the key steps. Namely, from loading data, summarizing your data, evaluating algorithms and making some predictions.

If you can do that, you have a template that you can use on dataset after dataset. You can fill in the gaps such as further data preparation and improving result tasks later, once you have more confidence.

Hello World of Machine Learning

The best small project to start with on a new tool is the classification of iris flowers (e.g. the iris dataset).

This is a good project because it is so well understood.

  • Attributes are numeric so you have to figure out how to load and handle data.
  • It is a classification problem, allowing you to practice with perhaps an easier type of supervised learning algorithms.
  • It is a mutli-class classification problem (multi-nominal) that may require some specialized handling.
  • It only has 4 attribute and 150 rows, meaning it is small and easily fits into memory (and a screen or A4 page).
  • All of the numeric attributes are in the same units and the same scale not requiring any special scaling or transforms to get started.

Let’s get started with your hello world machine learning project in R.

Need more Help with R for Machine Learning?

Take my free 14-day email course and discover how to use R on your project (with sample code).

Click to sign-up and also get a free PDF Ebook version of the course.

Start Your FREE Mini-Course Now!

Machine Learning in R: Step-By-Step Tutorial (start here)

In this section we are going to work through a small machine learning project end-to-end.

Here is an overview what we are going to cover:

  1. Installing the R platform.
  2. Loading the dataset.
  3. Summarizing the dataset.
  4. Visualizing the dataset.
  5. Evaluating some algorithms.
  6. Making some predictions.

Take your time. Work through each step.

Try to type in the commands yourself or copy-and-paste the commands to speed things up.

Any questions, please leave a comment at the bottom of the post.

1. Downloading Installing and Starting R

Get the R platform installed on your system if it is not already.

UPDATE: This tutorial was written and tested with R version 3.2.3. It is recommend that you use this version of R or higher.

I do not want to cover this in great detail, because others already have. This is already pretty straight forward, especially if you are a developer. If you do need help, ask a question in the comments.

Here is what we are going to cover in this step:

  1. Download R.
  2. Install R.
  3. Start R.
  4. Install R Packages.

1.1 Download R

You can download R from The R Project webpage.

When you click the download link, you will have to choose a mirror. You can then choose R for your operating system, such as Windows, OS X or Linux.

1.2 Install R

R is is easy to install and I’m sure you can handle it. There are no special requirements. If you have questions or need help installing see R Installation and Administration.

1.3 Start R

You can start R from whatever menu system you use on your operating system.

For me, I prefer the command line.

Open your command line, change (or create) to your project directory and start R by typing:

You should see something like the screenshot below either in a new window or in your terminal.

R Interactive Environment

R Interactive Environment

1.4 Install Packages

Install the packages we are going to use today. Packages are third party add-ons or libraries that we can use in R.

UPDATE: We may need other packages, but caret should ask us if we want to load them. If you are having problems with packages, you can install the caret packages and all packages that you might need by typing:

Now, let’s load the package that we are going to use in this tutorial, the caret package.

The caret package provides a consistent interface into hundreds of machine learning algorithms and provides useful convenience methods for data visualization, data resampling, model tuning and model comparison, among other features. It’s a must have tool for machine learning projects in R.

For more information about the caret R package see the caret package homepage.

2. Load The Data

We are going to use the iris flowers dataset. This dataset is famous because it is used as the “hello world” dataset in machine learning and statistics by pretty much everyone.

The dataset contains 150 observations of iris flowers. There are four columns of measurements of the flowers in centimeters. The fifth column is the species of the flower observed. All observed flowers belong to one of three species.
You can learn more about this dataset on Wikipedia.

Here is what we are going to do in this step:

  1. Load the iris data the easy way.
  2. Load the iris data from CSV (optional, for purists).
  3. Separate the data into a training dataset and a validation dataset.

Choose your preferred way to load data or try both methods.

2.1 Load Data The Easy Way

Fortunately, the R platform provides the iris dataset for us. Load the dataset as follows:

You now have the iris data loaded in R and accessible via the dataset variable.

I like to name the loaded data “dataset”. This is helpful if you want to copy-paste code between projects and the dataset always has the same name.

2.2 Load From CSV

Maybe your a purist and you want to load the data just like you would on your own machine learning project, from a CSV file.

  1. Download the iris dataset from the UCI Machine Learning Repository (here is the direct link).
  2. Save the file as iris.csv your project directory.

Load the dataset from the CSV file as follows:

You now have the iris data loaded in R and accessible via the dataset variable.

2.3. Create a Validation Dataset

We need to know that the model we created is any good.

Later, we will use statistical methods to estimate the accuracy of the models that we create on unseen data. We also want a more concrete estimate of the accuracy of the best model on unseen data by evaluating it on actual unseen data.
That is, we are going to hold back some data that the algorithms will not get to see and we will use this data to get a second and independent idea of how accurate the best model might actually be.

We will split the loaded dataset into two, 80% of which we will use to train our models and 20% that we will hold back as a validation dataset.

You now have training data in the dataset variable and a validation set we will use later in the validation variable.

Note that we replaced our dataset variable with the 80% sample of the dataset. This was an attempt to keep the rest of the code simpler and readable.

3. Summarize Dataset

Now it is time to take a look at the data.

In this step we are going to take a look at the data a few different ways:

  1. Dimensions of the dataset.
  2. Types of the attributes.
  3. Peek at the data itself.
  4. Levels of the class attribute.
  5. Breakdown of the instances in each class.
  6. Statistical summary of all attributes.

Don’t worry, each look at the data is one command. These are useful commands that you can use again and again on future projects.

3.1 Dimensions of Dataset

We can get a quick idea of how many instances (rows) and how many attributes (columns) the data contains with the dim function.

You should see 120 instances and 5 attributes:

3.2 Types of Attributes

It is a good idea to get an idea of the types of the attributes. They could be doubles, integers, strings, factors and other types.

Knowing the types is important as it will give you an idea of how to better summarize the data you have and the types of transforms you might need to use to prepare the data before you model it.

You should see that all of the inputs are double and that the class value is a factor:

3.3 Peek at the Data

It is also always a good idea to actually eyeball your data.

You should see the first 5 rows of the data:

3.4 Levels of the Class

The class variable is a factor. A factor is a class that has multiple class labels or levels. Let’s look at the levels:

Notice above how we can refer to an attribute by name as a property of the dataset. In the results we can see that the class has 3 different labels:

This is a multi-class or a multinomial classification problem. If there were two levels, it would be a binary classification problem.

3.5 Class Distribution

Let’s now take a look at the number of instances (rows) that belong to each class. We can view this as an absolute count and as a percentage.

We can see that each class has the same number of instances (40 or 33% of the dataset)

3.6 Statistical Summary

Now finally, we can take a look at a summary of each attribute.

This includes the mean, the min and max values as well as some percentiles (25th, 50th or media and 75th e.g. values at this points if we ordered all the values for an attribute).

We can see that all of the numerical values have the same scale (centimeters) and similar ranges [0,8] centimeters.

4. Visualize Dataset

We now have a basic idea about the data. We need to extend that with some visualizations.

We are going to look at two types of plots:

  1. Univariate plots to better understand each attribute.
  2. Multivariate plots to better understand the relationships between attributes.

4.1 Univariate Plots

We start with some univariate plots, that is, plots of each individual variable.

It is helpful with visualization to have a way to refer to just the input attributes and just the output attributes. Let’s set that up and call the inputs attributes x and the output attribute (or class) y.

Given that the input variables are numeric, we can create box and whisker plots of each.

This gives us a much clearer idea of the distribution of the input attributes:

Box and Whisker Plots in R

Box and Whisker Plots in R

We can also create a barplot of the Species class variable to get a graphical representation of the class distribution (generally uninteresting in this case because they’re even).

This confirms what we learned in the last section, that the instances are evenly distributed across the three class:

Bar Plot of Iris Flower Species

Bar Plot of Iris Flower Species

4.2 Multivariate Plots

Now we can look at the interactions between the variables.

First let’s look at scatterplots of all pairs of attributes and color the points by class. In addition, because the scatterplots show that points for each class are generally separate, we can draw ellipses around them.

We can see some clear relationships between the input attributes (trends) and between attributes and the class values (ellipses):

Scatterplot Matrix of Iris Data in R

Scatterplot Matrix of Iris Data in R

We can also look at box and whisker plots of each input variable again, but this time broken down into separate plots for each class. This can help to tease out obvious linear separations between the classes.

This is useful to see that there are clearly different distributions of the attributes for each class value.

Box and Whisker Plot of Iris data by Class Value

Box and Whisker Plot of Iris data by Class Value

Next we can get an idea of the distribution of each attribute, again like the box and whisker plots, broken down by class value. Sometimes histograms are good for this, but in this case we will use some probability density plots to give nice smooth lines for each distribution.

Like he boxplots, we can see the difference in distribution of each attribute by class value. We can also see the Gaussian-like distribution (bell curve) of each attribute.

Density Plots of Iris Data By Class Value

Density Plots of Iris Data By Class Value

5. Evaluate Some Algorithms

Now it is time to create some models of the data and estimate their accuracy on unseen data.

Here is what we are going to cover in this step:

  1. Set-up the test harness to use 10-fold cross validation.
  2. Build 5 different models to predict species from flower measurements
  3. Select the best model.

5.1 Test Harness

We will 10-fold crossvalidation to estimate accuracy.

This will split our dataset into 10 parts, train in 9 and test on 1 and release for all combinations of train-test splits. We will also repeat the process 3 times for each algorithm with different splits of the data into 10 groups, in an effort to get a more accurate estimate.

We are using the metric of “Accuracy” to evaluate models. This is a ratio of the number of correctly predicted instances in divided by the total number of instances in the dataset multiplied by 100 to give a percentage (e.g. 95% accurate). We will be using the metric variable when we run build and evaluate each model next.

5.2 Build Models

We don’t know which algorithms would be good on this problem or what configurations to use. We get an idea from the plots that some of the classes are partially linearly separable in some dimensions, so we are expecting generally good results.

Let’s evaluate 5 different algorithms:

  • Linear Discriminant Analysis (LDA)
  • Classification and Regression Trees (CART).
  • k-Nearest Neighbors (kNN).
  • Support Vector Machines (SVM) with a linear kernel.
  • Random Forest (RF)

This is a good mixture of simple linear (LDA), nonlinear (CART, kNN) and complex nonlinear methods (SVM, RF). We reset the random number seed before reach run to ensure that the evaluation of each algorithm is performed using exactly the same data splits. It ensures the results are directly comparable.

Let’s build our five models:

Caret does support the configuration and tuning of the configuration of each model, but we are not going to cover that in this tutorial.

5.3 Select Best Model

We now have 5 models and accuracy estimations for each. We need to compare the models to each other and select the most accurate.

We can report on the accuracy of each model by first creating a list of the created models and using the summary function.

We can see the accuracy of each classifier and also other metrics like Kappa:

We can also create a plot of the model evaluation results and compare the spread and the mean accuracy of each model. There is a population of accuracy measures for each algorithm because each algorithm was evaluated 10 times (10 fold cross validation).

We can see that the most accurate model in this case was LDA:

Comparison of Machine Learning Algorithms on Iris Dataset in R

Comparison of Machine Learning Algorithms on Iris Dataset in R

The results for just the LDA model can be summarized.

This gives a nice summary of what was used to train the model and the mean and standard deviation (SD) accuracy achieved, specifically 97.5% accuracy +/- 4%

6. Make Predictions

The LDA was the most accurate model. Now we want to get an idea of the accuracy of the model on our validation set.

This will give us an independent final check on the accuracy of the best model. It is valuable to keep a validation set just in case you made a slip during such as overfitting to the training set or a data leak. Both will result in an overly optimistic result.

We can run the LDA model directly on the validation set and summarize the results in a confusion matrix.

We can see that the accuracy is 100%. It was a small validation dataset (20%), but this result is within our expected margin of 97% +/-4% suggesting we may have an accurate and a reliably accurate model.

You Can Do Machine Learning in R

Work through the tutorial above. It will take you 5-to-10 minutes, max!

You do not need to understand everything. (at least not right now) Your goal is to run through the tutorial end-to-end and get a result. You do not need to understand everything on the first pass. List down your questions as you go. Make heavy use of the ?FunctionName help syntax in R to learn about all of the functions that you’re using.

You do not need to know how the algorithms work. It is important to know about the limitations and how to configure machine learning algorithms. But learning about algorithms can come later. You need to build up this algorithm knowledge slowly over a long period of time. Today, start off by getting comfortable with the platform.

You do not need to be an R programmer. The syntax of the R language can be confusing. Just like other languages, focus on function calls (e.g. function()) and assignments (e.g. a <- “b”). This will get you most of the way. You are a developer, you know how to pick up the basics of a language real fast. Just get started and dive into the details later.

You do not need to be a machine learning expert. You can learn about the benefits and limitations of various algorithms later, and there are plenty of posts that you can read later to brush up on the steps of a machine learning project and the importance of evaluating accuracy using cross validation.

What about other steps in a machine learning project. We did not cover all of the steps in a machine learning project because this is your first project and we need to focus on the key steps. Namely, loading data, looking at the data, evaluating some algorithms and making some predictions. In later tutorials we can look at other data preparation and result improvement tasks.


In this post you discovered step-by-step how to complete your first machine learning project in R.

You discovered that completing a small end-to-end project from loading the data to making predictions is the best way to get familiar with a new platform.

Your Next Step

Do you work through the tutorial?

  1. Work through the above tutorial.
  2. List any questions you have.
  3. Search or research the answers.

Remember, you can use the ?FunctionName in R to get help on any function.

Do you have a question? Post it in the comments below.

Frustrated With Your Progress In R Machine Learning?

Master Machine Learning With R

Develop Your Own Models in Minutes

…with just a few lines of R code

Discover how in my new Ebook:
Machine Learning Mastery With R

Covers self-study tutorials and end-to-end projects like:
Loading data, visualization, build models, tuning, and much more…

Finally Bring Machine Learning To
Your Own Projects

Skip the Academics. Just Results.

Click to learn more.

250 Responses to Your First Machine Learning Project in R Step-By-Step (tutorial and template for future projects)

  1. Leszek Pawlowicz February 3, 2016 at 6:28 am #

    This is what I can’t stand about open-source packages like R (and Python, and LibreOffice): Nobody puts in the effort required to make sure things work properly, it’s almost impossible to duplicate working environments, and the error messages are cryptically impossible. Trying to generate the scatterplot matrix above, cutting and pasting the command into R, I got the following error message:

    Error in, name$name, strict) :
    Viewport ‘’ was not found

    Google Search provided no help. After getting featurePlot to work with all options other than “ellipse”, finally stumbled across the solution that you needed to have the “ellipse” package installed on your system. I’m guessing that you have that as a default library on your system, so you didn’t specify it was required to use that function. But how many people reading this post will be able to figure that out?

    • Jason Brownlee February 3, 2016 at 8:21 am #

      Thanks for pointing that out Leszek. To be honest I’ve not heard of that package before. Perhaps it is installed automatically with the “caret” or “lattice” packages?

      • Rajendra December 11, 2016 at 1:07 am #

        Thanks for the post… worked after installed ellipse package. not installed with caret.

        • Jason Brownlee December 11, 2016 at 5:24 am #

          Glad to hear it Rajendra.

        • shantesh January 20, 2018 at 8:41 pm #

          Yet it works after installing ellipse packages

          • Jason Brownlee January 21, 2018 at 9:09 am #


          • Sailing January 21, 2018 at 10:43 am #

            Thanks for the post. I tried Google first when I saw the error, interestingly the 5th search result is the link back to this post. 🙂 It works after installing ellipse package.

            Thanks Jason for this great learning tutorial!

          • Jason Brownlee January 22, 2018 at 4:40 am #

            Glad to hear it!

        • Marek January 23, 2018 at 2:48 am #

          the most important piece of information missing in the text above:

          Thanks Rajendra !!!

    • The R Enthusiast February 10, 2016 at 6:14 pm #

      He did not have the “ellipse” package as default on his system. What he did was that he installed the “caret” package using the code he provided above:

      install.packages(“caret”, dependencies = c(“Depends”, “Suggests”))

      The result was that ALL the packages that were likely to be used by the “caret” package were also installed… including the “ellipse” package. You could have avoided your frustration by simply following the instructions in the tutorial.

      • Jonathan March 30, 2017 at 11:35 pm #

        Wrong. I followed the instructions exactly as listed and it didn’t work for me either. When I explicitly installed the ellipse library it worked fine.

      • Laique Merlin Djeutchouang January 8, 2018 at 9:22 pm #

        Perfect remarks. Always follow the instructions of the tutorial. Great tutorial Jason, as usual of course.

    • Wael September 3, 2017 at 12:01 am #

      Thanks for highlighting the problem. True, it was hard to find a solution elsewhere on the Internet!

    • Mahmud February 3, 2018 at 6:48 am #

      Thanks! Your comment saved me!

    • Sharon November 18, 2018 at 3:20 am #

      Hi Jason,

      Recently I started with R. Great tutorials!

      I would like to ask you a question, hopefully you can point me in the right direction.
      Last year I bought software that develops trading systems for the stock market.
      It is capable to generate thousands of trading systems in a day,
      The price history can be cut in three parts: in sample, out of sample and validation.
      For every trading system and every price part I have metrics like: net profit, drawdown, average trade result and so on.
      I would like to use the in sample and out of sample results (metrics) to try and predict the results (metrics) in the validation period.So I can determine what trading systems perform the best accoridng to the in sample and out of sample metrics and the algorithm.
      What algorithm can you advice me to use in this particular case?

      Thanks again Jason!

  2. Leszek Pawlowicz February 3, 2016 at 6:39 am #

    And, moving on, found that there were additional packages that needed to be installed and loaded, and then wound up with an Accuracy table that didn’t get the same results as you did, despite copying and pasting all the commands exactly as written. This doesn’t give me a lot of confidence about reproducibility in R.

    • Jason Brownlee February 3, 2016 at 8:20 am #

      Post your results!

      It is true that strictly reproducible results can be difficult in R. I find you need to sprinkle a lot of set.seed(…) calls around the place, and even then it’s difficult.

    • The R Enthusiast February 10, 2016 at 6:18 pm #

      The reason why your accuracy table is not the same mainly comes from the fact that the “createDataPartition()” function chooses observations in the dataset randomly. This means that the training and validation datasets are essentially different for everybody. Consequently, the end results will be slightly different.

      • Hazem February 12, 2018 at 4:07 am #

        Yup ..

  3. Mohan Raj February 4, 2016 at 10:02 pm #

    Hi Jason,

    When i loaded the caret package using below query,

    > require(caret)

    Loading required package: caret
    Error in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]) :
    there is no package called ‘pbkrtest’
    In addition: Warning message:
    package ‘caret’ was built under R version 3.2.3

    I have assigned the iris dataset to dataset2. then i executed the below query
    createDataPartition(dataset2$Species, p=0.80, list=FALSE) is not working. I am getting the error message when i execute the above query.

    Error Message:
    Error: could not find function "createDataPartition"

    Let me know as what went wrong.


    • Jason Brownlee February 5, 2016 at 5:21 am #

      This looks like a problem specific to your environment. Consider re-installing the caret package with all dependencies:

      install.packages(“caret”, dependencies = c(“Depends”, “Suggests”))

      • Jason Brownlee February 7, 2016 at 6:43 am #

        I’ve added this command to the install packages section, just in case others find it useful.

  4. Rajesh February 6, 2016 at 5:52 am #

    If the R version is 3.2.1 or below the caret package may turn incompatible. I faced similar issue. After uninstalling the old version I installed R 3.2.3 which fixed the error.

    Hope it helps.


    • Jason Brownlee February 7, 2016 at 6:40 am #

      Thanks Rajesh, I updated the post and added a note to use R 3.2.3 or higher.

  5. CW February 7, 2016 at 3:32 am #

    Thanks for sharing this. I had to grab another package (kernlab) to run the SVM fit, but everything rolled smoothly, otherwise. Great 15min introduction!

  6. Ajit Jaokar February 16, 2016 at 3:17 am #

    Hello Jason, this is an interesting tutorial and getting to grips with Caret still. Qs is: in the sctarrerplot matix(which is used from caret I think) how do we know what colours corespond to which class Rgds Ajit

    • Jason Brownlee February 16, 2016 at 5:49 am #

      That is a good question. It is a good idea to add a legend to your graphs.

      I did not add a legend in this case because we were not interested in which class was which only in the general separation of the classes. Type ?featurePlot to learn more about adding a legend.

  7. Ajit Jaokar February 16, 2016 at 9:25 pm #

    Thanks Jason. But more to the point .. where in the code do you assign the legend(or does the legend get picked up automatically ie which colour to which class. If it does so implicitly, how do I know what colour coresponds to what class?

    Also another question
    It says “We will 10-fold cross validation to estimate accuracy. This will split our dataset into 10 parts, train in 9 and test on 1 and release for all combinations of train-test splits. We will also repeat the process 3 times for each algorithm with different splits of the data into 10 groups, in an effort to get a more accurate estimate.” Hence I should expect to see 15 steps(3 times per algorithm with different splits) but we see here 5 steps(once) where do we try the other two times? kind rgds Ajit

    • Roberto Ulloa December 8, 2016 at 12:26 am #

      Hi Ajit,

      The repetitions should be indicated in the trainControl function. When I was reading, I though 3 was the default, but this didn’t seem to be the case according to the documentation ?trainControl

      According to this (, the method parameter should have been “repeatedcv” and not just “cv”, and then the parameters repeats should have been 3.

      trainControl(method=”repeatedcv”, number=10, repeat=3)

      You can verify that the training takes longer and the confidence intervals of the plots are smaller, so I might be right. However, I am not absolutely sure if this is correct, because I don’t know how to visually check the folds. Also, I don’t know how to get each individual result of each cv and repetition from the fits, e.g. fit.lda.


  8. Kiri Kurukkal February 27, 2016 at 8:58 pm #

    Thanks Jason for the great tutorial. I was able to reproduce the same results by following your instructions carefully. BTW, I reviewed some of the other posts above and most of the dependencies could have been resolved by loading the library(caret) at the beginning. I did encounter one issue prior to loading the library(caret) with the Error: could not find function “createDataPartition”. This error was resolved by loading the required library(caret). This loaded other required packages.

    > library(caret)
    Loading required package: lattice
    Loading required package: ggplot2

    Happy coding / earning!

  9. Rick Pack February 29, 2016 at 11:29 am #

    If anyone wants more practice, I did my best to recall the code Chad Hines and I added to the tutorial so one can examine the mismatches for LDA on the training set. Thank you to Jason Brownlee for this tutorial and to Kevin Feasel and Jamie Dixon for coordinating the .NET Triangle “Introduction to R” dojo last week.

  10. Phuong May 24, 2016 at 5:14 pm #

    Hi Jason Brownlee,
    Thanks for your tutorial. However, I have a question about featurePlot function with plot = “density ” option. I couldn’t figure out the meaning of vertical axis in these plots for each features. Why the vertical axes have values that are greater than 1 (in the case of density)

  11. Johnny September 7, 2016 at 11:53 pm #

    Hi Jason, very thorough and great practice for a newbie like myself. I will definitely be referring back to this one often.

  12. Mrinal September 21, 2016 at 3:21 pm #

    Thanks, Brownlee. I would like to know of selecting best model. Is it guaranteed that a model giving highest accuracy can give the result of highest accuracy for test data? In this example, you have selected lda as the best model comparing the accuracies of the used models. But it may not predict best during testing. So my question is: when should we select our model? After training models or testing models?

    • Jason Brownlee September 22, 2016 at 8:07 am #

      We cannot be sure we have picked the best model.

      We must gather evidence to support a given decision.

      More testing with k-fold cross validation and hold-out validation datasets can increase our confidence.

  13. Justin Nunez October 18, 2016 at 10:34 am #

    So, what is next?

    What can one do to get better at this? Any practice? Anything that builds on this?

    What happens when there is “noise” in the data, how do we clean it and apply it to ML properly?

  14. Jerry October 27, 2016 at 11:41 am #

    Tested in rstudio-ide. Works fine! Two small changes required:
    # Install Packages
    install.packages(‘caret’, repos=’’)
    # e1071
    install.packages(‘e1071’, dependencies=TRUE)

  15. Kenneth November 24, 2016 at 5:20 am #

    Is this a typo on this page?

    “A machine learning project may not be linear, but (it has a has) a number of well known steps:”

    • Jason Brownlee November 24, 2016 at 10:43 am #

      Yes, I intended to talk about the process of a machine learning project not being linear.

      I’ve fixed the typo, thnaks.

  16. Rajendra December 11, 2016 at 12:59 am #

    Indeed it is good post, but as it is framed in the mind for ML Learners, would have explained in details of each section much more clear, for ex, 4.1 barplot section, would have explained understand number of diagram. mere walk-through would not help anything

  17. Stef January 13, 2017 at 6:59 pm #

    Excellent, thank you, managed to do this with my own dataset but struggling to plot an ROC curve after. Please can you help by posting the code to plot the ROC curve? thanks

  18. Lewis Walker February 26, 2017 at 10:30 pm #

    I have just started learning R and trying to use this Tutorial to fit my Dataset into it, and had a few problems like missing packages, I did however notice that when you library(caret) it will say what is missing so it’s a simple case of install.packages(missing package displayed).

    I would however like to split my dataset up a bit more, this tutorial uses
    “validation_index <- createDataPartition(dataset$Species, p=0.80, list=FALSE)"

    My dataset is pretty large and I would like to split it into 3 or 4, like rather than an 80/20 split I would like a 50/25/25 or a 40/30/30. As I said I'm new to R so if my way of splitting it isn't the way it should be done just tell me :).

    • Jason Brownlee February 27, 2017 at 5:52 am #

      Hi Lewis,

      I believe createDataPartition() is for creating train/test splits.

      You could use it to create one split, then re-split one of the halves if you like.

      I hope that helps.

  19. Sai March 25, 2017 at 6:34 am #

    Very Nice article. Thank you. It was a very good starter for me as a new R programmer.
    But one question I have is in section 6 (“Make Predictions”).

    I understand that we are predicting the accuracy of our model in that section. But can you please elaborate on how to make prediction for some new data set ? I am not clear in that prediction part.

  20. Muriel March 29, 2017 at 11:45 pm #

    This is very helpful. Thanks. But I have a question. In a case where I have two datasets, will name them trainingdata.csv and testdata.csv, how do I load them to R but train my algorithm on training data and test it on the data set?


  21. Muriel April 1, 2017 at 7:05 pm #

    I know how to load this data. My question is if I have two data sets, the training data and the test data. What functions must I use for R to recognise my training data to built the models on and test data to validate. Unlike on the Iris project where they have one data and splitted it on 80% 20%

    • Jason Brownlee April 2, 2017 at 6:25 am #

      Sorry, I don’t understand your question. Perhaps you can rephrase it?

  22. Muriel April 1, 2017 at 7:07 pm #

    on the iris project, am getting an error for the function to partition data. See below commands.

    > #attach the iris dataset to the environment
    > data(iris)
    > #rename the dataset
    > dataset # create a list of 80% of the rows in the original dataset we can use for training
    > validation_index validation_index <- createDataPartition(dataset$Species, p=0.80, list=FALSE)
    Error: could not find function "createDataParti

  23. Mike April 10, 2017 at 5:19 am #

    Great post, thanks. I got it working. But when I replaced my data with iris, I got an error:
    “Metric Accuracy not applicable for regression models” for all non-linier models. Here is my data:

    Any suggestions?

    • Jason Brownlee April 10, 2017 at 7:41 am #

      It sounds like your output variable is a real value (regression) and not a label (classification).

      You may want to convert your problem to classification or use regression algorithm and evaluation measure.

  24. Jonatan April 11, 2017 at 6:01 pm #

    Thanks for an excellent post Jason, great help!

  25. Yenework A Mola April 26, 2017 at 1:11 pm #

    Thank you sir ! This is really the best tutorial . I learned a lot from it and i applied it to a different dataset . But how about comparing the models using ROC curve using the caret package ?

  26. Virendra May 9, 2017 at 7:35 pm #

    In Multivariate Plots, while trying to scatterplot matrix I am getting following error:-

    Error in, name$name, strict) :
    Viewport ‘’ was not found

    I am using R x64 3.4.0.

    • Jason Brownlee May 10, 2017 at 8:47 am #

      Sorry, I have not seen that error before. Perhaps post it as a question on stackoverflow?

    • Gudipati pavan April 30, 2018 at 4:53 pm #

      This was solved. Please check in discussion.

      1) You have to install ‘ellipse” package. which is missing
      2) If you change plot=pairs, you can see output. If you want, ellipse, please install ellipse package.

  27. Andre Yakana May 11, 2017 at 12:49 pm #

    this is very interesting sir, but i will like help on how to better explain the plots and what each mean especially the scatterplot. i am saying in a situation where i would have to explain to an audience

  28. dds May 13, 2017 at 11:45 am #

    for print(fit.lda), I don’t see Accuracy SD or Kappa SD printed/displayed … any hints? Thanks.

    • Jason Brownlee May 14, 2017 at 7:24 am #

      I think caret API has changed since I posted the example.

  29. tahir May 20, 2017 at 10:23 pm #

    good job Jason , but can I plot the SVM results in R? So I get the hyperplane and support vector points

    • Jason Brownlee May 21, 2017 at 5:59 am #

      You may, I have not done this myself in a long time. I do not recall the function name off-hand sorry.

  30. Hari May 25, 2017 at 12:05 pm #

    Hello Jason! Thanks for making this ML tutorial. I am having trouble in the model building part. I am getting error in “rpart”, “knn”. When I run the code for rpart, the error is “Something is wrong: all the accuracy metric values are missing:” “Error: Stopping” “In addition: There were 26 warnings (use warnings() to see them)” , however for “knn”, the last error line I am getting 50 warnings.

    Also , when I run “svmRadial” , it seems to run without any problem, however when i run the code for ‘rf”, I get this

    Loading required package: randomForest
    randomForest 4.6-12
    Type rfNews() to see new features/changes/bug fixes.

    Attaching package: ‘randomForest’

    The following object is masked from ‘package:dplyr’:


    The following object is masked from ‘package:ggplot2’:


    I have a Version 1.0.136 of RStudio. Your help is much appreciated!

    • Jason Brownlee June 2, 2017 at 11:41 am #

      I’m sorry to hear that. Ensure you have the latest version of R and the caret package installed. You may also want to install all recommended dependencies.

  31. Hans June 6, 2017 at 10:47 am #

    Do you have such an R tutorial for regression problems too?

  32. Sunny June 7, 2017 at 4:48 am #

    This post is exactly what I was looking for. Very well put together and I’m excited about it. I will share it with some students over at UCSF. For those reading the comments, I typed everything in manually directly from Dr. Brownlee’s scripts. You learn more that way because you’re likely to make a mistake when typing at some point.

  33. sudi June 8, 2017 at 3:48 pm #

    this post helps a lot but need little more clarification about boxplot and barchart becoz i am new for ml and r.could u plz explain me…it would be more helpful for me
    what does this code tell us.cant understand plz help me

    for(i in 1:4) {
    boxplot(x[,i], main=names(iris)[i])

    • Jason Brownlee June 9, 2017 at 6:18 am #

      It creates a composite plot of 4 boxplots side by side.

    • Cason December 8, 2018 at 6:09 am #


      par(mfrow=c(1,4)) /this code specifies the gui enable a graphical display of 1 row with 4 columns
      for(i in 1:4) { / this line means for each column in columns 1:4 do the follow in { code block}
      boxplot(x[,i], main = names(iris)[i) / make a boxplot of the data for the column, labeled w col name

  34. Aman June 20, 2017 at 6:36 pm #

    Thank so much sir. This is very helpful. Sir, I have a question.
    When I run LDA, SVM, RF, CART model always shows that Loading required package: MASS for LDA and so on for all methods that you mention. Although I get the results without loading specific package for each methods,but is it any problem if load the specific package or not?

    And if I load the package for each methods then function will be change such as for random forest we need to call the model:- randomForest(…) with package “randomForest”.

    • Jason Brownlee June 21, 2017 at 8:10 am #

      It is normal for caret to load the packages it needes to make predictions.

  35. Manish Chakraborty July 2, 2017 at 1:16 am #

    Hi, I have installed the “caret” package. But after this when i am loading through library(caret), I am getting the below error:

    Loading required package: ggplot2
    Error: package or namespace load failed for ‘ggplot2’ in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]):
    there is no package called ‘munsell’
    Error: package ‘ggplot2’ could not be loaded

    • Jason Brownlee July 2, 2017 at 6:31 am #

      I’m sorry, I have not seen this error. Perhaps check on stackoverflow if anyone has had this fault or consider posting the error there.

      • Manish Chakraborty July 2, 2017 at 1:39 pm #

        Hi Jason,
        Post some R&D was able to resolve it. Below are the actions i did.









  36. Manish Chakraborty July 2, 2017 at 2:06 pm #

    Hi Jason,

    Need one help again. Thanks in advance.

    Since this is my first Data Science Project, so the question.

    What and how to interpret from the result of BoxPlot. It will be of help if you can kindly explain a bit of the outcome of the BoxPlot.

  37. Eesha July 6, 2017 at 2:33 pm #

    Hello Dr Brownlee,

    I am new to machine learning and attempting to go through your tutorial.
    I keep getting an error saying that the accuracy matrix values are missing for this line:

    results <- resamples(list(lda=fit.lda, cart=fit.cart, knn=fit.knn, svm=fit.svm, rf=fit))

    The accuracy matrix for lad works however cart, knn, svn and rf do not work.
    Do you have any suggestions for how to fix this?


    • Jason Brownlee July 9, 2017 at 10:23 am #

      I’m sorry to hear that. Confirm your packages are up to date.

  38. jui July 6, 2017 at 6:54 pm #

    sir, how could i plot this confusionMatrix “confusionMatrix(predictions, validation$Species)”?

  39. Sankalp July 28, 2017 at 4:17 pm #

    > predictions confusionMatrix(predictions, validation$Species)
    Error in confusionMatrix(predictions, validation$Species) :
    object ‘predictions’ not found

    Could anyone clarify this error ?

    • Sankalp July 28, 2017 at 4:18 pm #

      predictions confusionMatrix(predictions, validation$Species)
      Error in confusionMatrix(predictions, validation$Species) :
      object ‘predictions’ not found

      Could anyone clarify this error ?Earlier I posted something wrong

      • Jason Brownlee July 29, 2017 at 8:06 am #

        Perhaps double check that you have all of the code from the post?

  40. Saurabh August 4, 2017 at 11:00 pm #


    I am beginner in this so may be the question I am going to ask wont make sense but I would request you to please answer:
    So when we say lets predict something, what exactly we are predicting here ?
    In case of a machine (motor, pump etc) data(current, RPM, vibration) what is that can be predicted ?


    • Jason Brownlee August 5, 2017 at 5:47 am #

      In this tutorial, given the measurements of iris flowers, we use a model to predict the species.

  41. Manoj September 5, 2017 at 5:47 pm #

    > fit.lda <- train(Species~., data = data, method = "lda", metric = metric, trControl = control)
    The error i got, and also tried to install mass package but it not getting installed properly and showing the error again and again please help me sir.
    Loading required package: MASS
    Error in unloadNamespace(package) :
    namespace ‘MASS’ is imported by ‘lme4’, ‘pbkrtest’, ‘car’ so cannot be unloaded
    Failed with error: ‘Package ‘MASS’ version 7.3.45 cannot be unloaded’
    Error in unloadNamespace(package) :
    namespace ‘MASS’ is imported by ‘lme4’, ‘pbkrtest’, ‘car’ so cannot be unloaded
    Error in library(p, character.only = TRUE) :
    Package ‘MASS’ version 7.3.45 cannot be unloaded

    • Jason Brownlee September 7, 2017 at 12:43 pm #

      I’m sorry to hear that. Perhaps try installing the MASS package by itself in a new session?

  42. Sasikanth September 6, 2017 at 1:46 pm #

    Hello Jason,

    My question is regarding scaling. For some algorithms like adaboost/xgboost it is recommended to scale all the data. My question is how do I unscale the final predictions. I used the scale() function in R. The unscale() function expects the center(which could be mean/median) value of the predicted values. But my predicted values are already scaled. How can I unscale them to the appropriate predicted values. I am referring to prediction on unlabeled data set.
    I have searched for this in many websites but have not found any answer.

    • Jason Brownlee September 7, 2017 at 12:50 pm #

      Perhaps scale the data yourself, and use the coefficients min/max or mean/stdev to invert the scaling?

  43. Alok Kumar Padhi September 7, 2017 at 4:08 pm #

    I am getting an error while summarize the accuracy of models,
    Error in summary(results) : object ‘results’ not found

  44. Manoj Sah September 19, 2017 at 6:21 am #

    > library(tidyverse)
    Sir while adding this library in R, I have installed the package then also it is showing following the error: please help me

    Error in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]) :
    there is no package called ‘bindrcpp’
    Error: package or namespace load failed for ‘tidyverse’

    • Jason Brownlee September 19, 2017 at 7:50 am #

      Sorry, I am not familiar with that package or the error. Perhaps try posting on stackoverflow?

  45. RATNA NITIN PATIL September 29, 2017 at 4:06 pm #

    Dear Jason,
    I am not familiar with R tool. When I started reading this tutorial, I thought of installing R. After the installation when I typed the Rcommand, I got the following error message. Please give me the suggestion…

    > install.packages(“caret”)
    Installing package into ‘C:/Users/Ratna/Documents/R/win-library/3.4’
    (as ‘lib’ is unspecified)
    — Please select a CRAN mirror for use in this session —
    trying URL ‘’
    Content type ‘application/zip’ length 5097236 bytes (4.9 MB)
    downloaded 4.9 MB

    package ‘caret’ successfully unpacked and MD5 sums checked

    The downloaded binary packages are in

  46. RATNA NITIN PATIL September 29, 2017 at 4:26 pm #

    Hi Jasson,
    I tried the following but got the error,

    > library(caret)
    Loading required package: lattice
    Loading required package: ggplot2
    Error: package or namespace load failed for ‘caret’ in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]):
    there is no package called ‘kernlab’

    • Jason Brownlee September 30, 2017 at 7:37 am #

      It looks like you might need to install the “kernlab” package.

  47. RATNA NITIN PATIL October 1, 2017 at 5:56 am #

    Thanks Jasson!!!!

  48. VENKATESH T November 1, 2017 at 9:37 pm #

    e1071 error i have installed all packages…..

  49. Dan Larkin November 3, 2017 at 3:20 am #

    Hi All,

    When I created the updated ‘dataset’ in step 2.3 with the 120 observations, the dataset for some reason created 24 N/A values leaving only 96 actual observations. Copy and pasted the code from the post above. Any idea what caused or how to fix so that the ‘dataset’ is inclusive of all the training data observations? Doesn’t seem to be anything wrong with the IRIS dataset or either of the validation_index or validation datasets.

    • Jason Brownlee November 3, 2017 at 5:20 am #

      Perhaps double check you have the most recent version of R?

  50. Dan Larkin November 3, 2017 at 3:26 am #

    Update to OP, I reran the original commands from that section and was able to pull in all 120 observations for the training data. Not sure why it didn’t fetch all the data the first time but looks ok now.

  51. madmarks November 8, 2017 at 10:49 pm #

    Just confirming, the above tutorial is a multiclass problem? Therefore, I should be able to apply the above methodology to a different k=3 problem. Is this correct?

  52. swagjeezy November 19, 2017 at 8:01 am #


    For my first Machine Learning Project, this was EXTREMELY helpful and I thank you for the tutorial. I had no problems going through the script and even applied to a dummy dataset and it worked great. So thank you.

    My question is more related to automation. Instead of manually assessing the accuracy of each model to determine which one to use for prediction, is there a way to automatically call the model with the highest accuracy in the “predictions <- predict([best model], validation)" script. Hope to hear from you soon.

  53. Tejwant Navalkar November 28, 2017 at 11:20 pm #

    I did not get 100% Accuracy after following the tutorial example. I got :
    Confusion Matrix and Statistics

    Prediction Iris-setosa Iris-versicolor Iris-virginica
    Iris-setosa 10 0 0
    Iris-versicolor 0 8 0
    Iris-virginica 0 2 10

    Overall Statistics

    Accuracy : 0.9333
    95% CI : (0.7793, 0.9918)
    No Information Rate : 0.3333
    P-Value [Acc > NIR] : 8.747e-12

    Kappa : 0.9
    Mcnemar’s Test P-Value : NA

    Statistics by Class:

    Class: Iris-setosa Class: Iris-versicolor Class: Iris-virginica
    Sensitivity 1.0000 0.8000 1.0000
    Specificity 1.0000 1.0000 0.9000
    Pos Pred Value 1.0000 1.0000 0.8333
    Neg Pred Value 1.0000 0.9091 1.0000
    Prevalence 0.3333 0.3333 0.3333
    Detection Rate 0.3333 0.2667 0.3333
    Detection Prevalence 0.3333 0.2667 0.4000
    Balanced Accuracy 1.0000 0.9000 0.9500

    • Jason Brownlee November 29, 2017 at 8:25 am #

      Perhaps try running the example multiple times?

  54. vikash December 6, 2017 at 1:44 am #

    sir, i want to learn r programing at vedio based tutorial which is the best tutorial to learn r programming quickly

    • Jason Brownlee December 6, 2017 at 9:06 am #

      Sorry, I don’t have good advice on how to learn R, I focus on teaching how to learn machine learning for R.

      • Carlos December 13, 2017 at 1:24 pm #

        For learning R I strongly recommend the “R Programming” certification course, When I took it it was free, now is paid, something around USD 50.

  55. Carlos December 13, 2017 at 12:42 pm #

    Json, nice article. I left working code with minor fixes in this repo, please comment on, thanks, Carlos

  56. johan January 4, 2018 at 2:23 am #

    what if the dataset is used EuStockMarkets, I error continue

  57. vijay January 9, 2018 at 10:57 pm #

    successfully done, and got the result.Thanks for the great tutorial.
    But now i wonder, what to do further, how to use it in a generic manner for any dataset.
    How to use the created pred.model anywhere.

    • Jason Brownlee January 10, 2018 at 5:26 am #

      Yes, you can use this process on other datasets.

      • vijay January 10, 2018 at 4:43 pm #

        ohk, but to use any dataset we need to make the dataset similar to that of the iris dataset, like 4 numberic columns, and one class.
        Also, accuracy output is similar over the traning dataset , and the validation dataset, but how does that help me to predict now what type of flower would be next if i provide it the similar parameters.
        Now, for example i have to create a model which predicts the cpu utilization of the servers in my Vcenter or complete DC, how can i create a model which will take my continious dataset and predict that when the CPU utilization will go high and i can take proactive measures.

  58. kumar January 14, 2018 at 6:00 am #

    Hello Jason,

    Thanks for the clear and set by step instructions. But I just want to understand what I need to do after creating the model and calculating its accuracy ? Can you please explain to draw some conclusions/predictions on the iris data set we used ?

  59. Saurabh January 16, 2018 at 10:05 pm #

    Hi Sir,

    For the confusionMatrix(predictions, validation$Species) command , I am getting an output as follows:

    [,1] [,2]
    [1,] 0 0
    [2,] 0 10

    I am not getting the same output as you got. Any suggestions on what I may be doing wrong.?The code worked exactly till this command.

    • Jason Brownlee January 17, 2018 at 9:57 am #

      Perhaps double check that you copied all of the code exactly?

      And that your Python environment and libraries are up to date?

  60. Rigo January 23, 2018 at 3:17 am #

    Hello good day Jason.

    Thank you very much for the tutorial I have been very useful but I have a question, in the section of “print (fit.lda)” does not deploy “Accuracy SD Kappa SD”.

    What remains of the tutorial if you have given me exact, could you help me with this doubt ?.


    • Jason Brownlee January 23, 2018 at 8:06 am #

      The API may have changed slightly since I wrote the post nearly 2 years ago.

  61. Sathish January 26, 2018 at 9:41 am #

    Great article for a beginner like me Jason! Appreciate your work in sharing your knowledge and educating.

    Is there a model fit for ‘multinomial logistic regression’ algorithm?

    Thank you!

    • Jason Brownlee January 27, 2018 at 5:50 am #

      There is, but I would not recommend it. Try LDA instead.

  62. Sathish January 27, 2018 at 3:14 am #

    Upon further reading of other articles written by you, I realize that I may not need to use ‘Regression’. My dataset has category variables as input and category attributes as output as well (having 7 levels). So, it is a classification problem and I’m assuming I can use one of the 5 models/fit you have given as examples here in this Iris project. Can you let me know if this is correct understanding?
    – Thank you

  63. Penny January 30, 2018 at 1:48 pm #

    It works for me with the iris data. Thanks a lot Jason! But there are no “Accuracy SD Kappa SD ” from the output of the fit models. Should I change some settings to get them?

  64. Ibrahin February 1, 2018 at 8:34 am #

    Dear Jason Brownlee
    I have a dataset with 36 predictors and one for classes (“1”, “2”, “3”) that I got it through clustering in the previous step.
    My question is: how can I reduce all my predictors into five variables representing specific dimensions in my study?
    Should I run PCA separately to produce a new dataset with 5 predictors and one for classes or is there any other ways?
    Thank you in advance.

    • Jason Brownlee February 2, 2018 at 8:03 am #

      Yes, you would run dimensionality reduction first to create a new input dataset with the same number of rows.

  65. Gunjan February 2, 2018 at 9:11 pm #

    Hi Jason, I am getting the error –
    Error: could not find function “trainControl”
    on typing tc<-trainControl(method="cv",number=10).
    What can be the solution for this?

    • Jason Brownlee February 3, 2018 at 8:36 am #

      Perhaps caret is not installed or caret is not loaded?

  66. Joeri February 24, 2018 at 12:29 am #

    Maybe a very stupid question. But I read “Build 5 different models to predict species from flower measurements”. So now I am wondering what the predictions of the model tell me about this, how I can use it. For example I now go to the forest take some measurements, assume that the flower is one of those tested, and want to know which flower it is exactly. In a traditional regression formula it is straightforward as you can put in your measurements in the formula and the calculated estimates and get an outcome. But I don’t know how to use the outcomes in this case.

  67. Amin March 8, 2018 at 12:06 am #

    Your Tutorial is just awesome . Thanks its really helpful

  68. Machindra March 8, 2018 at 11:11 pm #

    This tutorial really helpful. Thanks Jason.

  69. Amin March 10, 2018 at 9:06 pm #

    Hi Json how are ?
    I am new in machine learning. i want to invent a unique idea and prof about islami banking and conventional banking. how can i do that. if any suggestion please give me and i cant fund any islami banking data set like loan info or deposit bla bla bla. i want your valuable information

  70. Varun March 14, 2018 at 7:51 pm #

    Dear Sir, I am getting the following error

    Error in [.data.frame(out, , c(x$perfNames, “Resample”)) :
    undefined columns selected

    when i execute
    results <- resamples(list(lda=fit.lda,nb=fit.nb, cart=fit.cart, knn=fit.knn, svm=fit.svm, rf=fit.rf))

    What can be the solution for this?

  71. Hussain T March 17, 2018 at 4:10 pm #

    Hi Jason, First of all great work. May God bless you for all your sincere efforts in sharing the knowledge. You are making a big difference to the lives of people. Thank you for that.

    I have a basic question. Now we have a best fit model – how to use it in day to day usage – is there a way I can measure the dimensions of a flower and “apply” them in some kind of equation which will give the predicted flower name? How to use the results? Kindly advise when you are free.

  72. TNguyen March 22, 2018 at 2:36 pm #

    Hi Jason,
    Thank you for sharing your methods and codes. It was very useful and easy to follow. Could you please share how to score a new dataset using one of the models? For example, in my training, random forest has the best accuracy. Now I want to apply that model on a new dataset that doesn’t have the outcome variables, and make prediction.
    Thank you

    • Jason Brownlee March 22, 2018 at 2:54 pm #

      Great question, I answer it in this post:

      • TNguyen March 22, 2018 at 3:30 pm #

        Thanks Jason. I read through the link. I already finalized my model, now I need save the model and apply it for operational use. The dataset that I want to score doesn’t have the outcome variable. I am not sure which command I should use to make prediction after I have the final model. Can you suggest R codes to do so?

        • Jason Brownlee March 23, 2018 at 6:01 am #

          You can use the predict() function to make a prediction with your finalized model.

  73. Nicolas April 16, 2018 at 8:07 am #

    Hi Jason! Amazing post! I have the same doubt @TNguyen did. I Finalized the model and we know that LDA is the best model to apply in this case. How I predict the outcome variables (species) in a new dataframe without this variable? IN summary, how I deploy the model on a new dataset? Sorry, I´m new in this field and I´m learning new things all the time!

  74. Martin April 19, 2018 at 4:30 am #


    Thanks for the great tutorial. I have a problem and don’t know what’s wrong in the section
    3.1 Dimensions. When I execute dim(datset) I get the answer NULL.
    Do you know why R Studio doesn’t show me the dimensions of the “dataset”?

    Best regards

  75. Jatashankar Patel April 24, 2018 at 12:23 pm #

    Very nice, Its given overall structure to write the ML in R.

  76. Akshata April 25, 2018 at 5:23 pm #

    Hey, I am working on the package called polisci and I am asked to build a multiple linear regression modal. My dependent variable is human development index and my independent variable is economic freedom. Could ou please tell me how can I perform multiple linear regression modal. How do I go about in steps and what is the syntax in R to get to the results and get a graph? Any help would be greatly appreciated. Please help me as I am an undergrad student and I am learning this for the first time

    Thanks in advance

  77. Pawan Dhail April 26, 2018 at 4:59 am #

    Thanks Jason,

    Was able to execute the program in one go.. Excellent description

  78. anand May 6, 2018 at 4:53 am #

    Jason, Thank you very much for you above work. Its Ohsomesss, I am new to data science and want to make my carrier. I found so useful this superb……

  79. anand May 6, 2018 at 4:54 am #

    Please suggest me a path to become data scientist step by step, and how to become champion in R and python ??

  80. Lydia May 11, 2018 at 1:22 am #

    Thanks, Jason! This is a very helpful post. I did exactly as suggested, but when i print(fir.lda), I do not have the accuracy SD or kappa SD. How should I get them? Thanks

  81. Raul Guerrero May 12, 2018 at 9:03 am #

    Amazing tutorial!

    I just need to install 2 packages: e1071 and Ellipse

    After that, i wrote every single line, and i really appreciate the big effoct you done to explain so clear!!!

    Thank you

  82. Aashika May 18, 2018 at 8:38 pm #

    Thanks for the great tutorial. I have a problem and don’t know what’s wrong in the section
    6. Make predictions . When I execute predictions <- predict(fit.lda, validation)
    confusionMatrix(predictions, validation$Species) I get the error "error data and reference should be factors with the same levels."like this
    Do you know why R Studio doesn’t show me the Make predictions of the “dataset”?

  83. Alan May 19, 2018 at 12:31 pm #

    When I try to do the featurePlots I get NULL. I installed the ellipse package without error.

    featurePlot(x=x, y=y, plot=”ellipse”)
    > # box and whisker plots for each attribute
    > featurePlot(x=x, y=y, plot=”box”)
    > # density plots for each attribute by class value
    > scales featurePlot(x=x, y=y, plot=”density”, scales=scales)

    everything up to this point worked fine

    • Jason Brownlee May 20, 2018 at 6:34 am #

      I’m sorry to hear that. Perhaps there is another package that you must install?

  84. prateek May 26, 2018 at 6:05 am #

    I was also getting same error.

    You would like to check below link for the solution:

  85. Guru Umasankar May 27, 2018 at 4:14 am #

    Great tutorial Jason! Inspired me to look up and a learn a bit more about LDA and KNN etc. which is a bonus! Great self-learning experience. I have experience with analytics but am a relative R newbie but I could understand and follow with some googling about the underlying methods and R functions.. so, thanks!

    One thing… the final results comparison in Section 5.3 are different in my case and are different each time I run through it. Reason is likely that in Step 2.3 there is no set.seed() prior. So, when you create the validation dataset which is internally a random sample in createDataPartition().. results are different in the end?


    • Jason Brownlee May 27, 2018 at 6:50 am #

      Well done.

      Thanks. Yes, some minor differences should be expected.

  86. Alexander Rieem Dun June 13, 2018 at 7:11 pm #

    Jason Brownlee you the real MVP!

  87. BlueJay June 27, 2018 at 11:29 pm #

    Hello this is very helpful, but i don’t get how i should read the Scatterplot Matrix

    • Jason Brownlee June 28, 2018 at 6:20 am #

      Each plot compares one variable to another. It can help you get an idea of any obvious relationships between variables.

  88. Harpreet July 1, 2018 at 12:08 am #

    I have problem in this….

    # create a list of 80% of the rows in the original dataset we can use for training
    validation_index <- createDataPartition(dataset$Species, p=0.80, list=FALSE)
    # select 20% of the data for validation
    validation <- dataset[-validation_index,]
    # use the remaining 80% of data to training and testing the models
    dataset <- dataset[validation_index,]
    # create a list of 80% of the rows in the original dataset we can use for training
    validation_index <- createDataPartition(dataset$Species, p=0.80, list=FALSE)
    # select 20% of the data for validation
    validation <- dataset[-validation_index,]
    # use the remaining 80% of data to training and testing the models
    dataset <- dataset[validation_index,]

    Please help me out

  89. Harpreet July 1, 2018 at 12:25 am #

    it can’t findout the objects….and function also..! what can i do?

  90. Sam Nathan July 2, 2018 at 6:50 am #

    Jason, you’re indeed a MVP! Ran this in R 3.5.

    1. install.packages(“caret”, dependencies = c(“Depends”, “Suggests”)) ran for almost an hour. May be connectivity to mirrors.
    2. install.packages(“randomForest”) & library(“randomForest”) needed

    Would definitely recommend this to all ML aspirants as a “hello world!”

    Hearty Thanks!

  91. Giovanni Vigliotti July 4, 2018 at 5:55 am #

    First I’d like to say THANK YOU for making this available! It has given me the courage to pursue other ML endeavors.

    The only issue I have is that when summarizing the results of the LDA model using the print(fit.lda), my results do not show standard deviation. Do you know if this is due to a setting in R that needs to be changed?

    Any help is appreciated!


    • Jason Brownlee July 4, 2018 at 8:32 am #

      Yes, I believe the API changed since I wrote the tutorial.

  92. Shane July 7, 2018 at 6:19 am #


    First of all great tutorial, I followed and achieved the expected results
    Really helped me overcome ML jitters. Very very grateful to you.
    But I really wanted to know the mathematical side of these algorithms, what do these do and how?
    it would be wonderful if you could explain things like “relaxation=free” (What does this mean?) That do not have a straight answer on Google

    • Jason Brownlee July 7, 2018 at 6:21 am #

      Thanks for the feedback Shane.

      We focus on the applied side of ML here. For the math, I recommend an academic textbook.

  93. Jesus Suniaga July 12, 2018 at 5:50 am #

    Very nice tutorial. The caret package is a great invent. where can I find a rapid theory of the methods to understand it better?

  94. Ahmed Alwaeli August 3, 2018 at 1:57 am #

    Thanks, Brownlee.

  95. Paul August 23, 2018 at 9:37 am #

    Jason by any chance could you:

    1. Post an unsupervised Random Forest tutorial. I think Caret only supports supervised. If not could you please point me to an example other than Breiman’s

    2. More specifically I am looking for a predict program that takes a saved model eg Random Forest and loops through an input .csv file with class/Type predictions. Think I have the probability figured out

    • Jason Brownlee August 23, 2018 at 1:54 pm #

      Thanks for the suggestion. I generally don’t have material on unsupervised methods and I have not heard of an unsupervised random forest!

  96. Pankaj Sharma August 23, 2018 at 11:49 pm #

    Hi Jason,

    Thank you for the tutorial. This helps me greatly in my study. i am running the code for this sample contributed by Rick Pack, from

    When I insert my mysql database data in the dataset and try to run the above sample, I get the error:
    > validation_index <- createDataPartition(dataset$containsreason, p=0.80, list=FALSE)
    Error in cut.default(y, unique(quantile(y, probs = seq(0, 1, length = groups))), :
    invalid number of intervals

    I tried searching but could not find any instance of this error.


    • Jason Brownlee August 24, 2018 at 6:10 am #

      If you have a problem with someone else’s code, perhaps talk to the about it?

  97. Pankaj Sharma August 24, 2018 at 12:07 am #


    I just figured it out. The data was too sparse as I was including some unwanted columns in the dataset. Once removed, it worked fine.

  98. AMAN VATS September 8, 2018 at 12:00 am #

    It was really amazing.. Please guide me to another projects for practice and to improve my skill set .

    Thank You

  99. ishaq September 13, 2018 at 12:14 am #

    this really was life saving for as it was just when i was wondering to make a structure of my exam report in climate studies. Thank You sooooooooo much

    • Jason Brownlee September 13, 2018 at 8:05 am #

      You’re welcome, I’m happy that it helped!

  100. babu September 25, 2018 at 10:33 pm #

    Working perfectly.Had to install many packages though.But all worked out well with some mouse clicks and with some Google. MY FIRST SUCCESSFUL MACHINE LEARNING TUTORIAL EVER!!!!! MANY THANKS JASON!!-Love and respects from India.

  101. isa September 26, 2018 at 4:49 am #

    Dear Brownlee , first of all thanks for this wonderful tutorial.

    I would like to learn that when we found the most accurate model , how can we ask to our model to test further samples , ie how can we run our test for one more sample data ?

    for example in your test lda was the most accurate, so if you want to ask your program to check for another data what is the code for it?

  102. Tasya October 19, 2018 at 2:02 am #


    Thanks for the tutorial! I copy the code and it works fin till Predictions part. I get an error: Error in eval(predvars, data, env) : object ‘Sepal.Length’ not found. Could you please help me out? Thanks in advance!

  103. Chandrakant Bhogayata November 19, 2018 at 2:12 am #

    How can I analyze Gujarati language texts for readability research by using R package e1071?

  104. javed November 28, 2018 at 12:53 am #

    Hi Jason

    Could you plz guide how can we get the predicted value (especially in regression) for each instance of the dataset. For instance, we have 5 variables, then how can we get the five predicted (numeric) values so that we compare these values with the actual values of the dataset.

  105. Praveena November 29, 2018 at 9:20 pm #

    Hi jason

    I am an enthusiast of R language. I am a asst prof and research scholar so i am working on ML and R. The post was very useful. I already worked with different packages but this is very simple than all other. I need one small advice, how can i make R as favorite language for my students. They are strongly supporting python but i want to make same interest with R also.

    • Jason Brownlee November 30, 2018 at 6:31 am #

      Perhaps show things that R can do that Python cannot?

  106. kuta December 10, 2018 at 8:52 pm #

    Hi Jason, I’m at my wits end here. pulled all of my hair. have given up on google. searched high and low and cannot understand/get the answer to this question.

    i created a model ham/spam classifier…it’s fine. but now i want to use it on a BRAND NEW data. or run 1000 rows of brand new data through it. How the heck do i do this?

    i used the predict function, it works fine on the test data but on the new data (after i’ve done all the preprocessing) i get an error of object not found … object is a word and i’m assuming it doesn’t exist in one of the sets…i haven’t a clue.

    so i guess i’m missing a step somewhere? please help

    • Jason Brownlee December 11, 2018 at 7:42 am #

      Sorry to hear that.

      Perhaps post your code and error to stackoverflow or crossvalidated?

Leave a Reply