Kaggle competition: LANL Earthquake Prediction
<SOS> metric: MAE Kaggle data source: lanl-earthquake-prediction<save_to_csv>
import gc
import numpy as np
import pandas as pd
import lightgbm as lgbm
from sklearn.model_selection import KFold
from sklearn.metrics import mean_absolute_error
prob_sum = 0
for prob in probs:
prob_sum += prob
prob_avg = prob_sum/FOLDS
sub.loc[:, 'healthy':] = prob_avg
sub.to_csv('submission.csv', index=False)
 sub.head()<set_options>
X = pd.read_csv('.. /input/andrews-new-stuff/train_features.csv')
 X_test = pd.read_csv('.. /input/andrews-new-stuff/test_features.csv')
 y = pd.read_csv('.. /input/andrews-new-stuff/y.csv')
 submission = pd.read_csv('.. /input/andrews-new-stuff/submission.csv')
 X['target'] = y.target
X_test['target'] = -1
warnings.filterwarnings('ignore')
 __print__ = print
def print(string):
os.system(f'echo "{string}"')
 __print__(string )<define_variables>
lgbm_params = {
'task': 'train',
'boosting_type': 'gbdt',
'objective': 'binary',
'metric': 'binary_logloss',
"learning_rate": 0.01,
"num_leaves": 100,
"feature_fraction":.5,
"bagging_fraction":.5,
"max_depth": -1,
"reg_alpha": 0.3,
"reg_lambda": 0.1,
"min_child_weight":10,
"n_jobs":4
}
DIR_INPUT = '/kaggle/input/plant-pathology-2020-fgvc7'
SEED = 42
N_FOLDS = 5
N_EPOCHS = 10
BATCH_SIZE = 64
SIZE = 512<define_search_model>
bestparams = {0:(343, 0.33300775159479323),
1:(377, 0.3937832899637623),
2:(381, 0.43248276195861485),
3:(368, 0.4567765830007994),
4:(279, 0.4364333328398183),
5:(301, 0.3130615907253661)}
class PlantModel(nn.Module):
def __init__(self, num_classes=4):
super().__init__()
self.backbone = torchvision.models.resnet18(pretrained=True)
 in_features = self.backbone.fc.in_features
self.logit = nn.Linear(in_features, num_classes)
 def forward(self, x):
batch_size, C, H, W = x.shape
x = self.backbone.conv1(x)
 x = self.backbone.bn1(x)
 x = self.backbone.relu(x)
 x = self.backbone.maxpool(x)
 x = self.backbone.layer1(x)
 x = self.backbone.layer2(x)
 x = self.backbone.layer3(x)
 x = self.backbone.layer4(x)
 x = F.adaptive_avg_pool2d(x,1 ).reshape(batch_size,-1)
 x = F.dropout(x, 0.25, self.training)
 x = self.logit(x)
 return x 
<define_search_model>
folds = KFold(n_splits=5, shuffle=True, random_state=42)
 oof_preds = np.zeros(( X.shape[0],6))
sub_preds = np.zeros(( X_test.shape[0],6))
transforms_train = A.Compose([
A.RandomResizedCrop(height=SIZE, width=SIZE, p=1.0),
A.Flip() ,
A.ShiftScaleRotate(rotate_limit=1.0, p=0.8),
A.OneOf([
A.IAAEmboss(p=1.0),
A.IAASharpen(p=1.0),
A.Blur(p=1.0),
], p=0.5),
A.OneOf([
A.ElasticTransform(p=1.0),
A.IAAPiecewiseAffine(p=1.0)
 ], p=0.5),
A.Normalize(p=1.0),
ToTensorV2(p=1.0),
])
 transforms_valid = A.Compose([
A.Resize(height=SIZE, width=SIZE, p=1.0),
A.Normalize(p=1.0),
ToTensorV2(p=1.0),
] )<load_from_csv>
for i in [0, 1, 2, 3, 4, 5]:
optimal_rounds, best_cv_score = bestparams[i]
print(i, optimal_rounds, best_cv_score)
 for n_fold,(trn_idx, val_idx)in enumerate(folds.split(X)) :
print(n_fold)
 trn_x, trn_y = X[feats].iloc[trn_idx], x[trn_idx]>i
val_x, val_y = X[feats].iloc[val_idx], x[val_idx]>i
clf = lgbm.train(lgbm_params,
lgbm.Dataset(trn_x,trn_y),
num_boost_round = optimal_rounds + 1,
verbose_eval=200)
 oof_preds[val_idx,i] = clf.predict(val_x, num_iteration=optimal_rounds + 1)
 sub_preds[:,i] += clf.predict(X_test[feats], num_iteration=optimal_rounds + 1)/ folds.n_splits
del clf
del trn_x, trn_y, val_x, val_y
gc.collect()
submission_df = pd.read_csv(DIR_INPUT + '/sample_submission.csv')
 submission_df.iloc[:, 1:] = 0
submission_df.head()<load_pretrained>
off_preds_withbias = np.hstack([oof_preds,np.ones(shape=(oof_preds.shape[0],1)) ])
 sub_preds_withbias = np.hstack([sub_preds,np.ones(shape=(sub_preds.shape[0],1)) ] )
dataset_test = PlantDataset(df=submission_df, transforms=transforms_valid)
 dataloader_test = DataLoader(dataset_test, batch_size=BATCH_SIZE, num_workers=4, shuffle=False )<load_from_csv>
params = np.linalg.lstsq(off_preds_withbias, y.target,rcond=-1)[0]
train_df = pd.read_csv(DIR_INPUT + '/train.csv')
 train_labels = train_df.iloc[:, 1:].values
train_y = train_labels[:, 2] + train_labels[:, 3] * 2 + train_labels[:, 1] * 3
train_df.head()<choose_model_class>
trainpreds = np.dot(off_preds_withbias,params)
 print(mean_absolute_error(y.target,trainpreds))
<choose_model_class><EOS> 
testpreds = np.dot(sub_preds_withbias,params)
 sub = pd.DataFrame({'seg_id':submission.seg_id, 'time_to_failure':testpreds})
 sub.to_csv('cut.csv',index=False )