OverflowError: Python int too large to convert to C long on windows but not mac
OverflowError: Python int too large to convert to C long on windows but not mac
Youll get that error once your numbers are greater than sys.maxsize
:
>>> p = [sys.maxsize]
>>> preds[0] = p
>>> p = [sys.maxsize+1]
>>> preds[0] = p
Traceback (most recent call last):
File <stdin>, line 1, in <module>
OverflowError: Python int too large to convert to C long
You can confirm this by checking:
>>> import sys
>>> sys.maxsize
2147483647
To take numbers with larger precision, dont pass an int type which uses a bounded C integer behind the scenes. Use the default float:
>>> preds = np.zeros((1, 3))
You can use dtype=np.int64
instead of dtype=int
OverflowError: Python int too large to convert to C long on windows but not mac
Could anyone help explain why
Numpy arrays normally* have fixed size elements, including integers of various sizes, single or double precision floating point numbers, fixed length byte and Unicode strings and structures built up from the aforementioned types.
In Python 2 a python int was equivalent to a C long. In Python 3 an int is an arbitrary precision type but numpy still uses int it to represent the C type long when creating arrays.
The size of a C long is platform dependent. On windows it is always 32-bit. On unix-like systems it is normally 32 bit on 32 bit systems and 64 bit on 64 bit systems.
or give a solution for the code on windows? Thanks so much!
Choose a data type whose size is not platform dependent. You can find the list at https://docs.scipy.org/doc/numpy/reference/arrays.scalars.html#arrays-scalars-built-in the most sensible choice would probably be np.int64
* Numpy does allow arrays of python objects, but I dont think they are widely used.
Related posts on Over flow Error :
- Checking if a string can be converted to float in Python
- java – What is difference between Errors and Exceptions?
- recursion – Making a deep copy of a LinkedList in java
- python – Integer overflow in Python3
- python – Check if a number is a perfect square
- Python: OverflowError: math range error
- python – OverflowError: (34, Result too large)
- python – _csv.Error: field larger than field limit (131072)