Kaggle competition: New York City Taxi Fare Prediction
<SOS> metric: RMSE Kaggle data source: new-york-city-taxi-fare-prediction<load_from_csv>
pd.set_option('display.float_format', lambda x: '%.3f' % x)
 RSEED = 100
%matplotlib inline
plt.style.use('fivethirtyeight')
 plt.rcParams['font.size'] = 18
palette = sns.color_palette('Paired', 10 )
data = pd.read_csv('.. /input/data-without-drift/train_clean.csv')
 data.iloc[478587:478588, [1]] = -2
data.iloc[478609:478610, [1]] = -2
data_ = data[3500000:3642922].append(data[3822754:4000000])
 data = data[:3500000].append(data[4000000:] ).reset_index().append(data_, ignore_index=True)
 data.head()
data[["signal", "open_channels"]].plot(figsize=(19,5), alpha=0.7 )<string_transform>
data = pd.read_csv('.. /input/train.csv', nrows = 5_000_000,
parse_dates = ['pickup_datetime'] ).drop(columns = 'key')
 data = data.dropna()
data.head()
df = divide_and_add_features(data['signal'])
 df.head()<load_from_csv>
print(f"There are {len(data[data['fare_amount'] < 0])} negative fares.")
 print(f"There are {len(data[data['fare_amount'] == 0])} $0 fares.")
 print(f"There are {len(data[data['fare_amount'] > 100])} fares greater than $100." )
m_data = pd.read_csv('.. /input/viterbi/Viterbi_train.csv', names=["gb", "signal"])
 m_data_ = m_data[3500000:3642922].append(m_data[3822754:4000000])
 m_data = m_data[:3500000].append(m_data[4000000:] ).reset_index().append(m_data_, ignore_index=True)
 m_data.head()
m_data[["signal"]].plot(figsize=(19,5), alpha=0.7)
 df["m_signal"] = m_data["signal"][:-1].values/15
df = reduce_mem_usage(df)
 df_columns = df.columns<choose_model_class>
data = data[data['fare_amount'].between(left = 2.5, right = 100)]
def create_mpl(shape):

 X_input = L.Input(shape)
 X = L.Dense(150, activation='relu' )(X_input)
 X = L.Dense(125, activation='relu' )(X)
 X = L.Dense(75, activation='relu' )(X)
 X = L.Dense(50, activation='relu' )(X)
 X = L.Dense(50, activation='relu' )(X)
 X = L.Dense(25, activation='relu' )(X)
 X = L.Dense(11, activation='softmax' )(X)
 model = Model(inputs=X_input, outputs=X)
 return model<compute_train_metric>
def ecdf(x):

 x = np.sort(x)
 n = len(x)
 y = np.arange(1, n + 1, 1)/ n
