Home > Blockchain >  Numpy misindentifying data type
Numpy misindentifying data type

Time:12-15

I am calculating the coordinates of a H2O using numpy.
I know that am using proper data types in the creation itself.

r = 0.9820
angle_rad = np.deg2rad((180-103.718)/2)
off_x = r*np.cos(angle_rad)
off_y = r*np.sin(angle_rad)

h2o_base = np.array([['O', np.float64(0.), np.float64(0.), np.float64(0.)], 
            ['H', -off_x, off_y, np.float64(0.)],
            ['H',  off_x, off_y, np.float64(0.)]
           ])
h2o_base

I am expecting an array with (string, float, float, foat) data types but when I check it. the output of this code is

array([['O', '0.0', '0.0', '0.0'],
       ['H', '-0.7723363998864039', '0.606482056956765', '0.0'],
       ['H', '0.7723363998864039', '0.606482056956765', '0.0']],
      dtype='<U32')

Which is full of string elements.
May I ask for some help with this ?
Shouldn't it automatically detect the proper data type ?

CodePudding user response:

Adding the dtype=object specification

h2o_base = np.array([['O', np.float64(0.), np.float64(0.), np.float64(0.)], 
                     ['H', -off_x, off_y, np.float64(0.)],
                     ['H',  off_x, off_y, np.float64(0.)]], 
                    dtype=object)

should make the trick.

Indeed, unlike the built-in list type that can hold elements of different types, a np.array allows one data type only for all elements.

  • Related