# How to correct for results varying with random seed?

(I am mostly self-taught in this area, and apologize if there is a simple solution that I managed to miss somewhere along the way.)

For several of my models (applied to different datasets), I have noticed that the seed used in the model fitting process can have a significant impact on the results. I understand that we should expect some variability with the results in machine learning; however, the variability I was seeing was enough to change the interpretation of the results--not something that fills me with confidence in my models. I considered taking a random sample of random seeds and taking the average of the coefficients produced, but that would only work for models with coefficients. I considered taking that approach and averaging the chosen hyper-parameters, but the resulting model would still be prone to seed variance. I had the thought that I should include in the cross-validation process a means of minimizing variance in parameters, hyper-parameters or fitted valued across random seeds, but I did not want to experiment with applying that step without being able to justify that it could produce more robust results.

I have seen this topic broached on other posts, but the prescriptions have included everything from add features, remove features, use more folds, use less folds, use a different error function, use a different model, etc. I wanted to pose this specific question to get more precise advice for how to address this problem.

• If you're using a well-vetted random number generator, such as the Mersenne twister that's the default in R, then it should be rare (like 5% of the time) that you would get a truly statistically significant difference between two runs with two different seeds. // It would be interesting to see what your precise difficulty is. Can you replicate an example as an addendum to your question? Or is the procedure to long/messy for that? – BruceET Feb 1 at 0:45
• in R: set.seed(5678); rbinom(1, 100, .3) returns $24$ and set.seed(1234); rbinom(1, 100, .3) returns $28.$ Certainly, noticeably different, but not significantly different: prop.test(c(24, 28), c(100,100))$p.val returns$0.628656;$far above$5\% = 0.05.\$ – BruceET Feb 1 at 0:48
• @BruceET: even with a good RNG (or actual random numbers): if the model training procedure is unstable the random differences can cause substantial and significant variance in the models. – cbeleites unhappy with SX Feb 1 at 13:51
• Then the 'model training procedure' seems worthless. – BruceET Feb 1 at 18:25

First of all, observing substantial variation depending on the random seed means that you should explore this in more detail, just like you'd explore and deal with any other important source of random uncertainty in your modeling process:

• At the very least, measure the variance you get from this source of variation.
Do this by re-running the entire fitting procedure with varying random seeds sufficiently often to get an estimate of the variance this causes in your final results (e.g. various figures of merit of the verification/validation of the model).

Once you know the variance and know that it is important, there are usually 2 very different approaches to deal with random uncertainty:

• You can reduce the variance in the model training process, i.e. stabilize it/make it more robust. This is usually done by restricting model complexity, e.g. heavier regularization, using less features from the very beginning (data-driven feature selection will typically not help here, but rather exhibit unstable selection of features), put a penalty on hyperparameter optimization that acts towards less complex models, ...

• The 2nd approach is to average over a sufficiently large population that exhibits the variance in question, in order to obtain a "mean" that is subject to less variance.
This would be an aggregated model consisting of an ensemble of models that were obtained from varying random seeds in your case.

The verification and validation of the final optimized model should not enter these thoughts in any way: that part needs to be entirely independent of the training process.

However, the final model validation should include ruggedness wrt. random seed.

More or less folds in the cross validation should ideally not influence the cross validation results. Looking at variance between folds without distinguishing variance due to finite number of tested cases (which varies per fold but it constant once you look at all folds of a CV run) from variance due to model instability will give a hard to interpret conglomerate of these two factors.

There are some optimization heuristics (like the one-sd-rule) that nevertheless use between-fold-variance. Since you do have substantial model instability, you'd need to dig down and at least find out roughly what the dominating source of variance is.