我正在尝试在Python中实现randomforest。运行代码时出现此错误。虽然我已使用以下内容从float32
转换为float64
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').
请有人帮忙吗?
答案 0 :(得分:1)
问题不是你没有设置float64 dtype。错误消息显示:
输入包含NaN,无穷大或对于dtype('float32')而言太大的值。
因此,请先尝试检查这些条件:
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)
答案 1 :(得分:0)
我来到这里是因为标题中的问题仍然没有答案。
要在float32
对象中将float64
转换为numpy.ndarray
:
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])))
会给你:
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'>