0% found this document useful (0 votes)
33 views24 pages

A Time Series Forecasting Case Study - PART 1

Uploaded by

Teto Schedule
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views24 pages

A Time Series Forecasting Case Study - PART 1

Uploaded by

Teto Schedule
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

WALMART UNIT SALES PREDICTION — A Time Series ... https://m-mazumdar13.medium.com/walmart-unit-sales-predi...

1 of 24 11/8/2021, 2:32 PM
WALMART UNIT SALES PREDICTION — A Time Series ... https://m-mazumdar13.medium.com/walmart-unit-sales-predi...

2 of 24 11/8/2021, 2:32 PM
WALMART UNIT SALES PREDICTION — A Time Series ... https://m-mazumdar13.medium.com/walmart-unit-sales-predi...

3 of 24 11/8/2021, 2:32 PM
WALMART UNIT SALES PREDICTION — A Time Series ... https://m-mazumdar13.medium.com/walmart-unit-sales-predi...

4 of 24 11/8/2021, 2:32 PM
WALMART UNIT SALES PREDICTION — A Time Series ... https://m-mazumdar13.medium.com/walmart-unit-sales-predi...

5 of 24 11/8/2021, 2:32 PM
WALMART UNIT SALES PREDICTION — A Time Series ... https://m-mazumdar13.medium.com/walmart-unit-sales-predi...

