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 N based on a standard unit gaussian distribution.

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

We print the sample :

print(sample)

Out:

    [ X0        ]
0 : [  0.608202 ]
1 : [ -1.26617  ]
2 : [ -0.438266 ]
3 : [  1.20548  ]
4 : [ -2.18139  ]

A new set of randomly mixed indices is a realization of a permutation of N elements amongst N :

mixingDistribution = ot.KPermutationsDistribution(N, N)
newIndices = mixingDistribution.getSample(1)[0,:]

The new indices will be these ones :

print("New indices : ", newIndices)

Out:

New indices :  [2,4,3,0,1]

Eventually the randomized sample is

print( sample[[int(i) for i in newIndices]])

Out:

    [ X0        ]
0 : [ -0.438266 ]
1 : [ -2.18139  ]
2 : [  1.20548  ]
3 : [  0.608202 ]
4 : [ -1.26617  ]

Total running time of the script: ( 0 minutes 0.002 seconds)

Gallery generated by Sphinx-Gallery