<jupyter_start><jupyter_text>Exploratory Data Analysis<jupyter_code># x values are day of the year
x_axis = BlackFriday_df['Week_of_Year']

# y values are the weekly units sold
data = BlackFriday_df['Weekly_Units_Sold']

# Tells matplotlib that we want to make a scatter plot
plt.figure(figsize=(16,5))
plt.scatter(x_axis, data, marker="o", alpha=0.75)
# filter for product 1 based on minimum normalization
BlackFriday_P1_df = BlackFriday_df[BlackFriday_df['Product'] == 1]
# BlackFriday_P1_df = BlackFriday_df[BlackFriday_df['Store'] == 1]
# x values are day of the year
x_axis = BlackFriday_P1_df['Week_of_Year']

# y values are the weekly units sold
data = BlackFriday_P1_df['NormToMin_Weekly_Units_Sold']

# Tells matplotlib that we want to make a scatter plot
plt.figure(figsize=(16,5))
plt.scatter(x_axis, data, marker="o", alpha=0.75)
# filter for product 1 based on maximum normalization
BlackFriday_P1_df = BlackFriday_df[BlackFriday_df['Product'] == 1]
# BlackFriday_P1_df = BlackFriday_df[BlackFriday_df['Store'] == 1]
# x values are day of the year
x_axis = BlackFriday_P1_df['Week_of_Year']

# y values are the weekly units sold
data = BlackFriday_P1_df['NormToMax_Weekly_Units_Sold']

# Tells matplotlib that we want to make a scatter plot
plt.figure(figsize=(16,5))
plt.scatter(x_axis, data, marker="o", alpha=0.75)
# filter for product 2 based on minimum normalization
BlackFriday_P2_df = BlackFriday_df[BlackFriday_df['Product'] == 2]

# x values are day of the year
x_axis = BlackFriday_P2_df['Week_of_Year']

# y values are the weekly units sold
data = BlackFriday_P2_df['NormToMin_Weekly_Units_Sold']

# Tells matplotlib that we want to make a scatter plot
plt.figure(figsize=(16,5))
plt.scatter(x_axis, data, marker="o", alpha=0.75)
# filter for product 2 based on maximum normalization
BlackFriday_P2_df = BlackFriday_df[BlackFriday_df['Product'] == 2]

# x values are day of the year
x_axis = BlackFriday_P2_df['Week_of_Year']

# y values are the weekly units sold
data = BlackFriday_P2_df['NormToMax_Weekly_Units_Sold']

# Tells matplotlib that we want to make a scatter plot
plt.figure(figsize=(16,5))
plt.scatter(x_axis, data, marker="o", alpha=0.75)
# filter for product 3 based on minimum normalization
BlackFriday_P3_df = BlackFriday_df[BlackFriday_df['Product'] == 3]

# x values are day of the year
x_axis = BlackFriday_P3_df['Week_of_Year']

# y values are the weekly units sold
data = BlackFriday_P3_df['NormToMin_Weekly_Units_Sold']

# Tells matplotlib that we want to make a scatter plot
plt.figure(figsize=(16,5))
plt.scatter(x_axis, data, marker="o", alpha=0.75)
# filter for product 3 based on maximum normalization
BlackFriday_P3_df = BlackFriday_df[BlackFriday_df['Product'] == 3]

# x values are day of the year
x_axis = BlackFriday_P3_df['Week_of_Year']

# y values are the weekly units sold
data = BlackFriday_P3_df['NormToMax_Weekly_Units_Sold']

# Tells matplotlib that we want to make a scatter plot
plt.figure(figsize=(16,5))
plt.scatter(x_axis, data, marker="o", alpha=0.75)<jupyter_output><empty_output><jupyter_text>Preprocessing<jupyter_code>BlackFriday_df.columns
from datetime import datetime, date
day_of_year = date(2007, 12, 31).timetuple().tm_yday  # returns 1 for January 1st
day_of_year
# Convert categorical data to numeric with `pd.get_dummies`
BlackFriday_convert_df = pd.get_dummies(BlackFriday_df, columns=['Product', 'Is_Holiday'])

# Drop the 'Is_Holiday_False' column since it is just the reverse of the 'Is_Holiday_True' column
BlackFriday_convert_df = BlackFriday_convert_df.drop('Is_Holiday_False', axis = 1)
BlackFriday_convert_df = BlackFriday_convert_df.drop('Store', axis = 1)
# BlackFriday_convert_df = BlackFriday_convert_df.drop('Unnamed: 0', axis = 1)
BlackFriday_convert_df
# filter for Holidays
BlackFriday_Is_Holiday_True_df = BlackFriday_convert_df[BlackFriday_convert_df['Is_Holiday_True'] == 1]
BlackFriday_Is_Holiday_True_df
# drop the date column
BlackFriday_convert_df.drop(BlackFriday_convert_df.iloc[:, 0:1], inplace = True, axis = 1)

BlackFriday_convert_df.dtypes
# Split our preprocessed data into our features and target arrays
y = BlackFriday_convert_df["NormToMax_Weekly_Units_Sold"].values
BlackFriday_convert_df.drop(BlackFriday_convert_df.iloc[:, 3:7], inplace = True, axis = 1)

X = BlackFriday_convert_df


# Split the preprocessed data into a training and testing dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)
# Create a StandardScaler instances
scaler = StandardScaler()

