Kaggle competition: Predicting Molecular Properties
<SOS> metric: GroupMeanLogMAE Kaggle data source: predicting-molecular-properties<save_to_csv>
%matplotlib inline
pd.options.display.precision = 15
warnings.filterwarnings("ignore")
 %matplotlib inline
submission = pd.DataFrame({
"datetime": datetimecol,
"count": [max(0, x)for x in np.exp(predsTest)]
})
 submission.to_csv('bike_predictions_gbm_separate_without_fe.csv', index=False )<import_modules>
def prepare_altair() :

 vega_url = 'https://cdn.jsdelivr.net/npm/vega@' + v3.SCHEMA_VERSION
vega_lib_url = 'https://cdn.jsdelivr.net/npm/vega-lib'
vega_lite_url = 'https://cdn.jsdelivr.net/npm/vega-lite@' + alt.SCHEMA_VERSION
vega_embed_url = 'https://cdn.jsdelivr.net/npm/vega-embed@3'
noext = "?noext"
paths = {
'vega': vega_url + noext,
'vega-lib': vega_lib_url + noext,
'vega-lite': vega_lite_url + noext,
'vega-embed': vega_embed_url + noext
}
workaround = f
return workaround
def add_autoincrement(render_func):
cache = {}
def wrapped(chart, id="vega-chart", autoincrement=True):
if autoincrement:
if id in cache:
counter = 1 + cache[id]
cache[id] = counter
else:
cache[id] = 0
actual_id = id if cache[id] == 0 else id + '-' + str(cache[id])
 else:
if id not in cache:
cache[id] = 0
actual_id = id
return render_func(chart, id=actual_id)
 return wrapped
@add_autoincrement
def render(chart, id="vega-chart"):

 chart_str =
return HTML(
 chart_str.format(
 id=id,
chart=json.dumps(chart)if isinstance(chart, dict)else chart.to_json(indent=None)
)
)
 def reduce_mem_usage(df, verbose=True):
numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
start_mem = df.memory_usage().sum() / 1024**2
for col in df.columns:
col_type = df[col].dtypes
if col_type in numerics:
c_min = df[col].min()
c_max = df[col].max()
if str(col_type)[:3] == 'int':
if c_min > np.iinfo(np.int8 ).min and c_max < np.iinfo(np.int8 ).max:
df[col] = df[col].astype(np.int8)
 elif c_min > np.iinfo(np.int16 ).min and c_max < np.iinfo(np.int16 ).max:
df[col] = df[col].astype(np.int16)
 elif c_min > np.iinfo(np.int32 ).min and c_max < np.iinfo(np.int32 ).max:
df[col] = df[col].astype(np.int32)
 elif c_min > np.iinfo(np.int64 ).min and c_max < np.iinfo(np.int64 ).max:
df[col] = df[col].astype(np.int64)
 else:
if c_min > np.finfo(np.float16 ).min and c_max < np.finfo(np.float16 ).max:
df[col] = df[col].astype(np.float16)
 elif c_min > np.finfo(np.float32 ).min and c_max < np.finfo(np.float32 ).max:
df[col] = df[col].astype(np.float32)
 else:
df[col] = df[col].astype(np.float64)
 end_mem = df.memory_usage().sum() / 1024**2
if verbose: print('Mem.usage decreased to {:5.2f} Mb({:.1f}% reduction)'.format(end_mem, 100 *(start_mem - end_mem)/ start_mem))
return df
@jit
def fast_auc(y_true, y_prob):

 y_true = np.asarray(y_true)
 y_true = y_true[np.argsort(y_prob)]
nfalse = 0
auc = 0
n = len(y_true)
 for i in range(n):
y_i = y_true[i]
nfalse +=(1 - y_i)
 auc += y_i * nfalse
auc /=(nfalse *(n - nfalse))
return auc
def eval_auc(y_true, y_pred):

 return 'auc', fast_auc(y_true, y_pred), True
def group_mean_log_mae(y_true, y_pred, types, floor=1e-9):

 maes =(y_true-y_pred ).abs().groupby(types ).mean()
return np.log(maes.map(lambda x: max(x, floor)) ).mean()
def train_model_regression(X, X_test, y, params, folds, model_type='lgb', eval_metric='mae', columns=None, plot_feature_importance=False, model=None,
verbose=10000, early_stopping_rounds=200, n_estimators=50000):

 columns = X.columns if columns is None else columns
X_test = X_test[columns]
metrics_dict = {'mae': {'lgb_metric_name': 'mae',
'catboost_metric_name': 'MAE',
'sklearn_scoring_function': metrics.mean_absolute_error},
'group_mae': {'lgb_metric_name': 'mae',
'catboost_metric_name': 'MAE',
'scoring_function': group_mean_log_mae},
'mse': {'lgb_metric_name': 'mse',
'catboost_metric_name': 'MSE',
'sklearn_scoring_function': metrics.mean_squared_error}
}
result_dict = {}
oof = np.zeros(len(X))
prediction = np.zeros(len(X_test))
scores = []
feature_importance = pd.DataFrame()
for fold_n,(train_index, valid_index)in enumerate(folds.split(X)) :
print(f'Fold {fold_n + 1} started at {time.ctime() }')
 if type(X)== np.ndarray:
X_train, X_valid = X[columns][train_index], X[columns][valid_index]
y_train, y_valid = y[train_index], y[valid_index]
else:
X_train, X_valid = X[columns].iloc[train_index], X[columns].iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
if model_type == 'lgb':
model = lgb.LGBMRegressor(**params, n_estimators = n_estimators, n_jobs = -1)
 model.fit(X_train, y_train,
eval_set=[(X_train, y_train),(X_valid, y_valid)], eval_metric=metrics_dict[eval_metric]['lgb_metric_name'],
verbose=verbose, early_stopping_rounds=early_stopping_rounds)
 y_pred_valid = model.predict(X_valid)
 y_pred = model.predict(X_test, num_iteration=model.best_iteration_)
 if model_type == 'xgb':
