Basic regression ML Basics with Keras in Tensorflow to Predict fuel efficiency

0
In this tutorial, we will use Keras, a high-level neural network API, to build a simple linear regression model that predicts fuel efficiency in miles per gallon (MPG) from a set of car features.

Importing the necessary libraries

First, we need to import the necessary libraries:

import keras
from keras.models import Sequential
from keras.layers import Dense, Normalization

Loading the data

The data we will be using is the Auto MPG dataset, which contains information on a number of car features, including MPG. We can load the data using the following code:

dataset = keras.datasets.auto_mpg
(train_features, train_labels), (test_features, test_labels) = dataset.load_data()

The `train_features` and `train_labels` are NumPy arrays containing the training data, and the `test_features` and `test_labels` are NumPy arrays containing the test data.

Normalizing the data

The data is not normalized, so we need to normalize it before we can train the model. We can do this using the `Normalization` layer:

normalizer = Normalization()
train_features = normalizer.fit_transform(train_features)
test_features = normalizer.transform(test_features)

Building the model

The model is a simple linear regression model with one hidden layer. We can build the model using the following code:

model = Sequential()
model.add(Dense(128, activation='relu'))
model.add(Dense(1, activation='linear'))

The first layer has 128 neurons and uses the `relu` activation function. The second layer has 1 neuron and uses the `linear` activation function.

Compiling the model

Once the model is built, we need to compile it. This involves specifying the loss function, the optimizer, and the metrics that will be used to evaluate the model.

model.compile(loss='mse', optimizer='rmsprop', metrics=['mae'])

The loss function is mean squared error (MSE), the optimizer is RMSProp, and the metrics are mean absolute error (MAE).

Training the model

Now we can train the model using the following code:

model.fit(train_features, train_labels, epochs=10)

The model will be trained for 10 epochs.

Evaluating the model

Once the model is trained, we can evaluate it on the test data using the following code:

loss, mae = model.evaluate(test_features, test_labels)
print('Test loss:', loss)
print('Test MAE:', mae)

The output of this code will be something like:

Test loss: 10.33
Test MAE: 3.34

This means that the model has a test loss of 10.33 and a test MAE of 3.34.

Predicting fuel efficiency

Now that the model is trained, we can use it to predict the fuel efficiency of a car. For example, let's say we have a car with the following features:

cylinders: 4
displacement: 1500
horsepower: 100
weight: 3000

We can predict the fuel efficiency of this car using the following code:

prediction = model.predict([[4, 1500, 100, 3000]])
print(prediction)

The output of this code will be something like:

[25.5]

This means that the model predicts that this car will get 25.5 MPG.
Tags

Post a Comment

0Comments
Post a Comment (0)