1

I have a list of which I want to know the mean value. I found online that the mean() function of the statistics package is designed to be precise ( Why is statistics.mean() so slow?) However, I find it rather inprecise, rounding my means.

example, a list (agent.rr) that of length 100 that contains 5 nonzero inputs: -200, -200, -200, -200 and -240.

if I calcualte:

statistics.mean(agent.rr)

I get the output

-10

If i calculate:

mean(agent.rr)/len(agent.rr)

I get the correct output

-10.4

Why does statistics.mean() not provide the correct answer?

I tried to make some random examples, like:

mylist = [1,2,2]

But statistics.mean() does seem to provide the correct answer whenever I just manually try some lists, but whenever I call it on the list agent.rr it always rounds.

Rens
  • 177
  • 9
  • 2
    Where does `agent.rr` come from? i.e. what is the actual type of that sequence, and what are the types of each numerical element? – Random Davis Sep 21 '20 at 15:35
  • 2
    I cannot reproduce the issue. ``statistics.mean([-200, -200, -200, -200, -240, *[0]*95])`` evaluates to ``-10.4``. – MisterMiyagi Sep 21 '20 at 15:37
  • agent.rr gets filled in a loop. rewards = [] . for j in range(100): r = eval.env.step(action) rewards.append(r) statistics.mean(rewards) . To be able to see the rewards list I save it : self.rr = rewards – Rens Sep 21 '20 at 16:26
  • I am afraid that is not helpful either. What is ``eval``, what is ``eval.env``, what is ``eval.env.step``? As has been asked can you please clarify what is the type of the elements in the list? – MisterMiyagi Sep 21 '20 at 16:44
  • It are all integers. eval.env was an error, that should be eval_env. This is an environment I have build (for Reinforcement Learning). It returns a reward for performing an action when being in a certain state – Rens Sep 21 '20 at 17:26
  • Please try to provide a [mcve]. As noted above, this is not reproducible with a list of all integers. – MisterMiyagi Sep 21 '20 at 17:48
  • 1
    @Rens all relevant/required code and examples should be in the original question, not supplied as comments (or external links). – Random Davis Sep 21 '20 at 20:40

0 Answers0