Kaggle competition: Predicting Molecular Properties
<SOS> metric: GroupMeanLogMAE Kaggle data source: predicting-molecular-properties<compute_train_metric>
pd.options.display.float_format = '{:,}'.format
train_df = pd.read_csv(".. /input/champs-scalar-coupling/train.csv")
 test_df = pd.read_csv(".. /input/champs-scalar-coupling/test.csv")
 structures = pd.read_csv(".. /input/champs-scalar-coupling/structures.csv")
 yuk = pd.read_csv(".. /input/submolecularyukawapotential/structures_yukawa.csv",
usecols=[
'dist_O_0', 'dist_O_1',
'dist_F_0','dist_F_1',
'dist_C_0','dist_C_1', 'dist_C_2', 'dist_C_3',
'dist_H_0','dist_H_1', 'dist_H_2', 'dist_H_3'
] 
)
 yuk['molecule_name'] = structures.molecule_name
yuk['atom_index'] = structures.atom_index
yuk.to_csv("yuk.csv", index=False)
 del yuk
target = train_df['scalar_coupling_constant']
train_size = train_df.shape[0]
train_df = train_df\ 
.merge(structures, left_on=['molecule_name', 'atom_index_0'], right_on=['molecule_name', 'atom_index'], how='left')\ 
.drop(['atom_index'], axis=1)\ 
.merge(structures, left_on=['molecule_name', 'atom_index_1'], right_on=['molecule_name', 'atom_index'], how='left',
suffixes=('_0', '_1')) \ 
.drop(['atom_index'], axis=1)
 test_df = test_df\ 
.merge(structures, left_on=['molecule_name', 'atom_index_0'], right_on=['molecule_name', 'atom_index'], how='left')\ 
.drop(['atom_index'], axis=1)\ 
.merge(structures, left_on=['molecule_name', 'atom_index_1'], right_on=['molecule_name', 'atom_index'], how='left',
suffixes=('_0', '_1')) \ 
.drop(['atom_index'], axis=1)
 del structures
gc.collect()
x_test_cuda = torch.tensor(X_test, dtype=torch.long ).cuda()
test = torch.utils.data.TensorDataset(x_test_cuda)
 test_loader = torch.utils.data.DataLoader(test, batch_size=batch_size, shuffle=False)
 def threshold_search(y_true, y_proba):
best_threshold = 0
best_score = 0
for threshold in tqdm([i * 0.01 for i in range(100)], disable=True):
score = f1_score(y_true=y_true, y_pred=y_proba > threshold)
 if score > best_score:
best_threshold = threshold
best_score = score
search_result = {'threshold': best_threshold, 'f1': best_score}
return search_result
def seed_everything(seed=1234):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything() 
<init_hyperparams>
target = train_df.pop('scalar_coupling_constant')
 features = [col for col in train_df.columns if col != 'id' and col != 'scalar_coupling_constant']
all_data = pd.concat([train_df[features], test_df[features]], axis=0, sort=False)
 all_data[['molecule_name', 'atom_index_0', 'atom_index_1']].to_csv("all_data_index.csv", index=False)
 test_df = test_df[['id']]
del train_df
gc.collect()
all_data['distance'] =(
(all_data['x_0'] - all_data['x_1'] ).pow(2)+(all_data['y_0'] - all_data['y_1'] ).pow(2)+(
 all_data['z_0'] - all_data['z_1'] ).pow(2)
 ).pow(0.5 )
class Args:
modelType = "RNN_atten"
max_norm = None
embed_num = max_features+1
embed_dim = embed_size
pre_word_Embedding = True
init_weight = True
init_weight_value = 1
cuda = True
class_num = 6
dropout = 0.1
dropout_embed = 0.6
kernel_num = 32
kernel_sizes = [1,2,3,5]
wide_conv = False
batch_normalizations = False
bath_norm_momentum = 0.1
batch_norm_affine = False
output_size = class_num
hidden_size = 100
vocab_size = embed_num
embedding_size = embed_dim
if modelType == "charCNN":
charDict = CharCNNDictionary()
charDict.build_dictionary()
charDict
vocabulary_size = charDict.vocabulary_size
embedding = charDict.embedding
mode = "small"
max_length = 300
if modelType == "CLSTM":
kernel_num = 400
kernel_sizes = [3]
wide_conv = True
args=Args()<set_options>
structures = pd.read_csv(".. /input/champs-scalar-coupling/structures.csv")
 structures_df = structures.merge(structures, how='left', on= ['molecule_name'], suffixes =('_0', '_1'))
del structures
gc.collect()
structures_df['distance'] =(
(structures_df['x_0'] - structures_df['x_1'] ).pow(2)+(structures_df['y_0'] - structures_df['y_1'] ).pow(2)+(
 structures_df['z_0'] - structures_df['z_1'] ).pow(2)
 ).pow(0.5)
 structures_df = structures_df[structures_df.atom_1.isin(['F', 'O'])]
gc.collect()
class Config() :
vocab = max_features
n_ctx = maxlen
n_head = 6
n_embd = embed_size
ACT_FNS = 'gelu'
n_layer = 1
C = 6
pre_word_Embedding = True
embedding_matrix = embedding_matrix
embd_pdrop = 0.5
attn_pdrop = 0.5
resid_pdrop = 0.5
cfg = Config()<create_dataframe>
for atom in ['O', 'F']:
rank_col = '{0}_rank'.format(atom)
 structures_df.loc[structures_df.atom_1 == atom, rank_col] = structures_df[structures_df.atom_1 == atom].groupby(
 ['molecule_name', 'atom_index_0'])['distance'].rank(method='first')
 atom_distance_ranks = structures_df[structures_df[rank_col] <= 2][['molecule_name', 'atom_index_0', 'distance', rank_col]]
atom_distance_ranks[rank_col] = atom_distance_ranks[rank_col].astype(int ).astype(str)
 atom_distance_ranks = atom_distance_ranks.pivot_table(
 index=['molecule_name', 'atom_index_0'], columns=[rank_col], values=['distance'])
 atom_distance_ranks.columns = [atom + '_' + '_'.join(col)for col in atom_distance_ranks.columns]
atom_distance_ranks = atom_distance_ranks.reset_index()
all_data = all_data.merge(atom_distance_ranks, on=['molecule_name', 'atom_index_0'], how='left')
 del atom_distance_ranks
gc.collect()
del structures_df
gc.collect()
for atom in ['H', 'C', 'N']:
rank_col = '{0}_rank'.format(atom)
 all_data.loc[all_data.atom_1 == atom, rank_col] = all_data[all_data.atom_1 == atom].groupby(
 ['molecule_name', 'atom_index_0'])['distance'].rank(method='first')
 atom_distance_ranks = all_data[all_data[rank_col] <= 4]
atom_distance_ranks[rank_col] = atom_distance_ranks[rank_col].astype(int ).astype(str)
 atom_distance_ranks = atom_distance_ranks.pivot_table(
 index=['molecule_name', 'atom_index_0'], columns=[rank_col], values=['distance'])
 atom_distance_ranks.columns = [atom + '_' + '_'.join(col)for col in atom_distance_ranks.columns]
atom_distance_ranks = atom_distance_ranks.reset_index()
all_data = all_data.merge(atom_distance_ranks, on=['molecule_name', 'atom_index_0'], how='left')
 del atom_distance_ranks
gc.collect()
all_data.drop(columns=['C_rank', 'H_rank', 'N_rank'], inplace=True)
 gc.collect()
num_features = 0
prev_features = 0
num_features = all_data.shape[1]
all_data.iloc[:5,prev_features:num_features].head()
if args.modelType == "charCNN":
x_train = list_sentences_train.map(lambda x:x[:args.max_length])
 x_test = list_sentences_test.map(lambda x:x[:args.max_length])
 X_train = TextDataset(x_train,args.charDict,False,args.max_length,args.max_length ).get_all()
X_test = TextDataset(x_test,args.charDict,False,args.max_length,args.max_length ).get_all()
X_train = np.asarray(X_train)
 X_test = np.asarray(X_test )<split>
grps = [['molecule_name', 'atom_index_0'], ['molecule_name', 'atom_index_1']]
grp_columns = ['distance']
gc.collect()
for grp in grps:
for column in grp_columns:
stats = all_data.groupby(grp ).agg(
 {column: ["mean", "min", "std", "max"]} 
)
 stats.columns = ['_'.join(col)for col in stats.columns]
stats.columns = ['_'.join(grp)+ '_' + col for col in stats.columns]
all_data = all_data.join(stats, on=grp, how='left')
 del stats
gc.collect()
columns_prefix = '_'.join(grp)+ '_' + column
all_data[columns_prefix + '_mean_ratio'] = all_data[column] / all_data[columns_prefix + '_mean']
all_data[columns_prefix + '_mean_diff'] = all_data[column] - all_data[columns_prefix + '_mean']
all_data[columns_prefix + '_mean_diff_std_ratio'] = all_data[columns_prefix + '_mean_diff'] \
/ all_data[columns_prefix + '_std']
prev_features = num_features
num_features = all_data.shape[1]
all_data.iloc[:5,prev_features:num_features].head()
splits = list(KFold(n_splits=5, shuffle=True, random_state=10 ).split(X_train, y_train))
train_preds = np.zeros(len(X_train))
test_preds = np.zeros(( len(X_test), len(splits)))
 for size in [1]:
print("The kernel size is {}".format(size))
tmp = []
for i,(train_idx, valid_idx)in enumerate(splits):
x_train_fold = torch.tensor(X_train[train_idx], dtype=torch.long ).cuda()
y_train_fold = torch.tensor(y_train[train_idx, np.newaxis], dtype=torch.float32 ).cuda()
x_val_fold = torch.tensor(X_train[valid_idx], dtype=torch.long ).cuda()
y_val_fold = torch.tensor(y_train[valid_idx, np.newaxis], dtype=torch.float32 ).cuda()
train = torch.utils.data.TensorDataset(x_train_fold, y_train_fold)
 valid = torch.utils.data.TensorDataset(x_val_fold, y_val_fold)
 train_loader = torch.utils.data.DataLoader(train, batch_size=batch_size, shuffle=True)
 valid_loader = torch.utils.data.DataLoader(valid, batch_size=batch_size, shuffle=False)
 print(f'Fold {i + 1}')
 seed_everything(seed + i)
 model = RNN_atten(args)
 model.cuda()
valid_preds_fold, test_preds_fold,train_loss,valid_loss,valid_auc = train_model(model,
x_train_fold,
y_train_fold,
x_val_fold,
y_val_fold, validate=True)
 tmp.append(test_preds_fold/len(splits))
test_preds_fold = reduce(lambda x,y:x+y,tmp)
 submid = pd.DataFrame({'id': subm["id"]})
 submission = pd.concat([submid, pd.DataFrame(test_preds_fold, columns = label_cols)], axis=1)
 submission.to_csv('submission'+str(size)+".csv", index=False )<set_options>
bond_data = pd.read_csv(".. /input/submolecular-bond-data/structures_bond.csv", usecols = [
'molecule_name', 'atom_index', 'n_bonds', 'bond_lengths_mean', 'bond_lengths_std'])
 all_data = all_data.merge(bond_data, left_on=['molecule_name', 'atom_index_0'],
right_on=['molecule_name', 'atom_index'], how='left')\
all_data.drop(columns=['atom_index'], inplace=True)
 all_data = all_data.merge(bond_data, left_on=['molecule_name', 'atom_index_1'],
right_on=['molecule_name', 'atom_index'], how='left', suffixes=('_0', '_1')) \
del bond_data
gc.collect()
all_data.drop(columns=['atom_index'], inplace=True)
 prev_features = num_features
num_features = all_data.shape[1]
all_data.iloc[:5,prev_features:num_features].head()
structures = pd.read_csv(".. /input/champs-scalar-coupling/structures.csv")
 atom_counts = structures.pivot_table(
 index=['molecule_name'], columns=['atom'], values='atom_index', aggfunc='count')\ 
.reset_index()
atom_counts.columns = [col + '_structure_count' if col != 'molecule_name' else col for col in atom_counts.columns ]
del structures
gc.collect()
atoms_unique = all_data.pivot_table(
 index='molecule_name', columns=['atom_1'], values='atom_index_1', aggfunc='nunique')\ 
.reset_index()
atoms_unique.columns = [col + '_atoms_count' if col != 'molecule_name' else col for col in atoms_unique.columns ]
all_data = all_data.merge(atom_counts, on=['molecule_name'], how='left')
 all_data = all_data.merge(atoms_unique, on=['molecule_name'], how='left')
 del atoms_unique, atom_counts
