-2

I implemented a loop to eliminate the 0 values from the sample dataset.

x=[1,4,4,7,11,13,15,15,17,18,19,19,20,20,22,23,28,29,31,32,36,37,47,48,49,50,54,54,55,59,59,61,61,66,72,72,75,78,78,81,93,96,99,108,113,114,120,120,120,123,124,129,131,137,145,151,156,171,176,182,188,189,195,203,208,215,217,217,217,224,228,233,255,271,275,275,275,286,291,312,312,312,315,326,326,329,330,336,338,345,348,354,361,364,369,378,390,457,467,498,517,566,644,745,871,1312,1357,1613,1630]
from numpy import linspace
import scipy.stats
import numpy as np
p_set=linspace(0.0,1,10,endpoint=True)
q_set=linspace(0.0,1,10,endpoint=True)
temp=0

for k in x:
    if (scipy.stats.gamma.pdf(k,2,1)==0.0):
    x.remove(k)
print(x)
for k in x:
    temp+=np.log(scipy.stats.gamma.pdf(k,2,1)).sum()

but this appears
RuntimeWarning: divide by zero encountered in log

I printed the x to check and realised that two values are not eliminated from the list. the values are 0000000e+000. what can be the reason? and how will i deal with this??

ruhi
  • 3
  • 2

4 Answers4

1

Using list-comprehension:

x_lst=[1,4,4,7,11,13,15,15,17,18,19,19,20,20,22,23,28,29,31,32,36,37,47,48,49,50,54,54,55,59,59,61,61,66,72,72,75,78,78,81,93,96,99,108,113,114,120,120,120,123,124,129,131,137,145,151,156,171,176,182,188,189,195,203,208,215,217,217,217,224,228,233,255,271,275,275,275,286,291,312,312,312,315,326,326,329,330,336,338,345,348,354,361,364,369,378,390,457,467,498,517,566,644,745,871,1312,1357,1613,1630]

To remove all the entities containg 0, i.e. 1,2,50 > 1,2:

print([x for x in x_lst if not '0' in str(x)])

To remove only the 0 from an item, i.e. 1,2,50 > 1,2,5:

print([int(str(x).strip('0')) if '0' in str(x) else x for x in x_lst ])
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
0

I would use a numpy filter instead of looping through the array like so:

x=[1,4,4,7,11,13,15,15,17,18,19,19,20,20,22,23,28,29,31,32,36,37,47,48,49,50,54,54,55,59,59,61,61,66,72,72,75,78,78,81,93,96,99,108,113,114,120,120,120,123,124,129,131,137,145,151,156,171,176,182,188,189,195,203,208,215,217,217,217,224,228,233,255,271,275,275,275,286,291,312,312,312,315,326,326,329,330,336,338,345,348,354,361,364,369,378,390,457,467,498,517,566,644,745,871,1312,1357,1613,1630]
from numpy import linspace
import scipy.stats
import numpy as np
p_set=linspace(0.0,1,10,endpoint=True)
q_set=linspace(0.0,1,10,endpoint=True)
temp=0
x = np.array(x)
x = x[np.where(scipy.stats.gamma.pdf(x,2,1)==0.0, False, True)]
print(x)
for k in x:
    temp+=np.log(scipy.stats.gamma.pdf(k,2,1)).sum()

However I would also like to point out that comparing any floating value using == is generally a bad idea because of rounding and precision issues (e.g. as described in this post: What is the best way to compare floats for almost-equality in Python?)

Michamei
  • 348
  • 1
  • 11
0

While I agree that Michameis answer, I want to add another thing: You can use np.seterr(all="raise") to raise all numpy warnings as exceptions. This way, you don't have to filter the values beforehand and can just try out if the operations work:

import scipy.stats
import numpy as np

x=[1,4,4,7,11,13,15,15,17,18,19,19,20,20,22,23,28,29,31,32,36,37,47,48,49,50,54,54,55,59,59,61,61,66,72,72,75,78,78,81,93,96,99,108,113,114,120,120,120,123,124,129,131,137,145,151,156,171,176,182,188,189,195,203,208,215,217,217,217,224,228,233,255,271,275,275,275,286,291,312,312,312,315,326,326,329,330,336,338,345,348,354,361,364,369,378,390,457,467,498,517,566,644,745,871,1312,1357,1613,1630]

np.seterr(all='raise')
temp = 0
for k in x:
    try:
        temp += np.log(scipy.stats.gamma.pdf(k, 2, 1)).sum()
    except FloatingPointError:
        pass
Niklas Mertsch
  • 1,399
  • 12
  • 24
0
x=[1,4,4,7,11,13,15,15,17,18,19,19,20,20,22,23,28,29,31,32,36,37,47,48,49,50,54,54,55,59,59,61,61,66,72,72,75,78,78,81,93,96,99,108,113,114,120,120,120,123,124,129,131,137,145,151,156,171,176,182,188,189,195,203,208,215,217,217,217,224,228,233,255,271,275,275,275,286,291,312,312,312,315,326,326,329,330,336,338,345,348,354,361,364,369,378,390,457,467,498,517,566,644,745,871,1312,1357,1613,1630]
x = [i for i in x if i != 0]

Will do the job for you. ^-^

Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40