在尝试计算均方对数误差时,出现以下错误:
ValueError: Input contains NaN, infinity or a value too large for dtype('float64').
计算均方误差不会给出错误。以下代码可用于重现该问题:
from sklearn.datasets import load_boston
dataset = load_boston()
import pandas as pd
df = pd.DataFrame(dataset.data, columns=dataset.feature_names, )
df["MEDV"] = dataset.target
#tried this, no difference
df = df.reset_index()
df.isnull().sum()
#No missing values
df.dtypes
# all float64
cols = ["LSTAT", "RM"]
X = df[cols]#.astype(np.float)
y = df["MEDV"]#.astype(np.float)
from sklearn.linear_model import LinearRegression
slr = LinearRegression()
slr.fit(X, y)
y_pred = slr.predict(X)
np.all(np.isfinite(X))
# true
np.all(np.isfinite(y))
#true
np.all(np.isfinite(y_pred))
#true
from sklearn.metrics import mean_squared_error
mse = mean_squared_error(y, y_pred)
print(mse)
from sklearn.metrics import mean_squared_log_error
# THIS produces the error message:
msle = mean_squared_log_error(y, y_pred)
print(msle)
我做了几项检查:
- 没有缺失值
- 没有无限的价值
- 数据类型是 float64
我不明白为什么它给了我错误。有人知道我在做什么错吗?
亲切的问候,
雅普