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)
    [ X0        ]
0 : [ -0.721533 ]
1 : [ -0.241223 ]
2 : [ -1.78796  ]
3 : [  0.40136  ]
4 : [  1.36783  ]

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)
New indices :  [2,3,1,4,0]

Eventually the randomized sample is

print(sample[[int(i) for i in newIndices]])
    [ X0        ]
0 : [ -1.78796  ]
1 : [  0.40136  ]
2 : [ -0.241223 ]
3 : [  1.36783  ]
4 : [ -0.721533 ]

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