Commit ·
1cfd038
1
Parent(s): 96bc6ef
Updates
Browse files- app.py +2 -2
- inference.py +3 -4
- model_train.py +6 -3
app.py
CHANGED
|
@@ -14,7 +14,7 @@ save_directory = 'Generated Data/'
|
|
| 14 |
if not os.path.exists(save_directory):
|
| 15 |
os.makedirs(save_directory)
|
| 16 |
|
| 17 |
-
#
|
| 18 |
class LinearRegression(tf.Module):
|
| 19 |
def __init__(self, input_size, output_size):
|
| 20 |
self.W = tf.Variable(tf.random.normal([input_size, output_size]), name='weights')
|
|
@@ -78,7 +78,7 @@ def index():
|
|
| 78 |
fig.update_layout(title_text='Predicted Monthly Receipts Count Over Time')
|
| 79 |
fig.update_xaxes(title_text='Time Period')
|
| 80 |
fig.update_yaxes(title_text='Number of Receipts')
|
| 81 |
-
#
|
| 82 |
fig.add_trace(go.Scatter(x=data['Date'], y=data['Receipt_Count'], mode='markers', marker=dict(color='#93E9BE'), name='Actual Daily Receipt Count'))
|
| 83 |
|
| 84 |
plot_div = fig.to_html(full_html=False)
|
|
|
|
| 14 |
if not os.path.exists(save_directory):
|
| 15 |
os.makedirs(save_directory)
|
| 16 |
|
| 17 |
+
# Define the Linear Regression Model
|
| 18 |
class LinearRegression(tf.Module):
|
| 19 |
def __init__(self, input_size, output_size):
|
| 20 |
self.W = tf.Variable(tf.random.normal([input_size, output_size]), name='weights')
|
|
|
|
| 78 |
fig.update_layout(title_text='Predicted Monthly Receipts Count Over Time')
|
| 79 |
fig.update_xaxes(title_text='Time Period')
|
| 80 |
fig.update_yaxes(title_text='Number of Receipts')
|
| 81 |
+
# Adding scatter plot
|
| 82 |
fig.add_trace(go.Scatter(x=data['Date'], y=data['Receipt_Count'], mode='markers', marker=dict(color='#93E9BE'), name='Actual Daily Receipt Count'))
|
| 83 |
|
| 84 |
plot_div = fig.to_html(full_html=False)
|
inference.py
CHANGED
|
@@ -9,7 +9,7 @@ save_directory = 'Generated Data/'
|
|
| 9 |
if not os.path.exists(save_directory):
|
| 10 |
os.makedirs(save_directory)
|
| 11 |
|
| 12 |
-
#Define the
|
| 13 |
class LinearRegression(tf.Module):
|
| 14 |
def __init__(self, input_size, output_size):
|
| 15 |
self.W = tf.Variable(tf.random.normal([input_size, output_size]), name='weights')
|
|
@@ -21,7 +21,7 @@ class LinearRegression(tf.Module):
|
|
| 21 |
data = pd.read_csv('data_daily.csv', parse_dates=['# Date'], index_col=None) # Load the dataset
|
| 22 |
data.rename(columns={"# Date": "Date"}, inplace=True) # Rename Date Column
|
| 23 |
|
| 24 |
-
date_range = pd.date_range(start='2022-01-01', end='2022-12-31', freq='D')
|
| 25 |
inference_data = pd.DataFrame({'Date': date_range})
|
| 26 |
|
| 27 |
initial_date = np.datetime64('2021-01-01')
|
|
@@ -39,10 +39,9 @@ model.b.assign(loaded_model.b)
|
|
| 39 |
|
| 40 |
model(tf.constant([[0.0]], dtype=tf.float32)) # Run the model to initialize variables
|
| 41 |
|
| 42 |
-
y_predict = model(X)
|
| 43 |
y_predict *= np.max(data['Receipt_Count'].to_numpy())
|
| 44 |
|
| 45 |
-
# Assuming y_predict_tensor is your TensorFlow tensor
|
| 46 |
df = pd.DataFrame({
|
| 47 |
'Date': pd.date_range(start='2022-01-01', periods=365),
|
| 48 |
'Receipts': y_predict.numpy().squeeze()
|
|
|
|
| 9 |
if not os.path.exists(save_directory):
|
| 10 |
os.makedirs(save_directory)
|
| 11 |
|
| 12 |
+
# Define the Linear Regression Model
|
| 13 |
class LinearRegression(tf.Module):
|
| 14 |
def __init__(self, input_size, output_size):
|
| 15 |
self.W = tf.Variable(tf.random.normal([input_size, output_size]), name='weights')
|
|
|
|
| 21 |
data = pd.read_csv('data_daily.csv', parse_dates=['# Date'], index_col=None) # Load the dataset
|
| 22 |
data.rename(columns={"# Date": "Date"}, inplace=True) # Rename Date Column
|
| 23 |
|
| 24 |
+
date_range = pd.date_range(start='2022-01-01', end='2022-12-31', freq='D') # Creating Inference Dataframe
|
| 25 |
inference_data = pd.DataFrame({'Date': date_range})
|
| 26 |
|
| 27 |
initial_date = np.datetime64('2021-01-01')
|
|
|
|
| 39 |
|
| 40 |
model(tf.constant([[0.0]], dtype=tf.float32)) # Run the model to initialize variables
|
| 41 |
|
| 42 |
+
y_predict = model(X) # Run the model to make predictions
|
| 43 |
y_predict *= np.max(data['Receipt_Count'].to_numpy())
|
| 44 |
|
|
|
|
| 45 |
df = pd.DataFrame({
|
| 46 |
'Date': pd.date_range(start='2022-01-01', periods=365),
|
| 47 |
'Receipts': y_predict.numpy().squeeze()
|
model_train.py
CHANGED
|
@@ -9,6 +9,7 @@ save_directory = 'Generated Data/'
|
|
| 9 |
if not os.path.exists(save_directory):
|
| 10 |
os.makedirs(save_directory)
|
| 11 |
|
|
|
|
| 12 |
class LinearRegression(tf.Module):
|
| 13 |
def __init__(self, input_size, output_size):
|
| 14 |
self.W = tf.Variable(tf.random.normal([input_size, output_size]), name='weights')
|
|
@@ -17,12 +18,14 @@ class LinearRegression(tf.Module):
|
|
| 17 |
def __call__(self, X):
|
| 18 |
return tf.matmul(X, self.W) + self.b
|
| 19 |
|
| 20 |
-
def mean_squared_error(y_true, y_pred):
|
| 21 |
return tf.reduce_mean(tf.square(y_true - y_pred))
|
| 22 |
|
| 23 |
df = pd.read_csv('data_daily.csv', parse_dates=['# Date'], index_col=None) # Load the dataset
|
| 24 |
df.rename(columns={"# Date": "Date"}, inplace=True) # Rename Date Column
|
| 25 |
|
|
|
|
|
|
|
| 26 |
plt.figure(figsize=(15, 7))
|
| 27 |
sns.scatterplot(x=df['Date'], y=df['Receipt_Count'], color='#93E9BE', label='Actual Daily Receipt Count')
|
| 28 |
plt.title('Comparison of Daily Receipt Trends of Year 2021')
|
|
@@ -33,11 +36,10 @@ plt.legend()
|
|
| 33 |
plt.savefig(os.path.join(save_directory, 'data_visualization.png'), bbox_inches='tight')
|
| 34 |
plt.show()
|
| 35 |
|
| 36 |
-
|
| 37 |
X_np = np.arange(0, 365) / 364.0
|
| 38 |
y_np = df['Receipt_Count'].to_numpy() / np.max(df['Receipt_Count'])
|
| 39 |
|
| 40 |
-
train_size = int(0.8 * len(X_np))
|
| 41 |
|
| 42 |
X_train, X_test = X_np[:train_size], X_np[train_size:]
|
| 43 |
y_train, y_test = y_np[:train_size], y_np[train_size:]
|
|
@@ -81,6 +83,7 @@ plt.legend()
|
|
| 81 |
plt.savefig(os.path.join(save_directory, 'training_visualization.png'), bbox_inches='tight')
|
| 82 |
plt.show()
|
| 83 |
|
|
|
|
| 84 |
X_test_tensor = tf.constant(X_test, dtype=tf.float32)[:, tf.newaxis]
|
| 85 |
y_test_tensor = tf.constant(y_test, dtype=tf.float32)[:, tf.newaxis]
|
| 86 |
|
|
|
|
| 9 |
if not os.path.exists(save_directory):
|
| 10 |
os.makedirs(save_directory)
|
| 11 |
|
| 12 |
+
# Define the Linear Regression Model
|
| 13 |
class LinearRegression(tf.Module):
|
| 14 |
def __init__(self, input_size, output_size):
|
| 15 |
self.W = tf.Variable(tf.random.normal([input_size, output_size]), name='weights')
|
|
|
|
| 18 |
def __call__(self, X):
|
| 19 |
return tf.matmul(X, self.W) + self.b
|
| 20 |
|
| 21 |
+
def mean_squared_error(y_true, y_pred): # Mean Square Error function
|
| 22 |
return tf.reduce_mean(tf.square(y_true - y_pred))
|
| 23 |
|
| 24 |
df = pd.read_csv('data_daily.csv', parse_dates=['# Date'], index_col=None) # Load the dataset
|
| 25 |
df.rename(columns={"# Date": "Date"}, inplace=True) # Rename Date Column
|
| 26 |
|
| 27 |
+
|
| 28 |
+
# Plot the basic data visualization using Seaborn
|
| 29 |
plt.figure(figsize=(15, 7))
|
| 30 |
sns.scatterplot(x=df['Date'], y=df['Receipt_Count'], color='#93E9BE', label='Actual Daily Receipt Count')
|
| 31 |
plt.title('Comparison of Daily Receipt Trends of Year 2021')
|
|
|
|
| 36 |
plt.savefig(os.path.join(save_directory, 'data_visualization.png'), bbox_inches='tight')
|
| 37 |
plt.show()
|
| 38 |
|
|
|
|
| 39 |
X_np = np.arange(0, 365) / 364.0
|
| 40 |
y_np = df['Receipt_Count'].to_numpy() / np.max(df['Receipt_Count'])
|
| 41 |
|
| 42 |
+
train_size = int(0.8 * len(X_np)) # Spliting train and test date in 80:20 ratio
|
| 43 |
|
| 44 |
X_train, X_test = X_np[:train_size], X_np[train_size:]
|
| 45 |
y_train, y_test = y_np[:train_size], y_np[train_size:]
|
|
|
|
| 83 |
plt.savefig(os.path.join(save_directory, 'training_visualization.png'), bbox_inches='tight')
|
| 84 |
plt.show()
|
| 85 |
|
| 86 |
+
# Making X_test and y_test tensors
|
| 87 |
X_test_tensor = tf.constant(X_test, dtype=tf.float32)[:, tf.newaxis]
|
| 88 |
y_test_tensor = tf.constant(y_test, dtype=tf.float32)[:, tf.newaxis]
|
| 89 |
|