Kaggle competition: Instacart Market Basket Analysis
<SOS> metric: MeanFScore Kaggle data source: instacart-market-basket-analysis<split>
gc.enable()
labels = df_train['target'][:len(features)]
train_features, test_features, train_labels, test_labels = train_test_split(features, labels )<train_on_grid>
orders = pd.read_csv('.. /input/orders.csv')
 order_products_train = pd.read_csv('.. /input/order_products__train.csv')
 order_products_prior = pd.read_csv('.. /input/order_products__prior.csv')
 products = pd.read_csv('.. /input/products.csv')
 aisles = pd.read_csv('.. /input/aisles.csv')
 departments = pd.read_csv('.. /input/departments.csv' )
clf = GradientBoostingClassifier(random_state=0)
 clf.fit(train_features, train_labels)
 clf.score(test_features, test_labels )<predict_on_test>
aisles['aisle'] = aisles['aisle'].astype('category')
 departments['department'] = departments['department'].astype('category')
 orders['eval_set'] = orders['eval_set'].astype('category')
 products['product_name'] = products['product_name'].astype('category' )
pred_val = clf.predict_proba(pd.DataFrame(test_features)) [:,1]
fpr, tpr, _ = roc_curve(test_labels, pred_val)
 roc_auc = auc(fpr, tpr )<load_from_csv>
op = orders.merge(order_products_prior, on='order_id', how='inner')
 op.head()
test_df = pd.read_csv('.. /input/nlp-getting-started/test.csv')
 test_tokenized = test_df['text'].apply(( lambda x: tokenizer.encode(x, add_special_tokens=True)))
 test_max_len = 0
for i in test_tokenized.values:
if len(i)> test_max_len:
test_max_len = len(i)
 test_padded = np.array([i + [0]*(test_max_len-len(i)) for i in test_tokenized.values])
 test_attention_mask = np.where(test_padded != 0, 1, 0)
 test_input_ids = torch.tensor(test_padded)
 test_attention_mask = torch.tensor(test_attention_mask)
 test_dataset = Dataset(input_ids=test_input_ids, attention_masks=test_attention_mask)
 test_dataloader = torch.utils.data.DataLoader(test_dataset, batch_size=8, drop_last=False )<categorify>
pbp = op.groupby(['user_id','product_id'])['product_id'].count().to_frame('times_p_order')
 pbp = pbp.reset_index()
pbp_new = pbp.groupby('user_id')['times_p_order'].sum().to_frame('total_products')
 pbp= pd.merge(pbp,pbp_new,on=["user_id"],how="left")
 pbp["%_of_bought_products_in_total"] = pbp.times_p_order/pbp.total_products
del pbp_new
test_features = []
for batch_input_ids, batch_attention_mask in tqdm(test_dataloader):
with torch.no_grad() :
tmp = model(batch_input_ids, attention_mask=batch_attention_mask)
 test_features.extend(tmp[0][:,0,:].numpy())
 clear_output(True )<save_to_csv>
grouped=op.groupby("product_id")["reordered"].count().to_frame("number_of_reorders" ).reset_index()
pbp = pd.merge(pbp,grouped,on="product_id",how="left")
 del grouped
predict = clf.predict(pd.DataFrame(test_features))
sub = pd.read_csv('.. /input/nlp-getting-started/sample_submission.csv')
 sub['target'] = predict
sub.to_csv('sample_submission.csv', index=False )<load_pretrained>
mean_time_order=op.groupby(['user_id','product_id'])['order_hour_of_day'].mean().to_frame('mean_time_of_order' ).round(decimals=0)
 pbp=pd.merge(pbp,mean_time_order,on=['user_id','product_id'],how="left")
 del mean_time_order
model_class, tokenizer_class, pretrained_weights =(ppb.BertForSequenceClassification, ppb.BertTokenizer, 'bert-base-cased')
 tokenizer = tokenizer_class.from_pretrained(pretrained_weights)
 model = model_class.from_pretrained(pretrained_weights)
 tokenized = df_train['text'].apply(( lambda x: tokenizer.encode(x, add_special_tokens=True)))
 max_len = 0
for i in tokenized.values:
if len(i)> max_len:
max_len = len(i)
 padded = np.array([i + [0]*(max_len-len(i)) for i in tokenized.values])
 attention_mask = np.where(padded != 0, 1, 0 )<set_options>
