# ANN applied to Boston Housing dataset returns negative value

This example is taken from the book Deep Learning With Python from Jason Brownlee. It applies a fully connected neural model with one hidden layer (13, 13, 1) using Keras library and the Tensorflow backend.

1 - Import the packages

import numpy
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline


boston = load_boston()
X = boston.data[:,0:13]
Y = boston.target


3 - Define base model

def baseline_model():
# create model
model = Sequential()
kernel_initializer='normal', activation='relu'))
# Compile model
return model


4 - Fix random seed for reproducibility

seed = 7
numpy.random.seed(seed)


5 - Fit & evaluate model

estimator = KerasRegressor(build_fn=baseline_model, epochs=10, \
batch_size=5, verbose=0)
kfold = KFold(n_splits=10, random_state=seed)
results = cross_val_score(estimator, X, Y, cv=kfold)


6 - Value of results parameter

Out:
array([-13.89524042, -14.2215869 ,  -6.21156782, -42.65242339,
-26.58890147, -56.30860755, -28.6575911 , -89.67339525,
-27.7172946 , -22.67604859])


Which returns a mean negative value

print("Baseline: %.2f (%.2f) MSE" % (results.mean(), results.std()))


It returns about -30 and it should return about +30 according to the book. I've tried both Theano and Tensorflow with no success.

I've also tested this code both on Windows and Linux, having obtained the same bad result.

The problem seems to be in the cross-validation part, because if I don't run cross validation, I get more sensible results.

7 - Without cross-validation

model = Sequential()
activation='relu'))
model.fit(X, Y, epochs=150, batch_size=10)


Now if I evaluate the model I get a more sensible value

In : model.evaluate(X, Y)
506/506 [==============================] - 0s 58us/step
Out: 27.778296670423664


What can be happening here? Why is the cross-validation procedure returning negative values?
{"ft_lang_label":"__label__en","ft_lang_prob":0.53103447,"math_prob":0.9158015,"size":2448,"snap":"2020-34-2020-40","text_gpt3_token_len":624,"char_repetition_ratio":0.11743044,"word_repetition_ratio":0.006802721,"special_character_ratio":0.313317,"punctuation_ratio":0.2,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99275523,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-08-09T15:25:28Z\",\"WARC-Record-ID\":\"<urn:uuid:b436f279-377d-4f93-af49-7d89996736fb>\",\"Content-Length\":\"145547\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b9f73f12-e327-4f32-8c1b-e11708992974>\",\"WARC-Concurrent-To\":\"<urn:uuid:3451df00-9a12-4064-8790-1e92f030bfce>\",\"WARC-IP-Address\":\"151.101.65.69\",\"WARC-Target-URI\":\"https://datascience.stackexchange.com/questions/27293/ann-applied-to-boston-housing-dataset-returns-negative-value\",\"WARC-Payload-Digest\":\"sha1:MJB3SXDOX62MOTGZV7OPRN3YNJNCNSUD\",\"WARC-Block-Digest\":\"sha1:BZSRYF7W4Q3HSGCX4EQ2LGH46UT3HBTN\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-34/CC-MAIN-2020-34_segments_1596439738555.33_warc_CC-MAIN-20200809132747-20200809162747-00369.warc.gz\"}"}