Convert numpy array type and values from Float64 to Float32

127,800

Solution 1

Actually i tried hard but not able to do as the 'sklearn.tree._tree.Tree' objects is not writable.

It is causing a precision issue while generating a PMML file, so i raised a bug over there and they gave an updated solution for it by not converting it in to the Float64 internally.

For more info, you can follow this link: Precision Issue

Solution 2

The problem is that you do not do any type conversion of the numpy array. You calculate a float32 variable and put it as an entry into a float64 numpy array. numpy then converts it properly back to float64

Try someting like this:

a = np.zeros(4,dtype="float64") 
print a.dtype
print type(a[0])
a = np.float32(a)
print a.dtype
print type(a[0])

The output (tested with python 2.7)

float64
<type 'numpy.float64'>
float32
<type 'numpy.float32'>

a is in your case the array tree.tree_.threshold

Solution 3

You can try this:

tree.tree_.threshold[i]=tree.tree_.threshold[i].astype('float32',casting='same_kind’)
Share:
127,800
Akshay Tilekar
Author by

Akshay Tilekar

Ruling the world of Big Data Analytics and Machine Learning.

Updated on July 09, 2022

Comments

  • Akshay Tilekar
    Akshay Tilekar almost 2 years

    I am trying to convert threshold array(pickle file of isolation forest from scikit learn) of type from Float64 to Float32

    for i in range(len(tree.tree_.threshold)):
        tree.tree_.threshold[i] = tree.tree_.threshold[i].astype(np.float32)
    

    ​ Then Printing it

    for value in tree.tree_.threshold[:5]:
        print(type(value))
        print(value)
    

    the output i am getting is :

    <class 'numpy.float64'>
    526226.0
    <class 'numpy.float64'>
    91.9514312744
    <class 'numpy.float64'>
    3.60330319405
    <class 'numpy.float64'>
    -2.0
    <class 'numpy.float64'>
    -2.0
    

    I am not getting a proper conversion to Float32. I want to convert values and their type to Float32, Did anybody have any workaround this ?

  • Akshay Tilekar
    Akshay Tilekar over 6 years
    can you explain about np.zeroes parameters, specially "4"
  • Glostas
    Glostas over 6 years
    it was the fasted way of created a numpy array that is filled with something... here zeros. The 4 is just the number of zeros. I choose 4 because I like the number...
  • Akshay Tilekar
    Akshay Tilekar over 6 years
    ------------------------------------------------------------‌​--------------- AttributeError Traceback (most recent call last) <ipython-input-125-d20ee23285ad> in <module>() ----> 1 tree.tree_.threshold = np.zeros(4,dtype="float64") 2 print (tree.tree_.threshold.dtype) 3 tree.tree_.threshold= float32(tree.tree_.threshold) 4 print (a.dtype) AttributeError: attribute 'threshold' of 'sklearn.tree._tree.Tree' objects is not writable
  • Akshay Tilekar
    Akshay Tilekar over 6 years
    getting same error: AttributeError Traceback (most recent call last) <ipython-input-139-0ba8c9d97878> in <module>() ----> 1 tree.tree_.threshold = np.zeros(4,dtype="float64") 2 print (tree.tree_.threshold.dtype) 3 print (tree.tree_.threshold[0].dtype) 4 tree.tree_.threshold= float32(a) 5 print (tree.tree_.threshold.dtype) AttributeError: attribute 'threshold' of 'sklearn.tree._tree.Tree' objects is not writable
  • Glostas
    Glostas over 6 years
    well, if its not a writable object, you cannnot overwrite it. Therefore the solution wont work. It only works if you use a new numpy array which you probably don't want.
  • Akshay Tilekar
    Akshay Tilekar over 6 years
    yes, i want to modify the existed one.Is there any way to modify it ?
  • Glostas
    Glostas over 6 years
    you can try tree.tree_.threshold.dtype="float32"
  • Akshay Tilekar
    Akshay Tilekar over 6 years
    no, it is not allowing me to do it causing an error "ValueError: new type not compatible with array."