Why Do I Get Different Results Each Time in Machine Learning?

Are you getting different results for your machine learning algorithm?
Perhaps your results differ from a tutorial and you want to understand why.
Perhaps your model is making different predictions each time it is trained, even when it is trained on the same data set each time.
This is to be expected and might even be a feature of the algorithm, not a bug.
In this tutorial, you will discover why you can expect different results when using machine learning algorithms.
After completing this tutorial, you will know:

Machine learning algorithms will train different models if the training dataset is changed.
Stochastic machine learning algorithms use randomness during learning, ensuring a different model is trained each run.
Differences in the development environment, such as software versions and CPU type, can cause rounding error differences in predictions and model evaluations.

Let’s get started.

Why Do I Get Different Results Each Time in Machine Learning? Photo by Bonnie Moreland , some rights reserved.

Tutorial Overview
This tutorial is divided into five parts; they are:

I’m Getting Different Results!
Differences Caused by Training Data

How to Fix

Differences Caused by Learning Algorithm

How to Fix

Differences Caused by Evaluation Procedure

How to Fix

Differences Caused by Platform

How to Fix

1. I’m Getting Different Results!
Machine learning algorithms or models can give different results.
It’s not your fault. In fact, it is often a feature, not a bug.
We will clearly specify and explain the problem you are having.
First, let’s get a handle on the basics.
In applied machine learning, we run a machine learning “ algorithm ” on a dataset to get a machine learning “ model .” The model can then be evaluated on data not used during training or used to make predictions on new data, also not seen during training.

Algorithm : Procedure run on data that results in a model (e.g. training or learning).

Model : Data structure and coefficients used to make predictions on data.

For more on the difference between machine learning algorithms and models, see the tutorial:

Difference Between Algorithm and Model in Machine Learning

Supervised machine learning means we have examples (rows) with input and output variables (columns). We cannot write code to predict outputs given inputs because it is too hard, so we use machine learning algorithms to learn how to predict outputs from inputs given historical examples.
This is called function approximation , and we are learning or searching for a function that maps inputs to outputs on our specific prediction task in such a way that it has skill, meaning the performance of the mapping is better than random and...