1

When trying to calculate the sample mean of some data, based on the number of samples (first for 1 sample, then for 2 and so on...) I encounter this problem:

/usr/local/lib/python3.6/dist-packages/numpy/core/fromnumeric.py:3584: RuntimeWarning: Degrees of freedom <= 0 for slice
  **kwargs)
/usr/local/lib/python3.6/dist-packages/numpy/core/_methods.py:209: RuntimeWarning: invalid value encountered in double_scalars
  ret = ret.dtype.type(ret / rcount)

while using numpy function "np.var()" on a data array.

my only function is this one:

def estimate_var(lam, n):
    np.random.seed(7)
    data = np.random.exponential(scale=1/lam, size=n)
    new_data = [np.var(data[:index + 1], ddof=1) for index in range(len(data))]
    return new_data

(THE 4th line causes the problem)

asakasher
  • 13
  • 3

1 Answers1

0

That is not an error it is a warning. You are getting the warning because you are taking the variance (np.var) of an array which has only 1 value which does not exist.

When index=0 at the start of your list comprehension you are taking the variance of data[:0+1] which is just a single value.

If you want to take the variance of the data then just do np.var(data)

BenT
  • 3,172
  • 3
  • 18
  • 38