简体   繁体   中英

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

How do I fix this error message , "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) 

Input might have Nan's. So use np.nan_to_num(X) to fill them with zeroes first.

尝试声明一个变量。

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

In my case that error was due to big numbers, in particular I found those with scientific notation, such as 3.63E+08, 1.25E+09... The solution is to replace those numbers with something smaller: you can either simply replace them with x / 1000 or, the best solution, use a function to scale or normalise the data. After that, you can train your model

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM