Kaggle competition: Plant Pathology 2020 - FGVC7
<SOS> metric: MCAUC Kaggle data source: plant-pathology-2020-fgvc7<concatenate>
!pip install fastai2
!pip install pretrainedmodels
del train['matchType']
del test['matchType']
all_data = pd.concat(( train.loc[:,'assists':'winPoints'],
test.loc[:,'assists':'winPoints']))
train = train.loc[:,'assists':'winPlacePerc'] 
<data_type_conversions>
warnings.filterwarnings('ignore' )
all_data = all_data.fillna(all_data.mean())
 train = train.fillna(train.mean() )<prepare_x_and_y>
def seed_everything(seed):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed_all(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
SEED=42
seed_everything(SEED )
X_train = all_data[:train.shape[0]]
X_test = all_data[train.shape[0]:]<import_modules>
working_path=Path('.. /input/plant-pathology-2020-fgvc7')
 image_path = working_path/'images'
train_path = working_path/'train.csv'
test_path = working_path/'test.csv'
sample_path = working_path/'sample_submission.csv'
import gc
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from catboost import CatBoostRegressor
import lightgbm as lgb<split>
train_df = pd.read_csv(train_path)
 train_df.head(10 )
x_train, x_valid, y_train, y_valid = train_test_split(X_train, train['winPlacePerc'],test_size=0.15, random_state=27 )<choose_model_class>
train_df.set_index('image_id',inplace=True)
 train_df = train_df[train_df==1].stack().reset_index().drop(0,1 )
model_lgb = lgb.LGBMRegressor(objective='regression',num_leaves=5,
learning_rate=0.1, n_estimators=2000,
max_bin = 55, bagging_fraction = 0.8,
bagging_freq = 5, feature_fraction = 0.2319,
feature_fraction_seed=9, bagging_seed=9,
min_data_in_leaf =6, min_sum_hessian_in_leaf = 11)
<train_model>
test_df = pd.read_csv(test_path )
model_lgb.fit(x_train, y_train, eval_set=[(x_train, y_train),(x_valid, y_valid)],
eval_metric= 'mae', verbose=1, early_stopping_rounds=50 )<predict_on_test>
def get_model(model, cut=-2, head=None, from_save=None):
children = list(model.children())
 head = ifnone(head, nn.Sequential(nn.AdaptiveAvgPool2d(1), Flatten() ,
nn.Linear(children[-1].in_features,4)))
 model = nn.Sequential(nn.Sequential(*children[:cut]), head)
 if from_save:
state_dict = torch.load(f'models/{from_save}.pth')
 model[0].load_state_dict(state_dict)
 return model
predictions_ = model_lgb.predict(X_test )<save_to_csv>
def create_model(dls,model,metrics):
model = get_model(model)
 return Learner(dls,model,metrics=metrics,opt_func=ranger)
 def train_learner(learn,lr,epochs):
learn.freeze()
learn.fit_flat_cos(1,lr)
 learn.unfreeze()
lr /= 2
learn.fit_flat_cos(epochs,slice(lr/100,lr))
return learn
def validate_learner(dls,learn,test_df):
test_dl = dls.test_dl(test_df)
 _, metric = learn.validate()
preds, _ = learn.get_preds(dl=test_dl)
 return metric, preds
my_submission = pd.DataFrame({'Id':test.Id, 'winPlacePerc': predictions_})
 print(predictions_[0:10])
 my_submission.to_csv('submission.csv', index=False )<define_variables>
kf = StratifiedKFold(n_splits=5,shuffle=True,random_state=SEED )
gc.enable()
INPUT_DIR = ".. /input/"<load_from_csv>
fold_scores = []
fold_preds = []
i=0
for train_idx, valid_idx in kf.split(train_df,train_df.level_1):
data_block, dls = get_data(train_df,valid_idx,256)
 model = pretrainedmodels.se_resnext50_32x4d(pretrained='imagenet')
 learn = create_model(dls,model,RocAuc())
 learn = train_learner(learn,5e-3,10)
 metric, preds = validate_learner(dls,learn,test_df)
 learn.export('export'+str(i)+'.pkl')
 fold_scores.append(metric)
 fold_preds.append(preds)
 i+=1
fold_preds = torch.mean(torch.stack(fold_preds),dim=0 ).cpu().numpy()
def feature_engineering(is_train=True):
if is_train:
print("processing train.csv")
 df = pd.read_csv(INPUT_DIR + 'train_V2.csv')
 df = df[df['maxPlace'] > 1]
else:
print("processing test.csv")
 df = pd.read_csv(INPUT_DIR + 'test_V2.csv')
 df['totalDistance'] = df['rideDistance'] + df["walkDistance"] + df["swimDistance"]
print("remove some columns")
 target = 'winPlacePerc'
features = list(df.columns)
 features.remove("Id")
 features.remove("teamKills")
 features.remove("vehicleDestroys")
 features.remove("swimDistance")
 features.remove("walkDistance")
 features.remove("rideDistance")
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchType")
 y = None
print("get target")
 if is_train:
y = np.array(df.groupby(['matchId','groupId'])[target].agg('mean'), dtype=np.float64)
 features.remove(target)
 print("get group mean feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('mean')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
if is_train: df_out = agg.reset_index() [['matchId','groupId']]
else: df_out = df[['matchId','groupId']]
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_mean", "_mean_rank"], how='left', on=['matchId', 'groupId'])
 print("get group max feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('max')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_max", "_max_rank"], how='left', on=['matchId', 'groupId'])
 print("get group min feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('min')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_min", "_min_rank"], how='left', on=['matchId', 'groupId'])
 print("get group size feature")
 agg = df.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_out = df_out.merge(agg, how='left', on=['matchId', 'groupId'])
 print("get match mean feature")
 agg = df.groupby(['matchId'])[features].agg('mean' ).reset_index()
