Kaggle competition: Costa Rican Household Poverty Level Prediction
<SOS> metric: MacroFScore Kaggle data source: costa-rican-household-poverty-level-prediction<feature_engineering>
%matplotlib inline
print(os.listdir(".. /input"))
train["month"]=train["Date"].dt.month
test["month"]=test["Date"].dt.month
train["day"]=train["Date"].dt.day
test["day"]=test["Date"].dt.day
train["year"]=train["Date"].dt.year
test["year"]=test["Date"].dt.year<load_from_csv>
def dprint(*args, **kwargs):
print("[{}] ".format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M")) + \
" ".join(map(str,args)) , **kwargs)
 id_name = 'Id'
target_name = 'Target'
store=pd.read_csv("/kaggle/input/walmart-recruiting-store-sales-forecasting/stores.csv")
 store.head()<merge>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
train=pd.merge(train,store)
 test=pd.merge(test,store )<categorify>
train['is_test'] = 0
test['is_test'] = 1
df_all = pd.concat([train, test], axis=0 )
train["Type"]=train["Type"].replace({"A":1,"B":2,"C":3})
 test["Type"]=test["Type"].replace({"A":1,"B":2,"C":3} )<drop_column>
dprint('Clean features...')
 cols = ['dependency']
for c in tqdm(cols):
x = df_all[c].values
strs = []
for i, v in enumerate(x):
try:
val = float(v)
 except:
strs.append(v)
 val = np.nan
x[i] = val
strs = np.unique(strs)
 for s in strs:
df_all[c + '_' + s] = df_all[c].apply(lambda x: 1 if x == s else 0)
 df_all[c] = x
df_all[c] = df_all[c].astype(float)
 dprint("Done." )
train=train.drop(["Weekly_Sales","Date"],axis=1)
 train.head()<drop_column>
dprint("Extracting features...")
 def extract_features(df):
df['bedrooms_to_rooms'] = df['bedrooms']/df['rooms']
df['rent_to_rooms'] = df['v2a1']/df['rooms']
df['rent_to_bedrooms'] = df['v2a1']/df['bedrooms']
df['tamhog_to_rooms'] = df['tamhog']/df['rooms']
df['tamhog_to_bedrooms'] = df['tamhog']/df['bedrooms']
df['r4t3_to_tamhog'] = df['r4t3']/df['tamhog']
df['r4t3_to_rooms'] = df['r4t3']/df['rooms']
df['r4t3_to_bedrooms'] = df['r4t3']/df['bedrooms']
df['rent_to_r4t3'] = df['v2a1']/df['r4t3']
df['v2a1_to_r4t3'] = df['v2a1']/(df['r4t3'] - df['r4t1'])
 df['hhsize_to_rooms'] = df['hhsize']/df['rooms']
df['hhsize_to_bedrooms'] = df['hhsize']/df['bedrooms']
df['rent_to_hhsize'] = df['v2a1']/df['hhsize']
df['qmobilephone_to_r4t3'] = df['qmobilephone']/df['r4t3']
df['qmobilephone_to_v18q1'] = df['qmobilephone']/df['v18q1']
extract_features(train)
 extract_features(test)
 dprint("Done." )
tesst=test.drop(["Date"],axis=1)
 tesst.head()<train_model>
def encode_data(df):
yes_no_map = {'no': 0, 'yes': 1}
df['dependency'] = df['dependency'].replace(yes_no_map ).astype(np.float32)
 df['edjefe'] = df['edjefe'].replace(yes_no_map ).astype(np.float32)
 df['edjefa'] = df['edjefa'].replace(yes_no_map ).astype(np.float32)
 df['idhogar'] = LabelEncoder().fit_transform(df['idhogar'] )
rf=RandomForestRegressor(n_estimators=100, n_jobs=-1)
 rf.fit(train,y )<predict_on_test>
dprint("Encoding Data.... ")
 encode_data(train)
 encode_data(test)
 dprint("Done..." )
value=rf.predict(tesst)
 value<load_from_csv>
def do_features(df):
feats_div = [('children_fraction', 'r4t1', 'r4t3'), 
('working_man_fraction', 'r4h2', 'r4t3'), 
('all_man_fraction', 'r4h3', 'r4t3'), 
('human_density', 'tamviv', 'rooms'), 
('human_bed_density', 'tamviv', 'bedrooms'), 
('rent_per_person', 'v2a1', 'r4t3'), 
('rent_per_room', 'v2a1', 'rooms'), 
('mobile_density', 'qmobilephone', 'r4t3'), 
('tablet_density', 'v18q1', 'r4t3'), 
('mobile_adult_density', 'qmobilephone', 'r4t2'), 
('tablet_adult_density', 'v18q1', 'r4t2'),
]
feats_sub = [('people_not_living', 'tamhog', 'tamviv'), 
('people_weird_stat', 'tamhog', 'r4t3')]
for f_new, f1, f2 in feats_div:
df['fe_' + f_new] =(df[f1] / df[f2] ).astype(np.float32)
 for f_new, f1, f2 in feats_sub:
df['fe_' + f_new] =(df[f1] - df[f2] ).astype(np.float32)
 aggs_num = {'age': ['min', 'max', 'mean'],
'escolari': ['min', 'max', 'mean']
}
aggs_cat = {'dis': ['mean']}
for s_ in ['estadocivil', 'parentesco', 'instlevel']:
for f_ in [f_ for f_ in df.columns if f_.startswith(s_)]:
aggs_cat[f_] = ['mean', 'count']
for name_, df_ in [('18', df.query('age >= 18')) ]:
df_agg = df_.groupby('idhogar' ).agg({**aggs_num, **aggs_cat} ).astype(np.float32)
 df_agg.columns = pd.Index(['agg' + name_ + '_' + e[0] + "_" + e[1].upper() for e in df_agg.columns.tolist() ])
 df = df.join(df_agg, how='left', on='idhogar')
 del df_agg
df.drop([f_ for f_ in df.columns if f_.startswith('SQB')or f_ == 'agesq'], axis=1, inplace=True)
 df.drop(['Id', 'idhogar'], axis=1, inplace=True)
 df.drop(['hhsize', 'female', 'area2'], axis=1, inplace=True)
 return df
sample=pd.read_csv("/kaggle/input/walmart-recruiting-store-sales-forecasting/sampleSubmission.csv.zip")
 sample.head()<feature_engineering>
dprint("Do_feature Engineering.... ")
 train = do_features(train)
 test = do_features(test)
 dprint("Done.... " )
sample["Weekly_Sales"]=value<save_to_csv>
dprint("Fill Na value.... ")
 train = train.fillna(0)
 test = test.fillna(0)
 dprint("Done.... " )
sample.to_csv("20200309.csv",index=False )<load_from_csv>
cols_to_drop = [
id_name,
target_name,
]
X = train.drop(cols_to_drop, axis=1, errors='ignore')
 y = train[target_name].values
warnings.filterwarnings("ignore")
 %matplotlib inline
tf.random.set_seed(2)
 train = dd.read_csv('/kaggle/input/jane-street-market-prediction/train.csv' ).compute().sort_values('ts_id')
 features = [c for c in train.columns if "feature" in c]
def create_mlp(
 num_columns, num_labels, hidden_units, dropout_rates, label_smoothing, learning_rate 
):
inp = tf.keras.layers.Input(shape=(num_columns,))
x = tf.keras.layers.BatchNormalization()(inp)
 x = tf.keras.layers.Dropout(dropout_rates[0] )(x)
 for i in range(len(hidden_units)) :
x = tf.keras.layers.Dense(hidden_units[i] )(x)
 x = tf.keras.layers.BatchNormalization()(x)
 x = tf.keras.layers.Activation(tf.keras.activations.swish )(x)
 x = tf.keras.layers.Dropout(dropout_rates[i + 1] )(x)
 x = tf.keras.layers.Dense(num_labels )(x)
 out = tf.keras.layers.Activation("sigmoid" )(x)
 model = tf.keras.models.Model(inputs=inp, outputs=out)
 model.compile(
 optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate),
loss=tf.keras.losses.BinaryCrossentropy(label_smoothing=label_smoothing),
metrics=tf.keras.metrics.AUC(name="AUC"), 
)
 return model