train_data = xgb.DMatrix(data=X_train, label=y_train, feature_names=X.columns)
 valid_data = xgb.DMatrix(data=X_valid, label=y_valid, feature_names=X.columns)
 watchlist = [(train_data, 'train'),(valid_data, 'valid_data')]
model = xgb.train(dtrain=train_data, num_boost_round=20000, evals=watchlist, early_stopping_rounds=200, verbose_eval=verbose, params=params)
 y_pred_valid = model.predict(xgb.DMatrix(X_valid, feature_names=X.columns), ntree_limit=model.best_ntree_limit)
 y_pred = model.predict(xgb.DMatrix(X_test, feature_names=X.columns), ntree_limit=model.best_ntree_limit)
 if model_type == 'sklearn':
model = model
model.fit(X_train, y_train)
 y_pred_valid = model.predict(X_valid ).reshape(-1,)
 score = metrics_dict[eval_metric]['sklearn_scoring_function'](y_valid, y_pred_valid)
 print(f'Fold {fold_n}.{eval_metric}: {score:.4f}.')
 print('')
 y_pred = model.predict(X_test ).reshape(-1,)
 if model_type == 'cat':
model = CatBoostRegressor(iterations=20000, eval_metric=metrics_dict[eval_metric]['catboost_metric_name'], **params,
loss_function=metrics_dict[eval_metric]['catboost_metric_name'])
 model.fit(X_train, y_train, eval_set=(X_valid, y_valid), cat_features=[], use_best_model=True, verbose=False)
 y_pred_valid = model.predict(X_valid)
 y_pred = model.predict(X_test)
 oof[valid_index] = y_pred_valid.reshape(-1,)
 if eval_metric != 'group_mae':
scores.append(metrics_dict[eval_metric]['sklearn_scoring_function'](y_valid, y_pred_valid))
else:
scores.append(metrics_dict[eval_metric]['scoring_function'](y_valid, y_pred_valid, X_valid['type']))
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)
 prediction /= folds.n_splits
print('CV mean 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
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
def train_model_classification(X, X_test, y, params, folds, model_type='lgb', eval_metric='auc', columns=None, plot_feature_importance=False, model=None,
verbose=10000, early_stopping_rounds=200, n_estimators=50000):

 columns = X.columns if columns == None else columns
X_test = X_test[columns]
metrics_dict = {'auc': {'lgb_metric_name': eval_auc,
'catboost_metric_name': 'AUC',
'sklearn_scoring_function': metrics.roc_auc_score},
}
result_dict = {}
oof = np.zeros(( len(X), len(set(y.values))))
prediction = np.zeros(( len(X_test), oof.shape[1]))
scores = []
feature_importance = pd.DataFrame()
for fold_n,(train_index, valid_index)in enumerate(folds.split(X)) :
print(f'Fold {fold_n + 1} started at {time.ctime() }')
 if type(X)== np.ndarray:
X_train, X_valid = X[columns][train_index], X[columns][valid_index]
y_train, y_valid = y[train_index], y[valid_index]
else:
X_train, X_valid = X[columns].iloc[train_index], X[columns].iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
if model_type == 'lgb':
model = lgb.LGBMClassifier(**params, n_estimators=n_estimators, n_jobs = -1)
 model.fit(X_train, y_train,
eval_set=[(X_train, y_train),(X_valid, y_valid)], eval_metric=metrics_dict[eval_metric]['lgb_metric_name'],
verbose=verbose, early_stopping_rounds=early_stopping_rounds)
 y_pred_valid = model.predict_proba(X_valid)
 y_pred = model.predict_proba(X_test, num_iteration=model.best_iteration_)
 if model_type == 'xgb':
train_data = xgb.DMatrix(data=X_train, label=y_train, feature_names=X.columns)
 valid_data = xgb.DMatrix(data=X_valid, label=y_valid, feature_names=X.columns)
 watchlist = [(train_data, 'train'),(valid_data, 'valid_data')]
model = xgb.train(dtrain=train_data, num_boost_round=n_estimators, evals=watchlist, early_stopping_rounds=early_stopping_rounds, verbose_eval=verbose, params=params)
 y_pred_valid = model.predict(xgb.DMatrix(X_valid, feature_names=X.columns), ntree_limit=model.best_ntree_limit)
 y_pred = model.predict(xgb.DMatrix(X_test, feature_names=X.columns), ntree_limit=model.best_ntree_limit)
 if model_type == 'sklearn':
model = model
model.fit(X_train, y_train)
 y_pred_valid = model.predict(X_valid ).reshape(-1,)
 score = metrics_dict[eval_metric]['sklearn_scoring_function'](y_valid, y_pred_valid)
 print(f'Fold {fold_n}.{eval_metric}: {score:.4f}.')
 print('')
 y_pred = model.predict_proba(X_test)
 if model_type == 'cat':
model = CatBoostClassifier(iterations=n_estimators, eval_metric=metrics_dict[eval_metric]['catboost_metric_name'], **params,
loss_function=metrics_dict[eval_metric]['catboost_metric_name'])
 model.fit(X_train, y_train, eval_set=(X_valid, y_valid), cat_features=[], use_best_model=True, verbose=False)
 y_pred_valid = model.predict(X_valid)
 y_pred = model.predict(X_test)
 oof[valid_index] = y_pred_valid
scores.append(metrics_dict[eval_metric]['sklearn_scoring_function'](y_valid, y_pred_valid[:, 1]))
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)
 prediction /= folds.n_splits
