<jupyter_start><jupyter_text>Goal: Predict value of car saleBased on blog post [here](https://datascienceplus.com/keras-regression-based-neural-networks/).<jupyter_code>import numpy as np
import pandas
import boto3
import datetime
import matplotlib.pyplot as plt

s3 = boto3.client('s3')


def get_csv_from_s3(key, bucket="intuitiveml-data-sets"):
    obj = s3.get_object(Bucket=bucket, Key=key)
    return pandas.read_csv(obj["Body"])


def get_obj_s3(key, bucket="intuitiveml-data-sets"):
    obj = s3.get_object(Bucket=bucket, Key=key)["Body"]
    return obj
df = get_csv_from_s3("cars.csv")
df.head()
df.shape
from keras.models import Sequential
from keras.layers import Dense

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
dataset = df.to_numpy()
x = dataset[:, 0:5]
y = dataset[:, 5]
y = np.reshape(y, (-1, 1))

scaler_x = MinMaxScaler()
scaler_y = MinMaxScaler()
print(scaler_x.fit(x))
x_scale = scaler_x.transform(x)
print(scaler_y.fit(y))
y_scale = scaler_y.transform(y)
X_train, X_test, y_train, y_test = train_test_split(x_scale, y_scale)<jupyter_output><empty_output><jupyter_text>Configure Keras Model<jupyter_code>model = Sequential([
    Dense(12, input_dim=5, kernel_initializer="normal", activation="relu"),
    Dense(8, activation="relu"),
    Dense(1, activation="linear")
])

model.summary()
model.compile(
    loss="mse",
    optimizer="adam",
    metrics=["mse", "mae"]
)<jupyter_output><empty_output><jupyter_text>Fit Model<jupyter_code>history = model.fit(
    X_train, 
    y_train, 
    epochs=150, 
    batch_size=50,  
    verbose=1, 
    validation_split=0.2
)
print(history.history.keys())
# "Loss"
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()<jupyter_output>dict_keys(['val_loss', 'val_mse', 'val_mae', 'loss', 'mse', 'mae'])<jupyter_text>Make Predictions<jupyter_code>y_test_predictions = model.predict(X_test)
scaler_y.inverse_transform(y_test[0:5])
scaler_y.inverse_transform(y_test_predictions[0:5])<jupyter_output><empty_output><jupyter_text>Try to maximize it<jupyter_code>Xnew = np.array([[40, 0, 26, 9000, 8000]])
plt.scatter(
    df.age.values,
    df.sales.values
)
plt.scatter(
    df.debt.values,
    df.sales.values
)
np.cov(df.age.values, df.sales.values)
df.corr()
import seaborn as sns
sns.heatmap(
    df.corr()
)
import tensorflow as tf
a = [1, 10, 26.9, 2.8, 166.32, 62.3]
b = tf.math.argmax(input = a)
c = tf.keras.backend.eval(b)
df.age.min()
step = 0.0001
x_grid_scaled = np.array([
    np.arange(0, 1, step),
    np.arange(0, 1, step),
    np.arange(0, 1, step),
    np.arange(0, 1, step),
    np.arange(0, 1, step),
]).T


y_test_predictions_grid = model.predict(x_grid_scaled)

y_argmax = tf.math.argmax(input = y_test_predictions_grid)
argmax_index = tf.keras.backend.eval(y_argmax)
x_grid_scaled[argmax_index]<jupyter_output><empty_output><jupyter_text>Above we have our argmax-not terribly surprising considering most of these relationships are likely linear. Even debt has as positive correlation to sale price.<jupyter_code>scaler_y.inverse_transform(y_test_predictions_grid).max()
scaler_y.inverse_transform(model.predict(x_grid_scaled[9999].reshape(1, -1)))
scaler_y.inverse_transform(model.predict(x_grid_scaled[5000].reshape(1, -1)))
# Essentially can perform a generic grid search<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14634146341463414, 'fraction_numerical': 0.02496413199426112, 'mean_word_length': 5.944223107569721, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 1, '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': '103650', 'license': 'MIT', 'n_tokens_mistral': 1439, 'n_tokens_neox': 1357, 'n_words': 239, 'path': 'notebooks/Programming Appendix/Keras Regression Example.ipynb', 'repo_name': 'NathanielDake/NathanielDake.github.io', 'size': '3485'}