text_plain_output_1.png
import matplotlib as plt

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

dates = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/calendar.csv')
data = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/sales_train_validation.csv')
sale_data = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/sell_prices.csv')
submission = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/sample_submission.csv')

last_date = 1913
original_features = ['id', 'item_id', 'dept_id', 'cat_id', 'store_id', 'state_id']
total_sales = data.mean()
x_data = np.array([int(x[2:]) for x in total_sales.index])
y_data = np.array(total_sales.array)
m, b = np.polyfit(x_data, y_data, 1)

state_groups = data.groupby('state_id')
state_data = state_groups.mean()
x_data = range(1, last_date + 1)
for i, g in enumerate(state_groups.groups.keys()):
    y_data = [state_data['d_' + str(x)].iloc[i] for x in x_data]
    m, b = np.polyfit(x_data, y_data, 1)

category_groups = data.groupby('cat_id')
category_data = category_groups.mean()

x_data = range(1, last_date+1)

for i, c in enumerate(category_groups.groups.keys()):
    y_data = [category_data['d_' + str(x)].iloc[i] for x in x_data]
    plt.pyplot.scatter(x_data, y_data, s=5, alpha=0.3)
    m, b = np.polyfit(x_data, y_data, 1)
    print("Category ", c, "Gradient:", m, "Intercept:", b)
    plt.pyplot.plot([0, last_date], [b, m*last_date + b], linewidth=4, label=c)
plt.pyplot.legend(loc="upper left")

cs_group = data.groupby(['state_id','cat_id']).mean().reset_index()
colours = ['blue', 'darkorange', 'green']
for state in state_groups.groups.keys():
    plt.pyplot.figure(figsize=(20, 12))
    for i, c in enumerate(category_groups.groups.keys()):
        plt.pyplot.subplot(int('33' + str(i+1)))
        y_data = cs_group[(cs_group.state_id == state) & (cs_group.cat_id == c)].iloc[0].tail(last_date).to_list()
        plt.pyplot.scatter(x_data, y_data, s=5, alpha=0.3, c=colours[i])
        
        m, b = np.polyfit(x_data, y_data, 1)
        print("State: " + state, "Category: " + c, "Gradient:", m, "Intercept:", b)
        plt.pyplot.plot([0, last_date], [b, m*last_date + b], linewidth=4, label=c, c=colours[i])
        plt.pyplot.legend(loc="upper left")
        plt.pyplot.title(state)

ss_group = data.groupby(['state_id', 'store_id']).mean().reset_index()
for state in state_groups.groups.keys():
    store_group = ss_group[ss_group.state_id == state].groupby('store_id')
    plt.pyplot.figure(figsize=(16, 4))
    for i, s in enumerate(store_group.groups.keys()):
        y_data = ss_group[ss_group.store_id == s].iloc[0].tail(last_date).to_list()
        plt.pyplot.scatter(x_data, y_data, s=6, alpha=0.4)
        m, b = np.polyfit(x_data, y_data, 1)
        print('State: ' + state, 'Store: ' + c, 'Gradient:', m, 'Intercept:', b)
        plt.pyplot.plot([0, last_date], [b, m * last_date + b], linewidth=4, label=s)
        plt.pyplot.legend(loc='upper left')
        plt.pyplot.title(state)
plt.pyplot.figure(figsize=(16, 4))
plt.pyplot.subplot(121)
y_data = ss_group[ss_group.store_id == 'WI_1'].iloc[0].tail(last_date).to_list()
plt.pyplot.scatter(x_data, y_data, s=6, alpha=0.4, label='WI_1')
plt.pyplot.legend(loc='upper left')
plt.pyplot.title('WI_1 Anomaly at day 700')
plt.pyplot.subplot(122)
y_data = ss_group[ss_group.store_id == 'WI_2'].iloc[0].tail(last_date).to_list()
plt.pyplot.scatter(x_data, y_data, s=6, alpha=0.4, label='WI_2', c='darkorange')
plt.pyplot.legend(loc='upper left')
plt.pyplot.title('WI_2 Anomaly at day 500')