Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How can model be evaluated based on Auto MPG using TensorFlow?
TensorFlow is a machine learning framework provided by Google. It is an open-source framework used with Python to implement algorithms, deep learning applications, and much more for research and production purposes.
The Auto MPG dataset contains fuel efficiency data from 1970s and 1980s automobiles. It includes attributes like weight, horsepower, displacement, and more. The goal is to predict the fuel efficiency of specific vehicles using regression techniques.
Installing TensorFlow
Install TensorFlow using pip ?
pip install tensorflow
Model Evaluation Function
Here's how to evaluate a trained model and visualize the training progress ?
import matplotlib.pyplot as plt
import tensorflow as tf
import pandas as pd
import numpy as np
# Sample data preparation (Auto MPG dataset)
url = 'http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data'
column_names = ['MPG','Cylinders','Displacement','Horsepower','Weight',
'Acceleration', 'Model Year', 'Origin']
dataset = pd.read_csv(url, names=column_names,
na_values = "?", comment='\t',
sep=" ", skipinitialspace=True)
# Clean data
dataset = dataset.dropna()
dataset['Horsepower'] = pd.to_numeric(dataset['Horsepower'])
# Split features and labels
train_dataset = dataset.sample(frac=0.8,random_state=0)
test_dataset = dataset.drop(train_dataset.index)
train_features = train_dataset.copy()
test_features = test_dataset.copy()
train_labels = train_features.pop('MPG')
test_labels = test_features.pop('MPG')
# Simple linear model for demonstration
hrspwr_model = tf.keras.Sequential([
tf.keras.layers.Dense(units=1, input_shape=[1])
])
hrspwr_model.compile(optimizer='adam', loss='mean_absolute_error')
# Train the model
history = hrspwr_model.fit(
train_features['Horsepower'],
train_labels,
epochs=100,
verbose=0,
validation_data=(test_features['Horsepower'], test_labels))
# Plot loss function
def plot_loss(history):
plt.plot(history.history['loss'], label='loss')
plt.plot(history.history['val_loss'], label='val_loss')
plt.ylim([0, 10])
plt.xlabel('Epoch')
plt.ylabel('Error [MPG]')
plt.legend()
plt.grid(True)
plt.show()
plot_loss(history)
# Evaluate model performance
test_results = {}
test_results['hrspwr_model'] = hrspwr_model.evaluate(
test_features['Horsepower'],
test_labels, verbose=0)
print(f"Test MAE: {test_results['hrspwr_model']:.2f} MPG")
Understanding Model Evaluation
The evaluation process involves several key components ?
Loss Visualization ? The
plot_loss()function displays training and validation loss over epochs to identify overfitting or underfittingModel Evaluation ? The
evaluate()method tests the model on unseen data to measure generalization performancePerformance Metrics ? Mean Absolute Error (MAE) indicates the average prediction error in MPG units
Key Evaluation Metrics
| Metric | Description | Interpretation |
|---|---|---|
| Training Loss | Error on training data | Lower is better |
| Validation Loss | Error on validation data | Should be close to training loss |
| Test MAE | Mean absolute error on test set | Final model performance |
Conclusion
Model evaluation using the Auto MPG dataset involves plotting training/validation loss curves and measuring test performance with metrics like MAE. This helps assess how well the model generalizes to new automotive fuel efficiency predictions.
