dimanche 30 avril 2023

How to generate random numbers fast from "An Extremely Big Array" in Python?

Recently, I got stuck searching for a fast way to generate random numbers in Python. Here like the code below, I collect points from an area with more than 10 million points. And I want to sample 8192 points randomly from the area points. However, np.random.choice(np.arange(...)) makes the program so slow since the large index arrays.

So, how to make this procedure faster? With any useful Python package or function? Thanks for your attention!

import numpy as np

area_idx = self.area_idxs[idx]
points = self.area_points[area_idx]  # 14350962 * 3
labels = self.area_labels[area_idx]  # 14350962
npoints = 8192
# np.random.choice(np.arange(...)) makes the program so slow.
selected_point_idxs = np.random.choice(np.arange(len(points)), npoints, replace=False)

Generate random numbers fast from "An Extremely Big Array" in Python.




Aucun commentaire:

Enregistrer un commentaire