Home > Enterprise >  How to get a random value from a numpy array?
How to get a random value from a numpy array?

Time:02-22

So I have a large numpy array with different data inside. I would like to extract a random array from this.

The dataset would look something like this:

[[ 0.00770334 -1.4224063  -2.4392433  ...  2.1296244   1.7076529
0.2145994 ]
[-0.9572602  -2.1521447  -2.7491045  ... -3.784852   -2.7787943
-1.727039  ]
[ 0.0106896  -1.8116834  -2.879299   ... -2.1569414  -0.56878865
0.49337843]
...
[-1.3776227  -3.2443993  -3.999786   ...  0.94508815  0.28934643
-1.6581179 ]
[-1.3281708  -2.0682802  -2.1291382  ... -0.1525341   0.0662522
0.54083973]
[ 0.7783776  -0.07668249 -0.9315217  ... -3.2098405  -2.137057
-1.103238  ]]

So I would like to get one of these random arrays out e.g.

[-1.3776227  -3.2443993  -3.999786   ...  0.94508815  0.28934643
-1.6581179 ]

CodePudding user response:

data[np.random.choice(len(data), size=##Number of samples you need, replace=False)]

CodePudding user response:

try this:

random_index = np.random.randint(0, array.shape[0])
array[random_index]
  • Related