gpt4 book ai didi

python - 从 numpy 数组 float32 到 numpy 数组 float64 的转换

转载 作者:太空宇宙 更新时间:2023-11-03 16:22:05 25 4
gpt4 key购买 nike

我正在尝试用 Python 实现随机森林。运行代码时我收到此错误。尽管我已经使用以下方法从 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').

请问有人可以帮忙吗?

最佳答案

问题不在于您未能设置 float64 dtype。错误消息显示:

Input contains NaN, infinity or a value too large for 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)

关于python - 从 numpy 数组 float32 到 numpy 数组 float64 的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38351778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com