Kaggle competition: Costa Rican Household Poverty Level Prediction
<SOS> metric: MacroFScore Kaggle data source: costa-rican-household-poverty-level-prediction<init_hyperparams>
%matplotlib inline
print(os.listdir(".. /input"))
lgb_params = {
'learning_rate': 0.1,
'max_depth': 25,
'num_leaves': 1000,
'objective': 'regression',
'feature_fraction': 0.9,
'bagging_fraction': 0.5,
'max_bin': 1000}<train_model>
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'
model_lgb = lgb.train(lgb_params,
dtrain,
num_boost_round=1200)
<compute_train_metric>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
cv_results = lgb.cv(
 lgb_params,
dtrain,
num_boost_round=100,
nfold=3,
metrics='mae',
early_stopping_rounds=10,
stratified=False
)<train_model>
train['is_test'] = 0
test['is_test'] = 1
df_all = pd.concat([train, test], axis=0)
print('Current parameters:
', lgb_params)
 print('
Best num_boost_round:', len(cv_results['l1-mean']))
print('Best CV score:', cv_results['l1-mean'][-1] )<predict_on_test>
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." )
pred_test = np.exp(model_lgb.predict(X_test))
pred_test<load_from_csv>
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." )
submit = pd.read_csv('.. /input/sample_submission.csv' )<feature_engineering>
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'])
submit['trip_duration'] = pred_test
submit.head()<create_dataframe>
dprint("Encoding Data.... ")
 encode_data(train)
 encode_data(test)
 dprint("Done..." )
submit_file = pd.DataFrame({"id": test.id, "trip_duration": pred_test} )<save_to_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
submit_file.to_csv('submission.csv', index=False )<load_from_csv>
dprint("Do_feature Engineering.... ")
 train = do_features(train)
 test = do_features(test)
 dprint("Done.... " )
df = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<groupby>
dprint("Fill Na value.... ")
 train = train.fillna(0)
 test = test.fillna(0)
 dprint("Done.... " )
one = df.groupby('vendor_id' ).count() ['id'].iloc[0:1]
two = df.groupby('vendor_id' ).count() ['id'].iloc[1:2]
print('Vendor_id 2 has received ' , np.subtract(two, one ).values[0], 'more bookings then Vendor_id 1.' )<count_missing_values>
cols_to_drop = [
id_name,
target_name,
]
X = train.drop(cols_to_drop, axis=1, errors='ignore')
 y = train[target_name].values
df.isna().sum()<feature_engineering>
R = 6371.0
lat1 = np.radians(df['pickup_latitude'])
 lon1 = np.radians(df['pickup_longitude'])
 lat2 = np.radians(df['dropoff_latitude'])
 lon2 = np.radians(df['dropoff_longitude'])
 dlon = lon2 - lon1
dlat = lat2 - lat1
a = np.sin(dlat / 2)**2 + np.cos(lat1)* np.cos(lat2)* np.sin(dlon / 2)**2
c = 2 * np.arctan2(np.sqrt(a), np.sqrt(1 - a))
distance = R * c
distance_df = pd.DataFrame({'travel_distance_km':distance})
 df['travel_distance_km'] = distance_df
print(np.mean(distance_df))
lat1 = np.radians(test['pickup_latitude'])
 lon1 = np.radians(test['pickup_longitude'])
 lat2 = np.radians(test['dropoff_latitude'])
 lon2 = np.radians(test['dropoff_longitude'])
 dlon = lon2 - lon1
dlat = lat2 - lat1
a = np.sin(dlat / 2)**2 + np.cos(lat1)* np.cos(lat2)* np.sin(dlon / 2)**2
c = 2 * np.arctan2(np.sqrt(a), np.sqrt(1 - a))
distance = R * c
distance_df = pd.DataFrame({'travel_distance_km':distance})
 test['travel_distance_km'] = distance_df 
<feature_engineering>
params = {'bagging_fraction': 0.9957236684465528,
'colsample_bytree': 0.7953949538181928,
'feature_fraction': 0.7333800304661316,
'lambda_l1': 1.79753950286893,
'lambda_l2': 1.710590311253639,
'max_depth': 6,
'min_child_samples': 48,
'min_child_weight': 48.94067592560281,
'min_split_gain': 0.016737988780906453,
'num_leaves': 34,
'subsample': 0.9033449610388691}
df['pickup_datetime'] = pd.to_datetime(df['pickup_datetime'])
 df['dropoff_datetime'] = pd.to_datetime(df['dropoff_datetime'])
 df['hour_pickup'] = np.array(df['pickup_datetime'].dt.hour)
 df['day_pickup'] = np.array(df['pickup_datetime'].dt.day_name())
 test['pickup_datetime'] = pd.to_datetime(test['pickup_datetime'])
 test['hour_pickup'] = np.array(test['pickup_datetime'].dt.hour)
 test['day_pickup'] = np.array(test['pickup_datetime'].dt.day_name() )<feature_engineering>
lgb_model = lgb.LGBMClassifier(max_depth=-1, learning_rate=0.2, objective='multiclass',
silent=True, metric='None',
n_jobs=4, n_estimators=5000, class_weight='balanced',
colsample_bytree= 0.93,
min_child_samples= 56, num_leaves= 19,
subsample= 0.84)
 lgb_model.set_params(**params )
category_dict = {'0-5': 0,'5-10': 0, '10-15': 0, '15-20': 0, '20-25':0 }
for x in np.array(df[(df['travel_distance_km'] >= 0)&(df['travel_distance_km'] < 25)]['travel_distance_km']):
if x <= 5:
category_dict['0-5'] = category_dict['0-5'] + 1
elif x > 5 and x <= 10 :
category_dict['5-10'] = category_dict['5-10'] + 1
elif x > 10 and x <= 15 :
category_dict['10-15'] = category_dict['10-15'] + 1
elif x > 15 and x <= 20 :
category_dict['15-20'] = category_dict['15-20'] + 1
elif x > 20 and x <= 25 :
category_dict['20-25'] = category_dict['20-25'] + 1
pd.DataFrame(category_dict, index=[0] ).T.plot(kind='pie', subplots=True, figsize=(8, 8))
plt.title('Percentage of Trips Between 0 to 25 km')
 print('% of trips between 0 to 5 km for the trip distance between 0 to 25 km dataset : ', category_dict['0-5']/sum(category_dict.values()))<prepare_x_and_y>
dprint("Prepared Model.....")
 lgb_model.fit(X, y)
 dprint("Done Model.....")
 dprint("Predict Test Value.....")
 target_lgb = lgb_model.predict(test)
 dprint("Done Prediction....." )
sns.scatterplot(
 x=df[(df['travel_distance_km'] > 0)&(df['travel_distance_km'] < 25)&(df['trip_duration'] < 20000)]['trip_duration'],
y=df[(df['travel_distance_km'] > 0)&(df['travel_distance_km'] < 25)&(df['trip_duration'] < 20000)]['travel_distance_km'] )<data_type_conversions>
lgb_model.score(X,y )
df['day_pickup'] = df['day_pickup'].astype('category' ).cat.codes
df['store_and_fwd_flag'] = df['store_and_fwd_flag'].astype('category' ).cat.codes
test['day_pickup'] = test['day_pickup'].astype('category' ).cat.codes
test['store_and_fwd_flag'] = test['store_and_fwd_flag'].astype('category' ).cat.codes<normalization>
scaler = StandardScaler()
scaler.fit(df[['vendor_id','passenger_count',
'pickup_longitude', 'pickup_latitude',
'dropoff_longitude', 'dropoff_latitude', 'store_and_fwd_flag',
'travel_distance_km', 'hour_pickup',
'day_pickup']])
 scaled_features = scaler.transform(df[['vendor_id','passenger_count',
'pickup_longitude', 'pickup_latitude',
'dropoff_longitude', 'dropoff_latitude', 'store_and_fwd_flag',
'travel_distance_km', 'hour_pickup',
'day_pickup']])
 df_feat = pd.DataFrame(scaled_features, columns=['vendor_id','passenger_count',
'pickup_longitude', 'pickup_latitude',
'dropoff_longitude', 'dropoff_latitude', 'store_and_fwd_flag',
'travel_distance_km', 'hour_pickup',
'day_pickup'])
 scaler_test = StandardScaler()
scaler_test.fit(test[['vendor_id','passenger_count',
'pickup_longitude', 'pickup_latitude',
'dropoff_longitude', 'dropoff_latitude', 'store_and_fwd_flag',
'travel_distance_km', 'hour_pickup',
'day_pickup']])
 scaled_features_test = scaler.transform(test[['vendor_id','passenger_count',
'pickup_longitude', 'pickup_latitude',
'dropoff_longitude', 'dropoff_latitude', 'store_and_fwd_flag',
'travel_distance_km', 'hour_pickup',
'day_pickup']])
 df_feat_test = pd.DataFrame(scaled_features_test, columns=['vendor_id','passenger_count',
'pickup_longitude', 'pickup_latitude',
'dropoff_longitude', 'dropoff_latitude', 'store_and_fwd_flag',
'travel_distance_km', 'hour_pickup',
'day_pickup'])
 test = pd.concat([test['id'],df_feat_test], axis=1)
<split>
sub = pd.read_csv(".. /input/sample_submission.csv" )
X = df_feat
y = np.log(df['trip_duration'])
 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42 )<split>
sub['Target'] = target_lgb
<predict_on_test><EOS> 
sub.to_csv("cat_boost.csv", index= False )