print('CV mean 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
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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
import datetime
import calendar<import_modules>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 sub = pd.read_csv('.. /input/sample_submission.csv')
 structures = pd.read_csv('.. /input/structures.csv')
 scalar_coupling_contributions = pd.read_csv('.. /input/scalar_coupling_contributions.csv')
 print('Train dataset shape is -> rows: {} cols:{}'.format(train.shape[0],train.shape[1]))
print('Test dataset shape is -> rows: {} cols:{}'.format(test.shape[0],test.shape[1]))
print('Sub dataset shape is -> rows: {} cols:{}'.format(sub.shape[0],sub.shape[1]))
print('Structures dataset shape is -> rows: {} cols:{}'.format(structures.shape[0],structures.shape[1]))
print('Scalar_coupling_contributions dataset shape is -> rows: {} cols:{}'.format(scalar_coupling_contributions.shape[0],
scalar_coupling_contributions.shape[1]))
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error 
<import_modules>
n_estimators_default = 4000
from keras.models import Sequential
from keras.layers.recurrent import LSTM, SimpleRNN
from keras.layers.core import Dense, Activation, Dropout
from keras.optimizers import RMSprop,Adam<set_options>
train = pd.merge(train, scalar_coupling_contributions, how = 'left',
left_on = ['molecule_name', 'atom_index_0', 'atom_index_1', 'type'],
right_on = ['molecule_name', 'atom_index_0', 'atom_index_1', 'type'] )
warnings.filterwarnings("ignore" )<load_from_csv>
pd.concat(objs=[train['scalar_coupling_constant'],scalar_coupling_contributions['fc'] ],axis=1)[:10]
train_data = pd.read_csv(path_in + 'train.csv', parse_dates = ['datetime'],
index_col='datetime', infer_datetime_format=True)
 test_data = pd.read_csv(path_in + 'test.csv', parse_dates = ['datetime'],
index_col='datetime', infer_datetime_format=True)
 samp_subm = pd.read_csv(path_in+'sampleSubmission.csv', parse_dates = ['datetime'],
index_col='datetime', infer_datetime_format=True )<compute_test_metric>
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}
atoms = structures['atom'].values
atoms_en = [electronegativity[x] for x in tqdm(atoms)]
atoms_rad = [atomic_radius[x] for x in tqdm(atoms)]
structures['EN'] = atoms_en
structures['rad'] = atoms_rad
display(structures.head() )
def rmse(y_true, y_pred):

 return K.sqrt(K.mean(K.square(y_pred - y_true)) )<define_variables>
i_atom = structures['atom_index'].values
p = structures[['x', 'y', 'z']].values
p_compare = p
m = structures['molecule_name'].values
m_compare = m
r = structures['rad'].values
r_compare = r
source_row = np.arange(len(structures))
max_atoms = 28
bonds = np.zeros(( len(structures)+1, max_atoms+1), dtype=np.int8)
 bond_dists = np.zeros(( len(structures)+1, max_atoms+1), dtype=np.float32)
 print('Calculating bonds')
 for i in tqdm(range(max_atoms-1)) :
p_compare = np.roll(p_compare, -1, axis=0)
 m_compare = np.roll(m_compare, -1, axis=0)
 r_compare = np.roll(r_compare, -1, axis=0)
 mask = np.where(m == m_compare, 1, 0)
 dists = np.linalg.norm(p - p_compare, axis=1)* mask
r_bond = r + r_compare
bond = np.where(np.logical_and(dists > 0.0001, dists < r_bond), 1, 0)
 source_row = source_row
target_row = source_row + i + 1
target_row = np.where(np.logical_or(target_row > len(structures), mask==0), len(structures), target_row)
 source_atom = i_atom
target_atom = i_atom + i + 1
target_atom = np.where(np.logical_or(target_atom > max_atoms, mask==0), max_atoms, target_atom)
 bonds[(source_row, target_atom)] = bond
bonds[(target_row, source_atom)] = bond
bond_dists[(source_row, target_atom)] = dists
bond_dists[(target_row, source_atom)] = dists
bonds = np.delete(bonds, axis=0, obj=-1)
 bonds = np.delete(bonds, axis=1, obj=-1)
 bond_dists = np.delete(bond_dists, axis=0, obj=-1)
 bond_dists = np.delete(bond_dists, axis=1, obj=-1)
 print('Counting and condensing bonds')
 bonds_numeric = [[i for i,x in enumerate(row)if x] for row in tqdm(bonds)]
bond_lengths = [[dist for i,dist in enumerate(row)if i in bonds_numeric[j]] for j,row in enumerate(tqdm(bond_dists)) ]
bond_lengths_mean = [ np.mean(x)for x in bond_lengths]
bond_lengths_median = [ np.median(x)for x in bond_lengths]
bond_lengths_std = [ np.std(x)for x in bond_lengths]
n_bonds = [len(x)for x in bonds_numeric]
bond_data = {'n_bonds':n_bonds, 'bond_lengths_mean': bond_lengths_mean,
'bond_lengths_std':bond_lengths_std, 'bond_lengths_median': bond_lengths_median }
bond_df = pd.DataFrame(bond_data)
 structures = structures.join(bond_df)
 display(structures.head(20))
num_months_per_year = 12
year_list = [2011, 2012]<data_type_conversions>
def map_atom_info(df, atom_idx):
df = pd.merge(df, structures, how = 'left',
left_on = ['molecule_name', f'atom_index_{atom_idx}'],
right_on = ['molecule_name', 'atom_index'])
 df = df.rename(columns={'atom': f'atom_{atom_idx}',
'x': f'x_{atom_idx}',
'y': f'y_{atom_idx}',
'z': f'z_{atom_idx}'})
 return df
train = map_atom_info(train, 0)
 train = map_atom_info(train, 1)
 test = map_atom_info(test, 0)
 test = map_atom_info(test, 1 )
train_data_temp = pd.DataFrame(columns=train_data.columns)
 for year in year_list:
for month in range(num_months_per_year):
start_date = datetime.datetime(year, month+1, 1, 0, 0, 0)
 end_date = datetime.datetime(year, month+1, 19, 23, 0, 0)
 temp = train_data[start_date:end_date].resample('H' ).asfreq()
features_fill_zero = ['casual', 'registered', 'count']
temp[features_fill_zero] = temp[features_fill_zero].fillna(0)
 features_fill_bbfil = ['season', 'holiday', 'workingday', 'weather']
temp[features_fill_bbfil] = temp[features_fill_bbfil].fillna(method='bfill')
 features_fill_linear = ['temp', 'atemp', 'humidity', 'windspeed']
temp[features_fill_linear] = temp[features_fill_linear].interpolate(method='linear')
 train_data_temp = train_data_temp.append(temp)
 train_data = train_data_temp<feature_engineering>
train_p_0 = train[['x_0', 'y_0', 'z_0']].values
train_p_1 = train[['x_1', 'y_1', 'z_1']].values
test_p_0 = test[['x_0', 'y_0', 'z_0']].values
test_p_1 = test[['x_1', 'y_1', 'z_1']].values
train['dist'] = np.linalg.norm(train_p_0 - train_p_1, axis=1)
 test['dist'] = np.linalg.norm(test_p_0 - test_p_1, axis=1)
 train['dist'] = 1/(train['dist']**3)
 test['dist'] = 1/(test['dist']**3)
 train['dist_x'] =(train['x_0'] - train['x_1'])** 2
test['dist_x'] =(test['x_0'] - test['x_1'])** 2
train['dist_y'] =(train['y_0'] - train['y_1'])** 2
test['dist_y'] =(test['y_0'] - test['y_1'])** 2
train['dist_z'] =(train['z_0'] - train['z_1'])** 2
test['dist_z'] =(test['z_0'] - test['z_1'])** 2
train['type_0'] = train['type'].apply(lambda x: x[0])
 test['type_0'] = test['type'].apply(lambda x: x[0])
test_data_temp = pd.DataFrame(columns=test_data.columns)
 for year in year_list:
for month in range(num_months_per_year):
start_date = datetime.datetime(year, month+1, 20, 0, 0, 0)
 last_day_of_month = calendar.monthrange(year,month+1)[1]
end_date = datetime.datetime(year, month+1, last_day_of_month, 23, 0, 0)
 temp = test_data[start_date:end_date].resample('H' ).asfreq()
features_fill_bbfil = ['season', 'holiday', 'workingday', 'weather']
temp[features_fill_bbfil] = temp[features_fill_bbfil].fillna(method='bfill')
 features_fill_linear = ['temp', 'atemp', 'humidity', 'windspeed']
temp[features_fill_linear] = temp[features_fill_linear].interpolate(method='linear')
 test_data_temp = test_data_temp.append(temp)
 test_data = test_data_temp<feature_engineering>
def create_features(df):
df['molecule_couples'] = df.groupby('molecule_name')['id'].transform('count')
 df['molecule_dist_mean'] = df.groupby('molecule_name')['dist'].transform('mean')
 df['molecule_dist_min'] = df.groupby('molecule_name')['dist'].transform('min')
 df['molecule_dist_max'] = df.groupby('molecule_name')['dist'].transform('max')
 df['atom_0_couples_count'] = df.groupby(['molecule_name', 'atom_index_0'])['id'].transform('count')
 df['atom_1_couples_count'] = df.groupby(['molecule_name', 'atom_index_1'])['id'].transform('count')
 df[f'molecule_atom_index_0_x_1_std'] = df.groupby(['molecule_name', 'atom_index_0'])['x_1'].transform('std')
 df[f'molecule_atom_index_0_y_1_mean'] = df.groupby(['molecule_name', 'atom_index_0'])['y_1'].transform('mean')
 df[f'molecule_atom_index_0_y_1_mean_diff'] = df[f'molecule_atom_index_0_y_1_mean'] - df['y_1']
df[f'molecule_atom_index_0_y_1_mean_div'] = df[f'molecule_atom_index_0_y_1_mean'] / df['y_1']
df[f'molecule_atom_index_0_y_1_max'] = df.groupby(['molecule_name', 'atom_index_0'])['y_1'].transform('max')
 df[f'molecule_atom_index_0_y_1_max_diff'] = df[f'molecule_atom_index_0_y_1_max'] - df['y_1']
df[f'molecule_atom_index_0_y_1_std'] = df.groupby(['molecule_name', 'atom_index_0'])['y_1'].transform('std')
 df[f'molecule_atom_index_0_z_1_std'] = df.groupby(['molecule_name', 'atom_index_0'])['z_1'].transform('std')
 df[f'molecule_atom_index_0_dist_mean'] = df.groupby(['molecule_name', 'atom_index_0'])['dist'].transform('mean')
 df[f'molecule_atom_index_0_dist_mean_diff'] = df[f'molecule_atom_index_0_dist_mean'] - df['dist']
df[f'molecule_atom_index_0_dist_mean_div'] = df[f'molecule_atom_index_0_dist_mean'] / df['dist']
df[f'molecule_atom_index_0_dist_max'] = df.groupby(['molecule_name', 'atom_index_0'])['dist'].transform('max')
 df[f'molecule_atom_index_0_dist_max_diff'] = df[f'molecule_atom_index_0_dist_max'] - df['dist']
df[f'molecule_atom_index_0_dist_max_div'] = df[f'molecule_atom_index_0_dist_max'] / df['dist']
df[f'molecule_atom_index_0_dist_min'] = df.groupby(['molecule_name', 'atom_index_0'])['dist'].transform('min')
 df[f'molecule_atom_index_0_dist_min_diff'] = df[f'molecule_atom_index_0_dist_min'] - df['dist']
df[f'molecule_atom_index_0_dist_min_div'] = df[f'molecule_atom_index_0_dist_min'] / df['dist']
df[f'molecule_atom_index_0_dist_std'] = df.groupby(['molecule_name', 'atom_index_0'])['dist'].transform('std')
 df[f'molecule_atom_index_0_dist_std_diff'] = df[f'molecule_atom_index_0_dist_std'] - df['dist']
df[f'molecule_atom_index_0_dist_std_div'] = df[f'molecule_atom_index_0_dist_std'] / df['dist']
df[f'molecule_atom_index_1_dist_mean'] = df.groupby(['molecule_name', 'atom_index_1'])['dist'].transform('mean')
 df[f'molecule_atom_index_1_dist_mean_diff'] = df[f'molecule_atom_index_1_dist_mean'] - df['dist']
df[f'molecule_atom_index_1_dist_mean_div'] = df[f'molecule_atom_index_1_dist_mean'] / df['dist']
df[f'molecule_atom_index_1_dist_max'] = df.groupby(['molecule_name', 'atom_index_1'])['dist'].transform('max')
 df[f'molecule_atom_index_1_dist_max_diff'] = df[f'molecule_atom_index_1_dist_max'] - df['dist']
df[f'molecule_atom_index_1_dist_max_div'] = df[f'molecule_atom_index_1_dist_max'] / df['dist']
df[f'molecule_atom_index_1_dist_min'] = df.groupby(['molecule_name', 'atom_index_1'])['dist'].transform('min')
 df[f'molecule_atom_index_1_dist_min_diff'] = df[f'molecule_atom_index_1_dist_min'] - df['dist']
df[f'molecule_atom_index_1_dist_min_div'] = df[f'molecule_atom_index_1_dist_min'] / df['dist']
df[f'molecule_atom_index_1_dist_std'] = df.groupby(['molecule_name', 'atom_index_1'])['dist'].transform('std')
 df[f'molecule_atom_index_1_dist_std_diff'] = df[f'molecule_atom_index_1_dist_std'] - df['dist']
df[f'molecule_atom_index_1_dist_std_div'] = df[f'molecule_atom_index_1_dist_std'] / df['dist']
df[f'molecule_atom_1_dist_mean'] = df.groupby(['molecule_name', 'atom_1'])['dist'].transform('mean')
 df[f'molecule_atom_1_dist_min'] = df.groupby(['molecule_name', 'atom_1'])['dist'].transform('min')
 df[f'molecule_atom_1_dist_min_diff'] = df[f'molecule_atom_1_dist_min'] - df['dist']
df[f'molecule_atom_1_dist_min_div'] = df[f'molecule_atom_1_dist_min'] / df['dist']
df[f'molecule_atom_1_dist_std'] = df.groupby(['molecule_name', 'atom_1'])['dist'].transform('std')
 df[f'molecule_atom_1_dist_std_diff'] = df[f'molecule_atom_1_dist_std'] - df['dist']
df[f'molecule_type_0_dist_std'] = df.groupby(['molecule_name', 'type_0'])['dist'].transform('std')
 df[f'molecule_type_0_dist_std_diff'] = df[f'molecule_type_0_dist_std'] - df['dist']
df[f'molecule_type_dist_mean'] = df.groupby(['molecule_name', 'type'])['dist'].transform('mean')
 df[f'molecule_type_dist_mean_diff'] = df[f'molecule_type_dist_mean'] - df['dist']
df[f'molecule_type_dist_mean_div'] = df[f'molecule_type_dist_mean'] / df['dist']
df[f'molecule_type_dist_max'] = df.groupby(['molecule_name', 'type'])['dist'].transform('max')
 df[f'molecule_type_dist_min'] = df.groupby(['molecule_name', 'type'])['dist'].transform('min')
 df[f'molecule_type_dist_std'] = df.groupby(['molecule_name', 'type'])['dist'].transform('std')
 df[f'molecule_type_dist_std_diff'] = df[f'molecule_type_dist_std'] - df['dist']
df = reduce_mem_usage(df)
 return df
train = create_features(train)
 test = create_features(test )
train_data['weekday'] = train_data.index.weekday
train_data['hour'] = train_data.index.hour
test_data['weekday'] = test_data.index.weekday
test_data['hour'] = test_data.index.hour 
<feature_engineering>
def map_atom_info(df_1,df_2, atom_idx):
df = pd.merge(df_1, df_2, how = 'left',
left_on = ['molecule_name', f'atom_index_{atom_idx}'],
right_on = ['molecule_name', 'atom_index'])
 df = df.drop('atom_index', axis=1)
 return df
def create_closest(df_train):
df_temp=df_train.loc[:,["molecule_name","atom_index_0","atom_index_1","dist","x_0","y_0","z_0","x_1","y_1","z_1"]].copy()
df_temp_=df_temp.copy()
df_temp_= df_temp_.rename(columns={'atom_index_0': 'atom_index_1',
'atom_index_1': 'atom_index_0',
'x_0': 'x_1',
'y_0': 'y_1',
'z_0': 'z_1',
'x_1': 'x_0',
'y_1': 'y_0',
'z_1': 'z_0'})
 df_temp=pd.concat(objs=[df_temp,df_temp_],axis=0)
 df_temp["min_distance"]=df_temp.groupby(['molecule_name', 'atom_index_0'])['dist'].transform('min')
 df_temp= df_temp[df_temp["min_distance"]==df_temp["dist"]]
df_temp=df_temp.drop(['x_0','y_0','z_0','min_distance'], axis=1)
 df_temp= df_temp.rename(columns={'atom_index_0': 'atom_index',
'atom_index_1': 'atom_index_closest',
'distance': 'distance_closest',
'x_1': 'x_closest',
'y_1': 'y_closest',
'z_1': 'z_closest'})
 for atom_idx in [0,1]:
df_train = map_atom_info(df_train,df_temp, atom_idx)
 df_train = df_train.rename(columns={'atom_index_closest': f'atom_index_closest_{atom_idx}',
'distance_closest': f'distance_closest_{atom_idx}',
'x_closest': f'x_closest_{atom_idx}',
'y_closest': f'y_closest_{atom_idx}',
'z_closest': f'z_closest_{atom_idx}'})
 return df_train
for year in year_list:
for month in range(num_months_per_year):
start_date = datetime.datetime(year, month+1, 1, 0, 0, 0)
 end_date = datetime.datetime(year, month+1, 19, 23, 0, 0)
 count_mean = train_data[start_date:end_date]['count'].mean()
train_data.loc[start_date:end_date, 'count_mean'] = count_mean
start_date = datetime.datetime(year, month+1, 20, 0, 0, 0)
 last_day_of_month = calendar.monthrange(year,month+1)[1]
end_date = datetime.datetime(year, month+1, last_day_of_month, 23, 0, 0)
 test_data.loc[start_date:end_date, 'count_mean'] = count_mean<groupby>
def add_cos_features(df):
df["distance_0"]=(( df['x_0']-df['x_closest_0'])**2+(df['y_0']-df['y_closest_0'])**2+(df['z_0']-df['z_closest_0'])**2)**(1/2)
 df["distance_1"]=(( df['x_1']-df['x_closest_1'])**2+(df['y_1']-df['y_closest_1'])**2+(df['z_1']-df['z_closest_1'])**2)**(1/2)
 df["vec_0_x"]=(df['x_0']-df['x_closest_0'])/df["distance_0"]
df["vec_0_y"]=(df['y_0']-df['y_closest_0'])/df["distance_0"]
df["vec_0_z"]=(df['z_0']-df['z_closest_0'])/df["distance_0"]
df["vec_1_x"]=(df['x_1']-df['x_closest_1'])/df["distance_1"]
df["vec_1_y"]=(df['y_1']-df['y_closest_1'])/df["distance_1"]
df["vec_1_z"]=(df['z_1']-df['z_closest_1'])/df["distance_1"]
df["vec_x"]=(df['x_1']-df['x_0'])/df["dist"]
df["vec_y"]=(df['y_1']-df['y_0'])/df["dist"]
df["vec_z"]=(df['z_1']-df['z_0'])/df["dist"]
df["cos_0_1"]=df["vec_0_x"]*df["vec_1_x"]+df["vec_0_y"]*df["vec_1_y"]+df["vec_0_z"]*df["vec_1_z"]
df["cos_0"]=df["vec_0_x"]*df["vec_x"]+df["vec_0_y"]*df["vec_y"]+df["vec_0_z"]*df["vec_z"]
df["cos_1"]=df["vec_1_x"]*df["vec_x"]+df["vec_1_y"]*df["vec_y"]+df["vec_1_z"]*df["vec_z"]
df=df.drop(['vec_0_x','vec_0_y','vec_0_z','vec_1_x','vec_1_y','vec_1_z','vec_x','vec_y','vec_z'], axis=1)
 return df
def hour_group(s):
if(( 0<=s)&(s<=6)) :
return 1
elif(( s==7)|(s==9)) :
return 2
elif(( s==8)|(s==16)|(s==19)) :
return 3
elif(( 10<=s)&(s<=15)) :
return 4
elif(( s==17)|(s==18)) :
return 5
elif(20<=s):
return 6<feature_engineering>
del_cols_list = ['id','molecule_name','sd','pso','dso']
def del_cols(df, cols):
del_cols_list_ = [l for l in del_cols_list if l in df]
df = df.drop(del_cols_list_,axis=1)
 return df
train = del_cols(train,del_cols_list)
 test = del_cols(test,del_cols_list )
train_data['hour_group'] = train_data['hour'].apply(hour_group)
 test_data['hour_group'] = test_data['hour'].apply(hour_group )<feature_engineering>
def encode_categoric_single(df):
lbl = LabelEncoder()
cat_cols=[]
try:
cat_cols = df.describe(include=['O'] ).columns.tolist()
for cat in cat_cols:
df[cat] = lbl.fit_transform(list(df[cat].values))
except Exception as e:
print('error: ', str(e))
return df
<data_type_conversions>
def encode_categoric(dtrain,dtest):
lbl = LabelEncoder()
objs_n = len(dtrain)
 dfmerge = pd.concat(objs=[dtrain,dtest],axis=0)
 cat_cols=[]
try:
cat_cols = dfmerge.describe(include=['O'] ).columns.tolist()
for cat in cat_cols:
dfmerge[cat] = lbl.fit_transform(list(dfmerge[cat].values))
except Exception as e:
print('error: ', str(e))
dtrain = dfmerge[:objs_n]
dtest = dfmerge[objs_n:]
return dtrain,dtest
<define_variables>
train = encode_categoric_single(train)
 test = encode_categoric_single(test )
scale_features = ['temp', 'atemp', 'humidity', 'hour', 'windspeed']<define_variables>
y_fc = train['fc']
X = train.drop(['scalar_coupling_constant','fc'],axis=1)
 y = train['scalar_coupling_constant']
X_test = test.copy()
feature_list = ['holiday', 'workingday', 'weather', 'temp', 'atemp',
'humidity', 'windspeed', 'hour_group',
'hour', 'weekday', 'count_mean']
no_features = ['casual', 'registered']<count_values>
good_columns = ['type',
'bond_lengths_mean_y',
'bond_lengths_median_y',
'bond_lengths_std_y',
'bond_lengths_mean_x',
'molecule_atom_index_0_dist_min_div',
'molecule_atom_index_0_dist_std_div',
'molecule_atom_index_0_dist_mean',
'molecule_atom_index_0_dist_max',
'dist_y',
'molecule_atom_index_1_dist_std_diff',
'z_0',
'molecule_type_dist_min',
'molecule_atom_index_0_y_1_mean_div',
'dist_x',
'x_0',
'y_0',
'molecule_type_dist_std',
'molecule_atom_index_0_y_1_std',
'molecule_dist_mean',
'molecule_atom_index_0_dist_std_diff',
'dist_z',
'molecule_atom_index_0_dist_std',
'molecule_atom_index_0_x_1_std',
'molecule_type_dist_std_diff',
'molecule_type_0_dist_std',
'dist',
'molecule_atom_index_0_dist_mean_diff',
'molecule_atom_index_1_dist_min_div',
'molecule_atom_index_1_dist_mean_diff',
'y_1',
'molecule_type_dist_mean_div',
'molecule_dist_max',
'molecule_atom_index_0_dist_mean_div',
'z_1',
'molecule_atom_index_0_z_1_std',
'molecule_atom_index_1_dist_mean_div',
'molecule_atom_index_1_dist_min_diff',
'molecule_atom_index_1_dist_mean',
'molecule_atom_index_1_dist_min',
'molecule_atom_index_1_dist_max',
'molecule_type_0_dist_std_diff',
'molecule_atom_index_0_dist_min_diff',
'molecule_type_dist_mean_diff',
'x_1',
'molecule_atom_index_0_y_1_max',
'molecule_atom_index_0_y_1_mean_diff',
'molecule_atom_1_dist_std_diff',
'molecule_atom_index_0_y_1_mean',
'molecule_atom_1_dist_std',
'molecule_type_dist_max']
X = X[good_columns].copy()
X_test = X_test[good_columns].copy()
train_data['workingday'].value_counts()<define_variables>
n_fold = 8
folds = KFold(n_splits=n_fold, shuffle=True, random_state=11 )
lookback = 3
horizon = 2
month = 1
year = 2011<prepare_x_and_y>
params = {'num_leaves': 50,
'min_child_samples': 79,
'min_data_in_leaf': 100,
'objective': 'regression',
'max_depth': 9,
'learning_rate': 0.3,
"boosting_type": "gbdt",
"subsample_freq": 1,
"subsample": 0.9,
"bagging_seed": 11,
"metric": 'mae',
"verbosity": -1,
'reg_alpha': 0.1,
'reg_lambda': 0.3,
'colsample_bytree': 1.0
}
def create_data(data, lookback, horizon, start, end, kind):
X, y = [], []
if kind == 'train':
start_shifted = start
else:
start_shifted = start - datetime.timedelta(hours=lookback)
 temp = data[start_shifted:end].copy()
temp.index = range(len(temp.index))
start_ix = lookback
n_samples = int(( len(temp.index)-lookback)/horizon)
 for i in range(n_samples):
end_ix = start_ix+horizon
seq_X = temp[(start_ix-lookback):start_ix][data.columns.difference(no_features)].values
seq_y = temp[start_ix:end_ix]['count'].values
X.append(seq_X)
 y.append(seq_y)
 start_ix = end_ix
return np.array(X), np.array(y )<prepare_x_and_y>
X_short = pd.DataFrame({'ind': list(X.index), 'type': X['type'].values, 'oof': [0] * len(X), 'target': y_fc.values})
 X_short_test = pd.DataFrame({'ind': list(X_test.index), 'type': X_test['type'].values, 'prediction': [0] * len(X_test)})
 result_dict_lgb_oof = {}
for t in X['type'].unique() :
print(f'Training of type {t}')
 X_t = X.loc[X['type'] == t]
X_test_t = X_test.loc[X_test['type'] == t]
y_t = X_short.loc[X_short['type'] == t, 'target']
result_dict_lgb_oof = train_model_regression(X=X_t, X_test=X_test_t, y=y_t, params=params, folds=folds, model_type='lgb', eval_metric='group_mae', plot_feature_importance=False,
verbose=500, early_stopping_rounds=200, n_estimators=n_estimators_default)
 X_short.loc[X_short['type'] == t, 'oof'] = result_dict_lgb_oof['oof']
X_short_test.loc[X_short_test['type'] == t, 'prediction'] = result_dict_lgb_oof['prediction']
lookback = 24
horizon = 1
month = 12
year = 2012
start_train = datetime.datetime(year, month, 1, 0, 0, 0)
 end_train = datetime.datetime(year, month, 19, 23, 0, 0)
 start_test = datetime.datetime(year, month, 20, 0, 0, 0)
 last_day_of_month = calendar.monthrange(year,month)[1]
end_test = datetime.datetime(year, month, last_day_of_month, 23, 0, 0)
 data = pd.concat([train_data[start_train:end_train],
test_data[start_test:end_test]])
 features_no_scale = ['season', 'holiday', 'workingday', 'count_mean']
data_scaled = data.copy()
data_mean = data_scaled.mean(axis=0)
 data_scaled[data.columns.difference(features_no_scale)] -= data_mean
data_std = data_scaled.std(axis=0)
 data_scaled[data.columns.difference(features_no_scale)] /= data_std
X_train, y_train = create_data(data_scaled, lookback, horizon, start_train, end_train, 'train')
 print(X_train[0])
 n_steps = X_train.shape[1]
n_features = X_train.shape[2]
model = Sequential()
model.add(LSTM(64, return_sequences=True, input_shape=(n_steps, n_features)))
 model.add(SimpleRNN(64, return_sequences=True, input_shape=(n_steps, n_features)))
 model.add(LSTM(32))
model.add(Dense(horizon))
optimizer = Adam(lr=1e-4)
 model.compile(optimizer=optimizer, loss='mse', metrics = ['mse'])
 history = model.fit(X_train, y_train,
epochs=1, verbose=1)
 start_test_temp = start_test
n_days = last_day_of_month-19
for i in range(n_days*24+1):
end_test_temp = start_test+datetime.timedelta(hours=i)
 X_test, y_test = create_data(data_scaled, lookback, horizon, start_test_temp, end_test_temp, 'test')
 if X_test.size != 0:
y_test = model.predict(X_test, verbose=0)
 data_scaled.loc[start_test_temp:(end_test_temp-datetime.timedelta(hours=1)) , 'count'] = y_test[0]
start_test_temp = end_test_temp
samp_subm.loc[start_test:end_test, 'count'] = data_scaled.loc[samp_subm[start_test:end_test].index]['count']*data_std['count']+data_mean['count']
samp_subm['count'] = np.where(samp_subm['count']<0, 0, samp_subm['count'])
 samp_subm['count'] = samp_subm['count'].interpolate()<define_variables>
X['oof_fc'] = X_short['oof']
X_test['oof_fc'] = X_short_test['prediction']
lookback = 24
horizon = 1
n_months = 12
year_list = [2011, 2012] 
<prepare_x_and_y>
good_columns = ['type','oof_fc',
'bond_lengths_mean_y',
'bond_lengths_median_y',
'bond_lengths_std_y',
'bond_lengths_mean_x',
'molecule_atom_index_0_dist_min_div',
'molecule_atom_index_0_dist_std_div',
'molecule_atom_index_0_dist_mean',
'molecule_atom_index_0_dist_max',
'dist_y',
'molecule_atom_index_1_dist_std_diff',
'z_0',
'molecule_type_dist_min',
'molecule_atom_index_0_y_1_mean_div',
'dist_x',
'x_0',
'y_0',
'molecule_type_dist_std',
'molecule_atom_index_0_y_1_std',
'molecule_dist_mean',
'molecule_atom_index_0_dist_std_diff',
'dist_z',
'molecule_atom_index_0_dist_std',
'molecule_atom_index_0_x_1_std',
'molecule_type_dist_std_diff',
'molecule_type_0_dist_std',
'dist',
'molecule_atom_index_0_dist_mean_diff',
'molecule_atom_index_1_dist_min_div',
'molecule_atom_index_1_dist_mean_diff',
'y_1',
'molecule_type_dist_mean_div',
'molecule_dist_max',
'molecule_atom_index_0_dist_mean_div',
'z_1',
'molecule_atom_index_0_z_1_std',
'molecule_atom_index_1_dist_mean_div',
'molecule_atom_index_1_dist_min_diff',
'molecule_atom_index_1_dist_mean',
'molecule_atom_index_1_dist_min',
'molecule_atom_index_1_dist_max',
'molecule_type_0_dist_std_diff',
'molecule_atom_index_0_dist_min_diff',
'molecule_type_dist_mean_diff',
'x_1',
'molecule_atom_index_0_y_1_max',
'molecule_atom_index_0_y_1_mean_diff',
'molecule_atom_1_dist_std_diff',
'molecule_atom_index_0_y_1_mean',
'molecule_atom_1_dist_std',
'molecule_type_dist_max']
X = X[good_columns].copy()
X_test = X_test[good_columns].copy()
n_samples = 0
n_features = len(train_data[train_data.columns.difference(no_features)].columns)
 print('features used: ', train_data[train_data.columns.difference(no_features)].columns)
 X_train, y_train = np.empty(shape=(n_samples, lookback, n_features)) , np.empty(shape=(n_samples, horizon))
for year in year_list:
for month in range(1,n_months+1):
start_train = datetime.datetime(year, month, 1, 0, 0, 0)
 end_train = datetime.datetime(year, month, 19, 23, 0, 0)
 data = train_data[start_train:end_train]
features_no_scale = ['season', 'holiday', 'workingday', 'count_mean']
data_scaled = data.copy()
data_mean = data_scaled.mean(axis=0)
 data_scaled[data.columns.difference(features_no_scale)] -= data_mean
data_std = data_scaled.std(axis=0)
 data_scaled[data.columns.difference(features_no_scale)] /= data_std
X_temp, y_temp = create_data(data_scaled, lookback, horizon, start_train, end_train, 'train')
 X_train = np.vstack(( X_train, X_temp))
y_train = np.vstack(( y_train, y_temp)) 
<choose_model_class>
params = {'num_leaves': 50,
'min_child_samples': 79,
'min_data_in_leaf': 100,
'objective': 'regression',
'max_depth': 9,
'learning_rate': 0.3,
"boosting_type": "gbdt",
"subsample_freq": 1,
"subsample": 0.9,
"bagging_seed": 11,
"metric": 'mae',
"verbosity": -1,
'reg_alpha': 0.1,
'reg_lambda': 0.3,
'colsample_bytree': 1.0
}
n_steps = X_train.shape[1]
n_features = X_train.shape[2]
model = Sequential()
model.add(LSTM(64, return_sequences=True, input_shape=(n_steps, n_features), dropout=0.0, recurrent_dropout=0.2,))
model.add(LSTM(64, return_sequences=True, input_shape=(n_steps, n_features), dropout=0.0, recurrent_dropout=0.2,))
model.add(LSTM(32))
model.add(Dense(horizon))
optimizer = Adam(lr=1e-4)
 model.compile(optimizer=optimizer, loss='mse', metrics = ['mse'])
 history = model.fit(X_train, y_train,
validation_split=0.1,
epochs=150, verbose=1 )<define_variables>
X_short = pd.DataFrame({'ind': list(X.index), 'type': X['type'].values, 'oof': [0] * len(X), 'target': y.values})
 X_short_test = pd.DataFrame({'ind': list(X_test.index), 'type': X_test['type'].values, 'prediction': [0] * len(X_test)})
 for t in X['type'].unique() :
print(f'Training of type {t}')
 X_t = X.loc[X['type'] == t]
X_test_t = X_test.loc[X_test['type'] == t]
y_t = X_short.loc[X_short['type'] == t, 'target']
result_dict_lgb3 = train_model_regression(X=X_t, X_test=X_test_t, y=y_t, params=params, folds=folds, model_type='lgb', eval_metric='group_mae', plot_feature_importance=False,
verbose=500, early_stopping_rounds=200, n_estimators=n_estimators_default)
 X_short.loc[X_short['type'] == t, 'oof'] = result_dict_lgb3['oof']
X_short_test.loc[X_short_test['type'] == t, 'prediction'] = result_dict_lgb3['prediction']
<save_to_csv><EOS> 
sub['scalar_coupling_constant'] = X_short_test['prediction']
sub.to_csv('submission_type.csv', index=False)
 sub.head()