ply.init_notebook_mode(connected=True)
 py.sign_in('redinton', 'jQfx5zOJGz7GNQa50ISx')
 t = np.linspace(0,len(train_loss),len(train_loss))
trace1 = go.Scatter(
 x = t,
y = train_loss,
mode = "lines",
name = "train_loss",
marker = dict(color = 'rgba(16, 112, 2, 0.8)'))
trace2 = go.Scatter(
 x = t,
y = valid_loss,
mode = "lines+markers",
name = "valid_loss",
marker = dict(color = 'rgba(80, 26, 80, 0.8)'))
data = [trace1, trace2]
layout = dict(title = 'The curve of training loss and validing loss',
xaxis= dict(title= 'Epoch',ticklen= 5,zeroline= False)
)
 fig = dict(data = data, layout = layout)
 iplot(fig )<predict_on_test>
atomic_radius = {'H':0.38, 'C':0.77, 'N':0.75, 'O':0.73, 'F':0.71}
fudge_factor = 0.05
atomic_radius = {k:v + fudge_factor for k,v in atomic_radius.items() }
print(atomic_radius)
 electronegativity = {'H':2.2, 'C':2.55, 'N':3.04, 'O':3.44, 'F':3.98}
for idx in [0,1]:
atom = 'atom_{0}'.format(idx)
 atoms = all_data[atom].values
atoms_EN = [electronegativity[x] for x in atoms]
atoms_rad = [atomic_radius[x] for x in atoms]
all_data['rad_{0}'.format(idx)] = atoms_rad
all_data['EN_{0}'.format(idx)] = atoms_EN
all_data['inverse_square'] = 1 / all_data['distance'].pow(2)
 all_data['inverse_cube'] = 1 / all_data['distance'].pow(3)
 all_data['inv_distance_minus_R'] = 1 /(
 all_data['distance'] - all_data['rad_0'] - all_data['rad_1'] ).pow(2)
 all_data['inv_distance_EN'] = 1 /(
 all_data['distance']*(0.5*all_data['EN_0'] + 0.5*(all_data['EN_1'])) ).pow(2)
 all_data['inv_distance_sum_atom_0'] = all_data.groupby(
 ['molecule_name', 'atom_index_0'])['inv_distance_minus_R'].transform('sum')
 all_data['inv_distance_sum_atom_1'] = all_data.groupby(
 ['molecule_name', 'atom_index_1'])['inv_distance_minus_R'].transform('sum')
 all_data['inv_distance_EN_atom_0'] = all_data.groupby(
 ['molecule_name', 'atom_index_0'])['inv_distance_EN'].transform('sum')
 all_data['inv_distance_EN_atom_1'] = all_data.groupby(
 ['molecule_name', 'atom_index_1'])['inv_distance_EN'].transform('sum')
 all_data['inv_distance_harmonic'] = all_data['inv_distance_sum_atom_0']*all_data['inv_distance_sum_atom_1']\
/(all_data['inv_distance_sum_atom_0'] + all_data['inv_distance_sum_atom_1'])
 all_data['inv_distance_EN_harmonic'] = all_data['inv_distance_EN_atom_0']*all_data['inv_distance_EN_atom_1']\
/(all_data['inv_distance_EN_atom_0'] + all_data['inv_distance_EN_atom_1'])
 all_data.drop(columns=['rad_0', 'EN_0', 'rad_1', 'EN_1'
], inplace=True)
 del atoms_EN, atoms_rad, atoms
gc.collect()
prev_features = num_features
num_features = all_data.shape[1]
all_data.iloc[:5,prev_features:num_features].head()
MAX_PRED = 1000
MAX_ROUNDS = 2500<define_variables>
all_data['dot_product'] = all_data['x_0']*all_data['x_1'] + all_data['y_0']*all_data['y_1'] + all_data['z_0']*all_data['z_1']
all_data['norm_atom_0'] = all_data['x_0'].pow(2)+ all_data['y_0'].pow(2)+ all_data['z_0'].pow(2)
 all_data['norm_atom_1'] = all_data['x_1'].pow(2)+ all_data['y_1'].pow(2)+ all_data['z_1'].pow(2)
 all_data['cosine'] = all_data['dot_product'] /(all_data['norm_atom_0']*all_data['norm_atom_1'])
 all_data.drop(columns=['dot_product', 'norm_atom_0', 'norm_atom_1'], inplace=True)
 gc.collect()
