Kaggle competition: University of Liverpool - Ion Switching
<SOS> metric: MacroFScore Kaggle data source: university-of-liverpool-ion-switching<set_options>
sns.set_style("whitegrid")
py.init_notebook_mode(connected=True)
<set_options>
MODEL_TYPE = 'lgb'
TRAINING = True
ENSEMBLE = False
GROUP_BATCH_SIZE = 4000
WINDOWS = [10, 50]
BASE_PATH = '/kaggle/input/liverpool-ion-switching'
DATA_PATH = '/kaggle/input/data-without-drift'
RFC_DATA_PATH = '/kaggle/input/ion-shifted-rfc-proba'
MODELS_PATH = '/kaggle/input/ensemble-models'
def reduce_mem_usage(df, use_float16=False):

 start_mem = df.memory_usage().sum() / 1024**2
print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))
for col in df.columns:
if is_datetime(df[col])or is_categorical_dtype(df[col]):
continue
col_type = df[col].dtype
if col_type != object:
c_min = df[col].min()
c_max = df[col].max()
if str(col_type)[:3] == 'int':
if c_min > np.iinfo(np.int8 ).min and c_max < np.iinfo(np.int8 ).max:
df[col] = df[col].astype(np.int8)
 elif c_min > np.iinfo(np.int16 ).min and c_max < np.iinfo(np.int16 ).max:
df[col] = df[col].astype(np.int16)
 elif c_min > np.iinfo(np.int32 ).min and c_max < np.iinfo(np.int32 ).max:
df[col] = df[col].astype(np.int32)
 elif c_min > np.iinfo(np.int64 ).min and c_max < np.iinfo(np.int64 ).max:
df[col] = df[col].astype(np.int64)
 else:
if use_float16 and c_min > np.finfo(np.float16 ).min and c_max < np.finfo(np.float16 ).max:
df[col] = df[col].astype(np.float16)
 elif c_min > np.finfo(np.float32 ).min and c_max < np.finfo(np.float32 ).max:
df[col] = df[col].astype(np.float32)
 else:
df[col] = df[col].astype(np.float64)
 else:
df[col] = df[col].astype('category')
 end_mem = df.memory_usage().sum() / 1024**2
print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))
print('Decreased by {:.1f}%'.format(100 *(start_mem - end_mem)/ start_mem))
return df<load_from_disk>
df = pd.read_csv(f"{DATA_PATH}/train_clean.csv")
 df = df.dropna().reset_index(drop=True)
 df["kfold"] = -1
kf = model_selection.StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
 for fold,(trn_, val_)in enumerate(kf.split(X=df, y=df.open_channels.values)) :
print(len(trn_), len(val_))
df.loc[val_, 'kfold'] = fold
df.to_csv("train_folds.csv", index=False )
%%time
root = Path('.. /input/ashrae-feather-format-for-fast-loading')
 train_df = pd.read_feather(root/'train.feather')
 weather_train_df = pd.read_feather(root/'weather_train.feather')
 building_meta_df = pd.read_feather(root/'building_metadata.feather' )<feature_engineering>
%%time
def create_rolling_features(df):
for window in WINDOWS:
df["rolling_mean_" + str(window)] = df['signal'].rolling(window=window ).mean()
df["rolling_std_" + str(window)] = df['signal'].rolling(window=window ).std()
df["rolling_var_" + str(window)] = df['signal'].rolling(window=window ).var()
df["rolling_min_" + str(window)] = df['signal'].rolling(window=window ).min()
df["rolling_max_" + str(window)] = df['signal'].rolling(window=window ).max()
df["rolling_min_max_ratio_" + str(window)] = df["rolling_min_" + str(window)] / df["rolling_max_" + str(window)]
df["rolling_min_max_diff_" + str(window)] = df["rolling_max_" + str(window)] - df["rolling_min_" + str(window)]
df = df.replace([np.inf, -np.inf], np.nan)
 df.fillna(0, inplace=True)
 return df
def create_features(df, batch_size):
df['group'] = df.groupby(df.index//batch_size, sort=False)['signal'].agg(['ngroup'] ).values
df['group'] = df['group'].astype(np.uint16)
 for window in WINDOWS:
df['signal_shift_pos_' + str(window)] = df.groupby('group')['signal'].shift(window ).fillna(0)
 df['signal_shift_neg_' + str(window)] = df.groupby('group')['signal'].shift(-1 * window ).fillna(0)
 df['signal_2'] = df['signal'] ** 2
return df
train_df['date'] = train_df['timestamp'].dt.date
train_df['meter_reading_log1p'] = np.log1p(train_df['meter_reading'] )<filter>
train = pd.read_csv(f'train_folds.csv', dtype={'time': np.float32, 'signal': np.float32, 'open_channels':np.int32})
 test = pd.read_csv(f'{DATA_PATH}/test_clean.csv', dtype={'time': np.float32, 'signal': np.float32})
 sub = pd.read_csv(f'{BASE_PATH}/sample_submission.csv', dtype={'time': np.float32})
 y_train_proba = np.load(f"{RFC_DATA_PATH}/Y_train_proba.npy")
 y_test_proba = np.load(f"{RFC_DATA_PATH}/Y_test_proba.npy")
 for i in range(11):
train[f"proba_{i}"] = y_train_proba[:, i]
test[f"proba_{i}"] = y_test_proba[:, i]
train = create_rolling_features(train)
 test = create_rolling_features(test)
 train_mean = train.signal.mean()
train_std = train.signal.std()
train['signal'] =(train.signal - train_mean)/ train_std
test['signal'] =(test.signal - train_mean)/ train_std
print('Shape of train is ',train.shape)
 print('Shape of test is ',test.shape )
building_meta_df[building_meta_df.site_id == 0]<filter>
batch_size = GROUP_BATCH_SIZE
train = create_features(train, batch_size)
 test = create_features(test, batch_size)
 cols_to_remove = ['time','signal','batch','batch_index','batch_slices','batch_slices2', 'group']
cols = [c for c in train.columns if c not in cols_to_remove]
cols_test = [c for c in test.columns if c not in cols_to_remove]
X = train[cols]
y = train['open_channels']
X_test = test[cols_test]
train_df = train_df.query('not(building_id <= 104 & meter == 0 & timestamp <= "2016-05-20")' )<define_variables>
del train
del test
gc.collect()
debug = False<feature_engineering>
def f1_score_calc(y_true, y_pred):
return f1_score(y_true, y_pred, average="macro")
 def lgb_Metric(preds, dtrain):
labels = dtrain.get_label()
preds = np.round(np.clip(preds, 0, 10)).astype(int)
 score = f1_score(labels, preds, average="macro")
 return('KaggleMetric', score, True )
def preprocess(df):
df["hour"] = df["timestamp"].dt.hour
df["weekend"] = df["timestamp"].dt.weekday
df["month"] = df["timestamp"].dt.month
df["dayofweek"] = df["timestamp"].dt.dayofweek 
<categorify>
def train_model(X, X_test, params, model_type='lgb', eval_metric='f1score',
columns=None, plot_feature_importance=False, model=None,
verbose=50, early_stopping_rounds=200, n_estimators=2000):
metrics_dict = {'f1score': {'lgb_metric_name': lgb_Metric,}}
results = []
oof = np.zeros(len(X))
prediction = np.zeros(( len(X_test)))
 feature_importance = pd.DataFrame()
if True:
for fold in range(0,5):
result_dict = {}
X_train = X[X.kfold != fold].reset_index(drop=True)
 X_valid = X[X.kfold == fold].reset_index(drop=True)
 y_train = X_train.open_channels
y_valid = X_valid.open_channels
X_train = X_train.drop(['kfold', 'open_channels'], axis=1)
 X_valid = X_valid.drop(['kfold', 'open_channels'], axis=1)
 if model_type == 'lgb':
model = lgb.train(params, lgb.Dataset(X_train, y_train),
n_estimators, lgb.Dataset(X_valid, y_valid),
verbose_eval=verbose, early_stopping_rounds=early_stopping_rounds, feval=lgb_Metric)
 preds = model.predict(X_valid, num_iteration=model.best_iteration)
 y_pred_valid = np.round(np.clip(preds, 0, 10)).astype(int)
 y_pred = model.predict(X_test, num_iteration=model.best_iteration)
 if model_type == 'xgb':
train_set = xgb.DMatrix(X_train, y_train)
 val_set = xgb.DMatrix(X_valid, y_valid)
 model = xgb.train(params, train_set, num_boost_round=2222, evals=[(train_set, 'train'),(val_set, 'val')],
verbose_eval=verbose, early_stopping_rounds=early_stopping_rounds)
 preds = model.predict(xgb.DMatrix(X_valid))
y_pred_valid = np.round(np.clip(preds, 0, 10)).astype(int)
 y_pred = model.predict(xgb.DMatrix(X_test))
print(f'FINAL score fold {fold}: {f1_score_calc(y_valid, y_pred_valid)}')
 print('*'*100)
 result_dict['pred_valid'] = preds
result_dict['pred_test'] = y_pred
result_dict['model'] = model
results.append(result_dict)
 return results
preprocess(train_df )<categorify>
if TRAINING and MODEL_TYPE == 'lgb':
params = {'learning_rate': 0.1, 'max_depth': 7, 'num_leaves':2**7+1, 'metric': 'rmse', 'random_state': 7, 'n_jobs':-1}
result_dict_lgb = train_model(X=X, X_test=X_test, params=params, model_type=MODEL_TYPE, eval_metric='f1score', plot_feature_importance=False,
verbose=50, early_stopping_rounds=150, n_estimators=3000 )
df_group = train_df.groupby('building_id')['meter_reading_log1p']
building_mean = df_group.mean().astype(np.float16)
 building_median = df_group.median().astype(np.float16)
 building_min = df_group.min().astype(np.float16)
 building_max = df_group.max().astype(np.float16)
 building_std = df_group.std().astype(np.float16)
 train_df['building_mean'] = train_df['building_id'].map(building_mean)
 train_df['building_median'] = train_df['building_id'].map(building_median)
 train_df['building_min'] = train_df['building_id'].map(building_min)
 train_df['building_max'] = train_df['building_id'].map(building_max)
 train_df['building_std'] = train_df['building_id'].map(building_std )<count_missing_values>
if TRAINING and MODEL_TYPE == 'xgb':
params_xgb = {'colsample_bytree': 0.375,'learning_rate': 0.1,'max_depth': 10, 'subsample': 1, 'objective':'reg:squarederror',
'eval_metric':'rmse'}
result_dict_xgb = train_model(X=X, X_test=X_test, params=params_xgb, model_type=MODEL_TYPE, eval_metric='f1score', plot_feature_importance=False,
verbose=50, early_stopping_rounds=250 )
weather_train_df.isna().sum()<groupby>
def get_prediction(test, model, model_type):
if model_type == 'xgb':
y_pred = model.predict(xgb.DMatrix(test))
else:
y_pred = model.predict(test, num_iteration=model.best_iteration)
 y_pred = np.round(np.clip(y_pred, 0, 10)).astype(int)
 return y_pred
weather_train_df.groupby('site_id' ).apply(lambda group: group.isna().sum() )<groupby>
if TRAINING:
sub = pd.read_csv(f'{BASE_PATH}/sample_submission.csv', dtype={'time': np.float32})
 if MODEL_TYPE == 'lgb':
preds = []
for fold in range(0,5):
preds.append(result_dict_lgb[fold][f'pred_test'])
 preds = np.average(preds, axis=0)
 y_pred = np.round(np.clip(preds, 0, 10)).astype(int)
 sub['open_channels'] = np.array(np.round(y_pred,0), np.int)
 elif MODEL_TYPE == 'xgb':
preds = []
for fold in range(0, 5):
preds.append(result_dict_xgb[fold][f'pred_test'])
 preds = np.average(preds, axis=0)
 y_pred = np.round(np.clip(preds, 0, 10)).astype(int)
 sub['open_channels'] = np.array(np.round(y_pred,0), np.int)
 sub.to_csv('submission.csv', index=False, float_format='%.4f')
 print(sub.head(10))
else:
if ENSEMBLE:
model_lgb = joblib.load(MODELS_PATH + 'lgb_0.sav')
 model_xgb = joblib.load(MODELS_PATH + 'xgb_0.sav')
 y_pred_lgb = get_prediction(X_test, model_lgb, 'lgb')
 y_pred_xgb = get_prediction(X_test, model_xgb, 'xgb')
 y_pred = 0.50 * y_pred_lgb + 0.50 * y_pred_xgb
else:
if MODEL_TYPE == 'lgb':
model_lgb = joblib.load(MODELS_PATH + 'lgb_0.sav')
 y_pred = get_prediction(X_test, model_lgb, MODEL_TYPE)
 elif MODEL_TYPE == 'xgb':
model_xgb = joblib.load(MODELS_PATH + 'xgb_1.sav')
 y_pred = get_prediction(X_test, model_xgb, MODEL_TYPE)
 sub = pd.read_csv(f'{BASE_PATH}/sample_submission.csv', dtype={'time': np.float32})
 sub['open_channels'] = np.array(np.round(y_pred,0), np.int)
 sub.to_csv('submission.csv', index=False, float_format='%.4f')
 print(sub.head(10))