Detect if a NumPy array contains at least one non-numeric value?

后端 未结 5 1420
逝去的感伤
逝去的感伤 2021-01-30 07:46

I need to write a function which will detect if the input contains at least one value which is non-numeric. If a non-numeric value is found I will raise an error (because the ca

5条回答
  •  心在旅途
    2021-01-30 08:38

    If infinity is a possible value, I would use numpy.isfinite

    numpy.isfinite(myarray).all()
    

    If the above evaluates to True, then myarray contains no, numpy.nan, numpy.inf or -numpy.inf values.

    numpy.nan will be OK with numpy.inf values, for example:

    In [11]: import numpy as np
    
    In [12]: b = np.array([[4, np.inf],[np.nan, -np.inf]])
    
    In [13]: np.isnan(b)
    Out[13]: 
    array([[False, False],
           [ True, False]], dtype=bool)
    
    In [14]: np.isfinite(b)
    Out[14]: 
    array([[ True, False],
           [False, False]], dtype=bool)
    

提交回复
热议问题