<jupyter_start><jupyter_text>Major Leagues- EECS 731 Project 5- Author: Lazarus- ID : 3028051   Problem Statement Shipping and delivering to a place near you1. Set up a data science project structure in a new git repository in your GitHub account2. Download the product demand data set fromhttps://www.kaggle.com/felixzhao/productdemandforecasting3. Load the data set into panda data frames4. Formulate one or two ideas on how feature engineering would help the data set to establish additional value using exploratory data analysis5. Build one or more forecasting models to determine the demand for a particular product using the other columns as features6. Document your process and results7. Commit your notebook, source code, visualizations and other supporting files to the git repository in GitHub What we want to do?- Build one or more forecasting models to determine the demand for a particular product using the other columns as features Step 1: Prepare Environment and import Data- Let's import pandas, numpy and pyplot libraries<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
product_data= pd.read_csv("../data/Historical_Product_Demand.csv")
product_data.head(5)<jupyter_output><empty_output><jupyter_text>Step 2: Data analysis and Feature Engineering - Lets look at how much data we are dealing with<jupyter_code>product_data.shape<jupyter_output><empty_output><jupyter_text>- Let's plot warehouses vs count of them<jupyter_code>sns.countplot(x="Warehouse", data= product_data)<jupyter_output><empty_output><jupyter_text>Treating Missing values<jupyter_code>print("::::::::before:::::::::::")
print(product_data.isnull().sum())
product_data=product_data.dropna()
print("::::::::after::::::::::::")
print(product_data.isnull().sum())<jupyter_output>::::::::before:::::::::::
Product_Code            0
Warehouse               0
Product_Category        0
Date                11239
Order_Demand            0
dtype: int64
::::::::after::::::::::::
Product_Code        0
Warehouse           0
Product_Category    0
Date                0
Order_Demand        0
dtype: int64<jupyter_text>Let's use describe function to find out most repeated product in dataset<jupyter_code>product_data.describe()<jupyter_output><empty_output><jupyter_text>- We see that Product_1359 is at the top so let's use this particular product for our forecasting Model- Let's create a new dataset which has only data related to product_1359<jupyter_code>df = product_data.loc[product_data['Product_Code'] == 'Product_1359']
df = df.drop(columns=['Product_Code', 'Warehouse', 'Product_Category'])
df['Date'] = pd.to_datetime(df['Date'])
df = df.sort_values(by=['Date'])
df = df.reset_index(drop=True)
df.head()<jupyter_output><empty_output><jupyter_text>- As we can see with same date there are multiple samples so lets combine such samples by adding demand values<jupyter_code>demands = []

for index, row in df.iterrows():
    demands.append(int(row['Order_Demand'].strip('()')))
    
df['Demand'] = demands
df = df.groupby('Date').agg({'Demand': 'sum'})
df.reset_index(inplace=True)
df.head()<jupyter_output><empty_output><jupyter_text>Let's plot the demand of product_1359<jupyter_code>f, ax = plt.subplots(figsize=(20,6))
sns.lineplot(x= "Date", y="Demand", data= df)<jupyter_output><empty_output><jupyter_text>- Now let's split date attribute to day, month and year<jupyter_code>df["Date"]=df['Date'].astype(str)
asl=df['Date'].str.split('-',n=-1,expand=True) 

df['year']=asl[0]
df['month']=asl[1]
df['day']=asl[2]
# df=df.drop(['Date'],axis=1)
df['year']=df['year'].astype(int)
df['month']=df['month'].astype(int)
df['day']=df['day'].astype(int)

df=df.set_index('Date')
df.head(5)<jupyter_output><empty_output><jupyter_text>Now we have our data to train lets split it such that we use 20% of data for testing<jupyter_code>from sklearn.model_selection import train_test_split
x = df.drop(['Demand'], axis=1)
y = df["Demand"]
xtr, xval, ytr, yval = train_test_split(x, y, test_size=.2, shuffle=False)<jupyter_output><empty_output><jupyter_text>Forecasting Models  Linear Regression model<jupyter_code>from sklearn.metrics import mean_squared_error
from sklearn.linear_model import LinearRegression

