Does numpy.bincount support numpy.float128 type weights?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Does numpy.bincount support numpy.float128 type weights?

Wei, Huayi
Hi, There,

Here is a sample code using `numpy.bincount`

     import numpy as np
     a = np.array([1.0, 2.0, 3.0], dtype=np.float128)
     b = np.array([1, 2, 0], dtype=np.int)
     c = np.bincount(b, weights=a)

If run it, I get the following error report:

     ----> 1 c = np.bincount(b, weights=a)
     TypeError: Cannot cast array data from dtype('float128') to
dtype('float64') according to the rule 'safe'

Is it a bug of `np.bincount`? Does there exist any similar function
which I can use to do the similar thing with numpy.float128 type weights?

Best

Huayi



_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Does numpy.bincount support numpy.float128 type weights?

Nathan Goldbaum
I think this is a deficiency in the current implementation of bincount, which always casts the weights to float64. This WIP pull request should probably fix it:


On Wed, Nov 30, 2016 at 3:54 PM, Wei, Huayi <[hidden email]> wrote:
Hi, There,

Here is a sample code using `numpy.bincount`

    import numpy as np
    a = np.array([1.0, 2.0, 3.0], dtype=np.float128)
    b = np.array([1, 2, 0], dtype=np.int)
    c = np.bincount(b, weights=a)

If run it, I get the following error report:

    ----> 1 c = np.bincount(b, weights=a)
    TypeError: Cannot cast array data from dtype('float128') to dtype('float64') according to the rule 'safe'

Is it a bug of `np.bincount`? Does there exist any similar function which I can use to do the similar thing with numpy.float128 type weights?

Best

Huayi



_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/numpy-discussion


_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Does numpy.bincount support numpy.float128 type weights?

Julian Taylor-3
Also note that float128 is rarely what you want.
It is not a quad precision value, it maps to C long double which is 80
bit on x86 and less on stuff like arm.

On 30.11.2016 22:59, Nathan Goldbaum wrote:

> I think this is a deficiency in the current implementation of bincount,
> which always casts the weights to float64. This WIP pull request should
> probably fix it:
>
> https://github.com/numpy/numpy/pull/7464
>
> On Wed, Nov 30, 2016 at 3:54 PM, Wei, Huayi <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Hi, There,
>
>     Here is a sample code using `numpy.bincount`
>
>         import numpy as np
>         a = np.array([1.0, 2.0, 3.0], dtype=np.float128)
>         b = np.array([1, 2, 0], dtype=np.int <http://np.int>)
>         c = np.bincount(b, weights=a)
>
>     If run it, I get the following error report:
>
>         ----> 1 c = np.bincount(b, weights=a)
>         TypeError: Cannot cast array data from dtype('float128') to
>     dtype('float64') according to the rule 'safe'
>
>     Is it a bug of `np.bincount`? Does there exist any similar function
>     which I can use to do the similar thing with numpy.float128 type
>     weights?
>
>     Best
>
>     Huayi
>
>
>
>     _______________________________________________
>     NumPy-Discussion mailing list
>     [hidden email] <mailto:[hidden email]>
>     https://mail.scipy.org/mailman/listinfo/numpy-discussion
>     <https://mail.scipy.org/mailman/listinfo/numpy-discussion>
>
>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> [hidden email]
> https://mail.scipy.org/mailman/listinfo/numpy-discussion
>

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Does numpy.bincount support numpy.float128 type weights?

Sebastian Berg
In reply to this post by Wei, Huayi
Fist off, a word of caution. float128 depends on your system and maps
to whatever longdouble is (IIRC) or may not even exist. So I hope you
don't expect IEEE 128 bit floats, if you are unsure, maybe check
`np.finfo`.

If speed does not matter
```
res = np.zeros(np.max(b), dtype=np.longdouble)
np.add.at(res, b, a)
```
will work, but do not expect it to be fast.

- Sebastian


On Do, 2016-12-01 at 05:54 +0800, Wei, Huayi wrote:

> Hi, There,
>
> Here is a sample code using `numpy.bincount`
>
>      import numpy as np
>      a = np.array([1.0, 2.0, 3.0], dtype=np.float128)
>      b = np.array([1, 2, 0], dtype=np.int)
>      c = np.bincount(b, weights=a)
>
> If run it, I get the following error report:
>
>      ----> 1 c = np.bincount(b, weights=a)
>      TypeError: Cannot cast array data from dtype('float128') to 
> dtype('float64') according to the rule 'safe'
>
> Is it a bug of `np.bincount`? Does there exist any similar function 
> which I can use to do the similar thing with numpy.float128 type
> weights?
>
> Best
>
> Huayi
>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> [hidden email]
> https://mail.scipy.org/mailman/listinfo/numpy-discussion
>
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/numpy-discussion

signature.asc (836 bytes) Download Attachment
Loading...