indir = '.. /input/preparing-data-ii/'
indir2 = '.. /input/favorita-grocery-sales-forecasting/'<load_from_csv>
for idx in [0, 1]:
atom_index = 'atom_index_{0}'.format(idx)
 atom_bond_counts = all_data.pivot_table(
 index=['molecule_name', atom_index], columns=['atom_1'], values='atom_0', aggfunc='count')\ 
.reset_index()
atom_bond_counts.columns = [
col + '_bond_count_{0}'.format(idx)if col not in ['molecule_name', atom_index]
else col for col in atom_bond_counts.columns ]
atom_bond_distances = all_data.pivot_table(
 index=['molecule_name', atom_index], columns=['atom_1'], values='distance', aggfunc='sum')\ 
.reset_index()
atom_bond_distances.columns = [
col + '_bond_distances_{0}'.format(idx)if col not in ['molecule_name', atom_index]
else col for col in atom_bond_distances.columns ]
all_data = all_data.merge(
 atom_bond_counts, on=['molecule_name', atom_index], how='left')
 all_data = all_data.merge(
 atom_bond_distances, on=['molecule_name', atom_index], how='left')
 all_data['C_bond_distances_{0}_mean'.format(idx)] = all_data[
'C_bond_distances_{0}'.format(idx)] / all_data['C_bond_count_{0}'.format(idx)]
all_data['H_bond_distances_{0}_mean'.format(idx)] = all_data[
'H_bond_distances_{0}'.format(idx)] / all_data['H_bond_count_{0}'.format(idx)]
all_data['N_bond_distances_{0}_mean'.format(idx)] = all_data[
'N_bond_distances_{0}'.format(idx)] / all_data['N_bond_count_{0}'.format(idx)]
del atom_bond_counts, atom_bond_distances
gc.collect()
gc.collect()
prev_features = num_features
num_features = all_data.shape[1]
all_data.iloc[:5,prev_features:num_features].head()
X_test = pd.read_csv(indir + 'X_test.csv')
 X_val = pd.read_csv(indir + 'X_val.csv')
 X_train = pd.read_csv(indir + 'X_train.csv')
 y_train = np.array(pd.read_csv(indir + 'y_train.csv'))
y_val = np.array(pd.read_csv(indir + 'y_val.csv'))
stores_items = pd.read_csv(indir + 'stores_items.csv', index_col=['store_nbr','item_nbr'])
 test_ids = pd.read_csv(indir + 'test_ids.csv', parse_dates=['date'] ).set_index(
 ['store_nbr', 'item_nbr', 'date'] )<load_from_csv>
yuk0 = pd.read_csv("yuk.csv" ).rename(columns={'atom_index': 'atom_index_0'})
 yuk1 = pd.read_csv("yuk.csv" ).rename(columns={'atom_index': 'atom_index_1'})
 all_data_index = pd.read_csv("all_data_index.csv")
 all_data_index = all_data_index\ 
.merge(yuk0, on=['molecule_name', 'atom_index_0'], how='left', copy=False)\ 
.merge(yuk1, on=['molecule_name', 'atom_index_1'], how='left',
suffixes=('_0', '_1'), copy=False)\
del yuk0
del yuk1
gc.collect()
all_data_index.head()
items = pd.read_csv(indir2 + 'items.csv' ).set_index("item_nbr")
 items = items.reindex(stores_items.index.get_level_values(1))<init_hyperparams>
for col in all_data_index.columns:
if col not in all_data.columns:
all_data[col] = all_data_index[col]
params = {
'num_leaves': 31,
'objective': 'regression',
'min_data_in_leaf': 200,
'learning_rate': 0.027,
'feature_fraction': 0.8,
'bagging_fraction': 0.8,
'bagging_freq': 2,
'metric': 'l2',
'num_threads': 4
}<train_model>
del all_data_index
gc.collect()
val_pred = []
test_pred = []
cate_vars = []
for i in range(16):
print("=" * 50)
 print("Step %d" %(i+1))
