Kaggle competition: Анализ потребительской корзины
<SOS> metric: multiclassloss Kaggle data source: dmia-sport-2019-fall-competition-1<import_modules>
import pandas as pd
from sklearn.metrics import log_loss
import gc
import numpy as np
import pandas as pd
import lightgbm as lgb
from datetime import datetime, timedelta<define_variables>
df_train = pd.read_csv('.. /input/dmia-sport-2019-fall-competition-1/train.csv' )
calendarDTypes = {"event_name_1": "category",
"event_name_2": "category",
"event_type_1": "category",
"event_type_2": "category",
"weekday": "category",
'wm_yr_wk': 'int16',
"wday": "int16",
"month": "int16",
"year": "int16",
"snap_CA": "float32",
'snap_TX': 'float32',
'snap_WI': 'float32' }
calendar = pd.read_csv(".. /input/m5-forecasting-accuracy/calendar.csv",
dtype = calendarDTypes)
 calendar["date"] = pd.to_datetime(calendar["date"])
 for col, colDType in calendarDTypes.items() :
if colDType == "category":
calendar[col] = calendar[col].cat.codes.astype("int16")
 calendar[col] -= calendar[col].min()
calendar.head()<load_from_csv>
checks_num = df_train.check_id.nunique()
checks_num
priceDTypes = {"store_id": "category",
"item_id": "category",
"wm_yr_wk": "int16",
"sell_price":"float32"}
prices = pd.read_csv(".. /input/m5-forecasting-accuracy/sell_prices.csv",
dtype = priceDTypes)
 for col, colDType in priceDTypes.items() :
if colDType == "category":
prices[col] = prices[col].cat.codes.astype("int16")
 prices[col] -= prices[col].min()
prices.head()<load_from_csv>
df_train = df_train.drop_duplicates(subset=['check_id', 'target'] )
firstDay = 250
lastDay = 1913
numCols = [f"d_{day}" for day in range(firstDay, lastDay+1)]
catCols = ['id', 'item_id', 'dept_id','store_id', 'cat_id', 'state_id']
dtype = {numCol: "float32" for numCol in numCols}
dtype.update({catCol: "category" for catCol in catCols if catCol != "id"})
 ds = pd.read_csv(".. /input/m5-forecasting-accuracy/sales_train_validation.csv",
usecols = catCols + numCols, dtype = dtype)
 for col in catCols:
if col != "id":
ds[col] = ds[col].cat.codes.astype("int16")
 ds[col] -= ds[col].min()
ds = pd.melt(ds,
id_vars = catCols,
value_vars = [col for col in ds.columns if col.startswith("d_")],
var_name = "d",
value_name = "sales")
 ds = ds.merge(calendar, on = "d", copy = False)
 ds = ds.merge(prices, on = ["store_id", "item_id", "wm_yr_wk"], copy = False)
 ds.head()<data_type_conversions>
target_probs = df_train.target.value_counts(normalize=True ).sort_index()
target_probs
dateFeatures = {"wday": "weekday",
"week": "weekofyear",
"month": "month",
"quarter": "quarter",
"year": "year",
"mday": "day"}
for featName, featFunc in dateFeatures.items() :
if featName in ds.columns:
ds[featName] = ds[featName].astype("int16")
 else:
ds[featName] = getattr(ds["date"].dt, featFunc ).astype("int16" )<prepare_x_and_y>
log_loss(df_train.target, [df_train.target.value_counts(normalize=True)]*df_train.shape[0] )
ds.dropna(inplace = True)
 unusedCols = ["id", "date", "sales","d", "wm_yr_wk", "weekday"]
trainCols = ds.columns[~ds.columns.isin(unusedCols)]
X_train = ds[trainCols]
y_train = ds["sales"]<create_dataframe>
df_test = pd.read_csv('.. /input/dmia-sport-2019-fall-competition-1/test.csv' )
np.random.seed(777)
 catFeats = ['item_id', 'dept_id','store_id', 'cat_id', 'state_id'] + \
["event_name_1", "event_name_2", "event_type_1", "event_type_2"]
validInds = np.random.choice(X_train.index.values, 2_000_000, replace = False)
 trainInds = np.setdiff1d(X_train.index.values, validInds)
 trainData = lgb.Dataset(X_train.loc[trainInds], label = y_train.loc[trainInds],
categorical_feature = catFeats, free_raw_data = False)
 validData = lgb.Dataset(X_train.loc[validInds], label = y_train.loc[validInds],
categorical_feature = catFeats, free_raw_data = False )<drop_column>
columns_names = ['target_{}'.format(i)for i in range(25)]
del ds, X_train, y_train, validInds, trainInds ; gc.collect()<init_hyperparams>
df_result = pd.DataFrame([target_probs.tolist() ]*len(ids), columns=columns_names)
 df_result.insert(0, 'check_id', ids)
 df_result.head(10 )
<train_model><EOS> 
df_result.to_csv('const_submission.csv', index=False )