<jupyter_start><jupyter_text>Univariate regression in KerasAuthor: <NAME>Regression example for simulated tomato dataset.<jupyter_code>import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

import keras

from utils import make_tomato
data = make_tomato(100)
type(data)
data.head(5)
plt.figure(dpi=100)
plt.plot(data['tomato_price'], data['ketchup_price'], '.')
plt.xlabel('tomato_price')
plt.ylabel('ketchup_price')
plt.title('Tomato dataset')
plt.show()<jupyter_output><empty_output><jupyter_text>Model MedValue ~ MedInc<jupyter_code>train = data.iloc[:25]
len(train)
y_train = train['ketchup_price'].values
type(y_train)
y_train.shape
X_train = train[['tomato_price']].values
X_train.shape
models = {}<jupyter_output><empty_output><jupyter_text>Flat model<jupyter_code>from keras.models import Sequential

model = Sequential()
from keras.layers import Dense

model.add(Dense(units=1, input_dim=1))
model.compile(loss='mse', optimizer='sgd')
model.summary()
# model.load_weights('weights/regression_flat.hdf5')
model.fit(X_train, y_train, epochs=25, batch_size=25)
# model.save_weights('weights/regression_flat.hdf5')
model.evaluate(X_train, y_train)
X = np.linspace(1, 8, num=100)
X = X.reshape(-1, 1)
y = model.predict(X)
plt.figure(dpi=150)
plt.plot(X[:, 0], y, 'g-', label='predicted')
plt.plot(X_train[:, 0], y_train, 'b.', label='observed')
plt.legend()
models['flat'] = model<jupyter_output><empty_output><jupyter_text>Medium model<jupyter_code>model = Sequential()
model.add(Dense(units=100, input_dim=1, activation='relu'))
model.add(Dense(units=100, activation='relu'))
model.add(Dense(units=100, activation='relu'))
model.add(Dense(units=1))

model.compile(
    loss='mse', 
    optimizer=keras.optimizers.SGD(lr=0.01, momentum=0.9, nesterov=True)
)
model.summary()
model.load_weights('weights/regression_medium.hdf5')
# model.fit(X_train, y_train, epochs=5_000, batch_size=25, verbose=False)
# model.save_weights('weights/regression_medium.hdf5')
model.evaluate(X_train, y_train)
X = np.linspace(1, 8, num=100)
X = X.reshape(-1, 1)
y = model.predict(X)
plt.figure(dpi=150)
plt.plot(X[:, 0], y, 'g-', label='predicted')
plt.plot(X_train[:, 0], y_train, 'b.', label='observed')
plt.legend()
models['med'] = model<jupyter_output><empty_output><jupyter_text>Big model<jupyter_code>model = Sequential()
model.add(Dense(units=150, input_dim=1, activation='relu'))
model.add(Dense(units=150, activation='relu'))
model.add(Dense(units=150, activation='relu'))
model.add(Dense(units=150, activation='relu'))
model.add(Dense(units=150, activation='relu'))
model.add(Dense(units=150, activation='relu'))
model.add(Dense(units=1))

model.compile(
    loss='mse', 
    optimizer=keras.optimizers.SGD(lr=0.01, momentum=0.9, nesterov=True)
)
model.load_weights('weights/regression_big.hdf5')
# model.fit(X_train, y_train, epochs=5_000, batch_size=25, verbose=False)
# model.save_weights('weights/regression_big.hdf5')
model.evaluate(X_train, y_train)
model.summary()
X = np.linspace(1, 8, num=100)
X = X.reshape(-1, 1)
y = model.predict(X)

plt.figure(dpi=150)
plt.plot(X[:, 0], y, 'g-', label='predicted')
plt.plot(X_train[:, 0], y_train, 'b.', label='observed')
plt.legend()
models['big'] = model<jupyter_output><empty_output><jupyter_text>Validation<jupyter_code>valid = data.iloc[25:50]
y_valid = valid['ketchup_price'].values
X_valid = valid[['tomato_price']].values
len(valid)
for k, m in models.items():
    score = m.evaluate(X_valid, y_valid, verbose=False)
    print(f'Model {k:10}: {score}')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1759572072072072, 'fraction_numerical': 0.03265765765765766, 'mean_word_length': 8.157216494845361, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '413458', 'license': 'MIT', 'n_tokens_mistral': 1493, 'n_tokens_neox': 1396, 'n_words': 220, 'path': 'regression.ipynb', 'repo_name': 'mslapek/devopole2018-keras', 'size': '3559'}