简体   繁体   中英

Weighted mean in numpy/python

I have a big continuous array of values that ranges from (-100, 100)

Now for this array I want to calculate the weighted average described here

since it's continuous I want also to set breaks for the values every 20 ie the values should be discrete as -100 -80 -60 .... 60 80 100

How can I do this in NumPy or python in general?

EDIT: the difference here from the normal mean, that the mean is calculated according to the frequency of values

You actually have 2 different questions.

  1. How to make data discrete, and
  2. How to make a weighted average.

It's usually better to ask 1 question at a time, but anyway.

Given your specification:

xmin = -100
xmax = 100
binsize = 20

First, let's import numpy and make some data:

import numpy as np
data = numpy.array(range(xmin, xmax))

Then let's make the binnings you are looking for:

bins_arange = numpy.arange(xmin, xmax + 1, binsize) 

From this we can convert the data to the discrete form:

counts, edges = numpy.histogram(data, bins=bins_arange)

Now to calculate the weighted average, we can use the binning middle (eg numbers between -100 and -80 will be on average -90):

bin_middles = (edges[:-1] + edges[1:]) / 2

Note that this method does not require the binnings to be evenly "spaced", contrary to the integer division method.

Then let's make some weights:

weights = numpy.array(range(len(counts)) / sum(range(len(counts))

Then to bring it all together:

average =          np.sum(bin_middles * counts * 1) / sum(counts)
weighted_average = np.sum(bin_middles * counts * weights) / sum(counts)

For the discretization (breaks), here is a method using the python integer division :

import numpy as np
values = np.array([0, 5, 10, 11, 21, 24, 48, 60])
(values/20) *20
# or (a/10).astype(int)*10 to force rounding

that will print :

aarray([ 0,  0,  0,  0, 20, 20, 40, 60])

For the weighted mean, if you have another array with the weights for each point, you can use :

weighted_means = sum([ w*v for w,v in zip(weights, values)]) / sum( w*w )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM