image_output_1.png
from scipy.cluster.hierarchy import linkage, fcluster

from tqdm import tqdm

import numpy as np # linear algebra

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

grid_df = pd.read_pickle('/kaggle/input/m5-simple-fe/grid_part_1.pkl')

grid_df = grid_df[['id', 'd', 'sales']].pivot(index='id', columns='d').reset_index()
ids = grid_df['id']

grid_df = grid_df['sales'].iloc[:, :1913]
grid_df = pd.DataFrame(np.where(grid_df.isnull(), np.nan, np.where(grid_df > 0, 1, -1)))
grid_df.columns = [f'd_{i}' for i in range(1, 1914)]

d1_peak = grid_df.notnull().sum(axis=1)
cluster = d1_peak.copy()

grid_df['cluster'] = cluster
grid_df = grid_df.fillna(0)

for clt in tqdm(range(4, 5)):
    df_name = f'cluster_{clt}_df'
    cluster_df = grid_df[grid_df['cluster'] == clt]
    cluster_array = cluster_df.values
    dist_matrix = np.dot(cluster_array, cluster_array.T)
    Z = linkage(dist_matrix, method='single')
    cluster_num = fcluster(Z, t=4, criterion='maxclust')
    cluster_df['cluster'] = cluster_df['cluster'] * cluster_num
    globals()[df_name] = cluster_df