How to Develop a Neural Net for Predicting Car Insurance Payout-4

Final Model and Make Predictions
Once we choose a model configuration, we can train a final model on all available data and use it to make predictions on new data.

In this case, we will use the deeper model with data normalization as our final model.

This means if we wanted to save the model to file, we would have to save the model itself (for making predictions), the transform for input data (for new input data), and the transform for target variable (for new predictions).

We can prepare the data and fit the model as before, although on the entire dataset instead of a training subset of the dataset.

…

split into input and output columns

X, y = df.values[:, :-1], df.values[:, -1]

ensure target is a 2d array

y = y.reshape((len(y),1))

prepare input data

pt1 = MinMaxScaler()
pt1.fit(X)
X = pt1.transform(X)

prepare target

pt2 = MinMaxScaler()
pt2.fit(y)
y = pt2.transform(y)

determine the number of input features

n_features = X.shape[1]

define model

model = Sequential()
model.add(Dense(10, activation=‘relu’, kernel_initializer=‘he_normal’, input_shape=(n_features,)))
model.add(Dense(8, activation=‘relu’, kernel_initializer=‘he_normal’))
model.add(Dense(1))

compile the model

model.compile(optimizer=‘adam’, loss=‘mse’)

…

split into input and output columns

X, y = df.values[:, :-1], df.values[:, -1]

ensure target is a 2d array

y = y.reshape((len(y),1))

prepare input data

pt1 = MinMaxScaler()
pt1.fit(X)
X = pt1.transform(X)

prepare target

pt2 = MinMaxScaler()
pt2.fit(y)
y = pt2.transform(y)

determine the number of input features

n_features = X.shape[1]

define model

model = Sequential()
model.add(Dense(10, activation=‘relu’, kernel_initializer=‘he_normal’, input_shape=(n_features,)))
model.add(Dense(8, activation=‘relu’, kernel_initializer=‘he_normal’))
model.add(Dense(1))

compile the model

model.compile(optimizer=‘adam’, loss=‘mse’)
We can then use this model to make predictions on new data.

First, we can define a row of new data, which is just one variable for this dataset.

…

define a row of new data

row = [13]

…

define a row of new data

row = [13]
We can then transform this new data ready to be used as input to the model.

…

transform the input data

X_new = pt1.transform([row])

transform the input data

X_new = pt1.transform([row])
We can then make a prediction.

…

make prediction

yhat = model.predict(X_new)

…

make prediction

yhat = model.predict(X_new)
Then invert the transform on the prediction so we can use or interpret the result in the correct scale.

…

invert transform on prediction

yhat = pt2.inverse_transform(yhat)

…

invert transform on prediction

yhat = pt2.inverse_transform(yhat)
And in this case, we will simply report the prediction.

…

report prediction

print(‘f(%s) = %.3f’ % (row, yhat[0]))

…

report prediction

print(‘f(%s) = %.3f’ % (row, yhat[0]))
Tying this all together, the complete example of fitting a final model for the auto insurance dataset and using it to make a prediction on new data is listed below.

fit a final model and make predictions on new data.

from pandas import read_csv
from sklearn.model_selection import KFold
from sklearn.metrics import mean_absolute_error
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense

load the dataset

path = ‘https://raw.githubusercontent.com/jbrownlee/Datasets/master/auto-insurance.csv’
df = read_csv(path, header=None)

split into input and output columns

X, y = df.values[:, :-1], df.values[:, -1]

ensure target is a 2d array

y = y.reshape((len(y),1))

prepare input data

pt1 = MinMaxScaler()
pt1.fit(X)
X = pt1.transform(X)

prepare target

pt2 = MinMaxScaler()
pt2.fit(y)
y = pt2.transform(y)

determine the number of input features

n_features = X.shape[1]

define model

model = Sequential()
model.add(Dense(10, activation=‘relu’, kernel_initializer=‘he_normal’, input_shape=(n_features,)))
model.add(Dense(8, activation=‘relu’, kernel_initializer=‘he_normal’))
model.add(Dense(1))

compile the model

model.compile(optimizer=‘adam’, loss=‘mse’)

fit the model

model.fit(X, y, epochs=200, batch_size=8, verbose=0)

define a row of new data

row = [13]

transform the input data

X_new = pt1.transform([row])

make prediction

yhat = model.predict(X_new)

invert transform on prediction

yhat = pt2.inverse_transform(yhat)

report prediction

print(‘f(%s) = %.3f’ % (row, yhat[0]))

fit a final model and make predictions on new data.

from pandas import read_csv
from sklearn.model_selection import KFold
from sklearn.metrics import mean_absolute_error
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense

load the dataset

path = ‘https://raw.githubusercontent.com/jbrownlee/Datasets/master/auto-insurance.csv’
df = read_csv(path, header=None)

split into input and output columns

X, y = df.values[:, :-1], df.values[:, -1]

ensure target is a 2d array

y = y.reshape((len(y),1))

prepare input data

pt1 = MinMaxScaler()
pt1.fit(X)
X = pt1.transform(X)

prepare target

pt2 = MinMaxScaler()
pt2.fit(y)
y = pt2.transform(y)

determine the number of input features

n_features = X.shape[1]

define model

model = Sequential()
model.add(Dense(10, activation=‘relu’, kernel_initializer=‘he_normal’, input_shape=(n_features,)))
model.add(Dense(8, activation=‘relu’, kernel_initializer=‘he_normal’))
model.add(Dense(1))

compile the model

model.compile(optimizer=‘adam’, loss=‘mse’)

fit the model

model.fit(X, y, epochs=200, batch_size=8, verbose=0)

define a row of new data

row = [13]

transform the input data

X_new = pt1.transform([row])

make prediction

yhat = model.predict(X_new)

invert transform on prediction

yhat = pt2.inverse_transform(yhat)

report prediction

print(‘f(%s) = %.3f’ % (row, yhat[0]))
Running the example fits the model on the entire dataset and makes a prediction for a single row of new data.

Note: Your results may vary given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the average outcome.

In this case, we can see that an input of 13 results in an output of 62 (thousand Swedish Krona).

f([13]) = 62.595