0
$\begingroup$

I'm trying to run cross validation with mean squared log error with sklearn and getting the following error message:

ValueError: Mean Squared Logarithmic Error cannot be used when targets contain negative values.

This would suggest that I have negative values in my 1d array y. However, I have tried about 10 different ways of checking, including importing into excel and I can see no negative values in there.

from sklearn.linear_model import LinearRegression
from sklearn.model_selection import cross_val_score
from sklearn.metrics import mean_squared_log_error
import pandas as pd
import numpy as np

train_csv = 'train.csv'
df_train = pd.read_csv(trainData)

# define variables
target = 'SalePrice'
indep_variable = 'OverallQual'

# scoring
scoring_cross_val = 'neg_mean_squared_log_error'
scoring = mean_squared_log_error

# initate model
lin_reg = LinearRegression()

# example data
X = df_train.drop(target, axis=1)
X = X[indep_variable].to_numpy().reshape(-1, 1) 
y = df_train[target].to_numpy().reshape(-1, 1) 

# fit model
lin_reg.fit(X, y)

# cross validated model error
cv = cross_val_score(lin_reg, X, y, cv=2, scoring=scoring_cross_val)

I created a version of the code above with some simple inputs to check it isn't a bug in my version of sklearn. The code runs without a problem.

from sklearn.linear_model import LinearRegression
from sklearn.model_selection import cross_val_score
from sklearn.metrics import mean_squared_log_error
import pandas as pd
import numpy as np

# scoring
scoring_cross_val = 'neg_mean_squared_log_error'
scoring = mean_squared_log_error

# initate model
lin_reg = LinearRegression()

# example data
X = np.array([1.,2.,3.]).reshape(-1, 1) 
y = np.array([4.,5.,6.]).reshape(-1, 1) 

# fit model
lin_reg.fit(X, y)

# cross validated model error
cv = cross_val_score(lin_reg, X, y, cv=2, scoring=scoring_cross_val)

If anyone gets the chance to help, the csv can be downloaded from Kaggle: https://www.kaggle.com/c/house-prices-advanced-regression-techniques/data

Iain

$\endgroup$
  • $\begingroup$ do you have zeros in your array? $\endgroup$ – Leevo Oct 11 '19 at 12:27
  • $\begingroup$ no zeros y.min() returns 34900 $\endgroup$ – Iain MacCormick Oct 11 '19 at 13:17
  • $\begingroup$ is it possible that there are negative values in the error (y - y_hat) ? In that case, log would return error $\endgroup$ – Leevo Oct 11 '19 at 13:19
  • $\begingroup$ all X, y values are in positive space. when I run lin_reg.predict(X).min() it returns 5.2. so seems unlikely but possible. is there a way to tap into the predictions made within cross val? $\endgroup$ – Iain MacCormick Oct 12 '19 at 2:47
  • $\begingroup$ ahh ok so, I've run cv = cross_val_predict(lin_reg, X, y, cv=10) then cv.min() and have a negative so that's my problem. Thanks for your help! $\endgroup$ – Iain MacCormick Oct 12 '19 at 2:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.