<jupyter_start><jupyter_text>Import Libraries<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns
import tensorflow as tf
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split

# Make NumPy printouts easier to read.
np.set_printoptions(precision=3, suppress=True)<jupyter_output><empty_output><jupyter_text>Dataset source URL<jupyter_code>url = 'http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data'<jupyter_output><empty_output><jupyter_text>Attribute Information||||-|-||mpg|continuous||cylinders|multi-valued discrete||displacement|continuous||horsepower|continuous||weight|continuous||acceleration|continuous||model year|multi-valued discrete||origin|multi-valued discrete||car name|string (unique for each instance)|<jupyter_code>column_names = [
    'MPG',
    'Cylinders',
    'Displacement',
    'Horsepower',
    'Weight',
    'Acceleration',
    'Model Year',
    'Origin'
]
raw_dataset = pd.read_csv(
    url,
    names=column_names,
    na_values='?',
    comment='\t',
    sep=' ',
    skipinitialspace=True
)
dataset = raw_dataset.copy()
dataset.head()
# Checking if there are any NaN
dataset.isna().sum()
dataset[dataset.Horsepower.isna()]
# Dropping rows with NaN
dataset.dropna(inplace=True)
dataset[dataset.Horsepower.isna()]
dataset.isna().sum()
dataset.dtypes
dataset.describe().transpose()
print(
    sorted(dataset.Cylinders.unique()),
    sorted(dataset['Model Year'].unique()),
    sorted(dataset.Origin.unique()),
    sep='\n'
)
# pd.get_dummies It converts categorical data into dummy or indicator variables.
# This is very similar to One-Hot-Encoding
dataset = pd.get_dummies(dataset, columns=['Origin'], prefix='', prefix_sep='')
dataset.head()
dataset.rename({'1': 'USA', '2': 'Europe', '3': 'Japan'}, axis='columns', inplace=True)
dataset.head()<jupyter_output><empty_output><jupyter_text>Observarions- Cylinders is a categorical column- Gives an hint to check if Cylinders is categorial on every other feature<jupyter_code>sns.pairplot(dataset[['MPG', 'Cylinders', 'Displacement', 'Weight']], diag_kind='kde')
sns.pairplot(dataset[['MPG', 'Cylinders', 'Displacement', 'Weight']], diag_kind='kde', hue='Cylinders')
X = dataset.loc[:, dataset.columns != 'MPG']
y = dataset.loc[:, dataset.columns == 'MPG']
X.head()
y.head()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4)
print(
    f'''
    X_train = {len(X_train)}, X_test = {len(y_train)},
    y_train = {len(X_test)}, y_test = {len(y_test)}
    '''
)
X_train.head()
X_test.head()
y_train.head()
y_test.head()
X_train.reset_index(drop=True, inplace=True)
X_train.head()
X_test.reset_index(drop=True, inplace=True)
X_test.head()
y_train.reset_index(drop=True, inplace=True)
y_train.head()
y_test.reset_index(drop=True, inplace=True)
y_test.head()
X_train.describe().transpose()<jupyter_output><empty_output><jupyter_text>Normalization<jupyter_code>normalizer = tf.keras.layers.Normalization(axis=-1)
normalizer.adapt(np.array(X_train))
print(normalizer.mean)
X_train.mean()
horsepower_normalizer = tf.keras.layers.Normalization(input_shape=[1, ], axis=None)
horsepower_normalizer.adapt(np.array(X_train.Horsepower))<jupyter_output><empty_output><jupyter_text>Keras Sequential model<jupyter_code>horsepower_model = tf.keras.Sequential([
    horsepower_normalizer,
    tf.keras.layers.Dense(units=1)
])
horsepower_model.summary()
horsepower_model.predict(np.array(X_train.Horsepower)[:10])
horsepower_model.compile(
    optimizer=tf.optimizers.Adam(learning_rate=0.1),
    loss='mean_absolute_error'
)
%%time
history = horsepower_model.fit(
    X_train.Horsepower,
    y_train,
    epochs=100,
    verbose=0,
    validation_split=0.2
)
hist = pd.DataFrame(history.history)
hist['epoch'] = history.epoch
hist.head()
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)
test_results = {}
test_results['horsepower_model'] = horsepower_model.evaluate(
    X_test.Horsepower,
    y_test,
    verbose=0
)
test_results
x = tf.linspace(0.0, 250, 251)
y = horsepower_model.predict(x)
def plot_horsepower(x, y):
    plt.scatter(X_train.Horsepower, y_train, label='Data')
    plt.plot(x, y, color='k', label='Predictions')
    plt.xlabel('Horsepower')
    plt.ylabel('MPG')
    plt.legend()