df_out = df_out.merge(agg, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 print("get match size feature")
 agg = df.groupby(['matchId'] ).size().reset_index(name='match_size')
 df_out = df_out.merge(agg, how='left', on=['matchId'])
 df_out.drop(["matchId", "groupId"], axis=1, inplace=True)
 X = np.array(df_out, dtype=np.float64)
 feature_names = list(df_out.columns)
 del df, df_out, agg, agg_rank
gc.collect()
return X, y, feature_names<normalization>
learn = create_model(dls,model,RocAuc())
 learn.lr_find()
x_train, y, feature_names = feature_engineering(True)
 scaler = preprocessing.MinMaxScaler(feature_range=(-1, 1), copy=False ).fit(x_train)
 print("x_train", x_train.shape, x_train.max() , x_train.min())
 scaler.transform(x_train)
 print("x_train", x_train.shape, x_train.max() , x_train.min())
 y = y * 2 - 1
print("y", y.shape, y.max() , y.min())
 epoch_train = 36
rate_init=0.08
hidden_size = 32
verbose=1
epoch_decay=2
mlp = MLP(layer_size=[x_train.shape[1], hidden_size, hidden_size, hidden_size, 1],bias_rate=[], regularization=1,importance_mul=0.0001, output_shrink=0.1, output_range=[-1,1], loss_type="hardmse")
 feature_importance = mlp.train(x_train, y, verbose=verbose, importance_out=True, iteration_log=20000, rate_init=rate_init, rate_decay=0.8, epoch_train=epoch_train, epoch_decay=epoch_decay)
 del x_train, y
gc.collect()
feature_importance = list(zip(feature_names, feature_importance))
feature_importance.sort(key=lambda x:x[1], reverse=True)
 print(feature_importance)
 x_test, _, _ = feature_engineering(False)
 scaler.transform(x_test)
 print("x_test", x_test.shape, x_test.max() , x_test.min())
 np.clip(x_test, out=x_test, a_min=-1, a_max=1)
 print("x_test", x_test.shape, x_test.max() , x_test.min())
 pred = mlp.predict(x_test)
 del x_test
gc.collect()
pred = pred.reshape(-1)
 pred =(pred + 1)/ 2
df_test = pd.read_csv(INPUT_DIR + 'test_V2.csv')
 print("fix winPlacePerc")
 for i in range(len(df_test)) :
winPlacePerc = pred[i]
maxPlace = int(df_test.iloc[i]['maxPlace'])
 if maxPlace == 0:
winPlacePerc = 0.0
elif maxPlace == 1:
winPlacePerc = 1.0
else:
gap = 1.0 /(maxPlace - 1)
 winPlacePerc = round(winPlacePerc / gap)* gap
if winPlacePerc < 0: winPlacePerc = 0.0
if winPlacePerc > 1: winPlacePerc = 1.0
pred[i] = winPlacePerc
if(i + 1)% 100000 == 0:
print(i, flush=True, end=" ")
 df_test['winPlacePerc'] = pred
submission = df_test[['Id', 'winPlacePerc']]
submission.to_csv('submission.csv', index=False )<set_options>
cv_score = np.mean(fold_scores)
 print(f'5-fold CV multi-class AUC-ROC: {cv_score}')
 print(f'Individual CV scores: {fold_scores}' )
!pip install ultimate
gc.enable() 
<define_variables>
sample_submission = pd.read_csv(sample_path)
 sample_submission[['healthy', 'multiple_diseases', 'rust', 'scab']] = fold_preds
sample_submission.head(10 )
<load_from_csv><EOS> 
sample_submission.to_csv('submission.csv',index=False )