Kaggle competition: PLAsTiCC Astronomical Classification
train_X = pd.concat([train_X_0, train_X_1], axis=0)
 train_X = train_X.reset_index(drop=True)
 print(train_X.shape)
 train_X.head()<concatenate>
trmeta=pd.read_csv('.. /input/PLAsTiCC-2018/training_set_metadata.csv')
 temeta=pd.read_csv('.. /input/PLAsTiCC-2018/test_set_metadata.csv')
 pdf=pd.read_csv('.. /input/blendmodels/blend_submission.csv')
 print(trmeta.shape)
 print(temeta.shape)
 print(pdf.shape )
y = pd.concat([y_0, y_1], axis=0)
 y = y.reset_index(drop=True)
 y[0].shape<load_from_csv>
def setZeroProbas(opdf, temeta, igClasses, egClasses):
pdf=copy.deepcopy(opdf)
 pdf=pdf.merge(temeta, on='object_id')
 rdf=pd.DataFrame()
rdf['object_id']=pdf['object_id']
igFilter=pdf.loc[:,'distmod']==0
egFilter=pdf.loc[:,'distmod']!=0
for eg in egClasses:
pdf.loc[igFilter,'class_' + str(eg)]=0
rdf.loc[:,'class_' + str(eg)]=pdf.loc[:,'class_' + str(eg)]
for ig in igClasses:
pdf.loc[egFilter,'class_' + str(ig)]=0
rdf.loc[:,'class_' + str(ig)]=pdf.loc[:,'class_' + str(ig)]
rdf['class_99']=pdf['class_99']
return rdf
test_X = pd.read_csv(".. /input/lanl-master-s-features-creating-0/test_X_features_10.csv")
<normalization>
predictions=setZeroProbas(pdf, temeta, igClasses, egClasses)
 predictions.to_csv('justSetZeroProbas.csv', index=False)
scaler = MinMaxScaler()
train_columns = train_X.columns
train_X[train_columns] = scaler.fit_transform(train_X[train_columns])
 test_X[train_columns] = scaler.transform(test_X[train_columns] )<define_variables>
fil={}
distmod={}
sigma={}
trFracs={}
teFracs={}
teMult={}
for i in trmeta.loc[:,'target'].unique() :
fil[i] = trmeta['target']==i
distmod[i] = np.average(trmeta.loc[fil[i],'distmod'])
 sigma[i] = np.std(trmeta.loc[fil[i],'distmod'])
 print('class ' + str(i)+ ': ' + str(distmod[i])+' +/- ' + str(sigma[i]))
print(fil[i].sum())
 for ig in igClasses:
trFracs[ig]=fil[ig].sum() / trmeta.shape[0]
teFracs[ig]=trFracs[ig] * teIgFrac / trIgFrac
print('class ' + str(ig)+ 'tr : ' + str(trFracs[ig])+', te : ' + str(teFracs[ig]))
for eg in egClasses:
trFracs[eg]=fil[eg].sum() / trmeta.shape[0]
teFracs[eg]=trFracs[eg] * teEgFrac / trEgFrac
print('class ' + str(eg)+ 'tr : ' + str(trFracs[eg])+', te : ' + str(teFracs[eg]))
train_columns = train_X.columns
n_fold = 5<split>
def applyRebalance(ordf, teMult):
rdf=copy.deepcopy(ordf)
 for cindex in rdf.columns:
if cindex != 'object_id':
theClass = int(cindex[6:])
 print(theClass)
 rdf[cindex]*=teMult[theClass]
return rdf
%%time
folds = KFold(n_splits=n_fold, shuffle = True, random_state=42)
 oof = np.zeros(len(train_X))
train_score = []
fold_idxs = []
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.values)) :
strLog = "fold {}".format(fold_)
 print(strLog)
 fold_idxs.append(val_idx)
 X_tr, X_val = train_X[train_columns].iloc[trn_idx], train_X[train_columns].iloc[val_idx]
y_tr, y_val = train_y.iloc[trn_idx], train_y.iloc[val_idx]
model = CatBoostRegressor(n_estimators=25000, verbose=-1, objective="MAE", loss_function="MAE", boosting_type="Ordered", task_type="GPU")
 model.fit(X_tr,
y_tr,
eval_set=[(X_val, y_val)],
verbose=2500,
early_stopping_rounds=500)
 oof[val_idx] = model.predict(X_val)
 fold_importance_df = pd.DataFrame()
