Integer overflow in numpy arrays

前端 未结 4 612
执笔经年
执笔经年 2020-11-30 12:37
import numpy as np
a = np.arange(1000000).reshape(1000,1000)
print(a**2)

With this code I get this answer. Why do I get negative values?

         


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 13:23

    A solution to this problem is as follows (taken from here):

    ...change in class StringConverter._mapper (numpy/lib/_iotools.py) from:

    {{{
     _mapper = [(nx.bool_, str2bool, False),
                (nx.integer, int, -1),
                (nx.floating, float, nx.nan),
                (complex, _bytes_to_complex, nx.nan + 0j),
                (nx.string_, bytes, asbytes('???'))]
    }}}
    

    to

    {{{
     _mapper = [(nx.bool_, str2bool, False),
                (nx.int64, int, -1),
                (nx.floating, float, nx.nan),
                (complex, _bytes_to_complex, nx.nan + 0j),
                (nx.string_, bytes, asbytes('???'))]
     }}}
    

    This solved a similar problem that I had with numpy.genfromtxt for me

    Note that the author describes this as a 'temporary' and 'not optimal' solution. However, I have had no side effects using v2.7 (yet?!).

提交回复
热议问题