Kaggle competition: Tabular Playground Series - Jan 2021
%matplotlib inline 
<install_modules>
train = pd.read_csv("/kaggle/input/tabular-playground-series-jan-2021/train.csv")
 test = pd.read_csv("/kaggle/input/tabular-playground-series-jan-2021/test.csv")
 sample_submission = pd.read_csv('.. /input/tabular-playground-series-jan-2021/sample_submission.csv' )
!pip install -U category_encoders<load_from_disk>
train.isna().sum()
train_data = pd.read_json('.. /input/two-sigma-connect-rental-listing-inquiries/train.json.zip', convert_dates=['created'])
 test_data = pd.read_json('.. /input/two-sigma-connect-rental-listing-inquiries/test.json.zip', convert_dates=['created'] )<define_variables>
test.isna().sum()
train_size = train_data.shape[0]<feature_engineering>
target = train['target'].values
train_data['target'] = train_data['interest_level'].apply(lambda x: 0 if x=='low' else 1 if x=='medium' else 2)
 train_data['low'] = train_data['interest_level'].apply(lambda x: 1 if x=='low' else 0)
 train_data['medium'] = train_data['interest_level'].apply(lambda x: 1 if x=='medium' else 0)
 train_data['high'] = train_data['interest_level'].apply(lambda x: 1 if x=='high' else 0 )<concatenate>
np.unique(target ).shape
full_data=pd.concat([train_data,test_data] )<define_variables>
train_oof = np.zeros(( 300000,))
test_preds = 0
train_oof.shape
num_vars = ['bathrooms','bedrooms','latitude','longitude','price']
cat_vars = ['building_id','manager_id','display_address','street_address']
text_vars = ['description','features']
date_var = 'created'
image_var = 'photos'
id_var = 'listing_id'<feature_engineering>
NUM_FOLDS = 10
kf = KFold(n_splits=NUM_FOLDS, shuffle=True, random_state=0)
 for f,(train_ind, val_ind)in tqdm(enumerate(kf.split(train, target))):
train_df, val_df = train.iloc[train_ind][columns], train.iloc[val_ind][columns]
train_target, val_target = target[train_ind], target[val_ind]
model = HistGradientBoostingRegressor(validation_fraction=None, max_iter=6500, l2_regularization=0.0, min_samples_leaf=28, max_depth=18, learning_rate = 0.01)
 model.fit(train_df, train_target)
 temp_oof = model.predict(val_df)
 temp_test = model.predict(test[columns])
 train_oof[val_ind] = temp_oof
test_preds += temp_test/NUM_FOLDS
full_data['created_datetime'] = pd.to_datetime(full_data['created'], format="%Y-%m-%d %H:%M:%S")
 full_data['created_year']=full_data['created_datetime'].apply(lambda x:x.year)
 full_data['created_datetime'] = pd.to_datetime(full_data['created'], format="%Y-%m-%d %H:%M:%S")
 full_data['created_month']=full_data['created_datetime'].apply(lambda x:x.month)
 full_data['created_day']=full_data['created_datetime'].apply(lambda x:x.day)
 full_data['created_dayofweek']=full_data['created_datetime'].apply(lambda x:x.dayofweek)
 full_data['created_dayofyear']=full_data['created_datetime'].apply(lambda x:x.dayofyear)
 full_data['created_weekofyear']=full_data['created_datetime'].apply(lambda x:x.weekofyear)
 full_data['created_hour']=full_data['created_datetime'].apply(lambda x:x.hour)
 full_data['created_epoch']=full_data['created_datetime'].apply(lambda x:x.value//10**9)
 date_num_vars = ['created_month','created_dayofweek','created_dayofyear'
,'created_weekofyear','created_hour','created_epoch']<feature_engineering>
mean_squared_error(train_oof, target, squared=False )
full_data["geo_area_50"] = \
full_data[['latitude', 'longitude']]\ 
.apply(lambda x:(int(x[0]*50)%50)*50+(int(-x[1]*50)%50),axis=1)
 full_data["geo_area_100"] = \
full_data[['latitude', 'longitude']]\ 
.apply(lambda x:(int(x[0]*100)%100)*100+(int(-x[1]*100)%100),axis=1)
 full_data["geo_area_200"] = \
full_data[['latitude', 'longitude']]\ 
.apply(lambda x:(int(x[0]*200)%200)*200+(int(-x[1]*200)%200),axis=1)
 lat=40.705628
lon=-74.010278
full_data['distance_to_fi'] = full_data[['latitude', 'longitude']].apply(lambda x:math.sqrt(( x[0]-lat)**2+(x[1]-lon)**2), axis=1)
 lat = 40.785091
lon = -73.968285
full_data['distance_to_cp'] = full_data[['latitude', 'longitude']].apply(lambda x:math.sqrt(( x[0]-lat)**2+(x[1]-lon)**2), axis=1)
 geo_cat_vars = ['geo_area_50', 'geo_area_100', 'geo_area_200']
geo_num_vars = ['distance_to_fi', 'distance_to_cp']<feature_engineering>
np.save('train_oof', train_oof)
 np.save('test_preds', test_preds )
<feature_engineering><EOS> 
sample_submission['target'] = test_preds
sample_submission.to_csv('submission.csv', index=False )