mean_prod = op.groupby(['user_id','order_number'])['add_to_cart_order'].max().to_frame("product_number_per_order")
 mean_prod = mean_prod.groupby("user_id")["product_number_per_order"].mean().to_frame("mean_product_cart" ).reset_index()
pbp=pd.merge(pbp,mean_prod,on="user_id",how="left")
 del mean_prod
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu' )<categorify>
mean_cart_ord = op.groupby(['user_id', 'product_id'])['add_to_cart_order'].mean().to_frame("mean_preference" ).reset_index()
pbp=pd.merge(pbp,mean_cart_ord,on=['user_id', 'product_id'],how="left")
 pbp.head()
del mean_cart_ord
class Dataset(torch.utils.data.Dataset):
def __init__(self, input_ids, attention_masks, labels):
self.input_ids = input_ids
self.attention_mask = attention_masks
self.labels = labels
def __len__(self):
return len(self.input_ids)
 def __getitem__(self, index):
input_ids = self.input_ids[index]
attention_mask = self.attention_mask[index]
labels = self.labels[index]
return input_ids, attention_mask, labels<create_dataframe>
pbp = pbp.drop(["times_p_order","total_products"],axis=1)
 pbp.head()
train_input_ids = torch.tensor(padded[:7000])
 train_attention_mask = torch.tensor(attention_mask[:7000])
 train_labels = list(df_train['target'][:7000])
 validation_input_ids = torch.tensor(padded[7000:7613])
 validation_attention_mask = torch.tensor(attention_mask[7000:7613])
 validation_labels = list(df_train['target'][7000:7613])
 dataset = Dataset(input_ids=train_input_ids, attention_masks=train_attention_mask, labels=train_labels)
 dataloader = torch.utils.data.DataLoader(dataset, batch_size=8, drop_last=False)
 validation_dataset = Dataset(input_ids=validation_input_ids, attention_masks=validation_attention_mask, labels=validation_labels)
 validation_dataloader = torch.utils.data.DataLoader(validation_dataset, batch_size=8, drop_last=False )<choose_model_class>
user = op.groupby('user_id')['order_number'].max().to_frame('u_total_orders')
 user.head()
optimizer = AdamW(model.parameters() , lr = 2e-5, eps = 1e-8)
 epochs = 2
total_steps = len(dataloader)* epochs
scheduler = get_linear_schedule_with_warmup(optimizer,
num_warmup_steps = 0,
num_training_steps = total_steps )<compute_test_metric>
user = user.merge(u_reorder, on='user_id', how='left')
 del u_reorder
gc.collect()
user.head()
def flat_accuracy(preds, labels):
pred_flat = np.argmax(preds, axis=1 ).flatten()
labels_flat = labels.flatten()
return np.sum(pred_flat == labels_flat)/ len(labels_flat )<train_model>
prd = op.groupby('product_id')['order_id'].count().to_frame('p_total_purchases')
 prd.head()
seed_val = 42
random.seed(seed_val)
 np.random.seed(seed_val)
 torch.manual_seed(seed_val)
 torch.cuda.manual_seed_all(seed_val)
 loss_values = []
for epoch_i in range(0, epochs):
print("")
 print('======== Epoch {:} / {:} ========'.format(epoch_i + 1, epochs))
print('Training...')
 t0 = time.time()
total_loss = 0
model.train()
for step, batch in enumerate(dataloader):
if step % 40 == 0 and not step == 0:
elapsed = format_time(time.time() - t0)
 print(' Batch {:>5,} of {:>5,}.Elapsed: {:}.'.format(step, len(dataloader), elapsed))
b_input_ids = batch[0].to(device)
 b_input_mask = batch[1].to(device)
 b_labels = batch[2].to(device)
 model.zero_grad()
outputs = model(b_input_ids,
token_type_ids=None,
attention_mask=b_input_mask,
labels=b_labels)
 loss = outputs[0]
total_loss += loss.item()
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters() , 1.0)
 optimizer.step()
scheduler.step()
avg_train_loss = total_loss / len(dataloader)
 loss_values.append(avg_train_loss)
 print("")
 print(" Average training loss: {0:.2f}".format(avg_train_loss))
print(" Training epcoh took: {:}".format(format_time(time.time() - t0)))
 print("")
 print("Running Validation...")
 t0 = time.time()
model.eval()
eval_loss, eval_accuracy = 0, 0
nb_eval_steps, nb_eval_examples = 0, 0
for batch in validation_dataloader:
batch = tuple(t.to(device)for t in batch)
 b_input_ids, b_input_mask, b_labels = batch
with torch.no_grad() :
outputs = model(b_input_ids,
token_type_ids=None,
attention_mask=b_input_mask)
 logits = outputs[0]
logits = logits.detach().cpu().numpy()
label_ids = b_labels.to('cpu' ).numpy()
tmp_eval_accuracy = flat_accuracy(logits, label_ids)
 eval_accuracy += tmp_eval_accuracy
nb_eval_steps += 1
print(" Accuracy: {0:.2f}".format(eval_accuracy/nb_eval_steps))
print(" Validation took: {:}".format(format_time(time.time() - t0)))
 print("")
 print("Training complete!" )<categorify>
p_reorder = op.groupby('product_id' ).filter(lambda x: x.shape[0] >40)
 p_reorder.head()
class Dataset(torch.utils.data.Dataset):
def __init__(self, input_ids, attention_masks):
self.input_ids = input_ids
self.attention_mask = attention_masks
def __len__(self):
return len(self.input_ids)
 def __getitem__(self, index):
input_ids = self.input_ids[index]
attention_mask = self.attention_mask[index]
return input_ids, attention_mask<load_from_csv>
prd = prd.merge(p_reorder, on='product_id', how='left')
 del p_reorder
gc.collect()
prd.head()
test_df = pd.read_csv('.. /input/nlp-getting-started/test.csv')
 test_tokenized = test_df['text'].apply(( lambda x: tokenizer.encode(x, add_special_tokens=True)))
 test_max_len = 0
for i in test_tokenized.values:
if len(i)> test_max_len:
test_max_len = len(i)
 test_padded = np.array([i + [0]*(test_max_len-len(i)) for i in test_tokenized.values])
 test_attention_mask = np.where(test_padded != 0, 1, 0)
 test_input_ids = torch.tensor(test_padded)
 test_attention_mask = torch.tensor(test_attention_mask)
 test_dataset = Dataset(input_ids=test_input_ids, attention_masks=test_attention_mask)
 test_dataloader = torch.utils.data.DataLoader(test_dataset, batch_size=8, drop_last=False )<predict_on_test>
prd['p_reorder_ratio'] = prd['p_reorder_ratio'].fillna(value=0)
 prd.head()
model.eval()
test_features = []
preds = []
for batch_input_ids, batch_attention_mask in tqdm(test_dataloader):
with torch.no_grad() :
logits = model(batch_input_ids, attention_mask=batch_attention_mask)
 a = logits[0]
a =(np.argmax(a.numpy() ,axis=1))
logits = outputs[0]
logits = logits.detach().cpu().numpy()
pred_flat = np.argmax(logits, axis=1 ).flatten()
preds.extend(a)
 clear_output(True )<save_to_csv>
uxp = op.groupby(['user_id', 'product_id'])['order_id'].count().to_frame('uxp_total_bought')
 uxp.head()
sub = pd.read_csv('.. /input/nlp-getting-started/sample_submission.csv')
 sub['target'] = preds
sub.to_csv('sample_submission2.csv', index=False )<import_modules>
times = op.groupby(['user_id', 'product_id'])[['order_id']].count()
times.columns = ['Times_Bought_N']
times.head()
from keras.layers import Embedding
from keras import layers
from keras.utils import to_categorical
from keras.regularizers import l2
from keras import Model, Input
from keras.callbacks import EarlyStopping, ModelCheckpoint
from keras.models import load_model<import_modules>
total_orders = op.groupby('user_id')['order_number'].max().to_frame('total_orders')
 total_orders.head()
from sklearn.model_selection import train_test_split
import tensorflow as tf
import keras
import regex as re<load_from_csv>
span = pd.merge(total_orders, first_order_no, on='user_id', how='right')
 span.head()
train = pd.read_csv("/kaggle/input/nlp-getting-started/train.csv")
 train<load_from_csv>
span['Order_Range_D'] = span.total_orders - span.first_order_number + 1
span.head()
test = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv")
 test<load_from_csv>
uxp_ratio = pd.merge(times, span, on=['user_id', 'product_id'], how='left')
 uxp_ratio.head()
sub = pd.read_csv("/kaggle/input/nlp-getting-started/sample_submission.csv")
 sub<categorify>
uxp_ratio['uxp_reorder_ratio'] = uxp_ratio.Times_Bought_N / uxp_ratio.Order_Range_D
uxp_ratio.head()
X = [str(train.iloc[i].keyword)+" "+str(train.iloc[i].location)+" "+train.iloc[i].text for i in range(len(train)) ]
X = [re.sub(r'[^a-zA-Z\ ]', '', s ).lower() for s in X]
X = np.array(X)
 y = [train.iloc[i].target for i in range(len(train)) ]
y = np.array(y)
 print(np.unique(y, return_counts=True))
X.shape, y.shape, X[0]<split>
uxp_ratio = uxp_ratio.drop(['Times_Bought_N', 'total_orders', 'first_order_number', 'Order_Range_D'], axis=1)
 uxp_ratio.head()
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.1)
 X_train.shape, X_val.shape, y_train.shape, y_val.shape<count_unique_values>
del [times, first_order_no, span]
_, counts = np.unique(y_train, return_counts=True)
 class_weight = [np.size(y_train)/c for c in counts]
counts, class_weight<normalization>
uxp = uxp.merge(uxp_ratio, on=['user_id', 'product_id'], how='left')
 del uxp_ratio
uxp.head()
vectorizer = TextVectorization(max_tokens=20000, output_sequence_length=200)
 text_ds = tf.data.Dataset.from_tensor_slices(X_train ).batch(128)
 vectorizer.adapt(X_train )<feature_engineering>
data = uxp.merge(user, on='user_id', how='left')
 data.head()
output = vectorizer(np.array([["the cat sat on the"]]))
output.numpy() [0, :5]<find_best_params>
data = data.merge(pbp, on=["user_id","product_id"], how="left")
 data.head()
voc = vectorizer.get_vocabulary()
word_index = dict(zip(voc, range(2, len(voc))))<define_variables>
data = data.merge(prd, on='product_id', how='left')
 data.head()
test = [b"the", b"cat", b"sat", b"on", b"the"]
[word_index[w] for w in test]<split>
del op, user, prd, uxp
gc.collect()
path_to_glove_file = "/kaggle/input/glove6b/glove.6B.100d.txt"
embeddings_index = {}
with open(path_to_glove_file)as f:
for line in f:
word, coefs = line.split(maxsplit=1)
 coefs = np.fromstring(coefs, "f", sep=" ")
 embeddings_index[word] = coefs
print("Found %s word vectors." % len(embeddings_index))<categorify>
orders_future = orders[(( orders.eval_set=='train')|(orders.eval_set=='test')) ]
orders_future = orders_future[ ['user_id', 'eval_set', 'order_id'] ]
orders_future.head(10)
num_tokens = len(voc)+ 2
embedding_dim = 100
hits = 0
misses = 0
embedding_matrix = np.zeros(( num_tokens, embedding_dim))
for word, i in word_index.items() :
embedding_vector = embeddings_index.get(word.decode("utf-8"))
if embedding_vector is not None:
embedding_matrix[i] = embedding_vector
hits += 1
else:
misses += 1
print("Converted %d words(%d misses)" %(hits, misses))<data_type_conversions>
data = data.merge(orders_future, on='user_id', how='left')
 data.head(10 )
X_train = vectorizer(np.array([[s] for s in X_train])).numpy()
X_val = vectorizer(np.array([[s] for s in X_val])).numpy()
X_test = vectorizer(np.array([[s] for s in X_test])).numpy()
X_train.shape, X_val.shape, X_test.shape<choose_model_class>
data_train = data[data.eval_set=='train']
data_train.head()
embedding_layer = Embedding(
 num_tokens,
embedding_dim,
embeddings_initializer=keras.initializers.Constant(embedding_matrix),
trainable=False,
)<init_hyperparams>
data_train = data_train.merge(order_products_train[['product_id','order_id', 'reordered']], on=['product_id','order_id'], how='left')
 data_train.head(15 )
dropout_rate = 0.5
l2_rate = 1.e-6<choose_model_class>
data_train['reordered'] = data_train['reordered'].fillna(0)
 data_train.head(15 )
int_sequences_input = keras.Input(shape=(None,), dtype="int64")
 embedded_sequences = embedding_layer(int_sequences_input)
 x = layers.Conv1D(128, 5, activation="relu", kernel_regularizer=l2(l2_rate))(embedded_sequences)
 x = layers.MaxPooling1D(5 )(x)
 x = layers.Dropout(dropout_rate )(x)
 x = layers.Conv1D(128, 5, activation="relu", kernel_regularizer=l2(l2_rate))(x)
 x = layers.MaxPooling1D(5 )(x)
 x = layers.Dropout(dropout_rate )(x)
 x = layers.Conv1D(128, 5, activation="relu", kernel_regularizer=l2(l2_rate))(x)
 x = layers.GlobalMaxPooling1D()(x)
 x = layers.Dropout(dropout_rate )(x)
 x = layers.Dense(128, activation="relu", kernel_regularizer=l2(l2_rate))(x)
 x = layers.Dropout(dropout_rate )(x)
 preds = layers.Dense(1, activation="sigmoid", kernel_regularizer=l2(l2_rate))(x)
 model = keras.Model(int_sequences_input, preds)
 model.summary()<choose_model_class>
data_train = data_train.set_index(['user_id', 'product_id'])
 data_train.head(15 )
early_stopping = EarlyStopping(patience=10, verbose=1)
 model_checkpoint = ModelCheckpoint("best_model.h5", save_best_only=True, verbose=1 )<train_model>
data_train = data_train.drop(['eval_set', 'order_id'], axis=1)
 data_train.head(15 )
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["acc"])
 model.fit(X_train, y_train, batch_size=128, epochs=100, validation_data=(X_val, y_val),
class_weight={0: class_weight[0], 1: class_weight[1]},
callbacks=[early_stopping, model_checkpoint] )<load_pretrained>
data_test = data[data.eval_set=='test']
data_test.head()
best_model = load_model("best_model.h5" )<predict_on_test>
data_test = data_test.set_index(['user_id', 'product_id'])
 data_test.head()
y_pred = best_model.predict(X_test, verbose=1, batch_size=128 )<prepare_x_and_y>
data_test = data_test.drop(['eval_set','order_id'], axis=1)
 data_test.head()
for i in range(len(y_pred)) :
sub.iloc[i].target = y_pred[i]
sub<save_to_csv>
X_train, X_val, y_train, y_val = train_test_split(data_train.drop('reordered', axis=1), data_train.reordered, test_size=0.8, random_state=42)
sub.to_csv("submission.csv", index=False )<import_modules>
clf = RandomForestClassifier()
parameters = {'n_estimators': [14,16,20],
'max_features': [3,4,5],
'max_depth': [4,5,6]
}
acc_scorer = make_scorer(accuracy_score)
 grid_obj = GridSearchCV(clf, parameters, scoring=acc_scorer)
 grid_obj = grid_obj.fit(X_train, y_train)
 clf = grid_obj.best_estimator_
