text_plain_output_1.png
import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

types = {'Semana': np.uint8, 'Cliente_ID': np.uint32, 'Producto_ID': np.uint16, 'Demanda_uni_equil': np.uint32}
df = pd.read_csv('../input/train.csv', usecols=types.keys(), dtype=types)

demand_sorted = df.Demanda_uni_equil.sort_values(ascending=True)

def demandVar(c_id, df, percent=False):
    """ Get the amounts by which product demand changed
    week-to-week for a given set of client ids. Returned
    object is a pandas dataframe with NaN entries where
    the product was not ordered the week before or after.
    """
    for week in range(4, 10):
        try:
            vals_a = df[(df.Cliente_ID.values == c_id) & (df.Semana.values == week - 1)].Demanda_uni_equil.values
            prod_a = df[(df.Cliente_ID.values == c_id) & (df.Semana.values == week - 1)].Producto_ID.values
            dict_a = {p: v for p, v in zip(prod_a, vals_a)}
            vals_b = df[(df.Cliente_ID.values == c_id) & (df.Semana.values == week)].Demanda_uni_equil.values
            prod_b = df[(df.Cliente_ID.values == c_id) & (df.Semana.values == week)].Producto_ID.values
            dict_b = {p: v for p, v in zip(prod_b, vals_b)}
            dict_merge = {}
            for key in np.unique(np.concatenate((prod_a, prod_b))):
                try:
                    if percent:
                        try:
                            dict_merge[key] = (dict_b[key].astype(int) - dict_a[key].astype(int)) / (dict_b[key].astype(int) + dict_a[key].astype(int))
                        except:
                            dict_merge[key] = 0.0
                    else:
                        dict_merge[key] = dict_b[key].astype(int) - dict_a[key].astype(int)
                except:
                    dict_merge[key] = np.nan
            if week == 4:
                df_return = pd.DataFrame({'week_3-4': list(dict_merge.values())}, index=list(dict_merge.keys()))
            else:
                df_new = pd.DataFrame({'week_' + str(week - 1) + '-' + str(week): list(dict_merge.values())}, index=list(dict_merge.keys()))
                df_return = pd.merge(df_return, df_new, how='outer', left_index=True, right_index=True)
        except:
    return df_return

c_ids = [df.Cliente_ID.values[int(i)] for i in np.linspace(0, len(df) - 1, 5)]
c_ids

var = demandVar(c_id=c_ids[0], df=df)
var

var = demandVar(c_id=c_ids[1], df=df, percent=True)
var

def get_vars(c_ids, percent=False):
    """ Return a list of variations in the demand
    week-to-week on individual products for a set of clients. """
    return_list = [[] for _ in range(len(c_ids))]
    for i, c_id in enumerate(c_ids):
        var = demandVar(c_id, df, percent)
        for col in var.columns:
            return_list[i] += list(var[col].dropna())
    return return_list

var_list = get_vars(c_ids)

colors = ['blue', 'red', 'green', 'turquoise', 'brown']
fig, ax = plt.subplots(1, 2)
plt.suptitle('Change in demand on individual poducts for 5 clients')
for i in range(5):
    ax[0].hist(var_list[i], color=colors[i], normed=True, alpha=0.5, bins=20)
ax[0].set_ylim(0, 0.2)
ax[0].set_xlabel('Change in demand')
ax[0].set_ylabel('Normed frequency')
for i in range(5):
    ax[1].hist(var_list[i], color=colors[i], normed=True, alpha=0.5, bins=20)
ax[1].set_ylim(0, 1)