SlideShare a Scribd company logo
Using the following code:
##Install Packages
!pip install tensorflow
!pip install matplotlib
!pip install numpy
!pip install pandas
##Import Statements
from datetime import datetime, timedelta
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
##Bringing in our dataset
url= 'https://raw.githubusercontent.com/BeeDrHU/Introduction-to-Python-CSPC-323-
/main/sales_forecast.csv'
data = pd.read_csv(url, sep=',')
##Filtering and Cleaning
data= data[['Store', 'Date', 'Temperature', 'Fuel_Price', 'CPI' , 'Unemployment', 'Weekly_Sales',
'IsHoliday_y']]
data['Date'] = pd.to_datetime(data['Date'], format='%d/%m/%Y')
data = data.set_index('Date')
data = data.sort_index()
##Checklist and Quality Assurance
data.isnull()
print(f'Number of rows with missing values: {data.isnull().any(axis=1).mean()}')
data.info()
##Subsetting variable to predict
df= data['Weekly_Sales']
df.plot()
##Train and Test Split
start_train = datetime(2010, 2, 5)
end_train = datetime(2011, 12, 30)
end_test = datetime(2012, 7, 13)
msk_train = (data.index >= start_train) & (data.index <= end_train)
msk_test = (data.index >= end_train) & (data.index <= end_test)
df_train = df.loc[msk_train]
df_test = df.loc[msk_test]
df_train.plot()
df_test.plot()
##Normalizing our data
uni_data= df.values.astype(float)
df_train= int(len(df_train))
uni_train_mean= uni_data[:df_train].mean()
uni_train_std= uni_data[:df_train].std()
uni_data= (uni_data-uni_train_mean)/uni_train_std
##Build the features dataset to make the model multivariate
features_considered = ['Temperature', 'Fuel_Price', 'CPI']
features = data[features_considered]
features.index = data.index
##Standardizing the Data
dataset = features.values
data_mean = dataset[:df_train].mean(axis=0)
data_std = dataset[:df_train].std(axis=0)
dataset = (dataset-data_mean)/data_std
##Splitting data into training and testing
x_train = dataset[msk_train]
y_train = uni_data[:df_train]
x_test = dataset[msk_test]
y_test = uni_data[df_train:]
##Defining the model architecture
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(64, activation='relu', input_shape=[x_train.shape[1]]),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(1)
])
##Compiling the model
model.compile(optimizer=tf.keras.optimizers.RMSprop(),
loss='mae')
##Fitting the model to the training data
history = model.fit(x_train, y_train,
epochs=100,
batch_size=64,
validation_split=0.2,
verbose=0)
##Evaluating the model on the test data
results = model.evaluate(x_test, y_test, verbose=0)
print(f'Test loss: {results}')
##Making predictions on new data
predictions = model.predict(x_test)
##Plotting the results
fig, ax = plt.subplots(figsize=(10,5))
ax.plot(np.arange(len(y_test)), y_test, label='Actual')
ax.plot(np.arange(len(predictions)), predictions, label='Predicted')
ax.legend()
plt.title('Actual vs Predicted Weekly Sales')
plt.xlabel('Week')
plt.ylabel('Normalized Sales')
plt.show()
*BUILD the RNN*
##Defining Function to Build Multivariate Data Structure
def multivariate_data(dataset, target, start_index, end_index, history_size,
target_size, step, single_step=False):
data = []
labels = []
start_index = start_index + history_size
if end_index is None:
end_index = len(dataset) - target_size
for i in range(start_index, end_index):
indices = range(i-history_size, i, step)
data.append(dataset[indices])
if single_step:
labels.append(target[i+target_size])
else:
labels.append(target[i:i+target_size])
return np.array(data), np.array(labels)
##Define the Multi-step Plotting Structure
def multi_step_plot(history, true_future, prediction):
plt.figure(figsize=(12, 6))
num_in = create_time_steps(len(history))
num_out = len(true_future)
plt.plot(num_in, np.array(history[:, 1]), label='History')
plt.plot(np.arange(num_out)/STEP, np.array(true_future), 'bo',
label='True Future')
if prediction.any():
plt.plot(np.arange(num_out)/STEP, np.array(prediction), 'ro',
label='Predicted Future')
plt.legend(loc='upper left')
plt.show()
##Define Function to Plot training and Validation over Time
def plot_train_history(history, title):
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(loss))
plt.figure()
plt.plot(epochs, loss, 'b', label='Training loss')
plt.plot(epochs, val_loss, 'r', label='Validation loss')
plt.title(title)
plt.legend()
plt.show()
##How far into the past do you want the model to see?
past_history=
##How far into the future do you want the model to see?
future_target=
##
STEP=
df_val = len(dataset) - past_history - future_target
x_train_multi, y_train_multi = multivariate_data(dataset, dataset[:, 1], 0,
df_train, past_history,
future_target, STEP)
x_val_multi, y_val_multi = multivariate_data(dataset, dataset[:, 1],
df_train, df_val, past_history,
future_target, STEP)
##Define Constants
BATCH_SIZE =
BUFFER_SIZE =
EPOCHS =
EVALUATION_INTERVAL =
##Adding Layers Due to Complexity
train_data_multi = tf.data.Dataset.from_tensor_slices(())
train_data_multi = train_data_multi.cache().shuffle().batch().repeat()
val_data_multi = tf.data.Dataset.from_tensor_slices(())
val_data_multi = val_data_multi.batch().repeat()
multi_step_model = tf.keras.models.Sequential()
multi_step_model.add(tf.keras.layers.LSTM())
multi_step_model.add(tf.keras.layers.LSTM())
multi_step_model.add(tf.keras.layers.Dense(72))
multi_step_model.compile(optimizer=tf.keras.optimizers.RMSprop(clipvalue=1.0), loss='mae')
##Train the Model
multi_step_history = multi_step_model.fit(train_data_multi, epochs=,
steps_per_epoch=,
validation_data=,
validation_steps=50)
##Predict Future Data
for x, y in .take(3):
multi_step_plot(x[0], y[0], .predict(x)[0])
#Evaluate the model on the test data
test_data_multi = tf.data.Dataset.from_tensor_slices())
test_data_multi = test_data_multi.batch()
# Define the number of steps in the test dataset
steps = len()
mae_values = []
for x, y in :
x_val, y_val= x[:, :-1], y[:,-1]
#Make predictions using the model
= .predict(x_val, steps=steps)
#Compute MAE
mae = tf.keras.metrics.mean_absolute_error().numpy()
mae_values.append(mae)
overall_mae = np.mean(mae_values)
print('Overall MAE:', overall_mae)
*Based on the model accuracy, do you think the features you selected to include in the RNN were
a good choice? What other features do you think could improve the model? Can be real or
theoretical features you're interested in.
If you had to keep the same features that are currently included in your model, what technique(s)
could you use to optimize the model? Explain what these techniques are doing to determine the
optimal model state.*