model = clf.fit(X_train, y_train )
import os, re
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tqdm.notebook import tqdm
import xgboost
import lightgbm
import catboost
from sklearn import ensemble, metrics, model_selection, linear_model, preprocessing, utils
import tensorflow as tf
from tensorflow import keras<compute_test_metric>
y_val_pred =(model.predict_proba(X_val)[:,1] >= 0.30 ).astype(int)
 print("RESULTS ON VALIDATION SET
====================")
 print("F1 Score: ",f1_score(y_val, y_val_pred, average='binary'), "
====================")
 print("Classification Report
", classification_report(y_val, y_val_pred), "
====================")
 print("Confusion Matrix
", confusion_matrix(y_val, y_val_pred))
def laplace_likelihood(y, p):
m, s = p
diff = np.minimum(1000, np.abs(y - m))
s = np.maximum(70, s)
 lik = -np.sqrt(2)* diff / s - np.log(np.sqrt(2)* s)
 return np.mean(lik)
 def laplace_likelihood_bound(y, p):
m = p
diff = np.minimum(1000, np.abs(y - m))
s = np.maximum(70, np.sqrt(2)* diff)
 lik = -np.sqrt(2)* diff / s - np.log(np.sqrt(2)* s)
 return np.mean(lik)
 def laplace_likelihood_avg(y, p):
m = p
diff = np.minimum(1000, np.abs(y - m))
s = np.sqrt(2)* metrics.mean_absolute_error(y, m)
 lik = -np.sqrt(2)* diff / s - np.log(np.sqrt(2)* s)
 return np.mean(lik )<split>
test_pred = model.predict(data_test ).astype(int)
 test_pred =(model.predict_proba(data_test)[:,1] >= 0.30 ).astype(int)
 test_pred[0:20]
def build_folds(X, y, group=None, k=5, shuffle=False, train_mask=None, valid_mask=None):
if isinstance(X, pd.DataFrame): X = X.values
if isinstance(y, pd.DataFrame): y = y.values
if isinstance(group, pd.DataFrame): group = group.values
if isinstance(train_mask, pd.DataFrame): train_mask = train_mask.values
if isinstance(valid_mask, pd.DataFrame): valid_mask = valid_mask.values
if group is None:
folds = list(model_selection.KFold(k, shuffle=True ).split(X, y))
else:
idx = utils.shuffle(np.arange(X.shape[0]))
if shuffle:
Xs, ys, groups = X.copy() [idx], y.copy() [idx], group.copy() [idx]
folds = list(model_selection.GroupKFold(k ).split(np.array(Xs), np.array(ys), np.array(groups)))
 else:
folds = list(model_selection.GroupKFold(k ).split(X, y, group))
if train_mask is not None:
for i in range(k):
folds[i] =(np.array([j for j in folds[i][0] if train_mask[j]]), folds[i][1])
 if valid_mask is not None:
for i in range(k):
folds[i] =(folds[i][0], np.array([j for j in folds[i][1] if valid_mask[j]]))
return folds<categorify>
data_test['prediction'] = test_pred
data_test.head(10 )
class GroupMeanTransformer:
def __init__(self, grouper, cols):
self.grouper = grouper
self.cols = cols
self.generated_features = []
self.means = dict()
pass
def fit_transform(self, x, y=None):
self.fit(x, y)
 return self.transform(x)
 def fit(self, x, y=None):
for gr in self.grouper:
group_name = '_'.join(gr)if isinstance(gr, list)else gr
for col in self.cols:
self.means[(group_name, col)] = x.groupby(gr)[col].mean()
self.means[(group_name, col)].name = '%s_mean_groupby_%s' %(col, group_name)
 self.generated_features += ['%s_mean_groupby_%s' %(col, group_name)]
self.generated_features += ['delta_%s_mean_groupby_%s' %(col, group_name)]
pass
def transform(self, x):
x = x.copy()
for k in self.means.keys() :
col = k[1]
m = self.means[k]
x[m.name] = x[m.index.names].merge(m, on=m.index.names)[m.name]
x['delta_' + m.name] = x[col] - x[m.index.names].merge(m, on=m.index.names)[m.name]
return x<categorify>
orders_test = orders.loc[orders.eval_set=='test',("user_id", "order_id")]
orders_test.head()
def _transform(x, mode: str=None, df=None):
if mode is None: return x
if mode.upper() == "DIFF": return x - df.FVC_base
if mode.upper() == "DIFF_FVC": return np.where(df.n_weeks == 0, 0.0, x - df.FVC_base)
 if mode.upper() == "PERC": return x / df.FVC_base
if mode.upper() == "PERC_FVC": return np.where(df.n_weeks == 0, 1.0, x / df.FVC_base)
 if mode.upper() == "WEEKLY": return x / np.maximum(1.0, np.abs(df.n_weeks))
if mode.upper() == "SYMLOG_WEEKLY": return x / np.log(1 + np.maximum(1.0, np.abs(df.n_weeks)))
 if re.match("^LOG", mode.upper()):
gamma = re.sub("^LOG[:]*", "", mode.upper())
 gamma = float(gamma)if len(gamma)> 0 else 0.0
return np.log(gamma + x)
 if re.match("^BOXCOX", mode.upper()):
gamma = re.sub("^BOXCOX[:]*", "", mode.upper())
 gamma = float(gamma)if len(gamma)> 0 else 1.0
return(x ** gamma - 1.0)/ gamma
return None
def _inverse_transform(x, mode=None, df=None):
if mode is None: return x
if mode.upper() == "DIFF": return x + df.FVC_base
if mode.upper() == "DIFF_FVC": return np.where(df.n_weeks == 0, df.FVC_base, x + df.FVC_base)
 if mode.upper() == "PERC": return x * df.FVC_base
if mode.upper() == "PERC_FVC": return np.where(df.n_weeks == 0, df.FVC_base, x * df.FVC_base)
 if mode.upper() == "WEEKLY": return x * np.maximum(1.0, np.abs(df.n_weeks))
if mode.upper() == "SYMLOG_WEEKLY": return x * np.log(1 + np.maximum(1.0, np.abs(df.n_weeks)))
 if re.match("^LOG", mode.upper()):
gamma = re.sub("^LOG[:]*", "", mode.upper())
 gamma = float(gamma)if len(gamma)> 0 else 0.0
return np.exp(x)- gamma
if re.match("^BOXCOX", mode.upper()):
gamma = re.sub("^BOXCOX[:]*", "", mode.upper())
 gamma = float(gamma)if len(gamma)> 0 else 1.0
return(1.0 + x * gamma)**(1 / gamma)
 return None
def transform(x, mode=None, df=None):
out = x.copy()
if isinstance(mode, list):
for m in mode:
out = _transform(out, mode=m, df=df)
 if isinstance(mode, str):
out = _transform(out, mode=mode, df=df)
 return out
def inverse_transform(x, mode=None, df=None):
out = x.copy()
if isinstance(mode, list):
for m in mode[::-1]:
out = _inverse_transform(out, mode=m, df=df)
 if isinstance(mode, str):
out = _inverse_transform(out, mode=mode, df=df)
 return out<data_type_conversions>
final = final.merge(orders_test, on='user_id', how='left')
 final.head()
def feature_eng(df):
df = df.copy()
df['n_weeks'] = df['Weeks_target'] - df['Weeks_base']
df['symlog_n_weeks'] = np.sign(df['n_weeks'])* np.log(1 + np.abs(df['n_weeks']))
df['symlog_n_weeks2'] = np.sign(df['n_weeks'])* np.log(1 + np.abs(df['n_weeks'])** 2)
 df['expdecay_n_weeks'] = np.exp(-np.abs(df['n_weeks']))
df['Sex_female'] =(df['Sex'] == 'Female' ).astype('float')
 df['Smoking_ex'] =(df['SmokingStatus'] == 'Ex-smoker' ).astype(int)
 df['Smoking_currently'] =(df['SmokingStatus'] == 'Currently smokes' ).astype(int)
 return df<define_variables>
final = final.drop('user_id', axis=1)
data_folder = ".. /input/osic-pulmonary-fibrosis-progression"<feature_engineering>
final['product_id'] = final.product_id.astype(int)
 del orders
del orders_test
gc.collect()
final.head()
df_train = pd.read_csv(os.path.join(data_folder, "train.csv")).drop_duplicates(keep=False, subset=['Patient', 'Weeks'])
 df_train['n_obs'] = df_train.groupby('Patient' ).Weeks.cumcount()
df_train['till_last'] = df_train.groupby('Patient' ).Weeks.transform('count')- 1 - df_train['n_obs']
df_train = df_train.merge(df_train.drop(['Age', 'Sex', 'Percent', 'SmokingStatus'], 1), on='Patient', suffixes=['_base', '_target'])
 cols_before_fe = df_train.columns
df_train = feature_eng(df_train)
 FEATURES = ['FVC_base', 'Percent', 'Age'] + [c for c in df_train.columns if c not in cols_before_fe]
df_train[FEATURES].head(2 )<define_variables>
d = dict()
for row in final.itertuples() :
if row.prediction== 1:
try:
d[row.order_id] += ' ' + str(row.product_id)
 except:
d[row.order_id] = str(row.product_id)
 for order in final.order_id:
if order not in d:
d[order] = 'None'
gc.collect()
d
train_mask =(df_train.n_weeks > 0)& df_train.n_obs_base.between(0, 5)& df_train.till_last_target.between(0, 3)
 valid_mask =(df_train.n_weeks > 0)& df_train.n_obs_base.between(0, 0)& df_train.till_last_target.between(0, 2 )<load_from_csv>
sub = pd.DataFrame.from_dict(d, orient='index')
 sub.reset_index(inplace=True)
 sub.columns = ['order_id', 'products']
sub.head()
<count_unique_values><EOS> 
sub.to_csv('sub.csv', index=False )