print("=" * 50)
 dtrain = lgb.Dataset(
 X_train, label=y_train[:, i],
categorical_feature=cate_vars,
weight=pd.concat([items["perishable"]] * 6)* 0.25 + 1 
)
 dval = lgb.Dataset(
 X_val, label=y_val[:, i], reference=dtrain,
weight=items["perishable"] * 0.25 + 1,
categorical_feature=cate_vars)
 bst = lgb.train(
 params, dtrain, num_boost_round=MAX_ROUNDS,
valid_sets=[dtrain, dval], early_stopping_rounds=150, verbose_eval=200 
)
 print("
".join(( "%s: %.2f" % x)for x in sorted(
 zip(X_train.columns, bst.feature_importance("gain")) ,
key=lambda x: x[1], reverse=True 
)))
 val_pred.append(bst.predict(
 X_val, num_iteration=bst.best_iteration or MAX_ROUNDS))
test_pred.append(bst.predict(
 X_test, num_iteration=bst.best_iteration or MAX_ROUNDS))<compute_test_metric>
types = all_data.dtypes
cat_columns = [t[0] for t in types.iteritems() if(( t[1] not in ['int64', 'float64'])) ]
print('Label encoding categorical columns:', cat_columns)
 encoders = {}
for col in cat_columns:
lbl = preprocessing.LabelEncoder()
all_data[col] = lbl.fit_transform(all_data[col].astype(str))
encoders[col] = lbl
n_public = 5
weights=pd.concat([items["perishable"]])* 0.25 + 1
print("Unweighted validation mse: ", mean_squared_error(
 y_val, np.array(val_pred ).transpose()))
print("Full validation mse: ", mean_squared_error(
 y_val, np.array(val_pred ).transpose() , sample_weight=weights))
print("'Public' validation mse: ", mean_squared_error(
 y_val[:,:n_public], np.array(val_pred ).transpose() [:,:n_public], sample_weight=weights))
print("'Private' validation mse: ", mean_squared_error(
 y_val[:,n_public:], np.array(val_pred ).transpose() [:,n_public:], sample_weight=weights))<create_dataframe>
def group_mean_log_mae(y_true, y_pred, groups, floor=1e-9):
maes =(y_true-y_pred ).abs().groupby(groups ).mean()
print(( y_true-y_pred ).abs().groupby(groups))
return np.log(maes.map(lambda x: max(x, floor)) ).mean()
y_test = np.array(test_pred ).transpose()
df_preds = pd.DataFrame(
 y_test, index=stores_items.index,
columns=pd.date_range("2017-08-16", periods=16)
 ).stack().to_frame("unit_sales")
 df_preds.index.set_names(["store_nbr", "item_nbr", "date"], inplace=True )<save_to_csv>
features = [col for col in all_data.columns if col != 'fc' and col != 'id' and col not in ['atom_index_0', 'atom_index_1', 'scalar_coupling_constant', 'molecule_name']]
def lgbm_model_oof(train_X, train_y, test_X, n_folds, lgbm_params):
folds = KFold(n_splits=n_folds, shuffle=False, random_state=7557)
 oof = np.zeros(len(train_X))
predictions = np.zeros(len(test_X))
feature_importance_df = pd.DataFrame()
for fold_,(trn_idx, val_idx)in enumerate(folds.split(train_X, train_y)) :
print("Fold {}".format(fold_))
trn_data = lgb.Dataset(
 train_X.iloc[trn_idx][features], label=train_y.iloc[trn_idx], free_raw_data=True)
 val_data = lgb.Dataset(
 train_X.iloc[val_idx][features], label=train_y.iloc[val_idx], free_raw_data=True)
 num_round = 1000
clf = lgb.train(lgbm_params, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=1000,
early_stopping_rounds = 200)
 oof[val_idx] = clf.predict(train_X.iloc[val_idx][features], num_iteration=clf.best_iteration)
 fold_importance_df = pd.DataFrame()
fold_importance_df["Feature"] = features
fold_importance_df["importance"] = clf.feature_importance()
fold_importance_df["fold"] = fold_ + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 predictions += clf.predict(test_X, num_iteration=clf.best_iteration)/ folds.n_splits
del trn_data, val_data
gc.collect()
return predictions, oof, feature_importance_df
submission = test_ids.join(df_preds, how="left" ).fillna(0)
 submission["unit_sales"] = np.clip(np.expm1(submission["unit_sales"]), 0, MAX_PRED)
 submission.to_csv('lgb_including_unsold.csv', float_format='%.4f', index=None )<import_modules>
