Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between numpy.float and numpy.float64

There seems to be a subtle difference between numpy.float and numpy.float64.

>>> import numpy as np
>>> isinstance(2.0, np.float)
True
>>> isinstance(2.0, np.float64)
False

Can someone clarify this? Thanks

like image 640
zell Avatar asked Sep 23 '16 17:09

zell


1 Answers

np.float is an alias for python float type. np.float32 and np.float64 are numpy specific 32 and 64-bit float types.

float?
Init signature: float(self, /, *args, **kwargs)
Docstring:     
float(x) -> floating point number

Convert a string or number to a floating point number, if possible.
Type:           type

np.float?
Init signature: np.float(self, /, *args, **kwargs)
Docstring:     
float(x) -> floating point number

Convert a string or number to a floating point number, if possible.
Type:           type

np.float32?
Init signature: np.float32(self, /, *args, **kwargs)
Docstring:      32-bit floating-point number. Character code 'f'. C float compatible.
File:           c:\python\lib\site-packages\numpy\__init__.py
Type:           type

np.float64?
Init signature: np.float64(self, /, *args, **kwargs)
Docstring:      64-bit floating-point number. Character code 'd'. Python float compatible.
File:           c:\python\lib\site-packages\numpy\__init__.py
Type:           type

Thus, when you do isinstance(2.0, np.float), it is equivalent to isinstance(2.0, float) as 2.0 is a plain python built-in float type... and not the numpy type.

isinstance(np.float64(2.0), np.float64) would obviously be True.

like image 154
Zeugma Avatar answered Sep 20 '22 23:09

Zeugma