# Fit the StandardScaler
X_scaler = scaler.fit(X_train)

# Scale the data
X_train_scaled = X_scaler.transform(X_train)
X_test_scaled = X_scaler.transform(X_test)<jupyter_output><empty_output><jupyter_text>Compile, Train and Evaluate the Model  Deep Neural Network 1<jupyter_code># # Define the model - deep neural net, i.e., the number of input features and hidden nodes for each layer.
# number_input_features = 10
# hidden_nodes_layer1 =  10
# hidden_nodes_layer2 = 5

# nn = tf.keras.models.Sequential()

# # First hidden layer
# nn.add(tf.keras.layers.Dense(units = hidden_nodes_layer1, input_dim = number_input_features, activation = "relu"))

# # Second hidden layer
# nn.add(tf.keras.layers.Dense(units = hidden_nodes_layer2, activation = "relu"))

# # Output layer
# nn.add(tf.keras.layers.Dense(units = 1, activation = "sigmoid"))

# # Check the structure of the model
# nn.summary()
# # Compile the model
# nn.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
# # Train the model
# fit_model = nn.fit(X_train_scaled, y_train, epochs=100)<jupyter_output><empty_output><jupyter_text>Deep Neural Network 2<jupyter_code># # Create a method that creates a new Sequential model with hyperparameter options
# def create_model(hp):
#     nn_model = tf.keras.models.Sequential()

#     # Allow kerastuner to decide which activation function to use in hidden layers
#     activation = hp.Choice('activation',['relu','tanh','sigmoid'])
    
#     # Allow kerastuner to decide number of neurons in first layer
#     nn_model.add(tf.keras.layers.Dense(units=hp.Int('first_units',
#         min_value=1,
#         max_value=10,
#         step=2), activation=activation, input_dim=7))

#     # Allow kerastuner to decide number of hidden layers and neurons in hidden layers
#     for i in range(hp.Int('num_layers', 1, 6)):
#         nn_model.add(tf.keras.layers.Dense(units=hp.Int('units_' + str(i),
#             min_value=1,
#             max_value=10,
#             step=2),
#             activation=activation))
    
#     nn_model.add(tf.keras.layers.Dense(units=1, activation="sigmoid"))

#     # Compile the model
#     nn_model.compile(loss="binary_crossentropy", optimizer='adam', metrics=["accuracy"])
    
#     return nn_model
# # Import the kerastuner library
# import keras_tuner as kt

# tuner = kt.Hyperband(
#     create_model,
#     objective="val_accuracy",
#     max_epochs=20,
#     hyperband_iterations=2)
# # Run the kerastuner search for best hyperparameters
# tuner.search(X_train_scaled,y_train,epochs=20,validation_data=(X_test_scaled,y_test))<jupyter_output><empty_output><jupyter_text>Random Forest<jupyter_code># Train the Logistic Regression model on the unscaled data and print the model score
classifier = LogisticRegression(max_iter=100)
classifier.fit(X_train, y_train)
print(f"Training Data Score: {round(classifier.score(X_train, y_train),4)}")
print(f"Testing Data Score: {round(classifier.score(X_test, y_test),4)}")
# Train a Random Forest Classifier model and print the model score
clf = RandomForestClassifier(random_state=1, n_estimators=500).fit(X_train, y_train)
print(f'Training Score: {round(clf.score(X_train, y_train),4)}')
print(f'Testing Score: {round(clf.score(X_test, y_test),4)}')
# Train the Logistic Regression model on the scaled data and print the model score
classifier = LogisticRegression(max_iter=100)
classifier.fit(X_train_scaled, y_train)
print(f"Training Data Score: {round(classifier.score(X_train_scaled, y_train),4)}")
print(f"Testing Data Score: {round(classifier.score(X_test_scaled, y_test),4)}")
# Train a Random Forest Classifier model on the scaled data and print the model score
clf = RandomForestClassifier(random_state=1, n_estimators=500).fit(X_train_scaled, y_train)
print(f'Training Score: {round(clf.score(X_train_scaled, y_train),4)}')
print(f'Testing Score: {round(clf.score(X_test_scaled, y_test),4)}')
# Import the kerastuner library
import keras_tuner as kt

tuner = kt.Hyperband(
    create_model,
    objective="val_accuracy",
    max_epochs=20,
    hyperband_iterations=2)
# Run the kerastuner search for best hyperparameters
tuner.search(X_train_scaled,y_train,epochs=20,validation_data=(X_test_scaled,y_test))
# Get best model hyperparameters
best_hyper = tuner.get_best_hyperparameters(1)[0]
best_hyper.values
X_test_scaled.shape
# Evaluate best model against full test data
best_model = tuner.get_best_models(1)[0]
model_loss, model_accuracy = best_model.evaluate(X_test_scaled,y_test,verbose=2)
print(f"Loss: {model_loss}, Accuracy: {model_accuracy}")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10606060606060606, 'fraction_numerical': 0.017786561264822136, 'mean_word_length': 6.229365079365079, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '375508', 'license': 'MIT', 'n_tokens_mistral': 3320, 'n_tokens_neox': 3147, 'n_words': 769, 'path': 'static/Work Notebooks/exploringML_JFC.ipynb', 'repo_name': 'jackatopolis/black-friday-predictor', 'size': '9108'}