More Related Content

DOCX
Here is my current code- I need help visualizing my data using Python-.docx
PDF
USING httpsraw.githubusercontent.comBeeDrHUIntroduction-to-Py.pdf
PDF
Naive application of Machine Learning to Software Development
PPTX
Data Analysis Using Python Programming Language.pptx
PDF
Forecast stock prices python
PPTX
Running Intelligent Applications inside a Database: Deep Learning with Python...
PPTX
Working with Graphs _python.pptx
PDF
DSC program.pdf
Here is my current code- I need help visualizing my data using Python-.docx
USING httpsraw.githubusercontent.comBeeDrHUIntroduction-to-Py.pdf
Naive application of Machine Learning to Software Development
Data Analysis Using Python Programming Language.pptx
Forecast stock prices python
Running Intelligent Applications inside a Database: Deep Learning with Python...
Working with Graphs _python.pptx
DSC program.pdf

Similar to Using the following code Install Packages pip install .pdf (20)

PDF
Procesamiento del lenguaje natural con python
PDF
AI & ML Lab Manual1_BCE.pdf artificial intelligence
DOCX
ggtimeseries-->ggplot2 extensions
PDF
A Map of the PyData Stack
PPTX
Python for Scientists
PPTX
Unit 4_Working with Graphs _python (2).pptx
PDF
Using an Array include ltstdiohgt include ltmpih.pdf
PPTX
dataframe_operations and various functions
PPT
Stat Design3 18 09
PDF
cluster(python)
PDF
Zoro123456789123456789123456789123456789
 
PDF
labb123456789123456789123456789123456789
 
PDF
Boost Productivity with 30 Simple Python Scripts.pdf
PPTX
Introduction to data analyticals123232.pptx
PDF
I have this python code and i am trying to Build a multivari.pdf
PDF
LeetCode Database problems solved using PySpark.pdf
PDF
Assignment 5.2.pdf
PPTX
DataStructures in Pyhton Pandas and numpy.pptx
PDF
Is HTML5 Ready? (workshop)
PDF
Is html5-ready-workshop-110727181512-phpapp02
Procesamiento del lenguaje natural con python
AI & ML Lab Manual1_BCE.pdf artificial intelligence
ggtimeseries-->ggplot2 extensions
A Map of the PyData Stack
Python for Scientists
Unit 4_Working with Graphs _python (2).pptx
Using an Array include ltstdiohgt include ltmpih.pdf
dataframe_operations and various functions
Stat Design3 18 09
cluster(python)
Zoro123456789123456789123456789123456789
 
labb123456789123456789123456789123456789
 
Boost Productivity with 30 Simple Python Scripts.pdf
Introduction to data analyticals123232.pptx
I have this python code and i am trying to Build a multivari.pdf
LeetCode Database problems solved using PySpark.pdf
Assignment 5.2.pdf
DataStructures in Pyhton Pandas and numpy.pptx
Is HTML5 Ready? (workshop)
Is html5-ready-workshop-110727181512-phpapp02
Ad

More from picscamshoppe (20)

PDF
VAKA ALIMASI 100 Hassas kaynak snrl projelerde bo zaman .pdf
PDF
Vaka 8 Patagonya Tutkulu alanlar ekmek ve Glendirmek .pdf
PDF
Uyumluluk denetimi genellikle denetimlerin tesine geerek a.pdf
PDF
Uzun vadeli yatrm kararlar ile ilgilenen finans alan olarak .pdf
PDF
Vaka 101 Zappos Yneticileri Ortadan Kaldryor evrimii ay.pdf
PDF
Utilizacin de la inteligencia emocional en el lugar de trab.pdf
PDF
Utilizando el concepto de conocimiento metacognitivo los pr.pdf
PDF
Usted es miembro del grupo de relaciones con los empleados d.pdf
PDF
Usted es consultor comercial de People for the Ethical Treat.pdf
PDF
usptrerl the following recerd was krpe ale 50PA a the .pdf
PDF
Using Ubuntu write a progam in C that do this A Wait for .pdf
PDF
Using Ubuntu write a program uin C that do this A Wait for.pdf
PDF
Using the RamseyCassKoopmans Model what policy would best.pdf
PDF
Using the pypcaptile library already available in the VLE.pdf
PDF
Using the Mpgcsv dataset b Run a basic linear regression.pdf
PDF
Using the payoffs for the HawkDove game that we discussed i.pdf
PDF
Using the line for the Annual mean calculate the average .pdf
PDF
Using the information below to answer the following two ques.pdf
PDF
Using the information below to answer the following three qu.pdf
PDF
Using the following UML please create this java code I spe.pdf
VAKA ALIMASI 100 Hassas kaynak snrl projelerde bo zaman .pdf
Vaka 8 Patagonya Tutkulu alanlar ekmek ve Glendirmek .pdf
Uyumluluk denetimi genellikle denetimlerin tesine geerek a.pdf
Uzun vadeli yatrm kararlar ile ilgilenen finans alan olarak .pdf
Vaka 101 Zappos Yneticileri Ortadan Kaldryor evrimii ay.pdf
Utilizacin de la inteligencia emocional en el lugar de trab.pdf
Utilizando el concepto de conocimiento metacognitivo los pr.pdf
Usted es miembro del grupo de relaciones con los empleados d.pdf
Usted es consultor comercial de People for the Ethical Treat.pdf
usptrerl the following recerd was krpe ale 50PA a the .pdf
Using Ubuntu write a progam in C that do this A Wait for .pdf
Using Ubuntu write a program uin C that do this A Wait for.pdf
Using the RamseyCassKoopmans Model what policy would best.pdf
Using the pypcaptile library already available in the VLE.pdf
Using the Mpgcsv dataset b Run a basic linear regression.pdf
Using the payoffs for the HawkDove game that we discussed i.pdf
Using the line for the Annual mean calculate the average .pdf
Using the information below to answer the following two ques.pdf
Using the information below to answer the following three qu.pdf
Using the following UML please create this java code I spe.pdf
Ad

