Search code examples
pythonarraysnumpynumpy-ndarray

Getting mean of 2D array


I have a vector representation of n = 1000 images, where each image is represented as 2048 numbers. So I have a numpy array with a shape of (1000, 2048) that I need to find the mean of in a 2048-d vector. If I run this function:

def get_means(f_embeddings):
    means = []
    for embedding in f_embeddings:
        means.append(np.mean(embedding))
    return np.array(means)

I get an ndarray of shape (1000,). How do I loop loop over the array correctly to have a 2048-d vector of means from the original array?


Solution

  • Try:

    np.mean(f_embeddings, axis=0)
    

    which should do it without the loop.