application_vnd.jupyter.stderr_output_1.png
import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

def read_data(path: str, files: list):
    dataframes = []
    for file in files:
        dataframes.append(pd.read_csv(path + file))
    return dataframes
path = '../input/competitive-data-science-predict-future-sales/'
files = ['sales_train.csv', 'items.csv', 'shops.csv', 'item_categories.csv', 'test.csv']
sales_train, items, shops, item_categories, test = read_data(path, files)

def remove_outliers(df: pd.DataFrame, max_price: int, max_cnt: int):
    df = df[df['item_price'] > 0]
    df = df[df['item_price'] < max_price]
    df = df[df['item_cnt_day'] > 0]
    df = df[df['item_cnt_day'] < max_cnt]
    return df
sales_train = remove_outliers(sales_train, 50000, 1000)

def remove_shop_duplicates(df_train: pd.DataFrame, df_test: pd.DataFrame, shop_dups: dict):
    for shop1, shop2 in shop_dups.items():
        df_train.loc[df_train['shop_id'] == shop1, 'shop_id'] = shop2
        df_test.loc[df_test['shop_id'] == shop1, 'shop_id'] = shop2
shop_dups = {0: 57, 1: 58, 10: 11, 39: 40}
remove_shop_duplicates(sales_train, test, shop_dups)

shps = list(sales_train.shop_id.unique())
shps.sort()

fig = plt.figure(figsize=(20, 50))
for i in range(len(shps)):
    plt.subplot(12, 5, i + 1)
    plt.plot(sales_train[sales_train.shop_id == shps[i]].groupby(['date_block_num'])['item_cnt_day'].sum())
    plt.title('Shop {} sales'.format(shps[i]))

shops_cluster = []
for shop in shps:
    temp = [shop]
    for month in range(34):
        temp += [sales_train[(sales_train.shop_id == shop) & (sales_train.date_block_num == month)]['item_cnt_day'].sum()]
    shops_cluster.append(np.array(temp))
shops_cluster = pd.DataFrame(np.vstack(shops_cluster), columns=['shop'] + ['{}'.format(i) for i in range(34)])
shops_cluster