text_html_output_2.png text_html_output_1.png text_plain_output_2.png text_plain_output_1.png
import holidays

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import warnings

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import mean_squared_error
from sklearn.linear_model import LinearRegression, Ridge
from sklearn.model_selection import train_test_split, GroupKFold
from sklearn.preprocessing import OrdinalEncoder, OneHotEncoder, MinMaxScaler, StandardScaler
from sklearn.pipeline import make_pipeline
import dateutil.easter as easter
import holidays
from tqdm import tqdm
import warnings
warnings.filterwarnings('ignore')
pd.set_option('display.max_columns', 100)
pd.set_option('display.max_rows', 6)
pd.set_option('display.float_format', '{:.2f}'.format)
plt.style.use('seaborn-whitegrid')
plt.rc('figure', autolayout=True, titlesize=18, titleweight='bold')
plt.rc('axes', labelweight='bold', labelsize='large', titlesize=8, titlepad=2)

train_df = pd.read_csv('../input/tabular-playground-series-sep-2022/train.csv', parse_dates=['date'], index_col='row_id')
test_df = pd.read_csv('../input/tabular-playground-series-sep-2022/test.csv', parse_dates=['date'], index_col='row_id')
sub_df = pd.read_csv('../input/tabular-playground-series-sep-2022/sample_submission.csv')

def feature_engineer(df):
    new_df = df.copy()
    new_df['year'] = df['date'].dt.year - 2016
    new_df['quarter'] = df['date'].dt.quarter
    new_df['month'] = df['date'].dt.month
    new_df['month_sin'] = np.sin(new_df['month'] * (2 * np.pi / 12))
    new_df['day'] = df['date'].dt.day
    new_df['day_of_week'] = df['date'].dt.dayofweek
    new_df['day_of_year'] = df['date'].dt.dayofyear
    new_df['week'] = df['date'].dt.week
    new_df['is_weekend'] = new_df.apply(lambda x: 1 if x['day_of_week'] >= 5 else 0, axis=1)
    new_df = new_df.drop('date', axis=1)
    important_dates = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 124, 125, 126, 127, 140, 141, 167, 168, 169, 170, 171, 173, 174, 175, 176, 177, 178, 179, 180, 181, 203, 230, 231, 232, 233, 234, 282, 289, 290, 307, 308, 309, 310, 311, 312, 313, 317, 318, 319, 320, 360, 361, 362, 363, 364, 365]
    new_df['important_dates'] = new_df['day_of_year'].apply(lambda x: x if x in important_dates else 0)
    new_df = new_df.drop(['day_of_year', 'month'], axis=1)
    return new_df
def get_holidays(df, country_name):
    years_list = [2017, 2018, 2019, 2020, 2021]
    country_map = {'Belgium': 'BE', 'France': 'FR', 'Germany': 'DE', 'Italy': 'IT', 'Poland': 'PL', 'Spain': 'ES'}
    holiday_ = holidays.CountryHoliday(country_map[country_name], years=years_list)
    df['holiday_name'] = df['date'].map(holiday_)
    df['is_holiday'] = np.where(df['holiday_name'].notnull(), 1, 0)
    df = df.drop('holiday_name', axis=1)
    return df

sub_df['num_sold'] = np.array(test_df['num_sold'])
sub_df.to_csv('submission.csv', index=False)
sub_df