batch_size = 4096
hidden_units = [384, 896, 896, 394]
dropout_rates = [
0.10143786981358652,
0.15720339053599725,
0.2203017847244654,
0.23148340929571917,
0.2457768967777311,
]
label_smoothing = 1e-2
learning_rate = 1e-5
num_models = 2
models = []
for i in range(num_models):
clf = create_mlp(
 len(features), 1, hidden_units, dropout_rates, label_smoothing, learning_rate 
)
 clf.load_weights(f".. /input/js-nn-models/JSModel_{i}.hdf5")
 models.append(clf)
 f_mean = np.load('.. /input/js-nn-models/f_mean.npy' )<define_variables>
gc.collect()
opt_th = 0.5034<categorify>
%%time
svm_model = SVC(kernel='rbf', gamma=0.8, C=12)
 fit = svm_model.fit(X, y)
 pred = fit.predict(test)
 sub = pd.read_csv(".. /input/sample_submission.csv")
 sub["Target"] = pred
sub.to_csv("submission_svm.csv" ,index=False )
env = janestreet.make_env()
env_iter = env.iter_test()
for test_df, pred_df in env_iter:
if test_df["weight"].item() > 0:
x_tt = test_df.loc[:, features].values
if np.isnan(x_tt[:, 1:].sum()):
x_tt[:, 1:] = np.nan_to_num(x_tt[:, 1:])+ np.isnan(x_tt[:, 1:])* f_mean
pred = 0.0
for clf in models:
pred += clf(x_tt, training=False ).numpy().item() / num_models
pred_df.action = np.where(pred >= opt_th, 1, 0 ).astype(int)
 else:
