<jupyter_start><jupyter_text>Walmart Weekly Sale Prediction System  Problem Statement Predict Walmart store sales, historic data of weekly sales with information of store type, markdowns has been provided.  import libraries<jupyter_code>import pandas as pd 
import numpy as np
import math
from datetime import datetime, timedelta
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
import warnings
warnings.filterwarnings('ignore')<jupyter_output><empty_output><jupyter_text>Data Collection<jupyter_code>train_data = pd.read_csv('train.csv')
train_data.head()
stores_data = pd.read_csv('stores.csv')
stores_data.head()
feature_data = pd.read_csv('features.csv')
feature_data.head()<jupyter_output><empty_output><jupyter_text>Data set preparation: Joining datasets train, store, feature<jupyter_code>train_data = pd.merge(train_data,stores_data,on='Store')
train_data = pd.merge(train_data,feature_data,on='Store')

train_data.head()
train_data.tail()
trainSet = pd.read_csv('train1.csv')
trainSet.head()
trainSet.shape<jupyter_output><empty_output><jupyter_text>Data Preprocessing<jupyter_code>#determining missing data
trainSet.isnull().sum()<jupyter_output><empty_output><jupyter_text>Missing values<jupyter_code>#Replacing missing markdown values as 0 as it implies no discount
from statistics import mean
trainSet['MarkDown1'] = trainSet['MarkDown1'].fillna(0)
trainSet['MarkDown2'] = trainSet['MarkDown2'].fillna(0)
trainSet['MarkDown3'] = trainSet['MarkDown3'].fillna(0)
trainSet['MarkDown4'] = trainSet['MarkDown4'].fillna(0)
trainSet['MarkDown5'] = trainSet['MarkDown5'].fillna(0)
trainSet.head()<jupyter_output><empty_output><jupyter_text>Data Exploration<jupyter_code>#Group by store type and identify number of stores belonging to each type
type_grouped_store = trainSet.groupby('Type')['Store'].nunique()
#plot pie chart of percentage of stores of each type
labels = 'A', 'B', 'C'
sizes = [(type_grouped_store[0]/45)*100, (type_grouped_store[1]/45)*100, (type_grouped_store[2]/45)*100]
colors = ['yellowgreen', 'lightskyblue', 'gold']
explode = (0, 0, 0.1) # explode 1st slice

# Plot
plt.pie(sizes, explode=explode, labels=labels, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=140)
plt.axis('equal')
plt.show()
store_sale = pd.concat([trainSet['Type'], trainSet['Weekly_Sales']], axis=1)
f, ax = plt.subplots(figsize=(8, 6))
fig = sns.boxplot(x='Type', y='Weekly_Sales', data=store_sale, showfliers=False)
# visualize correlation matrix in Seaborn using a heatmap
plt.figure(figsize=(15,12))
cmap = sns.diverging_palette(220, 10, as_cmap=True)
sns.heatmap(trainSet.corr(),annot=True,cmap=cmap)
plt.plot()
#Determine top 5 dates with highest sale
trainSet.loc[trainSet['Weekly_Sales'] >180000,"Date"].value_counts().head()
trainSet1 = trainSet.copy()
# Extract date features
trainSet1['Date'] = pd.to_datetime(trainSet1['Date'])
trainSet1['Date_dayofweek'] = trainSet1['Date'].dt.dayofweek
trainSet1['Date_month'] = trainSet1['Date'].dt.month 
trainSet1['Date_year'] = trainSet1['Date'].dt.year
trainSet1['Date_day'] = trainSet1['Date'].dt.day
trainSet1.head()<jupyter_output><empty_output><jupyter_text>seperating data year wise for further analysis<jupyter_code>train_2010=trainSet1[trainSet1['Date_year']==2010]
train_2011=trainSet1[trainSet1['Date_year']==2011]
train_2012=trainSet1[trainSet1['Date_year']==2012]<jupyter_output><empty_output><jupyter_text>checking yearly distribution<jupyter_code>year_sales = pd.concat([trainSet1['Date_year'], trainSet1['Weekly_Sales']], axis=1)
f, ax = plt.subplots(figsize=(8, 6))
fig = sns.boxplot(x='Date_year', y='Weekly_Sales', data=year_sales, showfliers=False)<jupyter_output><empty_output><jupyter_text>ploting yearly timeseries<jupyter_code>fig, ax = plt.subplots(figsize=(10,4))
sns.lineplot(x=train_2010['Date'], y=train_2010['Weekly_Sales'],data=train_2010)
plt.xticks(rotation=15)
plt.title('Weekly sales of year 2010')

fig, ax = plt.subplots(figsize=(10,4))
sns.lineplot(x=train_2011['Date'], y=train_2011['Weekly_Sales'],data=train_2011)
plt.xticks(rotation=15)
plt.title('Weekly sales of year 2011')

