How does python numpy.where() work?

前端 未结 3 1518
礼貌的吻别
礼貌的吻别 2020-12-04 14:54

I am playing with numpy and digging through documentation and I have come across some magic. Namely I am talking about numpy.where():



        
3条回答
  •  庸人自扰
    2020-12-04 15:33

    Old Answer it is kind of confusing. It gives you the LOCATIONS (all of them) of where your statment is true.

    so:

    >>> a = np.arange(100)
    >>> np.where(a > 30)
    (array([31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
           48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
           65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
           82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98,
           99]),)
    >>> np.where(a == 90)
    (array([90]),)
    
    a = a*40
    >>> np.where(a > 1000)
    (array([26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
           43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
           60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
           77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93,
           94, 95, 96, 97, 98, 99]),)
    >>> a[25]
    1000
    >>> a[26]
    1040
    

    I use it as an alternative to list.index(), but it has many other uses as well. I have never used it with 2D arrays.

    http://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html

    New Answer It seems that the person was asking something more fundamental.

    The question was how could YOU implement something that allows a function (such as where) to know what was requested.

    First note that calling any of the comparison operators do an interesting thing.

    a > 1000
    array([False, False, False, False, False, False, False, False, False,
           False, False, False, False, False, False, False, False, False,
           False, False, False, False, False, False, False, False,  True,
            True,  True,  True,  True,  True,  True,  True,  True,  True,
            True,  True,  True,  True,  True,  True,  True,  True,  True,
            True,  True,  True,  True,  True,  True,  True,  True,  True,
            True,  True,  True,  True,  True,  True,  True,  True,  True,
            True,  True,  True,  True,  True,  True,  True,  True,  True,
            True,  True,  True,  True,  True,  True,  True,  True,  True,
            True,  True,  True,  True,  True,  True,  True,  True,  True,
            True`,  True,  True,  True,  True,  True,  True,  True,  True,  True], dtype=bool)`
    

    This is done by overloading the "__gt__" method. For instance:

    >>> class demo(object):
        def __gt__(self, item):
            print item
    
    
    >>> a = demo()
    >>> a > 4
    4
    

    As you can see, "a > 4" was valid code.

    You can get a full list and documentation of all overloaded functions here: http://docs.python.org/reference/datamodel.html

    Something that is incredible is how simple it is to do this. ALL operations in python are done in such a way. Saying a > b is equivalent to a.gt(b)!

提交回复
热议问题