<i id='h4RRf'><tr id='h4RRf'><dt id='h4RRf'><q id='h4RRf'><span id='h4RRf'><b id='h4RRf'><form id='h4RRf'><ins id='h4RRf'></ins><ul id='h4RRf'></ul><sub id='h4RRf'></sub></form><legend id='h4RRf'></legend><bdo id='h4RRf'><pre id='h4RRf'><center id='h4RRf'></center></pre></bdo></b><th id='h4RRf'></th></span></q></dt></tr></i><div id='h4RRf'><tfoot id='h4RRf'></tfoot><dl id='h4RRf'><fieldset id='h4RRf'></fieldset></dl></div>

      <small id='h4RRf'></small><noframes id='h4RRf'>

    1. <legend id='h4RRf'><style id='h4RRf'><dir id='h4RRf'><q id='h4RRf'></q></dir></style></legend>
      <tfoot id='h4RRf'></tfoot>

        <bdo id='h4RRf'></bdo><ul id='h4RRf'></ul>

      Python numpy.nan 和逻辑函数:错误的结果

      Python numpy.nan and logical functions: wrong results(Python numpy.nan 和逻辑函数:错误的结果)
      <i id='Bc24O'><tr id='Bc24O'><dt id='Bc24O'><q id='Bc24O'><span id='Bc24O'><b id='Bc24O'><form id='Bc24O'><ins id='Bc24O'></ins><ul id='Bc24O'></ul><sub id='Bc24O'></sub></form><legend id='Bc24O'></legend><bdo id='Bc24O'><pre id='Bc24O'><center id='Bc24O'></center></pre></bdo></b><th id='Bc24O'></th></span></q></dt></tr></i><div id='Bc24O'><tfoot id='Bc24O'></tfoot><dl id='Bc24O'><fieldset id='Bc24O'></fieldset></dl></div>

          <bdo id='Bc24O'></bdo><ul id='Bc24O'></ul>

          <small id='Bc24O'></small><noframes id='Bc24O'>

          1. <tfoot id='Bc24O'></tfoot>
            <legend id='Bc24O'><style id='Bc24O'><dir id='Bc24O'><q id='Bc24O'></q></dir></style></legend>
              <tbody id='Bc24O'></tbody>
              • 本文介绍了Python numpy.nan 和逻辑函数:错误的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我在尝试评估时得到了一些令人惊讶的结果可能包含 nan 值的数据的逻辑表达式(在 numpy 中定义).

                I get some surprising results when trying to evaluate logical expressions on data that might contain nan values (as defined in numpy).

                我想了解为什么会出现这种结果以及如何正确实施.

                I would like to understand why this results arise and how to implement the correct way.

                我不明白为什么这些表达式的计算结果是它们所做的值:

                What I don't understand is why these expressions evaluate to the value they do:

                from numpy import nan
                
                nan and True
                >>> True
                # this is wrong.. I would expect to evaluate to nan
                
                True and nan
                >>> nan
                # OK
                
                nan and False
                >>> False
                # OK regardless the value of the first element 
                # the expression should evaluate to False
                
                False and nan
                >>> False
                #ok
                

                也类似:

                True or nan
                >>> True #OK
                
                nan or True
                >>> nan #wrong the expression is True
                
                False or nan
                >>> nan #OK
                
                nan or False
                >>> nan #OK
                

                如何(以有效的方式)实现正确的布尔函数,同时处理 nan 值?

                How can I implement (in an efficient way) the correct boolean functions, handling also nan values?

                推荐答案

                您可以使用 numpy 命名空间中的谓词:

                You can use predicates from the numpy namespace:

                >>> np.logical_and(True, np.nan), np.logical_and(False, np.nan)
                (True, False)
                >>> np.logical_and(np.nan, True), np.logical_and(np.nan, False)
                (True, False)
                >>>
                >>> np.logical_or(True, np.nan), np.logical_or(False, np.nan)
                (True, True)
                >>> np.logical_or(np.nan, True), np.logical_or(np.nan, False)
                (True, True)
                

                内置布尔运算符略有不同.来自文档:x and y 等价于 if x is false, then x, else y.因此,如果第一个参数的计算结果为 False,它们将返回它(不是它的布尔等价物,因为它是).因此:

                The built-in boolean operators are slightly different. From the docs : x and y is equivalent to if x is false, then x, else y. So, if the first argument evaluates to False, they return it (not its boolean equivalent, as it were). Therefore:

                >>> (None and True) is None
                True
                >>> [] and True
                []
                >>> [] and False
                []
                >>> 
                

                这篇关于Python numpy.nan 和逻辑函数:错误的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                How to extend Python class init(如何扩展 Python 类初始化)
                What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                Initialize list with same bool value(使用相同的布尔值初始化列表)
                setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                  • <legend id='f3oGc'><style id='f3oGc'><dir id='f3oGc'><q id='f3oGc'></q></dir></style></legend>
                  • <tfoot id='f3oGc'></tfoot>
                    <i id='f3oGc'><tr id='f3oGc'><dt id='f3oGc'><q id='f3oGc'><span id='f3oGc'><b id='f3oGc'><form id='f3oGc'><ins id='f3oGc'></ins><ul id='f3oGc'></ul><sub id='f3oGc'></sub></form><legend id='f3oGc'></legend><bdo id='f3oGc'><pre id='f3oGc'><center id='f3oGc'></center></pre></bdo></b><th id='f3oGc'></th></span></q></dt></tr></i><div id='f3oGc'><tfoot id='f3oGc'></tfoot><dl id='f3oGc'><fieldset id='f3oGc'></fieldset></dl></div>
                        <tbody id='f3oGc'></tbody>

                      <small id='f3oGc'></small><noframes id='f3oGc'>

                        • <bdo id='f3oGc'></bdo><ul id='f3oGc'></ul>