简体   繁体   English

无法从列表中消除零值

[英]inability to eliminate zero values from the list

I implemented a loop to eliminate the 0 values from the sample dataset.我实现了一个循环来消除样本数据集中的 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]
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 RuntimeWarning:在日志中遇到除以零

I printed the x to check and realised that two values are not eliminated from the list.我打印了 x 来检查并意识到两个值没有从列表中删除。 the values are 0000000e+000.值为 0000000e+000。 what can be the reason?可能是什么原因? and how will i deal with this??我将如何处理这个?

Using list-comprehension :使用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 , ie 1,2,50 > 1,2 :要删除所有包含0的实体,即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, ie 1,2,50 > 1,2,5 :要仅从项目中删除0 ,即1,2,50 > 1,2,5

print([int(str(x).strip('0')) if '0' in str(x) else x for x in x_lst ])

I would use a numpy filter instead of looping through the array like so:我会使用 numpy 过滤器,而不是像这样循环遍历数组:

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 (eg as described in this post: What is the best way to compare floats for almost-equality in Python? )但是我还想指出,由于舍入和精度问题,使用 == 比较任何浮点值通常是一个坏主意(例如,如本文所述: What is the best way to compare floats for most-equality in Python? )

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.虽然我同意 Michameis 的回答,但我想补充一点:您可以使用np.seterr(all="raise")将所有 numpy 警告作为例外。 This way, you don't have to filter the values beforehand and can just try out if the operations work:这样,您不必事先过滤值,只需try操作是否有效:

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
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.将为您完成工作。 ^-^ ^-^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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