params_dict = {}
params_dict['1JHC'] = param = {
'num_leaves': 150,
'objective': 'huber',
'colsample_bytree': 0.5,
'subsample': 0.9,
'eta': 0.05,
'n_estimators': 20000,
'metric': 'mae'
}
params_dict['2JHC'] = param = {
'num_leaves': 200,
'objective': 'huber',
'colsample_bytree': 0.5,
'subsample': 0.9,
'eta': 0.05,
'n_estimators': 10000,
'metric': 'mae'
}
params_dict['1JHN'] = {
'num_leaves': 200,
'objective': 'huber',
'colsample_bytree': 0.5,
'subsample': 0.9,
'eta': 0.05,
'n_estimators': 10000,
'metric': 'mae'
}
from datetime import date, timedelta
import pandas as pd
import numpy as np
from sklearn.metrics import mean_squared_error
import lightgbm as lgb<load_from_csv>
predictions_type = np.zeros(len(test_df))
feature_importance_dfs = {}
oof_type = np.zeros(train_size)
 for typ in all_data.type.unique() :
gc.collect()
print('Predicting type:', typ)
 print(encoders['type'].classes_[typ])
 if encoders['type'].classes_[typ] in params_dict.keys() :
param = params_dict[encoders['type'].classes_[typ]]
else:
param = {
'num_leaves': 200,
'min_child_samples': 79,
'objective': 'huber',
'colsample_bytree': 0.75,
'subsample': 0.8,
'eta': 0.3,
'n_estimators': 10000,
'reg_alpha': 0.1,
'reg_lambda': 0.3,
'metric': 'mae'
}
print(param)
 type_df_train = all_data[:train_size][all_data.type == typ]
type_df_train_indices = type_df_train[type_df_train.type == typ].index
type_target = target.iloc[type_df_train_indices]
type_df_test = all_data[train_size:].reset_index().drop(columns=['index'])
 type_df_test = type_df_test[type_df_test.type == typ]
type_df_test_indices = type_df_test[type_df_test.type == typ].index
predictions, oof, type_feature_importance_df = lgbm_model_oof(
 type_df_train[features], type_target, type_df_test[features], 3, param)
 predictions_type[type_df_test_indices] += predictions
oof_type[type_df_train_indices] += oof
feature_importance_dfs[typ] = type_feature_importance_df
print(
 type_feature_importance_df.groupby(['Feature'])[['importance']].mean().sort_values(
 "importance", ascending=False ).head(10)
)
 del type_df_train, type_target, type_df_test
gc.collect()
df_train = pd.read_csv(
 '.. /input/train.csv', usecols=[1, 2, 3, 4, 5],
dtype={'onpromotion': bool},
converters={'unit_sales': lambda u: np.log1p(
 float(u)) if float(u)> 0 else 0},
parse_dates=["date"],
skiprows=range(1, 66458909)
)
 df_test = pd.read_csv(
 ".. /input/test.csv", usecols=[0, 1, 2, 3, 4],
dtype={'onpromotion': bool},
parse_dates=["date"]
).set_index(
 ['store_nbr', 'item_nbr', 'date'] 
)
 items = pd.read_csv(
 ".. /input/items.csv",
).set_index("item_nbr" )<drop_column>
types = encoders['type'].inverse_transform(all_data[:train_size]['type'])
 maes =(target-oof_type ).abs().groupby(types ).mean()
floor=1e-9
print('Log of Mean absolute errors on VAL:')
 print(np.log(maes.map(lambda x: max(x, floor))))
print('')
 print('Overal Log Mae:')
 print(np.log(maes.map(lambda x: max(x, floor)) ).mean() )
df_2017 = df_train[df_train.date.isin(
 pd.date_range("2017-05-31", periods=7 * 11)) ].copy()
del df_train<concatenate>
test_df['scalar_coupling_constant'] = predictions_type
test_df[['id', 'scalar_coupling_constant']].to_csv('submission.csv', index=False )