2

I am trying to implement randomforest in Python. While running the code I got this error. Although I had already converted from float32 to float64 using :

x_arr = np.array(train_df, dtype='float64')

Traceback(most recent call last):
  File "C:\Python27\randomforest.py", line 67, in <module>     
    forest=forest.fit(x_array[0::,1::],x_array[0::,0])
  File "C:\Python27\lib\site-packages\sklearn\ensemble\forest.py", line 212, in fit
    X = check_array(X, dtype=DTYPE, accept_sparse="csc")   
  File "C:\Python27\lib\site-packages\sklearn\utils\validation.py", line 398, in check_array
    _assert_all_finite(array)
  File "C:\Python27\lib\site-packages\sklearn\utils\validation.py", line 54, in _assert_all_finite
    " or a value too large for %r." % X.dtype)
ValueError: Input contains NaN, infinity or a value too large for dtype('float32').  

Please can somebody help?

1
  • What happens when you try dtype=np.float64 instead? Commented Jul 13, 2016 at 13:42

2 Answers 2

1

The problem is not that you're failing to set a float64 dtype. The error message says:

Input contains NaN, infinity or a value too large for dtype('float32').

So try checking for those conditions first:

assert not np.any(np.isnan(x_arr))
assert np.all(np.isfinite(x_arr))
assert np.all(x_arr <= finfo('float32').max)
assert np.all(x_arr >= finfo('float32').min)
Sign up to request clarification or add additional context in comments.

Comments

1

I got here because of the question in the title which I still feel remains unanswered. To convert float32 to float64 in a numpy.ndarray object:

array32 = np.ndarray(shape=(2,2), dtype=np.float32, order='F')
print("Type of an object of 'array32': " + str(type(array32[0][0])))

# Convert to float64
array64 = array32.astype(np.float64)
print("Type of an object of 'array64': " + str(type(array64[0][0])))

# Convert back to float32
array32again = array64.astype(np.float32)
print("Type of an object of 'array32again': " + str(type(array32again[0][0])))

Will give you:

Type of an object of 'array32': <class 'numpy.float32'>
Type of an object of 'array64': <class 'numpy.float64'>
Type of an object of 'array32again': <class 'numpy.float32'>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.