2

我正在尝试在 Python 中实现随机森林。运行代码时出现此错误。虽然我已经从using转换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').  

请问有人可以帮忙吗?

4

2 回答 2

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)
于 2016-07-13T12:36:25.320 回答
0

我来到这里是因为标题中的问题我仍然觉得没有答案。在对象中转换float32为:float64numpy.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'>
于 2017-08-22T16:14:13.177 回答