reg= LinearRegression().fit(xtr,ytr)
print("Training R2 score",reg.score(xtr,ytr))
print("Testing R2 score",reg.score(xval,yval))
df["reg"]=reg.predict(x)
df.head(5)<jupyter_output>Training R2 score 0.00798308424338956
Testing R2 score -0.001335919802511798<jupyter_text>- we can not make much from this, lets visualize predictions to understand better how Linear Regression model performed<jupyter_code>plt.rcParams['figure.figsize'] = [20, 6]
plt.plot(xtr.index.values, ytr, color='blue', label='Training Data', alpha=0.5)
plt.plot(xval.index.values, yval, color='green', label='Testing Data', alpha=0.5)
plt.plot(df.index.values, df['reg'], color='red', label='Linear Regression Predictions')
plt.legend(loc='best')
plt.ylabel("Demand")
plt.xlabel("Date")
plt.title("Linear Regression Predictions on complete data")
plt.xticks([])
plt.show()<jupyter_output><empty_output><jupyter_text>- We see that it performed worst throughout- Let's check only for testing part<jupyter_code>plt.rcParams['figure.figsize'] = [20, 6]
plt.plot(xval.index.values, yval, color='green', label='Testing Data', alpha=0.8)
plt.plot(xval.index.values, reg.predict(xval), color='red', label='Linear Regression Predictions', alpha=0.7)
plt.legend(loc='best')
plt.ylabel("Demand")
plt.xlabel("Date")
plt.title("Linear Regression Predictions on testing data")
plt.xticks([])
plt.show()<jupyter_output><empty_output><jupyter_text>- We can do lot better with other models Gradient Boosting Model<jupyter_code>from xgboost import XGBRegressor

gb =XGBRegressor()
gb= gb.fit(xtr,ytr)
print("Training R2 score",gb.score(xtr,ytr))
print("Testing R2 score",gb.score(xval,yval))

df["xgboost"]=gb.predict(x)<jupyter_output>Training R2 score 0.852648878481129
Testing R2 score -0.6673676260758077<jupyter_text>- Let's visualize the results of this model<jupyter_code>plt.rcParams['figure.figsize'] = [20, 6]
plt.plot(xtr.index.values, ytr, color='blue', label='Training Data', alpha=0.5)
plt.plot(xval.index.values, yval, color='green', label='Testing Data', alpha=0.5)
plt.plot(df.index.values, df['xgboost'], color='red', label='XGBOOST Predictions', alpha=0.3)
plt.legend(loc='best')
plt.ylabel("Demand")
plt.xlabel("Date")
plt.title("Gradient Boosting Predictions on complete data")
plt.xticks([])
plt.show()<jupyter_output><empty_output><jupyter_text>- I think this is way more better than previous model- Lets just look at testing part<jupyter_code>plt.rcParams['figure.figsize'] = [20, 6]
plt.plot(xval.index.values, yval, color='green', label='Testing Data', alpha=0.8)
plt.plot(xval.index.values, gb.predict(xval), color='red', label='XGBOOST Predictions', alpha=0.7)
plt.legend(loc='best')
plt.xticks([])
plt.ylabel("Demand")
plt.xlabel("Date")
plt.title("Gradient Boosting Predictions on testing data")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14000566251415628, 'fraction_numerical': 0.025056625141562854, 'mean_word_length': 6.738225629791895, 'pattern_counts': {'":': 2, '<': 62, '<?xml version=': 0, '>': 62, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '151201', 'license': 'MIT', 'n_tokens_mistral': 2535, 'n_tokens_neox': 2346, 'n_words': 607, 'path': 'notebooks/Demand_Forecaster.ipynb', 'repo_name': 'SFLazarus/Demand-Forecaster', 'size': '7064'}