text_html_output_2.png text_plain_output_3.png text_html_output_1.png text_plain_output_2.png text_plain_output_1.png text_html_output_3.png
_, ax = plt.subplots(12, 4, figsize=(14, 50))
test_df['num_sold'] = 0
oh_cols = ['day', 'day_of_week', 'week', 'quarter', 'important_dates']
encoder = OneHotEncoder(sparse=False)
for country, i in zip(train_df['country'].unique(), range(6)):
    for store, k in zip(train_df['store'].unique(), range(2)):
        for product, j in zip(train_df['product'].unique(), range(4)):
            temp_df = None
            temp_roll = None
            temp_df = train_df.loc[(train_df['country'] == country) & (train_df['store'] == store) & (train_df['product'] == product), ['date', 'num_sold']]
            temp_test_df = test_df.loc[(test_df['country'] == country) & (test_df['store'] == store) & (test_df['product'] == product), ['date', 'num_sold']]
            temp_df_all = get_holidays(temp_df, country)
            temp_test_df_all = get_holidays(temp_test_df, country)
            temp_df_all = feature_engineer(temp_df_all)
            temp_test_df_all = feature_engineer(temp_test_df_all)
            X = temp_df_all.drop(['num_sold'], axis=1)
            X_temp = None
            X_temp = pd.concat([X[[col for col in X.columns if col not in oh_cols]], pd.DataFrame(encoder.fit_transform(X[oh_cols]), index=X.index)], axis=1)
            X = X_temp
            y = temp_df['num_sold']
            X_fore = temp_test_df_all.drop(['num_sold'], axis=1)
            X_fore_temp = None
            X_fore_temp = pd.concat([X_fore[[col for col in X_fore.columns if col not in oh_cols]], pd.DataFrame(encoder.transform(X_fore[oh_cols]), index=X_fore.index)], axis=1)
            X_fore = X_fore_temp
            model = Ridge(tol=0.01, max_iter=1000000, random_state=0)
            model.fit(X, y)
            y_pred = pd.Series(model.predict(X), index=X.index)
            y_fore = pd.Series(model.predict(X_fore), index=X_fore.index)
            y.plot(ax=ax[i * 2 + k, j], color='0.95', style='.-', markerfacecolor='0.25', markersize=10, title=f'{country} {store} {product}', label='Actual Values', legend=True, xlabel='')
            y_pred.plot(ax=ax[i * 2 + k, j], linewidth=0.4, label='Trend fitted', legend=True, xlabel='')
            y_fore.plot(ax=ax[i * 2 + k, j], linewidth=0.3, label='Trend Forecasted', color='C3', legend=True, xlabel='')
            test_df.loc[(test_df['country'] == country) & (test_df['store'] == store) & (test_df['product'] == product), ['num_sold']] = y_fore