Kaggle competition: BNP Paribas Cardif Claims Management
<SOS> metric: LogLoss Kaggle data source: bnp-paribas-cardif-claims-management<train_model>
!pip install -q https://github.com/pfnet-research/xfeat/archive/master.zip
def train_model() :
batch_size = 32
trainGen, validGen = load_data(batch_size=batch_size)
 model = base_model()
opt = Adam(1e-3)
 model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
 cbs = [ReduceLROnPlateau(monitor='loss', factor=0.5, patience=1, min_lr=1e-5, verbose=1)]
model.fit_generator(trainGen, steps_per_epoch=4922, epochs=3, validation_data=validGen, validation_steps=493, shuffle=True, callbacks=cbs)
 return model<save_to_csv>
USECOLS = [
"v10", "v12", "v14", "v21", "v22", "v24", "v30", "v31",
"v34", "v38", "v40", "v47", "v50", "v52", "v56", "v62",
"v66", "v72", "v75", "v79", "v91", "v112", "v113", "v114", "v129",
]
def preload() :
pd.concat([
pd.read_csv(".. /input/bnp-paribas-cardif-claims-management/train.csv.zip"),
pd.read_csv(".. /input/bnp-paribas-cardif-claims-management/test.csv.zip"),
], sort=False ).reset_index(drop=True)[USECOLS + ["target"]].to_feather(".. /working/train_test.ftr")
 preload()
def predict_model() :
test_data = pd.read_csv('.. /input/sample_submission.csv')
 pred = np.empty(( test_data.shape[0],))
for n in tqdm(range(test_data.shape[0])) :
data = np.array(Image.open('.. /input/test/test/'+test_data.id[n]))
data = data.astype(np.float32)/ 255.
 pred[n] = model.predict(data.reshape(( 1, 32, 32, 3)))[0][1]
test_data['has_cactus'] = pred
test_data.to_csv('sample_submission.csv', index=False )<train_model>
print("(1)Save numerical features")
 SelectNumerical().fit_transform(pd.read_feather(".. /working/train_test.ftr")).reset_index(
 drop=True
).to_feather(".. /working/feature_num_features.ftr" )
model = train_model()<import_modules>
print("(2)Categorical encoding using label encoding: 13 features")
 Pipeline([SelectCategorical() , LabelEncoder(output_suffix="")] ).fit_transform(
 pd.read_feather(".. /working/train_test.ftr")
 ).reset_index(drop=True ).to_feather(".. /working/feature_1way_label_encoding.ftr" )
import numpy as np
import pandas as pd
import os 
<import_modules>
print("(3)2-order combination of categorical features: 78 features(13 * 12 / 2 = 78)")
 Pipeline(
 [
SelectCategorical() ,
ConcatCombination(drop_origin=True, r=2),
LabelEncoder(output_suffix=""),
]
).fit_transform(pd.read_feather(".. /working/train_test.ftr")).reset_index(
 drop=True
).to_feather(
 ".. /working/feature_2way_label_encoding.ftr"
)
from fastai.vision import *<define_variables>
print("(4)3-order combination of categorical features")
 Pipeline(
 [
SelectCategorical() ,
ConcatCombination(drop_origin=True, include_cols=["v22"], r=3),
LabelEncoder(output_suffix=""),
]
).fit_transform(pd.read_feather(".. /working/train_test.ftr")).reset_index(
 drop=True
).to_feather(
 ".. /working/feature_3way_including_v22_label_encoding.ftr"
)
data_path = Path('/kaggle/input/aerial-cactus-identification')
 data_path.ls()<load_from_csv>
print("(5)Convert numerical to categorical using round: 12 features")
 df_rnum =(
 Pipeline(
 [
SelectNumerical() ,
LambdaEncoder(
 lambda x: str(x)[:-2],
output_suffix="_rnum",
exclude_cols=["target"], 
),
] 
)
.fit_transform(pd.read_feather(".. /working/train_test.ftr")) 
.reset_index(drop=True)
)
 df_rnum.to_feather(".. /working/feature_round_num.ftr")
 rnum_cols = [col for col in df_rnum.columns if col.endswith("_rnum")]
Pipeline([LabelEncoder(output_suffix="")] ).fit_transform(
 pd.read_feather(".. /working/feature_round_num.ftr")[rnum_cols]
).reset_index(drop=True ).to_feather(".. /working/feature_round_num_label_encoding.ftr" )
np.random.seed(4)
 data = ImageList.from_csv(data_path, 'train.csv', folder='train/train', cols='id')\ 
.split_by_rand_pct(0.1)\ 
.label_from_df(cols='has_cactus')\ 
.transform(get_transforms() , size=224)\ 
.databunch() \ 
.normalize(imagenet_stats )<choose_model_class>
print("(6)2-order Arithmetic combinations.")
 Pipeline(
 [
SelectNumerical() ,
ArithmeticCombinations(
 exclude_cols=["target"], drop_origin=True, operator="+", r=2, 
),
]
).fit_transform(pd.read_feather(".. /working/train_test.ftr")).reset_index(
 drop=True
).to_feather(
 ".. /working/feature_arithmetic_combi2.ftr"
)
learner = cnn_learner(data, models.resnet34, metrics=error_rate)
 learner.model_dir = '/kaggle/working/'<find_best_params>
print("(7)Add more combinations: 11-order concat combinations.")
 Pipeline(
 [
SelectCategorical() ,
ConcatCombination(drop_origin=True, include_cols=["v22"], r=11),
LabelEncoder(output_suffix=""),
]
).fit_transform(pd.read_feather(".. /working/train_test.ftr")).reset_index(
 drop=True
).to_feather(
 ".. /working/feature_11way_including_v22_label_encoding.ftr"
)
<train_model><EOS> 
def catboost_model() :
print("Load numerical features")
 df_num = pd.concat(
 [
pd.read_feather(".. /working/feature_num_features.ftr"),
pd.read_feather(".. /working/feature_arithmetic_combi2.ftr"),
],
axis=1, 
)
 y_train = df_num["target"].dropna()
df_num.drop(["target"], axis=1, inplace=True)
 print("Load categorical features")
 df = pd.concat(
 [
pd.read_feather(".. /working/feature_1way_label_encoding.ftr"),
pd.read_feather(".. /working/feature_2way_label_encoding.ftr"),
pd.read_feather(".. /working/feature_3way_including_v22_label_encoding.ftr"),
pd.read_feather(".. /working/feature_round_num_label_encoding.ftr"),
pd.read_feather(".. /working/feature_11way_including_v22_label_encoding.ftr"),
],
axis=1, 
)
 cat_cols = df.columns.tolist()
df = pd.concat([df, df_num], axis=1)
 print("Fit")
 params = {
"loss_function": "Logloss",
"eval_metric": "Logloss",
"learning_rate": 0.03,
"iterations": 3000,
"l2_leaf_reg": 3,
"random_seed": 432013,
"subsample": 0.66,
"od_type": "Iter",
"rsm": 0.2,
"depth": 6,
"border_count": 128,
}
model = cat.CatBoostClassifier(**params)
 train_data = cat.Pool(
 df.iloc[: y_train.shape[0]], label=y_train, cat_features=cat_cols 
)
 fit_model = model.fit(train_data, verbose=30)
 y_pred = fit_model.predict_proba(df.iloc[y_train.shape[0] :])
 submission = pd.read_csv(".. /input/bnp-paribas-cardif-claims-management/sample_submission.csv.zip")
 submission.loc[:, "PredictedProb"] = y_pred[:, 1]
submission.to_csv(".. /working/solution_small_1k.csv", index=False)
 catboost_model()