fig, ax = plt.subplots(figsize=(10,4))
sns.lineplot(x=train_2012['Date'], y=train_2012['Weekly_Sales'],data=train_2012)
plt.xticks(rotation=15)
plt.title('Weekly sales of year 2012')
plt.show()<jupyter_output><empty_output><jupyter_text>Highest sales is identified around Christmas and Black Friday  whole week before holiday marked as 1<jupyter_code>trainSet1['Super_Bowl'] = np.where((trainSet1['Date']==datetime(2010, 2, 12)) | (trainSet1['Date']==datetime(2011, 2, 11)) | (trainSet1['Date']==datetime(2012, 2, 10)) | (trainSet1['Date']==datetime(2013, 2, 8)),1,0)
trainSet1['Labour_Day'] = np.where((trainSet1['Date']==datetime(2010, 9, 10)) | (trainSet1['Date']==datetime(2011, 9, 9)) | (trainSet1['Date']==datetime(2012, 9, 7)) | (trainSet1['Date']==datetime(2013, 9, 6)),1,0)
trainSet1['BlackFriday'] = np.where((trainSet1['Date']==datetime(2010, 11, 26)) | (trainSet1['Date']==datetime(2011, 11, 25)) | (trainSet1['Date']==datetime(2012, 11, 23)) | (trainSet1['Date']==datetime(2013, 11, 29)),1,0)
trainSet1['Christmas'] = np.where((trainSet1['Date']==datetime(2010, 12, 31)) | (trainSet1['Date']==datetime(2011, 12, 30)) | (trainSet1['Date']==datetime(2012, 12, 28)) | (trainSet1['Date']==datetime(2013, 12, 27)),1,0)
trainSet1.head()
#converting categorical Holiday values to numerical values(False=0, True=1)
mapping = {False: 0, True: 1}
trainSet1['IsHoliday'] = trainSet1['IsHoliday'].map(mapping)
# Altering the isHoliday value depending on these new holidays...
trainSet1['IsHoliday']=trainSet1['IsHoliday']|trainSet1['Super_Bowl']|trainSet1['Labour_Day']|trainSet1['BlackFriday']|trainSet1['Christmas']
trainSet1.head(15)
# Droping the redundant holiday variables
holiday_val=['Super_Bowl','Labour_Day','BlackFriday','Christmas']
trainSet1.drop(holiday_val,axis=1,inplace=True)
trainSet1.head()
X = trainSet1.drop(['Weekly_Sales','Date','Type'], axis=1)
Y = trainSet1['Weekly_Sales']
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=0)

X_train.head()<jupyter_output><empty_output><jupyter_text>Prediction Model<jupyter_code>from sklearn import metrics
from sklearn.neighbors import KNeighborsRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor

from sklearn.metrics import mean_absolute_error, r2_score
from sklearn.model_selection import GridSearchCV,RandomizedSearchCV<jupyter_output><empty_output><jupyter_text>KNN Regressor<jupyter_code>#Method 1: KNN with 10-neighbors
knn = KNeighborsRegressor(n_neighbors=10,n_jobs=4)
knn.fit(X_train,y_train)
y_pred = knn.predict(X_test)
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
print(('R2 score:', r2_score(y_test, y_pred)))
#hyperparameter tuning for knn regressor

#grid_knn={'leaf_size':[10,20,30],
#         'n_neighbors':[10,15,20],
#         'p':[1,2]}
#knn_2 = KNeighborsRegressor()
#clf_grid = GridSearchCV(knn_2,grid_knn,cv = 5,n_jobs = 5,verbose=True)
#knn_grid = clf_grid.fit(X_train,y_train)
#y_pred = knn_grid.predict(X_test)
#print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred))
#print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
#print(('R2 score:', r2_score(y_test, y_pred)))<jupyter_output><empty_output><jupyter_text>Decision Tree Regressor<jupyter_code>#method 2: Decision Tree Regressor
dt = DecisionTreeRegressor(random_state=0,n_estimators= 500, max_depth= 6, learning_rate= 0.5, max_leaf_nodes=,max_features=)
dt.fit(X_train,y_train)
y_pred = dt.predict(X_test)
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
print(('R2 score:', r2_score(y_test, y_pred)))<jupyter_output>Mean Absolute Error: 1994.5497291603972
Root Mean Squared Error: 5599.504538959466
('R2 score:', 0.9390155367550804)<jupyter_text>XGB Regressor<jupyter_code>#method 4: XGB Regressor
xgb_clf = xgb.XGBRegressor(objective='reg:squarederror', nthread= 4, n_estimators= 500, max_depth= 6, learning_rate= 0.5) 
xb = xgb_clf.fit(X_train,y_train)
y_pred=xgb_clf.predict(X_test)
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
print(('R2 score:', r2_score(y_test, y_pred)))<jupyter_output>Mean Absolute Error: 2127.342503535667
Root Mean Squared Error: 4214.000758617059
('R2 score:', 0.9654610426902919)<jupyter_text>Random Forest Regressor<jupyter_code>#method 3: Random Forest with 20 trees

rfr = RandomForestRegressor(n_estimators =20)    
rfr.fit(X_train,y_train)
y_pred=rfr.predict(X_test)
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
print(('R2 score:', r2_score(y_test, y_pred)))
print(rfr.n_estimators)
print(rfr.max_features)
print(rfr.max_depth)
print(rfr.min_samples_split)
print(rfr.min_samples_leaf)
#Hyper parameter tuning

rfr = RandomForestRegressor()    

grid_rf = {'n_estimators': [20,30,40,50],
               'max_features': [10,15],
               'min_samples_split': [2,3],
              'min_samples_leaf':[1,2]}

rf_grid = RandomizedSearchCV(rfr,grid_rf,cv = 5,n_jobs = 5,verbose=True)

rf_grid.fit(X_train,y_train)

y_pred=rf_grid.predict(X_test)

print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
print(('R2 score:', r2_score(y_test, y_pred)))
(rf_grid.best_params_)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15244953849274775, 'fraction_numerical': 0.052845116137539304, 'mean_word_length': 9.48936170212766, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 60, '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': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '367142', 'license': 'MIT', 'n_tokens_mistral': 4080, 'n_tokens_neox': 3657, 'n_words': 629, 'path': 'Walmart_sales_prediction.ipynb', 'repo_name': 'manasishrotri/WalmartSalesPrediction', 'size': '9859'}