0

I am trying to compute a weighted average with the code:

    def estimate(particles, weights):
        mean = np.average(particles, axis=particles[:, :], weights=weights)
        var = np.average((particles - mean)**2, weights, axis=0)
        return mean, var

However, I cannot seem to get my axis correct. The shape of my particles is (100,2) and the shape of my weights is (100,), and I would like to do the averaging vertically, so adding particles[0][0] with particles[1][0].... particles[99][0] and the same with particles[0][1],... particles[99][1]. How do I create an axis to do this?

  • 1
    Please print out the shape of your array and weights array using `array.shape` – Ehsan Jun 29 '20 at 03:09
  • "How do I fix this?" Well, did you try providing a one-dimensional value for `weights`, like it told you, and like you apparently understand that it is telling you? – Karl Knechtel Jun 29 '20 at 03:10
  • 1
    I'm not understanding `axis=particles[:, :]`. Do you simply mean to average over the entire array? If so just delete the axis specification as `axis=None` is the default. See [this](https://numpy.org/doc/1.18/reference/generated/numpy.average.html). – Mike O'Connor Jun 29 '20 at 09:11

0 Answers0