Randomize the lines of a SampleΒΆ

import openturns as ot

In this short example we present a simple way to mix the lines of a sample thanks to the KPermutationsDistribution class.

We first define a small sample of size \sampleSize based on a standard unit Gaussian distribution.

distribution = ot.Normal()
N = 5
sample = distribution.getSample(N)

We print the sample :

sample
X0
00.6082017
1-1.266173
2-0.4382656
31.205478
4-2.181385


A new set of randomly mixed indices is a realization of a permutation of \sampleSize elements amongst \sampleSize: This generates a random permutation of the integers \{0, ..., \sampleSize - 1\}.

mixingDistribution = ot.KPermutationsDistribution(N, N)
newIndices = mixingDistribution.getRealization()

The new indices will be these ones :

print("New indices : ", newIndices)
New indices :  [2,4,3,0,1]

Eventually the randomized sample is

print(sample[[int(i) for i in newIndices]])
    [ X0        ]
0 : [ -0.438266 ]
1 : [ -2.18139  ]
2 : [  1.20548  ]
3 : [  0.608202 ]
4 : [ -1.26617  ]