Kaggle competition: Costa Rican Household Poverty Level Prediction
test_cv_preds_2 = []
NB_SPLITS = 7
SEED = [0,1,2,3,4,5,6]
for s in SEED:
for fold_nb,(train_idx, val_idx)in enumerate(MultilabelStratifiedKFold(n_splits = NB_SPLITS, random_state = s ).split(train_features_tabnet, target)) :
model1 = TabNetRegressor()
model2 = TabNetRegressor()
model2.load_model(f".. /input/moa-tabnet-last/TabNet_seed_{s}_fold_{fold_nb+1}.zip")
 preds_test_2 = model2.predict(X_test_tabnet)
 test_cv_preds_2.append(1 /(1 + np.exp(-preds_test_2)))
 test_preds_all_2 = np.stack(test_cv_preds_2)
 preds_tabnet_2 = test_preds_all_2.mean(axis = 0 )<prepare_output>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
predictions_ = 0.25*preds_pretrain + 0.25*preds_tabnet_1 + 0.25*preds_transfer + 0.25*preds_tabnet_2<save_to_csv>
df_combine = pd.concat([train,test], axis = 0)
 id_name = 'Id'
target_name = 'Target'
cols = [f_ for f_ in df_combine.columns if df_combine[f_].dtype == 'object' and f_ != id_name ]
print(cols)
 for col in tqdm(cols):
lb = LabelEncoder()
lb.fit(df_combine[col])
 train[col] = lb.transform(train[col].astype(str))
test[col] = lb.transform(test[col].astype(str))
del lb
gc.collect()
df = pd.read_csv('/kaggle/input/lish-moa/sample_submission.csv')
 public_id = list(df['sig_id'].values)
 test_id = list(test_features['sig_id'].values)
 private_id = list(set(test_id)-set(public_id))
df_submit = pd.DataFrame(index = public_id+private_id, columns=target_cols)
 df_submit.index.name = 'sig_id'
df_submit[:] = 0
df_submit.loc[test.sig_id,:] = predictions_
df_submit.loc[test_features[test_features.cp_type=='ctl_vehicle'].sig_id]= 0
df_submit.to_csv('submission.csv',index=True )<install_modules>
def extract_features(df):
df['bedrooms_to_rooms'] = df['bedrooms']/df['rooms']
df['rent_to_rooms'] = df['v2a1']/df['rooms']
df['tamhog_to_rooms'] = df['tamhog']/df['rooms']
df['r4h1_percent_in_male'] = df['r4h1'] / df['r4h3']
df['r4m1_percent_in_female'] = df['r4m1'] / df['r4m3']
df['r4h1_percent_in_total'] = df['r4h1'] / df['hhsize']
df['r4m1_percent_in_total'] = df['r4m1'] / df['hhsize']
df['r4t1_percent_in_total'] = df['r4t1'] / df['hhsize']
extract_features(train)
 extract_features(test )
!pip install.. /input/pytorch16gpu/torch-1.6.0cu101-cp37-cp37m-linux_x86_64.whl --quiet<set_options>
cols_to_drop = [id_name ,target_name]
X_train = train.drop(cols_to_drop, axis = 1)
 Y_train = train[target_name]
X_train.fillna(0)
 lgb = lgb.LGBMClassifier(class_weight='balanced',drop_rate=0.9, min_data_in_leaf=100, max_bin=255,
n_estimators=500,min_sum_hessian_in_leaf=1,importance_type='gain',learning_rate=0.1,bagging_fraction = 0.85,
colsample_bytree = 1.0,feature_fraction = 0.1,lambda_l1 = 5.0,lambda_l2 = 3.0,max_depth = 9,
min_child_samples = 55,min_child_weight = 5.0,min_split_gain = 0.1,num_leaves = 45,subsample = 0.75)
def seed_everything(seed_value):
random.seed(seed_value)
 np.random.seed(seed_value)
 torch.manual_seed(seed_value)
 os.environ['PYTHONHASHSEED'] = str(seed_value)
 if torch.cuda.is_available() :
torch.cuda.manual_seed(seed_value)
 torch.cuda.manual_seed_all(seed_value)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
seed_everything(42 )<load_from_csv>
k_folds = 5
kf = KFold(n_splits = k_folds, shuffle = True)
 for train_index, test_index in kf.split(X_train, Y_train):
x_train, x_val = X_train.iloc[train_index] , X_train.iloc[test_index]
y_train, y_val = Y_train.iloc[train_index] , Y_train.iloc[test_index]
lgb.fit(x_train, y_train , eval_set= [(x_val, y_val)], early_stopping_rounds = 400 )
train_features = pd.read_csv('.. /input/lish-moa/train_features.csv')
 train_targets = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 train_targets_nonscored = pd.read_csv('.. /input/lish-moa/train_targets_nonscored.csv')
 test_features = pd.read_csv('.. /input/lish-moa/test_features.csv')
 ss = pd.read_csv('.. /input/lish-moa/sample_submission.csv')
 cols = [c for c in ss.columns.values if c != 'sig_id']
GENES = [col for col in train_features.columns if col.startswith('g-')]
CELLS = [col for col in train_features.columns if col.startswith('c-')]<compute_test_metric>
X_test = test.drop('Id', axis = 1)
 X_test.fillna(0)
 pres = lgb.predict(X_test )
<compute_train_metric><EOS> 
id_test = test['Id']
subs = pd.DataFrame()
subs['Id'] = id_test
subs['Target'] = pres
subs.to_csv('mean_encoding_target.csv', index = False )