I have a matrix of dimensions n x d and a vector of labels 1 x n.
n = 3
d = 2
data = np.random.rand((n,d))
labels = np.random.choice(1, n)
I want to iterate over the ith column of data and the ith element of labels at the same time. So far, I have:
for i in range(n):
x = data[i]
y = labels[i]
... do something with them ..
I've tried to use np.nditer to do this, but have trouble getting the vector and matrix to work together nicely:
for x, y in np.nditer([data, labels]):
...
ValueError: operands could not be broadcast together with shapes (3,2) (3,)
nditer; it's too complicated for this. How aboutzip?zipseems a little slow as well, so I guess it must not be the iteration causing the bottleneck. Thanks!nditerdoes not speed it up. In most cases, the really slow part of iteration is the "do something". The iteration mechanism is a minor part of the overall time.