text_plain_output_1.png
from sklearn.metrics import mean_squared_error

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

import numpy as np
import pandas as pd
import os
import seaborn as sns
import plotly.express as px
import time
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error
mse = mean_squared_error
def downcast(df: pd.DataFrame) -> pd.DataFrame:
    float_cols = [c for c in df if df[c].dtype in ['float64']]
    int_cols = [c for c in df if df[c].dtype in ['int64']]
    df[float_cols] = df[float_cols].astype('float32')
    df[int_cols] = df[int_cols].astype('int16')
    return df
def lag_feature(df: pd.DataFrame, lag: int, col: str, merge_cols, fill_value=-10, suffix=''):
    temp = df[merge_cols + [col]]
    temp = temp.groupby(merge_cols).agg({f'{col}': 'mean'}).reset_index()
    new_col_name = f'{col}{suffix}_lag{lag}'
    temp.columns = merge_cols + [new_col_name]
    temp['date_block_num'] += lag
    if new_col_name not in df.columns:
        df = pd.merge(df, temp, on=merge_cols, how='left')
    temp = None
    df[new_col_name] = df[new_col_name].fillna(fill_value).astype('float32')
    return (df, new_col_name)

items = pd.read_csv('../input/data-preprocessing/items.csv')
shops = pd.read_csv('../input/data-preprocessing/shops.csv')
cats = pd.read_csv('../input/data-preprocessing/item_categories.csv')
train = pd.read_csv('../input/data-preprocessing/sales_train.csv')
test = pd.read_csv('../input/data-preprocessing/test.csv').set_index('ID')
dataframes = [train, shops, items, cats]
for d in dataframes:
    d = downcast(d)

train.sample(3)