plot_horsepower(x, y)<jupyter_output><empty_output><jupyter_text>Linear regression with multiple inputs<jupyter_code>linear_model = tf.keras.Sequential([
    normalizer,
    tf.keras.layers.Dense(units=1)
])
linear_model.summary()
linear_model.predict(X_train[:10])
linear_model.layers[1].kernel
linear_model.compile(
    optimizer=tf.optimizers.Adam(learning_rate=0.1),
    loss='mean_absolute_error'
)
%%time
history = linear_model.fit(
    X_train,
    y_train,
    epochs=100,
    verbose=0,
    validation_split=0.2
)
plot_loss(history)
test_results['linear_model'] = linear_model.evaluate(
    X_test, 
    y_test, 
    verbose=0
)<jupyter_output><empty_output><jupyter_text>Regression with a deep neural network (DNN)<jupyter_code>model = tf.keras.Sequential([
    horsepower_normalizer,
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(1)
])
model.summary()
model.compile(loss='mean_absolute_error', optimizer=tf.keras.optimizers.Adam(learning_rate=0.001))
%%time
history = model.fit(
    X_train.Horsepower,
    y_train,
    epochs=100,
    validation_split=0.2,
    verbose=0
)
plot_loss(history)
x = tf.linspace(0.0, 250, 251)
y = model.predict(x)
plot_horsepower(x, y)<jupyter_output><empty_output><jupyter_text>Regression using a DNN and multiple inputs<jupyter_code>model = tf.keras.Sequential([
    normalizer,
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(1)
])
model.compile(
    loss = 'mean_absolute_error',
    optimizer=tf.keras.optimizers.Adam(0.001)
)
model.summary()
%%time
history = model.fit(
    X_train,
    y_train,
    epochs=100,
    validation_split=0.2,
    verbose=0
)
plot_loss(history)
test_results['model'] = model.evaluate(X_test, y_test, verbose=0)
pd.DataFrame(test_results, index=['Mean absolute error [MPG]']).T
test_predictions = model.predict(X_test).flatten()
a = plt.axes(aspect='equal')
plt.scatter(y_test, test_predictions)
plt.xlabel('True Values [MPG]')
plt.ylabel('Predictions [MPG]')
lims = [0, 50]
plt.xlim(lims)
plt.ylim(lims)
_ = plt.plot(lims, lims)
error = test_predictions - np.array(y_test).reshape(-1)
plt.hist(error, bins=25)
plt.xlabel('Prediction Error [MPG]')
_ = plt.ylabel('Count')
model.save('dnn_model')
reloaded = tf.keras.models.load_model('dnn_model')
test_results['reloaded'] = reloaded.evaluate(X_test, y_test, verbose=0)
pd.DataFrame(test_results, index=['Mean absolute error [MPG]']).T<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15315443905224094, 'fraction_numerical': 0.01284613188695404, 'mean_word_length': 5.98604187437687, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '226998', 'license': 'MIT', 'n_tokens_mistral': 2697, 'n_tokens_neox': 2601, 'n_words': 400, 'path': 'Solution.ipynb', 'repo_name': 'Srushanth/Predict-Fuel-Efficiency-using-TensorFlow', 'size': '7006'}