简体   繁体   English

如何忽略-inf并移至for循环中的下一个迭代

[英]How to disregard -inf and move to next iteration in for loop

Hello I'm hoping for some help I'm quite new to python and I wanted some advice on ways I can set up a condition so that if -inf is encounter in my data then the programme will loop to the next iteration 您好,我希望对python刚起步的人有所帮助,并且希望获得一些关于如何设置条件的建议,以便在数据中遇到-inf时,程序将循环至下一次迭代

import numpy as np
import math
import matplotlib.pylab as plt
import pandas as pd
from scipy.interpolate import interp1d
from scipy.signal import butter, filtfilt
from scipy import interpolate
Ic = 400
lower_Ig = 720 #the lower limit of the generator current Ig
Upper_Ig = 1040 #Upper limit
Ix=range(-60,61,1)
for j in range(40, 80, 10):
    Var=(40000* j)/ 10000
    #print Var
    for c in range(lower_Ig, Upper_Ig+1, 40):
        #print c
        Names =['Vg','V3', 'V4']
        Data = pd.read_csv('/Documents/JTL_'+str(Var)+'/Ig='+str(c)+'/Grey_Zone.csv', names=Names)
        Vg = Data['Vg']
        V3 = Data['V3']
        V4 = Data ['V4']
        Prf = V4 / Vg
        #print Prf
        C = 0.802
        freq = 100
        b, a = butter(2, (5/C)/(freq/2), btype = 'low')
        yg = filtfilt(b, a, Vg)  # filter with phase shift correction
        y4 = filtfilt(b, a, V4)  # filter with phase shift correction
        SW = y4 / yg
        if SW == np.nan: #I need a condition here that if -inf is encountered then the programme should loop to next c value in for loop 
            continue 
            f = interp1d( SW, Ix )
            print f(0.25), f(0.5), f(0.75)
            print f(0.75)-f(0.25)

I have attempted using different numpy functions but I always get the same error 我尝试使用不同的numpy函数,但始终遇到相同的错误

The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 

I dont think I can use any() or all() since that will just include all the data and I want to ignore -inf. 我不认为我可以使用any()all()因为这将仅包括所有数据,并且我想忽略-inf。 Any help is greatly appreciated 任何帮助是极大的赞赏

Suppose we have SW in one iteration of the loop looking like this: 假设在循环的一次迭代中有SW ,如下所示:

>>> import numpy as np
>>> SW = [np.inf, -np.inf, np.nan, 0, 1]
>>> np.isfinite(SW)
[False, False, False, True, True]
>>> all(np.isfinite(SW))
False   # since one or more in the list is False

If you want to skip any SW that has nan, inf, -inf you can use 如果要跳过任何具有nan, inf, -inf SW ,则可以使用

if not all(np.isfinite(SW)):
    continue

if nan is not a problem and only -inf is then you can use 如果nan不是问题,只有-inf ,则可以使用

if any(np.isneginf(SW)):
   continue

Which will skip the iteration if any element of SW is -inf 如果SW任何元素为-inf它将跳过迭代

Note that you cannot compare for equality using == with np.nan 请注意,您不能将==np.nan进行相等性比较

>>> x = np.nan
>>> x == np.nan
False

instead you use isnan 相反,您使用isnan

>>> np.isnan(x)
True

您可以filter掉不需要的元素(例如newlist=filter(lambda n: not numpy.isneginf(n), list_of_numbers) ),或仅使用numpy.nan_to_num(...)转换为适当的数字列表。

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

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