return x, y
def lgb_Metric(preds, dtrain):
labels = dtrain.get_label()
num_labels = 11
preds = preds.reshape(num_labels, len(preds)//num_labels)
 preds = np.argmax(preds, axis=0)
 score = f1_score(labels, preds, average="macro")
 return('KaggleMetric', score, True)
 lgb_r_params = {
'objective': 'multiclass',
'num_class': 11,
'metric': 'multi_logloss',
'learning_rate': 0.00987173774816051,
'lambda_l1': 0.00031963798315506463,
'lambda_l2': 0.18977456778807847,
'num_leaves': 171,
'feature_fraction': 0.58733782457345,
'bagging_fraction': 0.7057826081907392,
'bagging_freq': 4}<load_from_csv>
data = data.loc[data['passenger_count'] < 6]
test_data = pd.read_csv('.. /input/data-without-drift/test_clean.csv')
 test_df = divide_and_add_features(test_data['signal'])
 test_df = reduce_mem_usage(test_df)
 m_data = pd.read_csv('.. /input/viterbi/Viterbi_test.csv' ).rename(columns={"open_channels": "signal"})
 m_data_ = m_data[3500000:3642922].append(m_data[3822754:4000000])
 m_data = m_data[:3500000].append(m_data[4000000:] ).reset_index().append(m_data_, ignore_index=True)
 m_data.head()
m_data[["signal"]].plot(figsize=(19,5), alpha=0.7)
 test_df["m_signal"] = m_data["signal"].values/15
test_df = reduce_mem_usage(test_df)
 test_df.shape<split>
print(f'Initial Observations: {data.shape[0]}' )
kf = KFold(n_splits=FOLD_NUM, shuffle=True, random_state=42)
 preds = np.zeros(2000000*11 ).reshape(( 2000000, 11))
X = df
y = data['open_channels']
y_values = data['open_channels'].values
for i,(tdx, vdx)in enumerate(kf.split(X, y)) :
print(f'Fold : {i}')
 X_train, X_valid, y_train, y_valid = X.iloc[tdx], X.iloc[vdx], y_values[tdx], y_values[vdx]
print(f"sep: {X_train.shape}, {X_valid.shape}, {y_train.shape}, {y_valid.shape}")
 mlp = create_mpl(X_train.values[0].shape)
 mlp.compile(optimizer="adam", loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy'])
 class_weight = get_class_weight(y_train)
 mlp.fit(x=X_train.values, y=y_train, epochs=MLP_EPOCH_NUM, batch_size=1024, class_weight=class_weight,
validation_data=(X_valid.values, y_valid), verbose=0)
 mlp_pred = mlp.predict(X_valid.values)
 f1_mlp = f1_score(y_valid, np.argmax(mlp_pred, axis=-1), average='macro')
 print(f"f1 score is :{f1_mlp}")
 plt.figure(1)
 plt.plot(mlp.history.history['loss'], 'b', label='loss')
 plt.plot(mlp.history.history['val_loss'], 'r', label='loss')
 plt.xlabel('epochs')
 plt.legend()
plt.figure(2)
 plt.plot(mlp.history.history['sparse_categorical_accuracy'], 'g', label='sparse_categorical_accuracy')
 plt.plot(mlp.history.history['val_sparse_categorical_accuracy'], 'r', label='val_sparse_categorical_accuracy')
 plt.xlabel('epochs')
 plt.legend()
plt.show()
lgb_dataset = lgb.Dataset(X_train.values, label=y_train, weight=class_weight[y_train])
 lgb_valid_dataset = lgb.Dataset(X_valid.values, label=y_valid, weight=class_weight[y_valid])
 print('Training LGBM...')
 gbc = lgb.train(lgb_r_params, lgb_dataset, LGBM_BOOST_NUM, valid_names=["train", "valid"],
valid_sets=[lgb_dataset, lgb_valid_dataset], verbose_eval=-1,
feval=lgb_Metric, early_stopping_rounds=10)
 print('LGBM trained!')
 gbc_pred = gbc.predict(X_valid.values, num_iteration=gbc.best_iteration)
 attr2 = {k: v for k, v in zip(df_columns, gbc.feature_importance())if 200 > v and v>0}
print("weak fe
print(attr2)
 print("
print(f1_score(y_valid, np.argmax(gbc_pred, axis=1), average='macro'))
f1s = []
alphas = []
for alpha in tqdm(np.linspace(0,1,101)) :
y_pred = alpha*mlp_pred +(1 - alpha)*gbc_pred
f1 = f1_score(y_valid, np.argmax(y_pred, axis=1), average='macro')
 f1s.append(f1)
 alphas.append(alpha)
 f1s = np.array(f1s)
 alphas = np.array(alphas)
 best_alpha = alphas[np.argmax(f1s)]
print('best_f1=', f1s.max())
 print('best_alpha=', best_alpha)
 plt.plot(alphas, f1s)
 plt.title('f1_score for ensemble')
 plt.xlabel('alpha')
 plt.ylabel('f1_score')
 plt.show()
mlp_pred = mlp.predict(test_df)
 gbc_pred = gbc.predict(test_df, num_iteration=gbc.best_iteration)
 pred = best_alpha*mlp_pred +(1 - best_alpha)*gbc_pred
preds += pred
print(f"f1_mlp is {f1_mlp}")
 gc.collect() 
<save_to_csv>
data = data.loc[data['pickup_latitude'].between(40, 42)]
data = data.loc[data['pickup_longitude'].between(-75, -72)]
data = data.loc[data['dropoff_latitude'].between(40, 42)]
data = data.loc[data['dropoff_longitude'].between(-75, -72)]
print(f'New number of observations: {data.shape[0]}' )
pred = np.argmax(preds, axis=1)
 print('Writing submission...')
 submission = pd.DataFrame()
submission['time'] = test_data['time']
submission['open_channels'] = pred
submission.to_csv('mlp_gbc_submission.csv', index=False, float_format='%.4f')
 print('Submission finished!' )<define_variables>
BB_zoom =(-74.1, -73.7, 40.6, 40.85)
 nyc_map_zoom = plt.imread('https://github.com/WillKoehrsen/Machine-Learning-Projects/blob/master/images/nyc_-74.1_-73.7_40.6_40.85.PNG?raw=true' )
EPOCHS = 150
NNBATCHSIZE = 16
GROUP_BATCH_SIZE = 4000
SEED = 321
LR = 0.001
SPLITS = 5
outdir = 'wavenet_models'
flip = False
noise = False
if not os.path.exists(outdir):
os.makedirs(outdir)
 def seed_everything(seed):
random.seed(seed)
 np.random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 tf.random.set_seed(seed )<load_from_csv>
color_mapping = {fare_bin: palette[i] for i, fare_bin in enumerate(data['fare-bin'].unique())}
color_mapping
def read_data() :
train = pd.read_csv('/kaggle/input/clean-kalman/train_clean_kalman.csv', dtype={'time': np.float32, 'signal': np.float32, 'open_channels':np.int32})
 test = pd.read_csv('/kaggle/input/clean-kalman/test_clean_kalman.csv', dtype={'time': np.float32, 'signal': np.float32})
 sub = pd.read_csv('/kaggle/input/liverpool-ion-switching/sample_submission.csv', dtype={'time': np.float32})
 return train, test, sub
def batching(df, batch_size):
df['group'] = df.groupby(df.index//batch_size, sort=False)['signal'].agg(['ngroup'] ).values
df['group'] = df['group'].astype(np.uint16)
 return df
def normalize(train, test):
train_input_mean = train.signal.mean()
train_input_sigma = train.signal.std()
train['signal'] =(train.signal - train_input_mean)/ train_input_sigma
test['signal'] =(test.signal - train_input_mean)/ train_input_sigma
return train, test
def lag_with_pct_change(df, windows):
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)
 return df
def run_feat_engineering(df, batch_size):
df = batching(df, batch_size = batch_size)
 df = lag_with_pct_change(df, [1, 2, 3])
 df['signal_2'] = df['signal'] ** 2
return df
def feature_selection(train, test):
features = [col for col in train.columns if col not in ['index', 'group', 'open_channels', 'time']]
train = train.replace([np.inf, -np.inf], np.nan)
 test = test.replace([np.inf, -np.inf], np.nan)
 for feature in features:
feature_mean = pd.concat([train[feature], test[feature]], axis = 0 ).mean()
train[feature] = train[feature].fillna(feature_mean)
 test[feature] = test[feature].fillna(feature_mean)
 return train, test, features
def split(GROUP_BATCH_SIZE=4000, SPLITS=5):
print('Reading Data Started...')
 train, test, sample_submission = read_data()
train, test = normalize(train, test)
 print('Reading and Normalizing Data Completed')
 print('Creating Features')
 print('Feature Engineering Started...')
 train = run_feat_engineering(train, batch_size=GROUP_BATCH_SIZE)
 test = run_feat_engineering(test, batch_size=GROUP_BATCH_SIZE)
 train, test, features = feature_selection(train, test)
 print(train.head())
 print('Feature Engineering Completed...')
 target = ['open_channels']
group = train['group']
kf = GroupKFold(n_splits=SPLITS)
 splits = [x for x in kf.split(train, train[target], group)]
new_splits = []
for sp in splits:
new_split = []
new_split.append(np.unique(group[sp[0]]))
new_split.append(np.unique(group[sp[1]]))
new_split.append(sp[1])
 new_splits.append(new_split)
 target_cols = ['open_channels']
print(train.head() , train.shape)
 train_tr = np.array(list(train.groupby('group' ).apply(lambda x: x[target_cols].values)) ).astype(np.float32)
 train = np.array(list(train.groupby('group' ).apply(lambda x: x[features].values)))
 test = np.array(list(test.groupby('group' ).apply(lambda x: x[features].values)))
 print(train.shape, test.shape, train_tr.shape)
 return train, test, train_tr, new_splits 
<define_search_model>
data['abs_lat_diff'] =(data['dropoff_latitude'] - data['pickup_latitude'] ).abs()
data['abs_lon_diff'] =(data['dropoff_longitude'] - data['pickup_longitude'] ).abs()
class Wave_Block(nn.Module):
def __init__(self, in_channels, out_channels, dilation_rates, kernel_size):
super(Wave_Block, self ).__init__()
self.num_rates = dilation_rates
self.convs = nn.ModuleList()
self.filter_convs = nn.ModuleList()
self.gate_convs = nn.ModuleList()
self.convs.append(nn.Conv1d(in_channels, out_channels, kernel_size=1))
dilation_rates = [2 ** i for i in range(dilation_rates)]
for dilation_rate in dilation_rates:
self.filter_convs.append(
 nn.Conv1d(out_channels, out_channels, kernel_size=kernel_size, padding=int(( dilation_rate*(kernel_size-1)) /2), dilation=dilation_rate))
self.gate_convs.append(
 nn.Conv1d(out_channels, out_channels, kernel_size=kernel_size, padding=int(( dilation_rate*(kernel_size-1)) /2), dilation=dilation_rate))
self.convs.append(nn.Conv1d(out_channels, out_channels, kernel_size=1))
def forward(self, x):
x = self.convs[0](x)
 res = x
for i in range(self.num_rates):
x = torch.tanh(self.filter_convs[i](x)) * torch.sigmoid(self.gate_convs[i](x))
x = self.convs[i + 1](x)
 res = res + x
return res
class Classifier(nn.Module):
def __init__(self, inch=8, kernel_size=3):
super().__init__()
self.wave_block1 = Wave_Block(inch, 16, 12, kernel_size)
 self.wave_block2 = Wave_Block(16, 32, 8, kernel_size)
 self.wave_block3 = Wave_Block(32, 64, 4, kernel_size)
 self.wave_block4 = Wave_Block(64, 128, 1, kernel_size)
 self.fc = nn.Linear(128, 11)
 def forward(self, x):
x = x.permute(0, 2, 1)
 x = self.wave_block1(x)
 x = self.wave_block2(x)
 x = self.wave_block3(x)
 x = self.wave_block4(x)
 x = x.permute(0, 2, 1)
 x = self.fc(x)
 return x
class EarlyStopping:
def __init__(self, patience=5, delta=0, checkpoint_path='checkpoint.pt', is_maximize=True):
self.patience, self.delta, self.checkpoint_path = patience, delta, checkpoint_path
self.counter, self.best_score = 0, None
self.is_maximize = is_maximize
def load_best_weights(self, model):
model.load_state_dict(torch.load(self.checkpoint_path))
def __call__(self, score, model):
if self.best_score is None or \ 
(score > self.best_score + self.delta if self.is_maximize else score < self.best_score - self.delta):
torch.save(model.state_dict() , self.checkpoint_path)
 self.best_score, self.counter = score, 0
return 1
else:
self.counter += 1
if self.counter >= self.patience:
return 2
return 0 
<train_model>
no_diff = data[(data['abs_lat_diff'] == 0)&(data['abs_lon_diff'] == 0)]
no_diff.shape
class IronDataset(Dataset):
def __init__(self, data, labels, training=True, transform=None, seq_len=5000, flip=0.5, noise_level=0, class_split=0.0):
self.data = data
self.labels = labels
self.transform = transform
self.training = training
self.flip = flip
self.noise_level = noise_level
self.class_split = class_split
self.seq_len = seq_len
def __len__(self):
return len(self.data)
 def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
data = self.data[idx]
labels = self.labels[idx]
return [data.astype(np.float32), labels.astype(int)] 
<split>
def minkowski_distance(x1, x2, y1, y2, p):
return(( abs(x2 - x1)** p)+(abs(y2 - y1)) ** p)**(1 / p )
train, test, train_tr, new_splits = split()<create_dataframe>
minkowski_distance(0, 3, 0, 4, 2 )
test_y = np.zeros([int(2000000/GROUP_BATCH_SIZE), GROUP_BATCH_SIZE, 1])
 test_dataset = IronDataset(test, test_y, flip=False)
 test_dataloader = DataLoader(test_dataset, NNBATCHSIZE, shuffle=False, num_workers=8, pin_memory=True)
 test_preds_all = np.zeros(( 2000000, 11))
oof_score = []
for index,(train_index, val_index, _)in enumerate(new_splits[0:], start=0):
print("Fold : {}".format(index))
train_dataset = IronDataset(train[train_index], train_tr[train_index], seq_len=GROUP_BATCH_SIZE, flip=flip, noise_level=noise)
 train_dataloader = DataLoader(train_dataset, NNBATCHSIZE, shuffle=True, num_workers=8, pin_memory=True)
 valid_dataset = IronDataset(train[val_index], train_tr[val_index], seq_len=GROUP_BATCH_SIZE, flip=False)
 valid_dataloader = DataLoader(valid_dataset, NNBATCHSIZE, shuffle=False, num_workers=4, pin_memory=True)
 it = 0
model = Classifier()
model = model.cuda()
early_stopping = EarlyStopping(patience=40, is_maximize=True,
checkpoint_path=os.path.join(outdir, "gru_clean_checkpoint_fold_{}_iter_{}.pt".format(index,
it)))
 weight = None
criterion = nn.CrossEntropyLoss(weight=weight)
 optimizer = torch.optim.Adam(model.parameters() , lr=LR)
 schedular = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='max', patience=3, factor=0.2)
 avg_train_losses, avg_valid_losses = [], []
for epoch in range(EPOCHS):
print('**********************************')
 print("Folder : {} Epoch : {}".format(index, epoch))
print("Curr learning_rate: {:0.9f}".format(optimizer.param_groups[0]['lr']))
train_losses, valid_losses = [], []
tr_loss_cls_item, val_loss_cls_item = [], []
model.train()
train_preds, train_true = torch.Tensor([] ).cuda() , torch.LongTensor([] ).cuda()
for x, y in tqdm(train_dataloader):
x = x.cuda()
y = y.cuda()
optimizer.zero_grad()
predictions = model(x)
 predictions_ = predictions.view(-1, predictions.shape[-1])
 y_ = y.view(-1)
 loss = criterion(predictions_, y_)
 loss.backward()
optimizer.step()
train_losses.append(loss.item())
 train_true = torch.cat([train_true, y_], 0)
 train_preds = torch.cat([train_preds, predictions_], 0)
 model.eval()
val_preds, val_true = torch.Tensor([] ).cuda() , torch.LongTensor([] ).cuda()
print('EVALUATION')
 with torch.no_grad() :
for x, y in tqdm(valid_dataloader):
x = x.cuda()
y = y.cuda()
predictions = model(x)
 predictions_ = predictions.view(-1, predictions.shape[-1])
 y_ = y.view(-1)
 loss = criterion(predictions_, y_)
 valid_losses.append(loss.item())
 val_true = torch.cat([val_true, y_], 0)
 val_preds = torch.cat([val_preds, predictions_], 0)
 train_loss = np.average(train_losses)
 valid_loss = np.average(valid_losses)
 avg_train_losses.append(train_loss)
 avg_valid_losses.append(valid_loss)
 print("train_loss: {:0.6f}, valid_loss: {:0.6f}".format(train_loss, valid_loss))
train_score = f1_score(train_true.cpu().detach().numpy() , train_preds.cpu().detach().numpy().argmax(1),
labels=list(range(11)) , average='macro')
 val_score = f1_score(val_true.cpu().detach().numpy() , val_preds.cpu().detach().numpy().argmax(1),
labels=list(range(11)) , average='macro')
 schedular.step(val_score)
 print("train_f1: {:0.6f}, valid_f1: {:0.6f}".format(train_score, val_score))
res = early_stopping(val_score, model)
 if res == 2:
print("Early Stopping")
 print('folder %d global best val max f1 model score %f' %(index, early_stopping.best_score))
break
elif res == 1:
print('save folder %d global val max f1 model score %f' %(index, val_score))
print('Folder {} finally best global max f1 score is {}'.format(index, early_stopping.best_score))
oof_score.append(round(early_stopping.best_score, 6))
model.eval()
pred_list = []
with torch.no_grad() :
for x, y in tqdm(test_dataloader):
x = x.cuda()
y = y.cuda()
predictions = model(x)
 predictions_ = predictions.view(-1, predictions.shape[-1])
 pred_list.append(F.softmax(predictions_, dim=1 ).cpu().numpy())
 test_preds = np.vstack(pred_list)
 test_preds_all += test_preds
print('all folder score is:%s'%str(oof_score))
print('OOF mean score is: %f'%(sum(oof_score)/len(oof_score)))
 print('Generate submission.............')
 submission_csv_path = '/kaggle/input/liverpool-ion-switching/sample_submission.csv'
ss = pd.read_csv(submission_csv_path, dtype={'time': str})
 test_preds_all = test_preds_all / np.sum(test_preds_all, axis=1)[:, None]
test_pred_frame = pd.DataFrame({'time': ss['time'].astype(str),
'open_channels': np.argmax(test_preds_all, axis=1)})
 test_pred_frame.to_csv("./gru_preds.csv", index=False)
 print('over')
<install_modules>
data.groupby('fare-bin')['euclidean'].agg(['mean', 'count'] )
!pip install --no-warn-conflicts -q tensorflow-addons<import_modules>
data.groupby('passenger_count')['fare_amount'].agg(['mean', 'count'] )
Add, AveragePooling1D, Multiply, GRU, GRUCell, LSTMCell, SimpleRNNCell, SimpleRNN, TimeDistributed, RNN,
RepeatVector, Conv1D, MaxPooling1D, Concatenate, GlobalAveragePooling1D, UpSampling1D)
 warnings.simplefilter('ignore')
 warnings.filterwarnings('ignore')
 pd.set_option('display.max_columns', 1000)
 pd.set_option('display.max_rows', 500)
 %matplotlib inline<define_variables>
test = pd.read_csv('.. /input/test.csv', parse_dates = ['pickup_datetime'])
 test['abs_lat_diff'] =(test['dropoff_latitude'] - test['pickup_latitude'] ).abs()
test['abs_lon_diff'] =(test['dropoff_longitude'] - test['pickup_longitude'] ).abs()
test_id = list(test.pop('key'))
test.describe()
EPOCHS=120
NNBATCHSIZE=20
BATCHSIZE = 4000
SEED = 321
SELECT = True
SPLITS = 5
LR = 0.001
fe_config = [ 
(True, 4000),
]<define_variables>
test['manhattan'] = minkowski_distance(test['pickup_longitude'], test['dropoff_longitude'],
test['pickup_latitude'], test['dropoff_latitude'], 1)
 test['euclidean'] = minkowski_distance(test['pickup_longitude'], test['dropoff_longitude'],
test['pickup_latitude'], test['dropoff_latitude'], 2 )
def seed_everything(seed : int)-> NoReturn :
rn.seed(seed)
 np.random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 tf.random.set_seed(seed)
 seed_everything(SEED)
<load_from_csv>
R = 6378
def haversine_np(lon1, lat1, lon2, lat2):

 lon1, lat1, lon2, lat2 = map(np.radians, [lon1, lat1, lon2, lat2])
 dlon = lon2 - lon1
dlat = lat2 - lat1
a = np.sin(dlat/2.0)**2 + np.cos(lat1)* np.cos(lat2)* np.sin(dlon/2.0)**2
c = 2 * np.arcsin(np.sqrt(a))
km = R * c
return km
def read_data(base : os.path.abspath)-> Tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame]:
train = pd.read_csv('/kaggle/input/clean-kalman/train_clean_kalman.csv', dtype={'time': np.float32, 'signal': np.float32, 'open_channels':np.int32})
 test = pd.read_csv('/kaggle/input/clean-kalman/test_clean_kalman.csv', dtype={'time': np.float32, 'signal': np.float32})
 sub = pd.read_csv('/kaggle/input/liverpool-ion-switching/sample_submission.csv', dtype={'time': np.float32})
 return train, test, sub 
<create_dataframe>
data['haversine'] = haversine_np(data['pickup_longitude'], data['pickup_latitude'],
data['dropoff_longitude'], data['dropoff_latitude'])
 test['haversine'] = haversine_np(test['pickup_longitude'], test['pickup_latitude'],
test['dropoff_longitude'], test['dropoff_latitude'] )
def batching(df : pd.DataFrame,
batch_size : int)-> pd.DataFrame :
df['group'] = df.groupby(df.index//batch_size, sort=False)['signal'].agg(['ngroup'] ).values
df['group'] = df['group'].astype(np.uint16)
 return df 
<create_dataframe>
data.groupby('fare-bin')['haversine'].agg(['mean', 'count'] )
def lag_with_pct_change(df : pd.DataFrame,
shift_sizes : Optional[List]=[1, 2],
add_pct_change : Optional[bool]=False,
add_pct_change_lag : Optional[bool]=False)-> pd.DataFrame:
for shift_size in shift_sizes:
df['signal_shift_pos_'+str(shift_size)] = df.groupby('group')['signal'].shift(shift_size ).fillna(0)
 df['signal_shift_neg_'+str(shift_size)] = df.groupby('group')['signal'].shift(-1*shift_size ).fillna(0)
 if add_pct_change:
df['pct_change'] = df['signal'].pct_change()
if add_pct_change_lag:
for shift_size in shift_sizes:
df['pct_change_shift_pos_'+str(shift_size)] = df.groupby('group')['pct_change'].shift(shift_size ).fillna(0)
 df['pct_change_shift_neg_'+str(shift_size)] = df.groupby('group')['pct_change'].shift(-1*shift_size ).fillna(0)
 return df 
<create_dataframe>
lr = LinearRegression()
def run_feat_enginnering(df : pd.DataFrame,
create_all_data_feats : bool,
batch_size : int)-> pd.DataFrame:
df = batching(df, batch_size=batch_size)
 if create_all_data_feats:
df = lag_with_pct_change(df, [1, 2, 3], add_pct_change=False, add_pct_change_lag=False)
 df['signal_2'] = df['signal'] ** 2
return df 
<feature_engineering>
X_train, X_valid, y_train, y_valid = train_test_split(data, np.array(data['fare_amount']),
stratify = data['fare-bin'],
random_state = RSEED, test_size = 1_000_000 )
def feature_selection(df : pd.DataFrame,
df_test : pd.DataFrame)-> Tuple[pd.DataFrame , pd.DataFrame, List]:
use_cols = [col for col in df.columns if col not in ['index','group', 'open_channels', 'time']]
df = df.replace([np.inf, -np.inf], np.nan)
 df_test = df_test.replace([np.inf, -np.inf], np.nan)
 for col in use_cols:
col_mean = pd.concat([df[col], df_test[col]], axis=0 ).mean()
df[col] = df[col].fillna(col_mean)
 df_test[col] = df_test[col].fillna(col_mean)
 gc.collect()
return df, df_test, use_cols 
<concatenate>
lr.fit(X_train[['abs_lat_diff', 'abs_lon_diff', 'passenger_count']], y_train)
 print('Intercept', round(lr.intercept_, 4))
print('abs_lat_diff coef: ', round(lr.coef_[0], 4),
'\tabs_lon_diff coef:', round(lr.coef_[1], 4),
'\tpassenger_count coef:', round(lr.coef_[2], 4))
def augment(X: np.array, y:np.array)-> Tuple[np.array, np.array]:
X = np.vstack(( X, np.flip(X, axis=1)))
 y = np.vstack(( y, np.flip(y, axis=1)))
 return X, y<create_dataframe>
warnings.filterwarnings('ignore', category = RuntimeWarning)
 def metrics(train_pred, valid_pred, y_train, y_valid):

 train_rmse = np.sqrt(mean_squared_error(y_train, train_pred))
valid_rmse = np.sqrt(mean_squared_error(y_valid, valid_pred))
train_ape = abs(( y_train - train_pred)/ y_train)
 valid_ape = abs(( y_valid - valid_pred)/ y_valid)
 train_ape[train_ape == np.inf] = 0
train_ape[train_ape == -np.inf] = 0
valid_ape[valid_ape == np.inf] = 0
valid_ape[valid_ape == -np.inf] = 0
train_mape = 100 * np.mean(train_ape)
 valid_mape = 100 * np.mean(valid_ape)
 return train_rmse, valid_rmse, train_mape, valid_mape
def evaluate(model, features, X_train, X_valid, y_train, y_valid):

 train_pred = model.predict(X_train[features])
 valid_pred = model.predict(X_valid[features])
 train_rmse, valid_rmse, train_mape, valid_mape = metrics(train_pred, valid_pred,
y_train, y_valid)
 print(f'Training: rmse = {round(train_rmse, 2)} \t mape = {round(train_mape, 2)}')
 print(f'Validation: rmse = {round(valid_rmse, 2)} \t mape = {round(valid_mape, 2)}' )
def run_cv_model_by_batch(train : pd.DataFrame,
test : pd.DataFrame,
splits : int,
batch_col : Text,
feats : List,
sample_submission: pd.DataFrame,
nn_epochs : int,
nn_batch_size : int)-> NoReturn:
seed_everything(SEED)
 K.clear_session()
config = tf.compat.v1.ConfigProto(intra_op_parallelism_threads=1,inter_op_parallelism_threads=1)
 sess = tf.compat.v1.Session(graph=tf.compat.v1.get_default_graph() , config=config)
 tf.compat.v1.keras.backend.set_session(sess)
 oof_ = np.zeros(( len(train), 11))
preds_ = np.zeros(( len(test), 11))
target = ['open_channels']
group = train['group']
kf = GroupKFold(n_splits=5)
 splits = [x for x in kf.split(train, train[target], group)]
new_splits = []
for sp in splits:
new_split = []
new_split.append(np.unique(group[sp[0]]))
new_split.append(np.unique(group[sp[1]]))
new_split.append(sp[1])
 new_splits.append(new_split)
 tr = pd.concat([pd.get_dummies(train.open_channels), train[['group']]], axis=1)
 tr.columns = ['target_'+str(i)for i in range(11)] + ['group']
target_cols = ['target_'+str(i)for i in range(11)]
train_tr = np.array(list(tr.groupby('group' ).apply(lambda x: x[target_cols].values)) ).astype(np.float32)
 train = np.array(list(train.groupby('group' ).apply(lambda x: x[feats].values)))
 test = np.array(list(test.groupby('group' ).apply(lambda x: x[feats].values)))
 for n_fold,(tr_idx, val_idx, val_orig_idx)in enumerate(new_splits[0:], start=0):
train_x, train_y = train[tr_idx], train_tr[tr_idx]
valid_x, valid_y = train[val_idx], train_tr[val_idx]
if n_fold < 2:
train_x, train_y = augment(train_x, train_y)
 gc.collect()
shape_ =(None, train_x.shape[2])
 model = Classifier(shape_)
 cb_lr_schedule = LearningRateScheduler(lr_schedule)
 cb_prg = tfa.callbacks.TQDMProgressBar(leave_epoch_progress=False,leave_overall_progress=False, show_epoch_progress=False,show_overall_progress=True)
 model.fit(train_x,train_y,
epochs=nn_epochs,
callbacks=[cb_prg, cb_lr_schedule, MacroF1(model, valid_x,valid_y)],
batch_size=nn_batch_size,verbose=0,
validation_data=(valid_x,valid_y))
preds_f = model.predict(valid_x)
 f1_score_ = f1_score(np.argmax(valid_y, axis=2 ).reshape(-1), np.argmax(preds_f, axis=2 ).reshape(-1), average = 'macro')
 logger.info(f'Training fold {n_fold + 1} completed.macro f1 score : {f1_score_ :1.5f}')
 preds_f = preds_f.reshape(-1, preds_f.shape[-1])
 oof_[val_orig_idx,:] += preds_f
te_preds = model.predict(test)
 te_preds = te_preds.reshape(-1, te_preds.shape[-1])
 preds_ += te_preds / SPLITS
f1_score_ =f1_score(np.argmax(train_tr, axis=2 ).reshape(-1), np.argmax(oof_, axis=1), average = 'macro')
 logger.info(f'Training completed.oof macro f1 score : {f1_score_:1.5f}')
 sample_submission['open_channels'] = np.argmax(preds_, axis=1 ).astype(int)
 sample_submission.to_csv('submission.csv', index=False, float_format='%.4f')
 display(sample_submission.head())
 np.save('oof.npy', oof_)
 np.save('preds.npy', preds_)
 return 
<set_options>
evaluate(lr, ['abs_lat_diff', 'abs_lon_diff', 'passenger_count'],
X_train, X_valid, y_train, y_valid )
def lr_schedule(epoch):
if epoch < 40:
lr = LR
elif epoch < 50:
lr = LR / 3
elif epoch < 60:
lr = LR / 6
elif epoch < 75:
lr = LR / 9
elif epoch < 85:
lr = LR / 12
elif epoch < 100:
lr = LR / 15
else:
lr = LR / 50
return lr<normalization>
train_mean = y_train.mean()
train_preds = [train_mean for _ in range(len(y_train)) ]
valid_preds = [train_mean for _ in range(len(y_valid)) ]
tr, vr, tm, vm = metrics(train_preds, valid_preds, y_train, y_valid)
 print(f'Baseline Training: rmse = {round(tr, 2)} \t mape = {round(tm, 2)}')
 print(f'Baseline Validation: rmse = {round(vr, 2)} \t mape = {round(vm, 2)}' )
class Mish(tf.keras.layers.Layer):
def __init__(self, **kwargs):
super(Mish, self ).__init__(**kwargs)
 self.supports_masking = True
def call(self, inputs):
return inputs * K.tanh(K.softplus(inputs))
def get_config(self):
base_config = super(Mish, self ).get_config()
return dict(list(base_config.items())+ list(config.items()))
def compute_output_shape(self, input_shape):
return input_shape
def mish(x):
	return tf.keras.layers.Lambda(lambda x: x*K.tanh(K.softplus(x)) )(x)
 get_custom_objects().update({'mish': Activation(mish)} )<set_options>
preds = lr.predict(test[['abs_lat_diff', 'abs_lon_diff', 'passenger_count']])
 sub = pd.DataFrame({'key': test_id, 'fare_amount': preds})
 sub.to_csv('sub_lr_simple.csv', index = False )
class Attention(Layer):

 def __init__(self, hidden_size,
num_heads = 8,
attention_dropout=.1,
trainable=True,
name='Attention'):
if hidden_size % num_heads != 0:
raise ValueError("Hidden size must be evenly divisible by the number of heads.")
 self.hidden_size = hidden_size
self.num_heads = num_heads
self.trainable = trainable
self.attention_dropout = attention_dropout
self.dense = tf.keras.layers.Dense(self.hidden_size, use_bias=False)
 super(Attention, self ).__init__(name=name)
 def split_heads(self, x):

 with tf.name_scope("split_heads"):
batch_size = tf.shape(x)[0]
length = tf.shape(x)[1]
depth =(self.hidden_size // self.num_heads)
 x = tf.reshape(x, [batch_size, length, self.num_heads, depth])
 return tf.transpose(x, [0, 2, 1, 3])
 def combine_heads(self, x):

 with tf.name_scope("combine_heads"):
batch_size = tf.shape(x)[0]
length = tf.shape(x)[2]
x = tf.transpose(x, [0, 2, 1, 3])
 return tf.reshape(x, [batch_size, length, self.hidden_size])
 def call(self, inputs):

 q = self.dense(inputs)
 k = self.dense(inputs)
 v = self.dense(inputs)
 q = self.split_heads(q)
 k = self.split_heads(k)
 v = self.split_heads(v)
 depth =(self.hidden_size // self.num_heads)
 q *= depth ** -0.5
logits = tf.matmul(q, k, transpose_b=True)
 weights = tf.nn.softmax(logits, name="attention_weights")
 if self.trainable:
weights = tf.nn.dropout(weights, 1.0 - self.attention_dropout)
 attention_output = tf.matmul(weights, v)
 attention_output = self.combine_heads(attention_output)
 attention_output = self.dense(attention_output)
 return attention_output
def compute_output_shape(self, input_shape):
return tf.TensorShape(input_shape )<compute_train_metric>
sns.distplot(sub['fare_amount'])
 plt.title('Distribution of Linear Regression Predictions');
def categorical_focal_loss(gamma=2.0, alpha=0.25):

 def focal_loss(y_true, y_pred):
epsilon = K.epsilon()
y_pred = K.clip(y_pred, epsilon, 1.0-epsilon)
 cross_entropy = -y_true*K.log(y_pred)
 weight = alpha * y_true * K.pow(( 1-y_pred), gamma)
 loss = weight * cross_entropy
loss = K.sum(loss, axis=1)
 return loss
return focal_loss<choose_model_class>
test.loc[sub[sub['fare_amount'] > 100].index]
def WaveNetResidualConv1D(num_filters, kernel_size, stacked_layer):
def build_residual_block(l_input):
resid_input = l_input
for dilation_rate in [2**i for i in range(stacked_layer)]:
l_sigmoid_conv1d = Conv1D(
 num_filters, kernel_size, dilation_rate=dilation_rate,
padding='same', activation='sigmoid' )(l_input)
 l_tanh_conv1d = Conv1D(
 num_filters, kernel_size, dilation_rate=dilation_rate,
padding='same', activation='mish' )(l_input)
 l_input = Multiply()([l_sigmoid_conv1d, l_tanh_conv1d])
 l_input = Conv1D(num_filters, 1, padding='same' )(l_input)
 resid_input = Add()([resid_input ,l_input])
 return resid_input
return build_residual_block
def Classifier(shape_):
num_filters_ = 16
kernel_size_ = 3
stacked_layers_ = [12, 8, 4, 1]
l_input = Input(shape=(shape_))
x = Conv1D(num_filters_, 1, padding='same' )(l_input)
 x = WaveNetResidualConv1D(num_filters_, kernel_size_, stacked_layers_[0] )(x)
 x = Conv1D(num_filters_*2, 1, padding='same' )(x)
 x = WaveNetResidualConv1D(num_filters_*2, kernel_size_, stacked_layers_[1] )(x)
 x = Conv1D(num_filters_*4, 1, padding='same' )(x)
 x = WaveNetResidualConv1D(num_filters_*4, kernel_size_, stacked_layers_[2] )(x)
 x = Conv1D(num_filters_*8, 1, padding='same' )(x)
 x = WaveNetResidualConv1D(num_filters_*8, kernel_size_, stacked_layers_[3] )(x)
 l_output = Dense(11, activation='softmax' )(x)
 model = models.Model(inputs=[l_input], outputs=[l_output])
 opt = Adam(lr=LR)
 opt = tfa.optimizers.SWA(opt)
 model.compile(loss=losses.CategoricalCrossentropy() , optimizer=opt, metrics=['accuracy'])
 return model<choose_model_class>
sub[sub['fare_amount'] > 100]
def Classifierx(shape_):
inp = Input(shape=(shape_))
x = Bidirectional(GRU(256, return_sequences=True))(inp)
 x = Attention(512 )(x)
 x = TimeDistributed(Dense(256, activation='mish'))(x)
 x = TimeDistributed(Dense(128, activation='mish'))(x)
 out = TimeDistributed(Dense(11, activation='softmax', name='out'))(x)
 model = models.Model(inputs=inp, outputs=out)
 opt = Adam(lr=LR)
 opt = tfa.optimizers.SWA(opt)
 model.compile(loss=losses.CategoricalCrossentropy() , optimizer=opt, metrics=['accuracy'])
 return model<predict_on_test>
lr.fit(X_train[['haversine', 'abs_lat_diff', 'abs_lon_diff', 'passenger_count']], y_train)
 evaluate(lr, ['haversine', 'abs_lat_diff', 'abs_lon_diff', 'passenger_count'],
X_train, X_valid, y_train, y_valid )
class MacroF1(Callback):
def __init__(self, model, inputs, targets):
self.model = model
self.inputs = inputs
self.targets = np.argmax(targets, axis=2 ).reshape(-1)
 def on_epoch_end(self, epoch, logs):
pred = np.argmax(self.model.predict(self.inputs), axis=2 ).reshape(-1)
 score = f1_score(self.targets, pred, average="macro")
 print(f' F1Macro: {score:.5f}')
<normalization>
random_forest = RandomForestRegressor(n_estimators = 20, max_depth = 20,
max_features = None, oob_score = True,
bootstrap = True, verbose = 1, n_jobs = -1)
 random_forest.fit(X_train[['haversine', 'abs_lat_diff', 'abs_lon_diff', 'passenger_count']], y_train )
def normalize(train, test):
train_input_mean = train.signal.mean()
train_input_sigma = train.signal.std()
train['signal'] =(train.signal-train_input_mean)/train_input_sigma
test['signal'] =(test.signal-train_input_mean)/train_input_sigma
return train, test<feature_engineering>
evaluate(random_forest, ['haversine', 'abs_lat_diff', 'abs_lon_diff', 'passenger_count'],
X_train, X_valid, y_train, y_valid )
def run_everything(fe_config : List)-> NoReturn:
not_feats_cols = ['time']
target_col = ['open_channels']
init_logger()
with timer(f'Reading Data'):
logger.info('Reading Data Started...')
 base = os.path.abspath('/kaggle/input/liverpool-ion-switching/')
 train, test, sample_submission = read_data(base)
 train, test = normalize(train, test)
 logger.info('Reading and Normalizing Data Completed...')
 with timer(f'Creating Features'):
logger.info('Feature Enginnering Started...')
 for config in fe_config:
train = run_feat_enginnering(train, create_all_data_feats=config[0], batch_size=config[1])
 test = run_feat_enginnering(test, create_all_data_feats=config[0], batch_size=config[1])
 train, test, feats = feature_selection(train, test)
 logger.info('Feature Enginnering Completed...')
 with timer(f'Running Wavenet model'):
logger.info(f'Training Wavenet model with {SPLITS} folds of GroupKFold Started...')
 run_cv_model_by_batch(train, test, splits=SPLITS, batch_col='group', feats=feats, sample_submission=sample_submission, nn_epochs=EPOCHS, nn_batch_size=NNBATCHSIZE)
 logger.info(f'Training completed...')
<load_from_csv>
preds = random_forest.predict(test[['haversine', 'abs_lat_diff', 'abs_lon_diff', 'passenger_count']])
 sub = pd.DataFrame({'key': test_id, 'fare_amount': preds})
 sub.to_csv('sub_rf_simple.csv', index = False)
 sns.distplot(sub['fare_amount'])
 plt.title('Distribution of Random Forest Predicted Fare Amount');
train = pd.read_csv('.. /input/data-without-drift/train_clean.csv')
 test = pd.read_csv('.. /input/data-without-drift/test_clean.csv')
 train.head()<choose_model_class>
sub.loc[simple_over_100]
def Kalman1D(observations,damping=1):
observation_covariance = damping
initial_value_guess = observations[0]
transition_matrix = 1
transition_covariance = 0.1
initial_value_guess
kf = KalmanFilter(
 initial_state_mean=initial_value_guess,
initial_state_covariance=observation_covariance,
observation_covariance=observation_covariance,
transition_covariance=transition_covariance,
transition_matrices=transition_matrix 
)
 pred_state, state_cov = kf.smooth(observations)
 return pred_state<define_variables>
lr_tpred = lr.predict(X_train[['haversine', 'abs_lat_diff', 'abs_lon_diff', 'passenger_count']])
 rf_tpred = random_forest.predict(X_train[['haversine', 'abs_lat_diff', 'abs_lon_diff', 'passenger_count']])
 lr_pred = lr.predict(X_valid[['haversine', 'abs_lat_diff', 'abs_lon_diff', 'passenger_count']])
 rf_pred = random_forest.predict(X_valid[['haversine', 'abs_lat_diff', 'abs_lon_diff', 'passenger_count']])
 train_pred =(lr_tpred + rf_tpred)/ 2
valid_pred =(lr_pred + rf_pred)/ 2
tr, vr, tm, vm = metrics(train_pred, valid_pred, y_train, y_valid)
 print(f'Combined Training: rmse = {round(tr, 2)} \t mape = {round(tm, 2)}')
 print(f'Combined Validation: rmse = {round(vr, 2)} \t mape = {round(vm, 2)}' )
batch_size = 500000
num_batches = 10
res = 1000
fs = 10000
nyq = 0.5 * fs<create_dataframe>
def model_rf(X_train, X_valid, y_train, y_valid, test, features,
model = RandomForestRegressor(n_estimators = 20, max_depth = 20,
n_jobs = -1),
return_model = False):

 model.fit(X_train[features], y_train)
 evaluate(model, features, X_train, X_valid, y_train, y_valid)
 preds = model.predict(test[features])
 sub = pd.DataFrame({'key': test_id, 'fare_amount': preds})
 feature_importances = pd.DataFrame({'feature': features,
'importance': model.feature_importances_} ).\
sort_values('importance', ascending = False ).set_index('feature')
 if return_model:
return sub, feature_importances, model
return sub, feature_importances
observation_covariance =.0015
signal_kalman_train = Kalman1D(train.signal.values,observation_covariance)
 kalman = pd.DataFrame(signal_kalman_train,columns=['signal'] )<statistical_test>
sub, fi = model_rf(X_train, X_valid, y_train, y_valid, test,
features = ['abs_lat_diff', 'abs_lon_diff', 'haversine', 'passenger_count',
'pickup_latitude', 'pickup_longitude', 'dropoff_latitude', 'dropoff_longitude'] )
f1_unfiltered = []
f1_unfiltered_avg = []
f1_filtered = []
f1_filtered_avg = []
for batch in range(1,10,1):
p_trans = calc_markov_p_trans(train.open_channels[batch_size*(batch-1):batch_size*batch])
 p_signal_unfiltered, signal_bins = calc_markov_p_signal(train.open_channels[batch_size*(batch-1):batch_size*batch], train.signal[batch_size*(batch-1):batch_size*batch])
 signal_dig = digitize_signal(train.signal[batch_size*(batch-1):batch_size*batch], signal_bins)
 p_in = np.ones(len(p_trans)) / len(p_trans)
 viterbi_state = viterbi(p_trans, p_signal_unfiltered, p_in, signal_dig)
 print(f'Batch = {batch}')
 f1_unfiltered = f1_score(y_pred=viterbi_state, y_true=train.open_channels[batch_size*(batch-1):batch_size*batch], average='macro')
 print("Unfiltered - F1 macro =", f1_unfiltered)
 f1_unfiltered_avg += f1_unfiltered
p_trans = calc_markov_p_trans(train.open_channels[batch_size*(batch-1):batch_size*batch])
 p_signal_filtered, signal_bins = calc_markov_p_signal(train.open_channels[batch_size*(batch-1):batch_size*batch], kalman.signal[batch_size*(batch-1):batch_size*batch])
 signal_dig = digitize_signal(kalman.signal[batch_size*(batch-1):batch_size*batch], signal_bins)
 p_in = np.ones(len(p_trans)) / len(p_trans)
 viterbi_state = viterbi(p_trans, p_signal_filtered, p_in, signal_dig)
 f1_filtered = f1_score(y_pred=viterbi_state, y_true=train.open_channels[batch_size*(batch-1):batch_size*batch], average='macro')
 print("Kalman Filtered - F1 macro =", f1_filtered)
 f1_filtered_avg += f1_filtered<feature_engineering>
sub.to_csv('sub_rf_8_features.csv', index = False)
 sub['fare_amount'].plot.hist() ;
train['signal_undrifted'] = signal_kalman_train
signal_kalman_test = Kalman1D(test.signal.values,observation_covariance)
 test['signal_undrifted'] = signal_kalman_test<feature_engineering>
def extract_dateinfo(df, date_col, drop=True, time=False,
start_ref = pd.datetime(1900, 1, 1),
extra_attr = False):

 df = df.copy()
fld = df[date_col]
fld_dtype = fld.dtype
if isinstance(fld_dtype, pd.core.dtypes.dtypes.DatetimeTZDtype):
fld_dtype = np.datetime64
if not np.issubdtype(fld_dtype, np.datetime64):
df[date_col] = fld = pd.to_datetime(fld, infer_datetime_format=True)
 pre = re.sub('[Dd]ate', '', date_col)
 pre = re.sub('[Tt]ime', '', pre)
 attr = ['Year', 'Month', 'Week', 'Day', 'Dayofweek', 'Dayofyear', 'Days_in_month', 'is_leap_year']
if extra_attr:
attr = attr + ['Is_month_end', 'Is_month_start', 'Is_quarter_end',
'Is_quarter_start', 'Is_year_end', 'Is_year_start']
if time:
attr = attr + ['Hour', 'Minute', 'Second']
for n in attr:
df[pre + n] = getattr(fld.dt, n.lower())
 df[pre + 'Days_in_year'] = df[pre + 'is_leap_year'] + 365
if time:
df[pre + 'frac_day'] =(( df[pre + 'Hour'])+(df[pre + 'Minute'] / 60)+(df[pre + 'Second'] / 60 / 60)) / 24
df[pre + 'frac_week'] =(df[pre + 'Dayofweek'] + df[pre + 'frac_day'])/ 7
df[pre + 'frac_month'] =(df[pre + 'Day'] +(df[pre + 'frac_day'])) /(df[pre + 'Days_in_month'] + 1)
 df[pre + 'frac_year'] =(df[pre + 'Dayofyear'] + df[pre + 'frac_day'])/(df[pre + 'Days_in_year'] + 1)
 df[pre + 'Elapsed'] =(fld - start_ref ).dt.total_seconds()
if drop:
df = df.drop(date_col, axis=1)
 return df
def features(df):
df = df.sort_values(by=['time'] ).reset_index(drop=True)
 df.index =(( df.time * 10_000)- 1 ).values
df['batch'] = df.index // 50_000
df['batch_index'] = df.index -(df.batch * 50_000)
 df['batch_slices'] = df['batch_index'] // 5_000
df['batch_slices2'] = df.apply(lambda r: '_'.join([str(r['batch'] ).zfill(3), str(r['batch_slices'] ).zfill(3)]), axis=1)
 for c in ['batch','batch_slices2']:
d = {}
d['mean'+c] = df.groupby([c])['signal_undrifted'].mean()
d['median'+c] = df.groupby([c])['signal_undrifted'].median()
d['max'+c] = df.groupby([c])['signal_undrifted'].max()
d['min'+c] = df.groupby([c])['signal_undrifted'].min()
d['std'+c] = df.groupby([c])['signal_undrifted'].std()
d['mean_abs_chg'+c] = df.groupby([c])['signal_undrifted'].apply(lambda x: np.mean(np.abs(np.diff(x))))
d['abs_max'+c] = df.groupby([c])['signal_undrifted'].apply(lambda x: np.max(np.abs(x)))
 d['abs_min'+c] = df.groupby([c])['signal_undrifted'].apply(lambda x: np.min(np.abs(x)))
 for v in d:
df[v] = df[c].map(d[v].to_dict())
 df['range'+c] = df['max'+c] - df['min'+c]
df['maxtomin'+c] = df['max'+c] / df['min'+c]
df['abs_avg'+c] =(df['abs_min'+c] + df['abs_max'+c])/ 2
df['signal_shift_+1'] = [0,] + list(df['signal_undrifted'].values[:-1])
 df['signal_shift_-1'] = list(df['signal_undrifted'].values[1:])+ [0]
for i in df[df['batch_index']==0].index:
df['signal_shift_+1'][i] = np.nan
for i in df[df['batch_index']==49999].index:
df['signal_shift_-1'][i] = np.nan
df['signal_shift_+2'] = [0,] + [1,] + list(df['signal_undrifted'].values[:-2])
 df['signal_shift_-2'] = list(df['signal_undrifted'].values[2:])+ [0] + [1]
for i in df[df['batch_index']==0].index:
df['signal_shift_+2'][i] = np.nan
for i in df[df['batch_index']==1].index:
df['signal_shift_+2'][i] = np.nan
for i in df[df['batch_index']==49999].index:
df['signal_shift_-2'][i] = np.nan
for i in df[df['batch_index']==49998].index:
df['signal_shift_-2'][i] = np.nan
for c in [c1 for c1 in df.columns if c1 not in ['time', 'signal_undrifted', 'open_channels', 'batch', 'batch_index', 'batch_slices', 'batch_slices2']]:
df[c+'_msignal'] = df[c] - df['signal_undrifted']
return df
train = features(train)
 test = features(test )<compute_train_metric>
test = extract_dateinfo(test, 'pickup_datetime', drop = False,
time = True, start_ref = data['pickup_datetime'].min())
 test.head()
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 train_model_classification(X, X_test, y, params, model_type='lgb', eval_metric='f1score',
columns=None, plot_feature_importance=False, model=None,
verbose=50, early_stopping_rounds=200, n_estimators=2000):
columns = X.columns if columns == None else columns
X_test = X_test[columns]
metrics_dict = {
'f1score': {'lgb_metric_name': lgb_Metric,}
}
result_dict = {}
oof = np.zeros(len(X))
prediction = np.zeros(( len(X_test)))
 scores = []
feature_importance = pd.DataFrame()

 if True:
X_train, X_valid, y_train, y_valid = model_selection.train_test_split(X, y, test_size=0.3, random_state=7)
 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, num_iteration=model.best_iteration)
 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))
y_pred = model.predict(xgb.DMatrix(X_test))
if model_type == 'cat':
model = CatBoostRegressor(params)
 model.fit(X_train, y_train)
 y_pred_valid = np.round(np.clip(preds, 0, 10)).astype(int)
 y_pred = model.predict(X_test, num_iteration=model.best_iteration)
 y_pred = np.round(np.clip(y_pred, 0, 10)).astype(int)
 oof = preds
scores.append(f1_score_calc(y, np.round(np.clip(preds,0,10)).astype(int)))
 prediction += y_pred
if model_type == 'lgb' and plot_feature_importance:
fold_importance = pd.DataFrame()
fold_importance["feature"] = columns
fold_importance["importance"] = model.feature_importances_
fold_importance["fold"] = fold_n + 1
feature_importance = pd.concat([feature_importance, fold_importance], axis=0)
 print('FINAL score: {0:.4f}, std: {1:.4f}.'.format(np.mean(scores), np.std(scores)))
 result_dict['oof'] = oof
result_dict['prediction'] = prediction
result_dict['scores'] = scores
result_dict['model'] = model
if model_type == 'lgb':
if plot_feature_importance:
feature_importance["importance"] /= folds.n_splits
cols = feature_importance[["feature", "importance"]].groupby("feature" ).mean().sort_values(
 by="importance", ascending=False)[:50].index
best_features = feature_importance.loc[feature_importance.feature.isin(cols)]
plt.figure(figsize=(16, 12)) ;
sns.barplot(x="importance", y="feature", data=best_features.sort_values(by="importance", ascending=False)) ;
plt.title('LGB Features(avg over folds)');
result_dict['feature_importance'] = feature_importance
return result_dict<prepare_x_and_y>
fare_counts = data.groupby('fare_amount')['haversine'].agg(['count', pd.Series.nunique] ).sort_values('count', ascending = False)
 fare_counts.head()
good_columns = [c for c in train.columns if c not in ['time', 'signal','open_channels', 'batch', 'batch_index', 'batch_slices', 'batch_slices2']]
X = train[good_columns].copy()
y = train['open_channels']
X_test = test[good_columns].copy()
del train, test<train_model>
X_train, X_valid, y_train, y_valid = train_test_split(data, np.array(data['fare_amount']),
stratify = data['fare-bin'],
random_state = RSEED, test_size = 1_000_000 )
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_classification(X=X[0:500000*8-1], X_test=X_test, y=y[0:500000*8-1], params=params_xgb, model_type='xgb', eval_metric='f1score', plot_feature_importance=False,
verbose=50, early_stopping_rounds=250 )<train_model>
time_features = ['pickup_frac_day', 'pickup_frac_week', 'pickup_frac_year', 'pickup_Elapsed']
features = ['abs_lat_diff', 'abs_lon_diff', 'haversine', 'passenger_count',
'pickup_latitude', 'pickup_longitude',
'dropoff_latitude', 'dropoff_longitude'] + time_features
params_lgb = {'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_classification(X=X[0:500000*8-1], X_test=X_test, y=y[0:500000*8-1], params=params_lgb, model_type='lgb', eval_metric='f1score', plot_feature_importance=False,
verbose=50, early_stopping_rounds=250, n_estimators=3000 )<define_variables>
sub, fi = model_rf(X_train, X_valid, y_train, y_valid, test,
features = features )
preds_ensemble = 0.50 * result_dict_lgb['prediction'] + 0.50 * result_dict_xgb['prediction']<save_to_csv>
lr = LinearRegression()
lr.fit(X_train[features], y_train)
 evaluate(lr, features, X_train, X_valid, y_train, y_valid )
sub = pd.read_csv('/kaggle/input/liverpool-ion-switching/sample_submission.csv')
 sub['open_channels'] = np.array(np.round(preds_ensemble,0), np.int)
 sub.to_csv('submission.csv', index=False, float_format='%.4f')
 sub.head(10 )<import_modules>
sub.to_csv('sub_rf_frac_time.csv', index = False )
import numpy as np
import pandas as pd
from keras.utils.np_utils import to_categorical
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split<load_from_csv>
features = list(data.columns)
 for f in ['pickup_datetime', 'fare_amount', 'fare-bin', 'color']:
features.remove(f)
 len(features )
df=pd.read_csv('.. /input/liverpool-ion-switching/train.csv')
 test = pd.read_csv('.. /input/liverpool-ion-switching/test.csv' )<compute_test_metric>
sub, fi, random_forest = model_rf(X_train, X_valid, y_train, y_valid, test,
features = features, return_model = True )
def log_posterior(theta, X, p_mean, p_cov, x_var):
inv_cov = np.linalg.inv(p_cov)
 determinant = np.prod(np.diagonal(p_cov))
prior = np.log(1./(2*np.pi*np.sqrt(determinant)))
 ll_constant = 1./(np.sqrt(2 * np.pi * x_var))
L = 0.5 * np.exp(-(1./(2*x_var)) *(X - theta[0])**2)+ \
0.5 * np.exp(-(1./(2*x_var)) *(X -(theta[0]+theta[1])) **2)
 L *= ll_constant
log_likelihood = np.sum(np.log(L))
assert not np.isnan(prior + log_likelihood)
 return np.squeeze(prior + log_likelihood)
 def get_noise(eps):

 return(np.random.multivariate_normal(np.array([0,0]), eps*np.eye(2)) ).reshape(( 2,1))
def get_info_for_contour(K=100):

 xlist = np.linspace(-1.5, 2.5, num=K)
 ylist = np.linspace(-3, 3, num=K)
 X_a,Y_a = np.meshgrid(xlist, ylist)
 Z_a = np.zeros(( K,K))
for i in range(K):
for j in range(K):
theta = np.array([[X_a[i,j]],[Y_a[i,j]]])
 Z_a[i,j] = log_posterior(theta, X, prior_mean, prior_cov, sigmax_sq)
 return X_a, Y_a, Z_a<split>
sub.to_csv('sub_rf_all_features.csv', index = False )
data = pd.merge(df, test)
 x_train, y_train, x_test, y_test = train_test_split(df.head(2000000), test, test_size=0.15 )<save_to_csv>
param_grid = {
'n_estimators': np.linspace(10, 100 ).astype(int),
'max_depth': [None] + list(np.linspace(5, 30 ).astype(int)) ,
'max_features': ['auto', 'sqrt', None] + list(np.arange(0.5, 1, 0.1)) ,
'max_leaf_nodes': [None] + list(np.linspace(10, 50, 500 ).astype(int)) ,
'min_samples_split': [2, 5, 10],
'bootstrap': [True, False]
}
estimator = RandomForestRegressor(random_state = RSEED)
 rs = RandomizedSearchCV(estimator, param_grid, n_jobs = -1,
scoring = 'neg_mean_absolute_error', cv = 3,
n_iter = 100, verbose = 1, random_state=RSEED )
def logistic_run() :
clf = LogisticRegression(random_state=0)
 clf.fit(np.array(x_train.head(300000)['open_channels'] ).reshape(-1, 1), np.array(y_train.open_channels ).reshape(-1, 1))
x = clf.predict(np.array(df['open_channels'] ).reshape(-1, 1))
x = np.array(x)
 x = pd.DataFrame({'time': df.time, 'open_channels': x})
 x.to_csv('log_sub.csv')
 x.head()<define_variables>
tune_data = data.sample(100_000, random_state = RSEED)
 time_features = ['pickup_frac_day', 'pickup_frac_week', 'pickup_frac_year', 'pickup_Elapsed']
features = ['abs_lat_diff', 'abs_lon_diff', 'haversine', 'passenger_count',
'pickup_latitude', 'pickup_longitude',
'dropoff_latitude', 'dropoff_longitude'] + time_features
rs.fit(tune_data[features], np.array(tune_data['fare_amount']))
TARGET = 'open_channels'
TOTAL_FOLDS = 5
RANDOM_SEED = 529
MODEL_TYPE = 'LGBM'
LEARNING_RATE = 0.009
SHUFFLE = True
NUM_BOOST_ROUND = 500_000
EARLY_STOPPING_ROUNDS = 100
N_THREADS = -1
OBJECTIVE = 'regression'
METRIC = 'rmse'
NUM_LEAVES = 2**8+1
MAX_DEPTH = -1
FEATURE_FRACTION = 1
BAGGING_FRACTION = 1
BAGGING_FREQ = 0
train = pd.read_csv('.. /input/liverpool-ion-switching/train.csv')
 test = pd.read_csv('.. /input/liverpool-ion-switching/test.csv')
 ss = pd.read_csv('.. /input/liverpool-ion-switching/sample_submission.csv')
 train['train'] = True
test['train'] = False
tt = pd.concat([train, test], sort=False ).reset_index(drop=True)
 tt['train'] = tt['train'].astype('bool')
 run_id = "{:%m%d_%H%M}".format(datetime.now())
 def update_tracking(
 run_id, field, value, csv_file="./tracking.csv",
integer=False, digits=None, nround=6,
drop_broken_runs=False):

 try:
df = pd.read_csv(csv_file, index_col=[0])
 except:
df = pd.DataFrame()
if drop_broken_runs:
df = df.dropna(subset=['1_f1'])
 if integer:
value = round(value)
 elif digits is not None:
value = round(value, digits)
 df.loc[run_id, field] = value
df = df.round(nround)
 df.to_csv(csv_file)
 update_tracking(run_id, 'model_type', MODEL_TYPE)
 update_tracking(run_id, 'seed', RANDOM_SEED, integer=True)
 update_tracking(run_id, 'nfolds', TOTAL_FOLDS, integer=True)
 update_tracking(run_id, 'lr', LEARNING_RATE)
 update_tracking(run_id, 'shuffle', SHUFFLE)
 update_tracking(run_id, 'boost_rounds', NUM_BOOST_ROUND)
 update_tracking(run_id, 'es_rounds', EARLY_STOPPING_ROUNDS)
 update_tracking(run_id, 'threads', N_THREADS)
 update_tracking(run_id, 'objective', OBJECTIVE)
 update_tracking(run_id, 'metric', METRIC)
 update_tracking(run_id, 'num_leaves', NUM_LEAVES)
 update_tracking(run_id, 'max_depth', MAX_DEPTH)
 update_tracking(run_id, 'feature_fraction', FEATURE_FRACTION)
 update_tracking(run_id, 'bagging_fraction', BAGGING_FRACTION)
 update_tracking(run_id, 'bagging_freq', BAGGING_FREQ)
 tt = tt.sort_values(by=['time'] ).reset_index(drop=True)
 tt.index =(( tt.time * 10_000)- 1 ).values
tt['batch'] = tt.index // 50_000
tt['batch_index'] = tt.index -(tt.batch * 50_000)
 tt['batch_slices'] = tt['batch_index'] // 5_000
tt['batch_slices2'] = tt.apply(lambda r: '_'.join(
 [str(r['batch'] ).zfill(3), str(r['batch_slices'] ).zfill(3)]), axis=1)
 tt['signal_batch_min'] = tt.groupby('batch')['signal'].transform('min')
 tt['signal_batch_max'] = tt.groupby('batch')['signal'].transform('max')
 tt['signal_batch_std'] = tt.groupby('batch')['signal'].transform('std')
 tt['signal_batch_mean'] = tt.groupby('batch')['signal'].transform('mean')
 tt['mean_abs_chg_batch'] = tt.groupby(['batch'])['signal'].transform(
 lambda x: np.mean(np.abs(np.diff(x))))
tt['abs_max_batch'] = tt.groupby(
 ['batch'])['signal'].transform(lambda x: np.max(np.abs(x)))
 tt['abs_min_batch'] = tt.groupby(
 ['batch'])['signal'].transform(lambda x: np.min(np.abs(x)))
 tt['range_batch'] = tt['signal_batch_max'] - tt['signal_batch_min']
tt['maxtomin_batch'] = tt['signal_batch_max'] / tt['signal_batch_min']
tt['abs_avg_batch'] =(tt['abs_min_batch'] + tt['abs_max_batch'])/ 2
tt['signal_batch_5k_min'] = tt.groupby(
 'batch_slices2')['signal'].transform('min')
 tt['signal_batch_5k_max'] = tt.groupby(
 'batch_slices2')['signal'].transform('max')
 tt['signal_batch_5k_std'] = tt.groupby(
 'batch_slices2')['signal'].transform('std')
 tt['signal_batch_5k_mean'] = tt.groupby(
 'batch_slices2')['signal'].transform('mean')
 tt['mean_abs_chg_batch_5k'] = tt.groupby(['batch_slices2'])[
'signal'].transform(lambda x: np.mean(np.abs(np.diff(x))))
tt['abs_max_batch_5k'] = tt.groupby(['batch_slices2'])[
'signal'].transform(lambda x: np.max(np.abs(x)))
 tt['abs_min_batch_5k'] = tt.groupby(['batch_slices2'])[
'signal'].transform(lambda x: np.min(np.abs(x)))
 tt['range_batch_5k'] = tt['signal_batch_5k_max'] - tt['signal_batch_5k_min']
tt['maxtomin_batch_5k'] = tt['signal_batch_5k_max'] / tt['signal_batch_5k_min']
tt['abs_avg_batch_5k'] =(tt['abs_min_batch_5k'] + tt['abs_max_batch_5k'])/ 2
tt['signal_shift+1'] = tt.groupby(['batch'] ).shift(1)['signal']
tt['signal_shift-1'] = tt.groupby(['batch'] ).shift(-1)['signal']
tt['signal_shift+2'] = tt.groupby(['batch'] ).shift(2)['signal']
tt['signal_shift-2'] = tt.groupby(['batch'] ).shift(-2)['signal']
for c in ['signal_batch_min', 'signal_batch_max',
'signal_batch_std', 'signal_batch_mean',
'mean_abs_chg_batch', 'abs_max_batch',
'abs_min_batch',
'range_batch', 'maxtomin_batch', 'abs_avg_batch',
'signal_shift+1', 'signal_shift-1',
'signal_batch_5k_min', 'signal_batch_5k_max',
'signal_batch_5k_std',
'signal_batch_5k_mean', 'mean_abs_chg_batch_5k',
'abs_max_batch_5k', 'abs_min_batch_5k',
'range_batch_5k', 'maxtomin_batch_5k',
'abs_avg_batch_5k','signal_shift+2','signal_shift-2']:
tt[f'{c}_msignal'] = tt[c] - tt['signal']
FEATURES = ['signal',
'signal_batch_min',
'signal_batch_max',
'signal_batch_std',
'signal_batch_mean',
'mean_abs_chg_batch',
'range_batch',
'maxtomin_batch',
'signal_batch_5k_min',
'signal_batch_5k_max',
'signal_batch_5k_std',
'signal_batch_5k_mean',
'mean_abs_chg_batch_5k',
'abs_max_batch_5k',
'abs_min_batch_5k',
'range_batch_5k',
'maxtomin_batch_5k',
'abs_avg_batch_5k',
'signal_shift+1',
'signal_shift-1',
'signal_batch_max_msignal',
'signal_batch_std_msignal',
'mean_abs_chg_batch_msignal',
'abs_max_batch_msignal',
'abs_min_batch_msignal',
'range_batch_msignal',
'maxtomin_batch_msignal',
'abs_avg_batch_msignal',
'signal_shift+1_msignal',
'signal_shift-1_msignal',
'signal_batch_5k_min_msignal',
'signal_batch_5k_max_msignal',
'signal_batch_5k_std_msignal',
'signal_batch_5k_mean_msignal',
'mean_abs_chg_batch_5k_msignal',
'abs_max_batch_5k_msignal',
'abs_min_batch_5k_msignal',
'maxtomin_batch_5k_msignal',
'abs_avg_batch_5k_msignal',
'signal_shift+2',
'signal_shift-2']
print('.... : FEATURE LIST :.... ')
 print([f for f in FEATURES])
 update_tracking(run_id, 'n_features', len(FEATURES), integer=True)
 update_tracking(run_id, 'target', TARGET)
 def lgb_Metric(preds, dtrain):
labels = dtrain.get_label()
print(preds.shape)
 print(preds)
 preds = np.argmax(preds, axis=0)
 score = f1_score(labels, preds, average='macro')
 return('KaggleMetric', score, True)
 tt['train'] = tt['train'].astype('bool')
 train = tt.query('train' ).copy()
test = tt.query('not train' ).copy()
train['open_channels'] = train['open_channels'].astype(int)
 X = train[FEATURES]
X_test = test[FEATURES]
y = train[TARGET].values
sub = test[['time']].copy()
groups = train['batch']
if OBJECTIVE == 'multiclass':
NUM_CLASS = 11
else:
NUM_CLASS = 1
params = {'learning_rate': LEARNING_RATE,
'max_depth': MAX_DEPTH,
'num_leaves': NUM_LEAVES,
'feature_fraction': FEATURE_FRACTION,
'bagging_fraction': BAGGING_FRACTION,
'bagging_freq': BAGGING_FREQ,
'n_jobs': N_THREADS,
'seed': RANDOM_SEED,
'metric': METRIC,
'objective': OBJECTIVE,
'num_class': NUM_CLASS
}
kfold = KFold(n_splits=TOTAL_FOLDS, shuffle=SHUFFLE, random_state=RANDOM_SEED)
 oof_df = train[['signal', 'open_channels']].copy()
fi_df = pd.DataFrame(index=FEATURES)
 fold = 1
for tr_idx, val_idx in kfold.split(X, y, groups=groups):
print(f'====== Fold {fold:0.0f} of {TOTAL_FOLDS} ======')
 X_tr, X_val = X.iloc[tr_idx], X.iloc[val_idx]
y_tr, y_val = y[tr_idx], y[val_idx]
train_set = lgb.Dataset(X_tr, y_tr)
 val_set = lgb.Dataset(X_val, y_val)
 model = lgb.train(params,
train_set,
num_boost_round=NUM_BOOST_ROUND,
early_stopping_rounds=EARLY_STOPPING_ROUNDS,
valid_sets=[train_set, val_set],
verbose_eval=50)
 if OBJECTIVE == 'multi_class':
preds = model.predict(X_val, num_iteration=model.best_iteration)
 preds = np.argmax(preds, axis=1)
 test_preds = model.predict(X_test, num_iteration=model.best_iteration)
 test_preds = np.argmax(test_preds, axis=1)
 elif OBJECTIVE == 'regression':
preds = model.predict(X_val, num_iteration=model.best_iteration)
 preds = np.round(np.clip(preds, 0, 10)).astype(int)
 test_preds = model.predict(X_test, num_iteration=model.best_iteration)
 test_preds = np.round(np.clip(test_preds, 0, 10)).astype(int)
 oof_df.loc[oof_df.iloc[val_idx].index, 'oof'] = preds
sub[f'open_channels_fold{fold}'] = test_preds
f1 = f1_score(oof_df.loc[oof_df.iloc[val_idx].index]['open_channels'],
oof_df.loc[oof_df.iloc[val_idx].index]['oof'],
average='macro')
 rmse = np.sqrt(mean_squared_error(oof_df.loc[oof_df.index.isin(val_idx)]['open_channels'],
oof_df.loc[oof_df.index.isin(val_idx)]['oof']))
update_tracking(run_id, f'{fold}_best_iter', model.best_iteration, integer=True)
 update_tracking(run_id, f'{fold}_rmse', rmse)
 update_tracking(run_id, f'{fold}_f1', f1)
 fi_df[f'importance_{fold}'] = model.feature_importance()
print(f'Fold {fold} - validation f1: {f1:0.5f}')
 print(f'Fold {fold} - validation rmse: {rmse:0.5f}')
 fold += 1
oof_f1 = f1_score(oof_df['open_channels'],
oof_df['oof'],
average='macro')
 oof_rmse = np.sqrt(mean_squared_error(oof_df['open_channels'],
oof_df['oof']))
update_tracking(run_id, f'oof_f1', oof_f1)
 update_tracking(run_id, f'oof_rmse', oof_rmse)
 s_cols = [s for s in sub.columns if 'open_channels' in s]
sub['open_channels'] = sub[s_cols].median(axis=1 ).astype(int)
 sub.to_csv(f'./pred_x_{oof_f1:0.6}.csv', index=False)
 sub[['time', 'open_channels']].to_csv(f'./sub_x_{oof_f1:0.10f}.csv',
index=False,
float_format='%0.4f')
 oof_df.to_csv(f'./oof_x_{oof_f1:0.6}.csv', index=False)
 fi_df['importance'] = fi_df.sum(axis=1)
 fi_df.to_csv(f'./fi_x_{oof_f1:0.6}.csv', index=True)
 fig, ax = plt.subplots(figsize=(15, 30))
fi_df.sort_values('importance')['importance'] \ 
.plot(kind='barh',
figsize=(15, 30),
title=f'x - Feature Importance',
ax=ax)
 plt.savefig(f'./x__{oof_f1:0.6}.png' )<install_modules>
model = rs.best_estimator_
print(f'The best parameters were {rs.best_params_} with a negative mae of {rs.best_score_}' )
!pip install pyhmc --quiet
!pip install triangle --quiet<import_modules>
model.n_jobs = -1
model.fit(X_train[features], y_train)
 evaluate(model, features, X_train, X_valid, y_train, y_valid )
import pyhmc
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import xgboost as xgb<load_from_csv>
pred = np.array(model.predict(test[features])).reshape(( -1))
sub = pd.DataFrame({'key': test_id, 'fare_amount': pred})
 sub.to_csv('sub_rf_tuned.csv', index = False)
 sub['fare_amount'].plot.hist() ;
plt.title('Predicted Test Fare Distribution');