Tony
Tony

Reputation: 177

Setting Seed to reproduce same results every time

I have the following code, but even though I have set the seed the result differs every time I run it.

boostraps_num = 1200
boostraps_auc =[]
n = len(y_predprob)

np.random.RandomState(3)
for i in range(boostraps_num):
    index = np.random.randint(0, n, n)
    score = roc_auc_score(y[index], y_predprob[index])
    boostraps_auc.append(score)

I also tried using the following but it just made each number the same.

np.random.RandomState(3).randint(0, n, n)

Upvotes: 0

Views: 762

Answers (1)

AKX
AKX

Reputation: 168863

In the first example, you're not using the new RandomState random-number generator you create at all; you create an object and throw it away.

In the second example, you create a new RandomState for each random number, the RNG always has the same initial state, and you only ever use it once, so you always get the same number.

import numpy as np

y_predprob = [1, 2, 3]
boostraps_num = 15
boostraps_auc = []
n = len(y_predprob)

rng = np.random.RandomState(3)
for i in range(boostraps_num):
    index = rng.randint(0, n, n)
    boostraps_auc.append(index)
print(boostraps_auc)

will always print the same result.

Upvotes: 1

Related Questions