pred_df.action = 0
env.predict(pred_df )<import_modules>
%%time
xgb_model = xgb.XGBClassifier(learning_rate= 0.1, n_estimators= 1000, max_depth= 5, min_child_weight= 1, gamma= 0,
subsample= 0.9, colsample_bytree= 0.8, objective= "multi:softmax", scale_pos_weight= 1,
eval_metric= "merror", silent= 1, verbose= False, num_class= 5, seed= 27)
 fit = xgb_model.fit(X, y)
 pred = fit.predict(test)
 sub = pd.read_csv(".. /input/sample_submission.csv")
 sub["Target"] = pred
sub.to_csv("submission_xgb.csv" ,index=False )
import os, logging,math
from collections import deque
from tqdm import tqdm
import numpy as np
import pandas as pd
from sklearn import metrics
import datatable as dt
from datatable import f
import mxnet as mx
from mxnet import nd, autograd, gluon
from mxnet.gluon import nn 
<import_modules>
%%time
lgbm_model = lgb.LGBMClassifier(
 n_estimators=2000,
learning_rate=0.1,
num_leaves=123,
colsample_bytree=.8,
subsample=.7,
max_depth=15,
reg_alpha=.1,
reg_lambda=.1,
min_split_gain=.01,
min_child_weight=2,
scale_pos_weight=5, 
)
 fit = lgbm_model.fit(X, y)
 pred = fit.predict(test)
 sub = pd.read_csv(".. /input/sample_submission.csv")
 sub["Target"] = pred
sub.to_csv("submission_lgbm.csv" ,index=False )
<choose_model_class><EOS> 
%%time
rf_model = RandomForestClassifier(
 n_jobs=4,
class_weight='balanced',
n_estimators=325,
max_depth=5 
)
 fit = lgbm_model.fit(X, y)
 pred = fit.predict(test)
 sub = pd.read_csv(".. /input/sample_submission.csv")
 sub["Target"] = pred
sub.to_csv("submission_rf.csv" ,index=False )