【问题标题】:Input contains NaN, infinity or a value too large for dtype('float32')输入包含 NaN、无穷大或对于 dtype('float32') 来说太大的值
【发布时间】:2019-07-21 14:53:04
【问题描述】:

如何修复此错误消息,“ValueError: Input contains NaN, infinity or a value too large for dtype('float32')”

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Loading the dataset
data = pd.read_csv(r'C:\Users\sam.jones\Desktop\Fixed Income project\Data Pull\Data\Fixed Income_Data dump_2018.csv',error_bad_lines=False,encoding = "ISO-8859-2")
X = np.array([data.iloc[:,158].values])
Y = data.iloc[:,92].values


#Fitting Random Forest Regression to the dataset
from sklearn.ensemble import RandomForestRegressor
regressor = RandomForestRegressor(n_estimators = 10, random_state = 0)
regressor.fit(X,Y) 

【问题讨论】:

  • 您的代码中出现此错误的地方是什么?如果没有文件 'C:\Users\sam.jones\Desktop\Fixed Income project\Data Pull\Data\Fixed Income_Data dump_2018.csv',我们无法重新创建错误

标签: python


【解决方案1】:

输入可能有 Nan 的。 因此,请先使用np.nan_to_num(X) 将它们填充为零。

【讨论】:

  • 如果这个答案足够,请标记绿色勾号.. :)
【解决方案2】:

尝试声明一个变量。

x = x.fillna(test.mean())

【讨论】:

    【解决方案3】:

    在我的情况下,错误是由于大数字造成的,特别是我发现那些有科学记数法的,例如 3.63E+08、1.25E+09... 解决方案是用更小的数字替换这些数字:您可以简单地用 x / 1000 替换它们,或者最好的解决方案是使用函数来缩放或规范化数据。之后,你就可以训练你的模型了

    【讨论】:

      猜你喜欢
      • 2019-12-03
      • 1970-01-01
      • 2019-11-28
      • 2019-04-10
      • 2019-10-12
      • 2017-02-19
      • 2022-01-21
      • 2020-05-18
      • 2016-03-25
      相关资源
      最近更新 更多