<jupyter_start><jupyter_text>Predict fuel efficiency Using Linear regression Tensorflow<jupyter_code>import numpy as np
import tensorflow as tf
print(tf.__version__)
import pandas as pd

from tensorflow import keras
from tensorflow.keras import layers
dataset_path = keras.utils.get_file("auto-mpg.data", "http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data")

dataset_path

column_names = ['MPG','Cylinders','Displacement','Horsepower','Weight',
                'Acceleration', 'Model Year', 'Origin']
df = pd.read_csv(dataset_path, sep=" ", names = column_names,skipinitialspace=True, na_values="?",comment='\t')
df.tail()
df.isna().sum()
df=df.dropna()
df.count()

df.size
df
df.tail()
df['Origin'] = df['Origin'].map(lambda x: {1: 'USA', 2: 'Europe', 3: 'Japan'}.get(x))
df = pd.get_dummies(df, prefix='', prefix_sep='')
df.tail()
train_df=df.sample(frac=0.8,random_state=0)
test_df= df.drop(train_df.index)
train_df.count()
test_df.count()
train_labels = train_df.pop('MPG')
test_labels = test_df.pop('MPG')
test_labels.values
from sklearn import preprocessing

def normalize_data(df):
    x = df.values #returns a numpy array
    min_max_scaler = preprocessing.MinMaxScaler()
    x_scaled = min_max_scaler.fit_transform(x)
    return  pd.DataFrame(x_scaled)

norm_train = normalize_data(train_df)
norm_test = normalize_data(test_df)
norm_test.tail()
len(norm_train.keys())
norm_train.keys() #this gives the number of columns
norm_train.count()
def build_model():
  model = tf.keras.Sequential([
    tf.keras.layers.Dense(64, activation='relu', input_shape=[len(norm_train.keys())]),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(1)
  ])

  optimizer = tf.keras.optimizers.RMSprop(0.001)

  model.compile(loss='mse',
                optimizer=optimizer,
                metrics=['mae', 'mse'])
  return model
model = build_model()
model.summary()
example_batch = norm_train[:10]
example_result = model.predict(example_batch)
example_result
# history = model.fit(norm_train.shuffle(10000).batch(512),
#                     epochs=20,
#                     validation_data=norm_test.batch(512),
#                     verbose=1)


num_epochs = 500
history = model.fit(norm_train, train_labels, epochs=num_epochs, validation_data=(norm_test, test_labels), verbose=2)
print(history.history)
def plot_history(history):
  hist = pd.DataFrame(history.history)
  hist['epoch'] = history.epoch

  plt.figure()
  plt.xlabel('Epoch')
  plt.ylabel('Mean Abs Error [MPG]')
  plt.plot(hist['epoch'], hist['mae'],
           label='Train Error')
  plt.plot(hist['epoch'], hist['val_mae'],
           label = 'Val Error')
  plt.ylim([0,5])
  plt.legend()

  plt.figure()
  plt.xlabel('Epoch')
  plt.ylabel('Mean Square Error [$MPG^2$]')
  plt.plot(hist['epoch'], hist['mse'],
           label='Train Error')
  plt.plot(hist['epoch'], hist['val_mse'],
           label = 'Val Error')
  plt.ylim([0,20])
  plt.legend()
  plt.show()


plot_history(history)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1584681413007593, 'fraction_numerical': 0.013535820402773192, 'mean_word_length': 4.489130434782608, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '579517', 'license': 'MIT', 'n_tokens_mistral': 1120, 'n_tokens_neox': 1076, 'n_words': 189, 'path': 'regression/Linear-regression-using-tensorflow.ipynb', 'repo_name': 'kadu9/tensorflow-playground', 'size': '3029'}