fold_importance_df["Feature"] = train_columns
fold_importance_df["importance"] = model.feature_importances_[:len(train_columns)]
fold_importance_df["fold"] = fold_ + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 predictions += model.predict(test_X[train_columns])/ folds.n_splits
train_score.append(model.best_score_['learn']["MAE"])
 cv_score = mean_absolute_error(train_y, oof)
 print(f"After {n_fold} test_CV = {cv_score:.3f} | train_CV = {np.mean(train_score):.3f} | {cv_score-np.mean(train_score):.3f}", end=" " )<save_to_csv>
def GenUnknown(data):
return(((((( data["mymedian"])+(((data["mymean"])/ 2.0)))/2.0)) +(((((1.0)-(((data["mymax"])*(((data["mymax"])*(data["mymax"])))))))/ 2.0)))/2.0 )
today = str(datetime.date.today())
 submission = pd.read_csv('.. /input/LANL-Earthquake-Prediction/sample_submission.csv')
 submission["time_to_failure"] = predictions
submission.to_csv(f'CatBoost_{today}_test_{cv_score:.3f}_train_{np.mean(train_score):.3f}.csv', index=False)
 submission.head()<choose_model_class>
feats = ['class_6', 'class_15', 'class_16', 'class_42', 'class_52', 'class_53',
'class_62', 'class_64', 'class_65', 'class_67', 'class_88', 'class_90',
'class_92', 'class_95']
y = pd.DataFrame()
y['mymean'] = predictions[feats].mean(axis=1)
 y['mymedian'] = predictions[feats].median(axis=1)
 y['mymax'] = predictions[feats].max(axis=1)
 predictions['class_99'] = GenUnknown(y )
def create_model(input_dim=10):
model = Sequential()
model.add(Dense(256, activation="relu",input_dim=input_dim))
model.add(Dropout(0.5))
model.add(Dense(128, activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(64, activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(1, activation="linear"))
opt = adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
 model.compile(
 loss='mae',
optimizer=opt, 
)
 return model
patience = 50
call_ES = EarlyStopping(monitor='val_loss', min_delta=0, patience=patience, verbose=1, mode='auto', baseline=None, restore_best_weights=True )<split>
def modUnknown(opdf, meta, ddfMult=0.5, mwMult=0.5, preserveMed=False):
pdf=copy.deepcopy(opdf)
 mdf=pdf.merge(meta,on='object_id')
 ddfilter=mdf.loc[:,'ddf']==1
mwfilter=mdf.loc[:,'hostgal_photoz']==0
print(ddfilter.sum())
 print(mwfilter.sum())
 mdf.loc[mwfilter,'class_99']=mwMult*mdf.loc[mwfilter,'class_99']
mdf.loc[ddfilter,'class_99']=ddfMult*mdf.loc[ddfilter,'class_99']
pdf.loc[:,'class_99']=mdf.loc[:,'class_99']
return pdf
npdf=modUnknown(predictions, temeta)
 npdf.head()
%%time
folds = KFold(n_splits=n_fold, shuffle = True, random_state=42)
 NN_oof = np.zeros(len(train_X))
train_score = []
fold_idxs = []
NN_predictions = np.zeros(len(test_X))
for fold_,(trn_idx, val_idx)in enumerate(folds.split(train_X,train_y.values)) :
strLog = "fold {}".format(fold_)
 print(strLog)
 fold_idxs.append(val_idx)
 X_tr, X_val = train_X[train_columns].iloc[trn_idx], train_X[train_columns].iloc[val_idx]
y_tr, y_val = train_y.iloc[trn_idx], train_y.iloc[val_idx]
model = create_model(train_X.shape[-1])
 model.fit(X_tr, y_tr, epochs=500, batch_size=32, verbose=0, callbacks=[call_ES,], validation_data=[X_val, y_val])
 NN_oof[val_idx] = model.predict(X_val)[:,0]
NN_predictions += model.predict(test_X[train_columns])[:,0] / folds.n_splits
history = model.history.history
tr_loss = history["loss"]
val_loss = history["val_loss"]
print(f"loss: {tr_loss[-patience]:.3f} | val_loss: {val_loss[-patience]:.3f} | diff: {val_loss[-patience]-tr_loss[-patience]:.3f}")
 train_score.append(tr_loss[-patience])
 cv_score = mean_absolute_error(train_y, NN_oof)
 print(f"After {n_fold} test_CV = {cv_score:.3f} | train_CV = {np.mean(train_score):.3f} | {cv_score-np.mean(train_score):.3f}", end=" " )<save_to_csv>
npdf.to_csv('probaZeroAndRe99.csv', index=False )