dimanche 16 août 2020

Why to initialize an array in Numpy

I'm doing a DataCamp course on statistical thinking in Python. At one point in the course, the instructor advises initializing an empty array before filling it with random floats, e.g.

rand_nums = np.empty(100_000)
for i in range(100_000):
    rand_nums[i] = np.random.random()

In theory, is there any reason to initialize an empty array before filling it? Does it save on memory? What is the advantage over the code above as compared to simply the following:

rand_nums = np.random.random(size=100_000)



Aucun commentaire:

Enregistrer un commentaire