Kaggle competition: Santander Value Prediction Challenge
<SOS> metric: RMSLE Kaggle data source: santander-value-prediction-challenge<categorify>
color = sns.color_palette()
%matplotlib inline
py.init_notebook_mode(connected=True)
 pd.options.mode.chained_assignment = None
pd.options.display.max_columns = 9999
train_df[TARGET_COLUMN] = train_df[TARGET_COLUMN].astype(np.bool8)
 train_df[IDENTITY_COLUMNS] = train_df[IDENTITY_COLUMNS].astype(np.bool8)
 sample_weights = np.ones(len(x_train_padded), dtype=np.float32)
 sample_weights += train_df[IDENTITY_COLUMNS].sum(axis=1 ).values
sample_weights += np.abs(train_df[TARGET_COLUMN] * \ 
(~train_df[IDENTITY_COLUMNS] ).sum(axis=1 ).values)
 sample_weights += np.abs(train_df[TARGET_COLUMN] *(~train_df[IDENTITY_COLUMNS] ).sum(axis=1 ).values)
 sample_weights += np.abs(~train_df[TARGET_COLUMN])* \
train_df[IDENTITY_COLUMNS].sum(axis=1 ).values * 5
sample_weights /= sample_weights.mean()
sample_weights = torch.from_numpy(sample_weights.values[:,np.newaxis] ).float() 
<create_dataframe>
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/test.csv")
 print("Train rows and columns : ", train_df.shape)
 print("Test rows and columns : ", test_df.shape )
train_dataset = data.TensorDataset(x_train_padded, train_lengths, y_train_torch, sample_weights )<create_dataframe>
np.sum(train_df.isna().sum() )
test_dataset = data.TensorDataset(x_test_padded, test_lengths )<drop_column>
dtype_df = train_df.dtypes.reset_index()
dtype_df.columns = ["Count", "Column Type"]
dtype_df.groupby("Column Type" ).aggregate('count' ).reset_index()
valid_dataset = data.Subset(train_dataset, indices=[0,1] )<choose_model_class>
unique_df = train_df.nunique().reset_index()
unique_df.columns = ["col_name", "unique_count"]
constant_df = unique_df[unique_df["unique_count"]==1]
constant_df.shape
train_collator = SequenceBucketCollator(lambda lenghts: lenghts.max() ,
sequence_index=0,
length_index=1,
label_index=2,
weight_index = 3)
 test_collator = SequenceBucketCollator(lambda lenghts: lenghts.max() , sequence_index=0, length_index=1)
 train_loader = data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True, collate_fn=train_collator)
 valid_loader = data.DataLoader(valid_dataset, batch_size=batch_size, shuffle=False, collate_fn=train_collator)
 test_loader = data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False, collate_fn=test_collator)
 databunch = DataBunch(train_dl=train_loader, valid_dl=valid_loader, collate_fn=train_collator )<define_variables>
print(train_df[train_df.columns[~train_df.columns.isin(constant_df.col_name.tolist())]].shape)
 train_df_without_uniques = train_df[train_df.columns[~train_df.columns.isin(constant_df.col_name.tolist())]]
fastText_wordEmbedder_f='.. /input/fasttext-crawl-300d-2m/crawl-300d-2M.vec'
glove_wordEmbedder_f='.. /input/glove840b300dtxt/glove.840B.300d.txt'
glove_twitter = '.. /input/glove-global-vectors-for-word-representation/glove.twitter.27B.200d.txt'<categorify>
train_X = train_df.drop(constant_df.col_name.tolist() + ["ID", "target"], axis=1)
 test_X = test_df.drop(constant_df.col_name.tolist() + ["ID"], axis=1)
 train_y = np.log1p(train_df["target"].values )
def get_coefs(word, *arr):
return word, np.asarray(arr, dtype='float16')
 def load_embeddings(path):
with open(path)as f:
return dict(get_coefs(*line.strip().split(' ')) for line in tqdm(f))
def build_matrix(word_index, path,dim = 300):
embedding_index = load_embeddings(path)
 embedding_matrix = np.zeros(( len(word_index)+ 1, dim))
unknown_words = []
for word, i in word_index.items() :
try:
embedding_matrix[i] = embedding_index[word]
except KeyError:
unknown_words.append(word)
 return embedding_matrix, unknown_words
def create_embedding_index(file,header=False):
f = open(file,'r')
 lines = []
if header:
line_vec = f.readlines() [1:]
else:
line_vec = f.readlines()
return dict(get_coefs(*line.strip().split(' ')) for line in line_vec )<choose_model_class>
def run_lgb(train_X, train_y, val_X, val_y, test_X):
params = {
"objective" : "regression",
"metric" : "rmse",
"num_leaves" : 30,
"learning_rate" : 0.01,
"bagging_fraction" : 0.7,
"feature_fraction" : 0.7,
"bagging_frequency" : 5,
"bagging_seed" : 2018,
"verbosity" : -1
}
lgtrain = lgb.Dataset(train_X, label=train_y)
 lgval = lgb.Dataset(val_X, label=val_y)
 evals_result = {}
model = lgb.train(params, lgtrain, 1000, valid_sets=[lgval], early_stopping_rounds=100, verbose_eval=200, evals_result=evals_result)
 pred_test_y = model.predict(test_X, num_iteration=model.best_iteration)
 return pred_test_y, model, evals_result
fastText_embedding_index,unknown_words_ft = \
build_matrix(tokenizer.word_index, fastText_wordEmbedder_f)
<prepare_x_and_y>
kf = model_selection.KFold(n_splits=5, shuffle=True, random_state=2017)
 pred_test_full = 0
for dev_index, val_index in kf.split(train_X):
dev_X, val_X = train_X.loc[dev_index,:], train_X.loc[val_index,:]
dev_y, val_y = train_y[dev_index], train_y[val_index]
pred_test, model, evals_result = run_lgb(dev_X, dev_y, val_X, val_y, test_X)
 pred_test_full += pred_test
pred_test_full /= 5.
 pred_test_full = np.expm1(pred_test_full )
<prepare_x_and_y><EOS> 
sub_df = pd.DataFrame({"ID":test_df["ID"].values})
 sub_df["target"] = pred_test_full
sub_df.to_csv("baseline_lgb.csv", index=False )