In [1]:
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
In [2]:
training_data_df = pd.read_csv("Exercise Files/03/sales_data_training.csv")
test_data_df = pd.read_csv("Exercise Files/03/sales_data_test.csv")
In [3]:
scaler = MinMaxScaler(feature_range=(0,1))
In [4]:
scaled_training = scaler.fit_transform(training_data_df)
scaled_testing = scaler.transform(test_data_df)
In [5]:
# Print out the adjustment that the scaler applied to the total_earnings column of data
print("Note: total_earnings values were scaled by multiplying by {:.10f} and adding {:.6f}".format(scaler.scale_[8], scaler.min_[8]))
In [6]:
# Create new pandas DataFrame objects from the scaled data
scaled_training_df = pd.DataFrame(scaled_training, columns=training_data_df.columns.values)
scaled_testing_df = pd.DataFrame(scaled_testing, columns=test_data_df.columns.values)
# Save scaled data dataframes to new CSV files
# scaled_training_df.to_csv("03/sales_data_training_scaled.csv", index=False)
# scaled_testing_df.to_csv("03/sales_data_test_scaled.csv", index=False)
In [7]:
from keras import backend as K
print(K.backend())
In [8]:
from keras.models import Sequential
from keras.layers import *
In [9]:
# training_data_df = pd.read_csv("sales_data_training_scaled.csv")
training_data_df = scaled_training_df.copy()
X = training_data_df.drop('total_earnings', axis=1).values
Y = training_data_df[['total_earnings']].values
# Define the model
model = Sequential()
model.add(Dense(50, input_dim=9, activation='relu'))
model.add(Dense(100, activation='relu'))
model.add(Dense(50, activation='relu'))
model.add(Dense(1, activation='linear'))
model.compile(loss="mean_squared_error", optimizer="adam")
In [10]:
model.fit(
X,
Y,
epochs=50,
shuffle=True,
verbose=2
)
Out[10]:
In [11]:
test_data_df = scaled_testing_df.copy()
X_test = test_data_df.drop('total_earnings', axis=1).values
Y_test = test_data_df[['total_earnings']].values
In [12]:
test_error_rate = model.evaluate(X_test, Y_test, verbose=0)
print("The mean squared error (MSE) for the test data set is: {}".format(test_error_rate))
In [13]:
X = pd.read_csv("04/proposed_new_product.csv").values
In [14]:
prediction = model.predict(X)
In [15]:
# Grab just the first element of the first prediction (since that's the only have one)
prediction = prediction[0][0]
In [16]:
# Re-scale the data from the 0-to-1 range back to dollars
# These constants are from when the data was originally scaled down to the 0-to-1 range
prediction = prediction + 0.1159
prediction = prediction / 0.0000036968
print("Earnings Prediction for Proposed Product - ${}".format(prediction))
In [17]:
model.save("trained_model.h5")
print("Model saved to disk")
In [18]:
from keras.models import load_model
In [19]:
model_disk = load_model("trained_model.h5")
In [20]:
X = pd.read_csv("Exercise Files/04/proposed_new_product.csv").values
prediction = model_disk.predict(X)
# Grab just the first element of the first prediction (since that's the only have one)
prediction = prediction[0][0]
# Re-scale the data from the 0-to-1 range back to dollars
# These constants are from when the data was originally scaled down to the 0-to-1 range
prediction = prediction + 0.1159
prediction = prediction / 0.0000036968
print("Earnings Prediction for Proposed Product - ${}".format(prediction))
In [ ]: