<jupyter_start><jupyter_text>Auto MPG data<jupyter_code># 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
dataset_path = '/Volumes/TimeMachine/data/DR7/eBOSS.ELG.NGC.DR7.table.5.r.npy'
data = np.load(dataset_path, allow_pickle=True).item()
data.keys()
class DATA(object):
    def __init__(self, data):
        self.x = data['features']
        self.y = data['label']
        self.p = data['hpind']
        self.w = data['fracgood']
    def norm_it(self, train_stats):
        self.X = (self.x - train_stats['x']['mean'])/train_stats['x']['std']
        self.Y = (self.y - train_stats['y']['mean'])/train_stats['y']['std']        

# Display training progress by printing a single dot for each completed epoch
class PrintDot(keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs):
        if epoch % 10 == 0: print('')
        print('.', end='')
        
#
def plot_history(history):
    hist = pd.DataFrame(history.history)
    hist['epoch'] = history.epoch

    fig, ax = plt.subplots(nrows=2, sharex=True, figsize=(8, 8))
    plt.subplots_adjust(hspace=0.0)
    # fig 0
    ax[0].set_ylabel('Mean Abs Error')
    ax[0].plot(hist['epoch'], hist['mae'],
           label='Train Error')
    ax[0].plot(hist['epoch'], hist['val_mae'],
           label = 'Val Error', ls='--')
    # fig 1 
    ax[1].set_xlabel('Epoch')
    ax[1].set_ylabel('Mean Square Error')
    ax[1].plot(hist['epoch'], hist['mse'],
           label='Train Error')
    ax[1].plot(hist['epoch'], hist['val_mse'],
           label = 'Val Error', ls='--')
    ax[0].legend()
    plt.show()
train = DATA(data['train']['fold3'])
test  = DATA(data['test']['fold3'])
valid = DATA(data['validation']['fold3'])
train
train_stats = {'x':{'mean':np.mean(train.x, axis=0), 
                    'std':np.std(train.x, axis=0, ddof=1)},
               'y':{'mean':np.mean(train.y, axis=0), 
                    'std':np.std(train.y, axis=0, ddof=1)}}
train.norm_it(train_stats)
test.norm_it(train_stats)
valid.norm_it(train_stats)
train.X.shape
def build_model(nfeature):
    tf.keras.backend.clear_session()
    kwargs = dict(kernel_regularizer=keras.regularizers.l2(0.0),
                  kernel_initializer=keras.initializers.he_normal(seed=123456))
    model = keras.Sequential([
        layers.Dense(20, activation='relu', input_shape=[nfeature], **kwargs),
        layers.Dense(20, activation='relu', **kwargs),
        layers.Dense(1, **kwargs)
        ])

    #optimizer = tf.keras.optimizers.RMSprop(0.001)
    optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)
    model.compile(loss='mse',
                optimizer=optimizer,
                metrics=['mae', 'mse'])
    return model
model = build_model(18)
model.summary()
example_batch  = train.X[:10]
example_result = model.predict(example_batch)
example_result
model = build_model(18)
# The patience parameter is the amount of epochs to check for improvement
early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', patience=10, min_delta=1.e-6)
EPOCHS = 1000
history = model.fit(
                train.X, train.Y,
                epochs=EPOCHS, validation_data=(valid.X, valid.Y), verbose=0,
                callbacks=[early_stop, PrintDot()], batch_size=256)

plot_history(history)
loss, mae, mse = model.evaluate(test.X, test.y, verbose=0)

print("Testing set Mean Abs Error : {:5.2f}\nTesting set Mean Square Error : {:5.2f}".format(mae, mse))
pd.DataFrame(history.history).tail()
test_predictions = model.predict(test.X).flatten()

plt.scatter(test.Y, test_predictions, alpha=0.1)
plt.xlabel('True Values')
plt.ylabel('Predictions')
plt.axis('equal')
plt.axis('square')
# plt.xlim([0,plt.xlim()[1]])
# plt.ylim([0,plt.ylim()[1]])
# _ = plt.plot([-100, 100], [-100, 100])
error = test_predictions - test.Y
plt.hist(error, bins=30)
plt.xlabel("Prediction Error")
# plt.yscale('log')
_ = plt.ylabel("Count")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17838509316770187, 'fraction_numerical': 0.02360248447204969, 'mean_word_length': 3.488294314381271, '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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '44248', 'license': 'MIT', 'n_tokens_mistral': 1501, 'n_tokens_neox': 1432, 'n_words': 239, 'path': 'notebooks/trunk/regression-DECaLS.ipynb', 'repo_name': 'mehdirezaie/LSSutils', 'size': '4025'}