image_output_1.png
from scipy.stats import pearsonr

import matplotlib.pyplot as plt

import numpy as np # linear algebra

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

import seaborn as sns

train = pd.read_csv('../input/tabular-playground-series-sep-2022/train.csv', index_col='row_id')
test = pd.read_csv('../input/tabular-playground-series-sep-2022/test.csv', index_col='row_id')

train['date'] = pd.to_datetime(train['date'])
test['date'] = pd.to_datetime(test['date'])

train.dtypes

train.nunique()

plt.figure(figsize=(16,5))
ax = sns.barplot(data=train, x='product', y='num_sold', hue='country')
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.title('Product distribution grouped by country')
plt.show()

store_corr = pearsonr(train.loc[train['store'] == 'KaggleMart', 'num_sold'], train.loc[train['store'] == 'KaggleRama', 'num_sold'])[0]
mult_factor = train.loc[train['store'] == 'KaggleMart', 'num_sold'].sum() / train.loc[train['store'] == 'KaggleRama', 'num_sold'].sum()

plt.figure(figsize=(12,5))
ax = sns.lineplot(data=train.groupby(['product','date']).sum()/train.groupby(['date']).sum(), x='date', y='num_sold', hue='product')
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.title('Ratio of sales by product')
plt.ylabel('Ratio')
plt.show()

def get_fourier_features(df):
    dayofbiyear = df['date'].dt.dayofyear + 365 * (1 - df['date'].dt.year % 2)
    for k in [1, 2, 4]:
        df[f'sin{k}'] = np.sin(2 * np.pi * k * dayofbiyear / (2 * 365))
        df[f'cos{k}'] = np.cos(2 * np.pi * k * dayofbiyear / (2 * 365))
        for product in df['product'].unique():
            df[f'sin_{k}_{product}'] = df[f'sin{k}'] * (df['product'] == product)
            df[f'cos_{k}_{product}'] = df[f'cos{k}'] * (df['product'] == product)
        df = df.drop([f'sin{k}', f'cos{k}'], axis=1)
    return df

def get_GDP_corr(df):
    feat_corr = []
    df['year'] = df['date'].dt.year
    GDP = pd.read_csv('../input/gdp-of-european-countries/GDP_table.csv', index_col='year')
    GDP_PC = pd.read_csv('../input/gdp-of-european-countries/GDP_per_capita_table.csv', index_col='year')
    GDP_dict = GDP.unstack().to_dict()
    GDP_PC_dict = GDP_PC.unstack().to_dict()
    df['GDP'] = df.set_index(['country', 'year']).index.map(GDP_dict.get)
    df['GDP_PC'] = df.set_index(['country', 'year']).index.map(GDP_PC_dict.get)
    for country in df['country'].unique():
        subset = df[(df['country'] == country) & (df['year'] <= 2019)].groupby(['year']).agg(S=('S', 'sum'), GDP=('GDP', 'mean'), GDP_PC=('GDP_PC', 'mean'))
        r1 = pearsonr(subset['S'], subset['GDP'])[0]
        r2 = pearsonr(subset['S'], subset['GDP_PC'])[0]
        feat_corr.append([f'{country}', r1, r2])
    return pd.DataFrame(feat_corr, columns=['Country', 'GDP_corr', 'GDP_PC_corr'])
corr_df = get_GDP_corr(train)
corr_df