6 of 24 11/8/2021, 2:32 PM
1 sales_ca=sales_df[sales_df['state_id']=='CA'].loc[:,'d_1':].sum(axis=0)
2 sales_wi=sales_df[sales_df['state_id']=='WI'].loc[:,'d_1':].sum(axis=0)
3 sales_tx=sales_df[sales_df['state_id']=='TX'].loc[:,'d_1':].sum(axis=0)
4
5 state_sales_df=pd.DataFrame({'sales_ca':list(sales_ca),
6 'sales_wi':list(sales_wi),
7 'sales_tx':list(sales_tx),
8 'date':list(calendar_df['date']),
9 'weekday':calendar_df['weekday'],
10 'wday':calendar_df['wday'],
11 'month':calendar_df['month'],
12 'event_type_1':calendar_df['event_type_1'],
13 'event_type_2':calendar_df['event_type_2']})
14
15
16 fig=go.Figure()
17 fig.add_trace(go.Scatter(y=state_sales_df['sales_ca'],x=state_sales_df['date'
18 fig.add_trace(go.Scatter(y=state_sales_df['sales_wi'],x=state_sales_df['date'
19 fig.add_trace(go.Scatter(y=state_sales_df['sales_tx'],x=state_sales_df['date'
20 fig.update_layout(title='Aggregate sales per state',
WALMART UNIT SALES PREDICTION — A Time Series ... https://m-mazumdar13.medium.com/walmart-unit-sales-predi...

1 state_ca_trend=seasonal_decompose(state_sales_df['sales_ca'],freq=180).trend
2 state_wi_trend=seasonal_decompose(state_sales_df['sales_wi'],freq=180).trend
3 state_tx_trend=seasonal_decompose(state_sales_df['sales_tx'],freq=180).trend
4
5 fig=go.Figure()
6 fig.add_trace(go.Scatter(y=state_ca_trend,x=state_sales_df['date'],mode='lines'
7 fig.add_trace(go.Scatter(y=state_wi_trend,x=state_sales_df['date'],mode='lines'
8 fig.add_trace(go.Scatter(y=state_tx_trend,x=state_sales_df['date'],mode='lines'
9 fig.update_layout(title='Sales trend per state',
10 xaxis_title='year',
11 yaxis_title='sales')

8 of 24 11/8/2021, 2:32 PM
WALMART UNIT SALES PREDICTION — A Time Series ... https://m-mazumdar13.medium.com/walmart-unit-sales-predi...

1 from tqdm import tqdm


2 df=sales_df.iloc[:,6:]
3 x=set(np.array(df).flatten())
4 full_sales_array=np.array(df).flatten()
5 y=[]
6
7 for i in tqdm(x):
8 y.append(np.count_nonzero(df==i)/len(full_sales_array))
9
10 fig=go.Figure()
11 fig.add_trace(go.Bar(x=[np.count_nonzero(df==0)],y=['zero unit sales'],orientation
12 fig.add_trace(go.Bar(x=[np.count_nonzero(df)],y=['nonzero unit sales'],orientation
13 fig.update_layout(width=700,
14 height=300,
15 xaxis_title='Count')
16
17 fig1=px.bar(x=list(x),y=y,title='Probability density of unit sales figures')
18 fig1.update_layout(xaxis_title='unit sales figures',
19 yaxis_title='probability')
20

10 of 24 11/8/2021, 2:32 PM
WALMART UNIT SALES PREDICTION — A Time Series ... https://m-mazumdar13.medium.com/walmart-unit-sales-predi...

1 sales_states_weekly=state_sales_df.groupby(by=['weekday','wday'],
2 as_index=False)['sales_ca',
3 'sales_wi',
4 'sales_tx'].agg('mean'
5
6 fig=make_subplots(rows=1,cols=3)
7 fig.append_trace(go.Bar(x=sales_states_weekly['weekday'],y=sales_states_weekly
8 fig.append_trace(go.Bar(x=sales_states_weekly['weekday'],y=sales_states_weekly
9 fig.append_trace(go.Bar(x=sales_states_weekly['weekday'],y=sales_states_weekly
10 fig.update_layout(
11 title='Walmart weekly sales',
12 xaxis_title='weekday',
13 yaxis_title='Average sale')

11 of 24 11/8/2021, 2:32 PM
1 sales_states_monthly=state_sales_df.groupby(by=['month'],
2 as_index=False
3 )['sales_ca',
4 'sales_wi',
5 'sales_tx'].agg('mean')
6
7 sales_states_monthly['month_name']=['Jan','Feb','Mar','Apr','May','June','July'
8 fig=make_subplots(rows=1,cols=3)
9 fig.append_trace(go.Line(x=sales_states_monthly['month_name'],y=sales_states_monthly
10 fig.append_trace(go.Line(x=sales_states_monthly['month_name'],y=sales_states_monthly
11 fig.append_trace(go.Line(x=sales_states_monthly['month_name'],y=sales_states_monthly
12 fig.update_layout(
13 title='Walmart monthly sales',
14 xaxis_title='month',
WALMART UNIT SALES PREDICTION — A Time Series ... https://m-mazumdar13.medium.com/walmart-unit-sales-predi...

1 walmart_sales=sales_df.loc[:,'d_1':].sum(axis=0)
2 plot_acf(walmart_sales,lags=60,title='Autocorrelation sales')
3 plt.ylabel('correlation')
4 plt.xlabel('time lag')
5 plt.show()

14 of 24 11/8/2021, 2:32 PM
WALMART UNIT SALES PREDICTION — A Time Series ... https://m-mazumdar13.medium.com/walmart-unit-sales-predi...

15 of 24 11/8/2021, 2:32 PM
WALMART UNIT SALES PREDICTION — A Time Series ... https://m-mazumdar13.medium.com/walmart-unit-sales-predi...

1 total_sales_hobbies=sales_df[sales_df['cat_id']=='HOBBIES'].loc[:,'d_1':].sum
2 total_sales_household=sales_df[sales_df['cat_id']=='HOUSEHOLD'].loc[:,'d_1':].
3 total_sales_foods=sales_df[sales_df['cat_id']=='FOODS'].loc[:,'d_1':].sum(axis
4
5 category_sales_df=pd.DataFrame({'sales_foods':list(total_sales_foods),
6 'sales_hobbies':list(total_sales_hobbies),
7 'sales_household':list(total_sales_household),
8 'date':list(calendar_df['date']),
9 'event_type_1':calendar_df['event_type_1'],
10 'event_type_2':calendar_df['event_type_2']},
11 )
12 no_event_sales_category=category_sales_df.iloc[:,:3][category_sales_df['event_type_1'
13 cat_sales_on_events=category_sales_df.groupby(['event_type_1'],as_index=False
14 'sales_hobbies',
15 'sales_household'].
16
17 cat_sales_on_events['sales_foods_diff']=cat_sales_on_events['sales_foods']-no_event_sales_cat
18 cat_sales_on_events['sales_hobbies_diff']=cat_sales_on_events['sales_hobbies'
19 cat_sales_on_events['sales_household_diff']=cat_sales_on_events['sales_household'
20 fig=go.Figure()
21 fig.add_trace(go.Bar(x=cat_sales_on_events['sales_foods_diff'],y=cat_sales_on_events
22 fig.add_trace(go.Bar(x=cat_sales_on_events['sales_hobbies_diff'],y=cat_sales_on_events
23 fig.add_trace(go.Bar(x=cat_sales_on_events['sales_household_diff'],y=cat_sales_on_events
24 fig.update_layout(width=700,
25 height=500,

16 of 24 11/8/2021, 2:32 PM
WALMART UNIT SALES PREDICTION — A Time Series ... https://m-mazumdar13.medium.com/walmart-unit-sales-predi...

1 state_cat_sales=pd.melt(sales_df.groupby(['state_id','cat_id'],as_index=False
2 id_vars=['state_id','cat_id'],var_name='d',value_name
3
4 state_cat_sales=state_cat_sales.merge(calendar_df[['snap_CA','snap_TX','snap_WI'
5
6 snap_CA_sales=state_cat_sales[state_cat_sales['state_id']=='CA'].groupby(['cat_id'
7 snap_WI_sales=state_cat_sales[state_cat_sales['state_id']=='WI'].groupby(['cat_id'
8 snap_TX_sales=state_cat_sales[state_cat_sales['state_id']=='TX'].groupby(['cat_id'
9
10 fig,axes=plt.subplots(1,3,figsize=(12,5))
11 snap_CA_sales.pivot("cat_id", "snap_CA", "sales").plot(kind='bar',ax=axes[0],
12 snap_WI_sales.pivot("cat_id", "snap_WI", "sales").plot(kind='bar',ax=axes[1])
13 snap_TX_sales pivot("cat_id" "snap_TX" "sales").plot(kind='bar' ax=axes[2])

18 of 24 11/8/2021, 2:32 PM
WALMART UNIT SALES PREDICTION — A Time Series ... https://m-mazumdar13.medium.com/walmart-unit-sales-predi...

1 price_df_1=price_df.merge(sales_df[['item_id','cat_id','dept_id']].drop_duplicates
2 del price_df
3 price_df_final=price_df_1.groupby(['wm_yr_wk','cat_id','dept_id'],as_index=False
4 del price_df_1
5 price_df_final=price_df_final[price_df_final['wm_yr_wk']<=11613]
6
7
8 sales_df_1=sales_df.melt(id_vars=['id','item_id','dept_id','cat_id','store_id'
9 var_name='d',value_name='sales')
10 del sales_df
11 sales_df_2=sales_df_1.merge(calendar_df[['d','date','wm_yr_wk']].drop_duplicates
12 del sales_df_1
13 sales_df_final=sales_df_2.groupby(['wm_yr_wk','cat_id','dept_id'],as_index=False
14
15
16 price_df_final['sales']=sales_df_final['sales']
17
18 for dept,row in zip(price_df_final['dept_id'].unique(),range(1,8)):
19 fig=make_subplots(rows=1,cols=2)
20 sales_trend=seasonal_decompose(price_df_final[price_df_final['dept_id'
21 price_trend=seasonal_decompose(price_df_final[price_df_final['dept_id'
22
23 fig.append_trace(go.Line(x=price_df_final[price_df_final['dept_id']==
24 ,y=sales_trend,name=dept+' weekly average sales trend'),row=1
25
26 fig.append_trace(go.Line(x=price_df_final[price_df_final['dept_id']==
27 price_trend dept ' weekly average sell price trend'),

19 of 24 11/8/2021, 2:32 PM
WALMART UNIT SALES PREDICTION — A Time Series ... https://m-mazumdar13.medium.com/walmart-unit-sales-predi...

20 of 24 11/8/2021, 2:32 PM
WALMART UNIT SALES PREDICTION — A Time Series ... https://m-mazumdar13.medium.com/walmart-unit-sales-predi...

21 of 24 11/8/2021, 2:32 PM
WALMART UNIT SALES PREDICTION — A Time Series ... https://m-mazumdar13.medium.com/walmart-unit-sales-predi...

22 of 24 11/8/2021, 2:32 PM
WALMART UNIT SALES PREDICTION — A Time Series ... https://m-mazumdar13.medium.com/walmart-unit-sales-predi...

23 of 24 11/8/2021, 2:32 PM
WALMART UNIT SALES PREDICTION — A Time Series ... https://m-mazumdar13.medium.com/walmart-unit-sales-predi...

24 of 24 11/8/2021, 2:32 PM

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy