Kaggle competition: Titanic - Machine Learning from Disaster
preds = mpnn.predict({'adj_input' : in_edges_test, 'nod_input': nodes_test}, verbose=1 )<save_model>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
np.save("preds_kernel.npy" , preds )<groupby>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
test_group = test.groupby('molecule_name' )<normalization>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
preds = preds.reshape(( -1,max_size, max_size))
out_unscaled = make_outs(test_group, preds )<feature_engineering>
train.Survived.value_counts()
test['scalar_coupling_constant'] = out_unscaled
coups_to_isolate = ['1JHC', '1JHN', '2JHC', '2JHH', '2JHN', '3JHC', '3JHH', '3JHN']
for i, coup in enumerate(coups_to_isolate):
scale_min = train['scalar_coupling_constant'].loc[train.type == coup].min()
scale_max = train['scalar_coupling_constant'].loc[train.type == coup].max()
scale_mid =(scale_max + scale_min)/2
scale_norm = scale_max - scale_mid
test.loc[test.type == coup, 'scalar_coupling_constant'] = test['scalar_coupling_constant'].loc[test.type == coup]*scale_norm + scale_mid
test.loc[test.type == coup, 'pred_scalar_coupling_constant'] = test['scalar_coupling_constant'].loc[test.type == coup] 
<save_to_csv>
train.isna().sum()
test[['id','scalar_coupling_constant']].to_csv('submission.csv', index=False )<set_options>
train.loc[train.Age.isna() , 'Age'] = train[~train.Age.isna() ].Age.mean()
%matplotlib inline 
<define_variables>
train.loc[train.Cabin.isna() ,'Cabin'] = "No Cabin"
DATA_PATH = '.. /input'
SUBMISSIONS_PATH = './'
ATOMIC_NUMBERS = {
'H': 1,
'C': 6,
'N': 7,
'O': 8,
'F': 9
}<set_options>
print(train.Embarked.value_counts())
 train.loc[train.Embarked.isna() ,'Embarked'] = "S"
pd.set_option('display.max_colwidth', -1)
 pd.set_option('display.max_rows', 120)
 pd.set_option('display.max_columns', 120 )<load_from_csv>
train.isna().sum()
train_csv = pd.read_csv('.. /input/data-of-distance-qm9-giba/fin_train.csv')
 test_csv = pd.read_csv('.. /input/data-of-distance-qm9-giba/fin_test.csv' )<load_from_csv>
train.loc[train.Fare > 200]
train_csv = reduce_mem_usage(train_csv,verbose = True)
 test_csv = reduce_mem_usage(test_csv,verbose = True )<save_to_csv>
train.Sex.value_counts()
train_csv.set_index('id',inplace = True)
 test_csv.set_index('id',inplace = True )<load_from_csv>
labelencoder = LabelEncoder()
train['Sex'] = labelencoder.fit_transform(train['Sex'])
 train.Sex.value_counts()
structures_csv = pd.read_csv(f'{DATA_PATH}/champs-scalar-coupling/structures.csv')
 structures_csv['molecule_index'] = structures_csv.molecule_name.str.replace('dsgdb9nsd_', '' ).astype('int32')
 structures_csv = structures_csv[['molecule_index', 'atom_index', 'atom', 'x', 'y', 'z']]
structures_csv['atom'] = structures_csv['atom'].replace(ATOMIC_NUMBERS ).astype('int8')
 structures_csv.head(10 )<data_type_conversions>
def features_engineering(df):
df.loc[df.Age.isna() , 'Age'] = df[~df.Age.isna() ].Age.mean()
df.loc[df.Cabin.isna() ,'Cabin'] = "No Cabin"
df.loc[df.Embarked.isna() ,'Embarked'] = "S"
df['persons_abroad_size'] =(df['Parch']+df['SibSp'] ).astype(int)
 df['alone'] = np.where(df['Parch']==0,1,0)
 df['Embarked'] = df['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 df['Sex'] = df['Sex'].map({'male': 1, 'female': 2} ).astype(int)
 df['log_fare'] = df['Fare'].apply(np.log)
 df['Room'] =(df['Cabin'] 
.str.slice(1,5 ).str.extract('([0-9]+)', expand=False)
.fillna(0)
.astype(int))
df['RoomBand'] = 0
df.loc[(df.Room > 0)&(df.Room <= 20), 'RoomBand'] = 1
df.loc[(df.Room > 20)&(df.Room <= 40), 'RoomBand'] = 2
df.loc[(df.Room > 40)&(df.Room <= 80), 'RoomBand'] = 3
df.loc[df.Room > 80, 'RoomBand'] = 4
df_id = df.PassengerId
df = df.drop('PassengerId', axis=1)
 return df,df_id
def build_type_dataframes(base, structures, coupling_type):
base = base[base['type'] == coupling_type].drop('type', axis=1 ).copy()
base = base.reset_index()
base['id'] = base['id'].astype('int32')
 structures = structures[structures['molecule_index'].isin(base['molecule_index'])]
return base, structures<merge>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 train,train_id = features_engineering(train)
 test,test_id = features_engineering(test )
def add_coordinates(base, structures, index):
df = pd.merge(base, structures, how='inner',
left_on=['molecule_index', f'atom_index_{index}'],
right_on=['molecule_index', 'atom_index'] ).drop(['atom_index'], axis=1)
 df = df.rename(columns={
'atom': f'atom_{index}',
'x': f'x_{index}',
'y': f'y_{index}',
'z': f'z_{index}'
})
 return df<merge>
X_train = train.drop('Survived',axis=1 ).select_dtypes(include=['int32','int64','float64'])
 y_train = train['Survived']
X_test = test.select_dtypes(include=['int32','int64','float64'])
 xg_boost = xgb.XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,
colsample_bytree=0.65, gamma=2, learning_rate=0.3, max_delta_step=1,
max_depth=4, min_child_weight=2, missing=None, n_estimators=280,
n_jobs=1, nthread=None, objective='binary:logistic', random_state=0,
reg_alpha=0, reg_lambda=1, scale_pos_weight=1, seed=None,
silent=True, subsample=1 )
def add_atoms(base, atoms):
df = pd.merge(base, atoms, how='inner',
on=['molecule_index', 'atom_index_0', 'atom_index_1'])
 return df<merge>
xg_boost.fit(X_train, y_train )
def merge_all_atoms(base, structures):
df = pd.merge(base, structures, how='left',
left_on=['molecule_index'],
right_on=['molecule_index'])
 df = df[(df.atom_index_0 != df.atom_index)&(df.atom_index_1 != df.atom_index)]
return df<feature_engineering>
print(xg_boost.score(X_train, y_train))
scores = model_selection.cross_val_score(xg_boost, X_train, y_train, cv=5, scoring='accuracy')
 print(scores)
 print("Kfold on XGBClassifier: %0.4f(+/- %0.4f)" %(scores.mean() , scores.std()))
def add_center(df):
df['x_c'] =(( df['x_1'] + df['x_0'])* np.float32(0.5))
df['y_c'] =(( df['y_1'] + df['y_0'])* np.float32(0.5))
df['z_c'] =(( df['z_1'] + df['z_0'])* np.float32(0.5))
def add_distance_to_center(df):
df['d_c'] =(( 
(df['x_c'] - df['x'])**np.float32(2)+ 
(df['y_c'] - df['y'])**np.float32(2)+ 
(df['z_c'] - df['z'])**np.float32(2)
)**np.float32(0.5))
def add_distance_between(df, suffix1, suffix2):
df[f'd_{suffix1}_{suffix2}'] =(( 
(df[f'x_{suffix1}'] - df[f'x_{suffix2}'])**np.float32(2)+ 
(df[f'y_{suffix1}'] - df[f'y_{suffix2}'])**np.float32(2)+ 
(df[f'z_{suffix1}'] - df[f'z_{suffix2}'])**np.float32(2)
)**np.float32(0.5))<categorify>
Y_pred = xg_boost.predict(X_test )
def add_distances(df):
n_atoms = 1 + max([int(c.split('_')[1])for c in df.columns if c.startswith('x_')])
 for i in range(1, n_atoms):
for vi in range(min(4, i)) :
add_distance_between(df, i, vi )<merge>
submission = pd.DataFrame({
"PassengerId": test_id,
"Survived": Y_pred
})
 submission.head(10 )
def add_n_atoms(base, structures):
dfs = structures['molecule_index'].value_counts().rename('n_atoms' ).to_frame()
return pd.merge(base, dfs, left_on='molecule_index', right_index=True )<drop_column>
submission.to_csv('submission.csv', index=False )
<define_variables><EOS> 
submission.to_csv('submission.csv', index=False )