List comprehensions in NumPy arrays

Viewed 114

In essence this is what I want to create

import numpy as np
N = 100  # POPULATION SIZE
D = 30  # DIMENSIONALITY
lowerB = [-5.12] * D  # LOWER BOUND (IN ALL DIMENSIONS)
upperB = [5.12] * D  # UPPER BOUND (IN ALL DIMENSIONS)
# INITIALISATION PHASE
X = np.empty([N, D])  # EMPTY FLIES ARRAY OF SIZE: (N,D)
# INITIALISE FLIES WITHIN BOUNDS
for i in range(N):
    for d in range(D):
        X[i, d] = np.random.uniform(lowerB[d], upperB[d])

but I want to do so without the for loops to save time and use List comprehensions I have try things like

np.array([(x,y)for x in range(N)for y in range(D)])

but this doesn’t get me to an array like array([100,30]). Does anyone know a tutorial or the correct documentation I should be looking at so I can learn exactly how to do this?

0 Answers
Related