Recently uploaded (20)

PDF
Computing-Curriculum for Schools in Ghana
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Cell Structure & Organelles in detailed.
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Basic Mud Logging Guide for educational purpose
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
RMMM.pdf make it easy to upload and study
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Cell Types and Its function , kingdom of life
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Classroom Observation Tools for Teachers
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Insiders guide to clinical Medicine.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
Computing-Curriculum for Schools in Ghana
Final Presentation General Medicine 03-08-2024.pptx
Cell Structure & Organelles in detailed.
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Microbial diseases, their pathogenesis and prophylaxis
Basic Mud Logging Guide for educational purpose
2.FourierTransform-ShortQuestionswithAnswers.pdf
RMMM.pdf make it easy to upload and study
Renaissance Architecture: A Journey from Faith to Humanism
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Cell Types and Its function , kingdom of life
O5-L3 Freight Transport Ops (International) V1.pdf
01-Introduction-to-Information-Management.pdf
VCE English Exam - Section C Student Revision Booklet
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Classroom Observation Tools for Teachers
STATICS OF THE RIGID BODIES Hibbelers.pdf
Insiders guide to clinical Medicine.pdf
PPH.pptx obstetrics and gynecology in nursing

Using the following code Install Packages pip install .pdf

  • 1. Using the following code: ##Install Packages !pip install tensorflow !pip install matplotlib !pip install numpy !pip install pandas ##Import Statements from datetime import datetime, timedelta import numpy as np import pandas as pd import matplotlib.pyplot as plt import tensorflow as tf ##Bringing in our dataset url= 'https://raw.githubusercontent.com/BeeDrHU/Introduction-to-Python-CSPC-323- /main/sales_forecast.csv' data = pd.read_csv(url, sep=',') ##Filtering and Cleaning data= data[['Store', 'Date', 'Temperature', 'Fuel_Price', 'CPI' , 'Unemployment', 'Weekly_Sales', 'IsHoliday_y']] data['Date'] = pd.to_datetime(data['Date'], format='%d/%m/%Y') data = data.set_index('Date') data = data.sort_index() ##Checklist and Quality Assurance data.isnull() print(f'Number of rows with missing values: {data.isnull().any(axis=1).mean()}') data.info() ##Subsetting variable to predict df= data['Weekly_Sales'] df.plot() ##Train and Test Split start_train = datetime(2010, 2, 5) end_train = datetime(2011, 12, 30) end_test = datetime(2012, 7, 13) msk_train = (data.index >= start_train) & (data.index <= end_train) msk_test = (data.index >= end_train) & (data.index <= end_test) df_train = df.loc[msk_train] df_test = df.loc[msk_test] df_train.plot() df_test.plot() ##Normalizing our data uni_data= df.values.astype(float) df_train= int(len(df_train))
  • 2. uni_train_mean= uni_data[:df_train].mean() uni_train_std= uni_data[:df_train].std() uni_data= (uni_data-uni_train_mean)/uni_train_std ##Build the features dataset to make the model multivariate features_considered = ['Temperature', 'Fuel_Price', 'CPI'] features = data[features_considered] features.index = data.index ##Standardizing the Data dataset = features.values data_mean = dataset[:df_train].mean(axis=0) data_std = dataset[:df_train].std(axis=0) dataset = (dataset-data_mean)/data_std ##Splitting data into training and testing x_train = dataset[msk_train] y_train = uni_data[:df_train] x_test = dataset[msk_test] y_test = uni_data[df_train:] ##Defining the model architecture model = tf.keras.models.Sequential([ tf.keras.layers.Dense(64, activation='relu', input_shape=[x_train.shape[1]]), tf.keras.layers.Dense(64, activation='relu'), tf.keras.layers.Dense(1) ]) ##Compiling the model model.compile(optimizer=tf.keras.optimizers.RMSprop(), loss='mae') ##Fitting the model to the training data history = model.fit(x_train, y_train, epochs=100, batch_size=64, validation_split=0.2, verbose=0) ##Evaluating the model on the test data results = model.evaluate(x_test, y_test, verbose=0) print(f'Test loss: {results}') ##Making predictions on new data predictions = model.predict(x_test) ##Plotting the results fig, ax = plt.subplots(figsize=(10,5)) ax.plot(np.arange(len(y_test)), y_test, label='Actual') ax.plot(np.arange(len(predictions)), predictions, label='Predicted') ax.legend()
  • 3. plt.title('Actual vs Predicted Weekly Sales') plt.xlabel('Week') plt.ylabel('Normalized Sales') plt.show() *BUILD the RNN* ##Defining Function to Build Multivariate Data Structure def multivariate_data(dataset, target, start_index, end_index, history_size, target_size, step, single_step=False): data = [] labels = [] start_index = start_index + history_size if end_index is None: end_index = len(dataset) - target_size for i in range(start_index, end_index): indices = range(i-history_size, i, step) data.append(dataset[indices]) if single_step: labels.append(target[i+target_size]) else: labels.append(target[i:i+target_size]) return np.array(data), np.array(labels) ##Define the Multi-step Plotting Structure def multi_step_plot(history, true_future, prediction): plt.figure(figsize=(12, 6)) num_in = create_time_steps(len(history)) num_out = len(true_future) plt.plot(num_in, np.array(history[:, 1]), label='History') plt.plot(np.arange(num_out)/STEP, np.array(true_future), 'bo', label='True Future') if prediction.any(): plt.plot(np.arange(num_out)/STEP, np.array(prediction), 'ro', label='Predicted Future') plt.legend(loc='upper left') plt.show() ##Define Function to Plot training and Validation over Time def plot_train_history(history, title): loss = history.history['loss'] val_loss = history.history['val_loss'] epochs = range(len(loss)) plt.figure() plt.plot(epochs, loss, 'b', label='Training loss') plt.plot(epochs, val_loss, 'r', label='Validation loss')
  • 4. plt.title(title) plt.legend() plt.show() ##How far into the past do you want the model to see? past_history= ##How far into the future do you want the model to see? future_target= ## STEP= df_val = len(dataset) - past_history - future_target x_train_multi, y_train_multi = multivariate_data(dataset, dataset[:, 1], 0, df_train, past_history, future_target, STEP) x_val_multi, y_val_multi = multivariate_data(dataset, dataset[:, 1], df_train, df_val, past_history, future_target, STEP) ##Define Constants BATCH_SIZE = BUFFER_SIZE = EPOCHS = EVALUATION_INTERVAL = ##Adding Layers Due to Complexity train_data_multi = tf.data.Dataset.from_tensor_slices(()) train_data_multi = train_data_multi.cache().shuffle().batch().repeat() val_data_multi = tf.data.Dataset.from_tensor_slices(()) val_data_multi = val_data_multi.batch().repeat() multi_step_model = tf.keras.models.Sequential() multi_step_model.add(tf.keras.layers.LSTM()) multi_step_model.add(tf.keras.layers.LSTM()) multi_step_model.add(tf.keras.layers.Dense(72)) multi_step_model.compile(optimizer=tf.keras.optimizers.RMSprop(clipvalue=1.0), loss='mae') ##Train the Model multi_step_history = multi_step_model.fit(train_data_multi, epochs=, steps_per_epoch=, validation_data=, validation_steps=50) ##Predict Future Data for x, y in .take(3): multi_step_plot(x[0], y[0], .predict(x)[0]) #Evaluate the model on the test data test_data_multi = tf.data.Dataset.from_tensor_slices())
  • 5. test_data_multi = test_data_multi.batch() # Define the number of steps in the test dataset steps = len() mae_values = [] for x, y in : x_val, y_val= x[:, :-1], y[:,-1] #Make predictions using the model = .predict(x_val, steps=steps) #Compute MAE mae = tf.keras.metrics.mean_absolute_error().numpy() mae_values.append(mae) overall_mae = np.mean(mae_values) print('Overall MAE:', overall_mae) *Based on the model accuracy, do you think the features you selected to include in the RNN were a good choice? What other features do you think could improve the model? Can be real or theoretical features you're interested in. If you had to keep the same features that are currently included in your model, what technique(s) could you use to optimize the model? Explain what these techniques are doing to determine the optimal model state.*