# 7. Model deployment

The concept of deployment in machine learning refers to the application of a model for the prediction of new data.

Indeed, the knowledge gained when building a predictive model will need to be organized and presented in a way that the customer can use it.

Depending on the requirements, the deployment phase can be as simple as generating a report or as complex as implementing a continous learning process.

The following techniques are used for deploying a neural network:

## Neural network outputs

A neural network produces a set of outputs for each set of inputs applied.

The following table shows the input values together with the corresponding output value for the case of estimating the fuel consumption of a car as a function of its attributes.

Cylinders Displacement Horsepower Weight Acceleration Model year Fuel consumption
8 307 130 3504 12 1980 17 mpg

## Output data

In many cases, the objective is to predict the outputs for many different cases stored in a data file. Each row in the data file contains the input values of the case to be predicted. A new file is created with the outputs of the model also arranged in rows.

The next table illustrates this deployment technique. Here a neural network predicts the probability of conversion for several customers in a marketing campaign as a function of engagement factors.

Recency Frequency Monetary Conversion
2 months 5 times 125 USD 70%
5 months 2 times 20 USD 8%
3 months 9 times 225 USD 85%
4 months 1 times 15 USD 5%
1 months 3 times 70 USD 75%

## Directional outputs

It is very useful to see how the outputs vary as a function of a single input, when all the others are fixed. This allows to perform prescriptions. Directional outputs can be seen as the cut of the neural network model along some input direction and through some reference point.

The next table shows the reference point for plotting all the directional output charts. In this case, the model estimates the residuary resistance of a sailing yacht as a function of design parameters and sailing conditions.

 Center of buoyancy -2.38 Prismatic coefficient 0.56 length displacement 4.79 Beam draught ratio 3.94 length beam ratio 3.21

The next chart shows how the residuary resistance varies with the Froude number (which represents the velocity of the yacht), with all the other inputs being fixed.

As we can see, the residuary resistance increases exponentially for high values of the Froude number. Therefore, this yacht design does not perform well at high speeds.

## Mathematical expression

Any neural network represents a function of the outputs with respect to the inputs. That function also depends on the parameters. The mathematical expression represented by the neural network can be used to embed it into another software, in the so-called production mode.

An example of the mathematical model represented by a neural network is listed next.

scaled_shear_rate = 2*(shear_rate-50)/(90-50)-1;
scaled_particle_diameter = 2*(particle_diameter-0.72)/(6.596-0.72)-1;
y_1_1 = tanh(-1.06007+(scaled_shear_rate*0.448487)+(scaled_particle_diameter*-0.861393));
y_1_2 = tanh(0.756922+(scaled_shear_rate*2.00716)+(scaled_particle_diameter*0.391539));
y_1_3 = tanh(0.862072+(scaled_shear_rate*-0.726115)+(scaled_particle_diameter*-1.46053));


## Python expression

The mathematical expression represented by the model can be exported to different programming languages, like Python.

The next listing is an example of a neural network in the Python programming language.

def expression(inputs) :
if type(inputs) != list:
print('Argument must be a list')
return
if len(inputs) != 4:
print('Incorrect number of inputs')
return
temperature=inputs[0]
exhaustt_vacuum=inputs[1]
ambient_pressure=inputs[2]
relative_humidity=inputs[3]
scaled_temperature = (temperature-19.6512)/7.45247
scaled_exhaustt_vacuum = 2*(exhaustt_vacuum-25.36)/(81.56-25.36)-1
scaled_ambient_pressure = (ambient_pressure-1013.26)/5.93878
scaled_relative_humidity = (relative_humidity-73.309)/14.6003
y_1_1 = tanh (-0.155062+ (scaled_temperature*0.203034)+ (scaled_exhaustt_vacuum*0.731056)+ (scaled_ambient_pressure*-0.191671)+ (scaled_relative_humidity*0.0158643))
y_1_2 = tanh (-0.29098+ (scaled_temperature*-0.023069)+ (scaled_exhaustt_vacuum*-0.263476)+ (scaled_ambient_pressure*-0.231774)+ (scaled_relative_humidity*0.333626))
y_1_3 = tanh (0.570558+ (scaled_temperature*0.573079)+ (scaled_exhaustt_vacuum*-0.0279703)+ (scaled_ambient_pressure*0.111165)+ (scaled_relative_humidity*0.00867248))
scaled_energy_output =  (0.15981+ (y_1_1*-0.383639)+ (y_1_2*-0.126028)+ (y_1_3*-0.745885))
energy_output = (0.5*(scaled_energy_output+1.0)*(495.76-420.26)+420.26)
return energy_output


## R expression

A programming language widely used for statistical analysis is R.

The next code is an example of a neural network in the R programming language.

Logistic <- function(x) {
1/(1+exp(-x))
}
Probability <- function(x) {
if(x < 0)  0
else if(x > 1)  1
else  x
}
expression <- function(inputs) {
if(length(inputs) != 3) {
print('Incorrect number of inputs')
return()
}
recency=inputs[1]
frequency=inputs[2]
time=inputs[3]
scaled_recency <- (recency-9.50668)/8.0954
scaled_frequency <- (frequency-5.51471)/5.83931
scaled_time <- (time-34.2821)/24.3767
y_1_1 <- logistic (-2.51065+ (scaled_recency*-1.85838)+ (scaled_frequency*-6.01766)+ (scaled_time*1.8059))
y_1_2 <- logistic (-2.88061+ (scaled_recency*-5.34961)+ (scaled_frequency*0.803782)+ (scaled_time*-1.87694))
non_probabilistic_donation <- logistic (-0.626608+ (y_1_1*-3.1418)+ (y_1_2*2.807))
donation <- probability(non_probabilistic_donation)
outputs
}