Kaggle competition: Tabular Playground Series - Jan 2021
idxT = X_train.index[:3*len(X_train)//4]
idxV = X_train.index[3*len(X_train)//4:]<train_on_grid>
import numpy as np
import pandas as pd
from math import sqrt
from xgboost import XGBRegressor
from lightgbm import LGBMRegressor
from sklearn.impute import SimpleImputer
from sklearn.ensemble import VotingRegressor
from sklearn.feature_selection import VarianceThreshold
from sklearn.metrics import make_scorer, mean_squared_error
from sklearn.model_selection import GridSearchCV, ParameterGrid, train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import FunctionTransformer, StandardScaler
oof = np.zeros(len(X_train))
preds = np.zeros(len(X_test))
skf = GroupKFold(n_splits=6)
 for i,(idxT, idxV)in enumerate(skf.split(X_train, y_train, groups=X_train['DT_M'])) :
month = X_train.iloc[idxV]['DT_M'].iloc[0]
print('Fold',i,'withholding month',month)
 print(' rows of train =',len(idxT),'rows of holdout =',len(idxV))
clf = xgb.XGBClassifier(
 n_estimators=5000,
max_depth=12,
learning_rate=0.02,
subsample=0.8,
colsample_bytree=0.4,
missing=-1,
eval_metric='auc',
tree_method='hist' 
)
 h = clf.fit(X_train[cols].iloc[idxT], y_train.iloc[idxT],
eval_set=[(X_train[cols].iloc[idxV],y_train.iloc[idxV])],
verbose=100, early_stopping_rounds=200)
 oof[idxV] += clf.predict_proba(X_train[cols].iloc[idxV])[:,1]
preds += clf.predict_proba(X_test[cols])[:,1]/skf.n_splits
print('
print('XGB96 OOF CV=',roc_auc_score(y_train,oof))<categorify>
RANDOM_STATE = 2021
CROSS_VALIDATION = 3
def encode_FE(df1, df2, cols):
for col in cols:
df = pd.concat([df1[col],df2[col]])
 vc = df.value_counts(dropna=True, normalize=True ).to_dict()
vc[-1] = -1
nm = col+'_FE'
df1[nm] = df1[col].map(vc)
 df1[nm] = df1[nm].astype('float32')
 df2[nm] = df2[col].map(vc)
 df2[nm] = df2[nm].astype('float32')
 print(nm,', ',end='')
 def encode_LE(col,train=X_train,test=X_test,verbose=True):
df_comb = pd.concat([train[col],test[col]],axis=0)
 df_comb,_ = df_comb.factorize(sort=True)
 nm = col
if df_comb.max() >32000:
train[nm] = df_comb[:len(train)].astype('int32')
 test[nm] = df_comb[len(train):].astype('int32')
 else:
train[nm] = df_comb[:len(train)].astype('int16')
 test[nm] = df_comb[len(train):].astype('int16')
 del df_comb; x=gc.collect()
if verbose: print(nm,', ',end='')
 def encode_AG(main_columns, uids, aggregations=['mean'], train_df=X_train, test_df=X_test,
fillna=True, usena=False):
for main_column in main_columns:
for col in uids:
for agg_type in aggregations:
new_col_name = main_column+'_'+col+'_'+agg_type
temp_df = pd.concat([train_df[[col, main_column]], test_df[[col,main_column]]])
 if usena: temp_df.loc[temp_df[main_column]==-1,main_column] = np.nan
temp_df = temp_df.groupby([col])[main_column].agg([agg_type] ).reset_index().rename(
 columns={agg_type: new_col_name})
 temp_df.index = list(temp_df[col])
 temp_df = temp_df[new_col_name].to_dict()
train_df[new_col_name] = train_df[col].map(temp_df ).astype('float32')
 test_df[new_col_name] = test_df[col].map(temp_df ).astype('float32')
 if fillna:
train_df[new_col_name].fillna(-1,inplace=True)
 test_df[new_col_name].fillna(-1,inplace=True)
 print("'"+new_col_name+"'",', ',end='')
 def encode_CB(col1,col2,df1=X_train,df2=X_test):
nm = col1+'_'+col2
df1[nm] = df1[col1].astype(str)+'_'+df1[col2].astype(str)
 df2[nm] = df2[col1].astype(str)+'_'+df2[col2].astype(str)
 encode_LE(nm,verbose=False)
 print(nm,', ',end='')
 def encode_AG2(main_columns, uids, train_df=X_train, test_df=X_test):
for main_column in main_columns:
for col in uids:
comb = pd.concat([train_df[[col]+[main_column]],test_df[[col]+[main_column]]],axis=0)
 mp = comb.groupby(col)[main_column].agg(['nunique'])['nunique'].to_dict()
train_df[col+'_'+main_column+'_ct'] = train_df[col].map(mp ).astype('float32')
 test_df[col+'_'+main_column+'_ct'] = test_df[col].map(mp ).astype('float32')
 print(col+'_'+main_column+'_ct, ',end='' )<data_type_conversions>
data_dir = '/kaggle/input/tabular-playground-series-jan-2021'
encode_CB('card1','addr1')
 X_train['day'] =X_train.TransactionDT /(24*60*60)
 X_train['uid'] = X_train.card1_addr1.astype(str)+'_'+np.floor(X_train.day-X_train.D1 ).astype(str)
 X_test['day'] = X_test.TransactionDT /(24*60*60)
 X_test['uid'] =X_test.card1_addr1.astype(str)+'_'+np.floor(X_test.day-X_test.D1 ).astype(str )<categorify>
df = pd.read_csv(f"{data_dir}/train.csv" ).set_index('id' ).convert_dtypes()
display(df.shape)
 df.head(2 )
%%time
X_train['cents'] =(X_train['TransactionAmt'] - np.floor(X_train['TransactionAmt'])).astype('float32')
 X_test['cents'] =(X_test['TransactionAmt'] - np.floor(X_test['TransactionAmt'])).astype('float32')
 encode_FE(X_train,X_test,['uid'])
 encode_AG(['TransactionAmt','D4','D9','D10','D15'],['uid'],['mean','std'],fillna=True,usena=True)
 encode_AG(['C'+str(x)for x in range(1,15)if x!=3],['uid'],['mean'],X_train,X_test,fillna=True,usena=True)
 encode_AG(['M'+str(x)for x in range(1,10)],['uid'],['mean'],fillna=True,usena=True)
 encode_AG2(['P_emaildomain','dist1','DT_M','id_02','cents'], ['uid'], train_df=X_train, test_df=X_test)
 encode_AG(['C14'],['uid'],['std'],X_test,X_test,fillna=True,usena=True)
 encode_AG2(['C13','V314'], ['uid'], train_df=X_train, test_df=X_test)
 encode_AG2(['V127','V136','V309','V307','V320'], ['uid'], train_df=X_train, test_df=X_test)
 X_train['outsider15'] =(np.abs(X_train.D1-X_train.D15)>3 ).astype('int8')
 X_test['outsider15'] =(np.abs(X_test.D1-X_test.D15)>3 ).astype('int8')
 print('outsider15' )<feature_engineering>
X = df.copy()
y = X.pop('target')
 X_train, X_valid, y_train, y_valid = train_test_split(
 X, y, train_size=0.8, test_size=0.2, random_state=RANDOM_STATE,
)
for i in range(1,16):
if i in [1,2,3,5,9]: continue
X_train['D'+str(i)] = X_train['D'+str(i)] - X_train.TransactionDT/np.float32(24*60*60)
 X_test['D'+str(i)] = X_test['D'+str(i)] - X_test.TransactionDT/np.float32(24*60*60 )<drop_column>
preprocessor = Pipeline(steps=[ 
('imputer', SimpleImputer()), 
('log', FunctionTransformer(np.log1p)) , 
('scaler', StandardScaler()),
] )
cols = list(X_train.columns)
 cols.remove('TransactionDT')
 for c in ['DT_M','day','uid']:
cols.remove(c)
<prepare_x_and_y>
pipeline = Pipeline([ 
('preprocessor', preprocessor), 
('variance_drop', VarianceThreshold(threshold=(0.95 *(1 - 0.95)))) , 
('voting', 'passthrough'),
] )
idxT = X_train.index[:3*len(X_train)//4]
idxV = X_train.index[3*len(X_train)//4:]<drop_column>
parameters = [
{
'voting': [VotingRegressor([ 
('lgbm', LGBMRegressor(random_state=RANDOM_STATE)) , 
('xgb', XGBRegressor(random_state=RANDOM_STATE))
])],
'voting__lgbm__n_estimators': [2000],
'voting__lgbm__max_depth': [12],
'voting__lgbm__learning_rate': [0.01],
'voting__lgbm__num_leaves': [256],
'voting__lgbm__min_child_weight': [12],
'voting__lgbm__feature_fraction': [0.4],
'voting__lgbm__bagging_fraction': [0.7],
'voting__lgbm__bagging_freq': [5],
'voting__lgbm__min_child_samples': [32],
'voting__lgbm__lambda_l1':[9],
'voting__lgbm__lambda_l2': [0.13],
'voting__xgb__n_estimators': [2000],
'voting__xgb__max_depth': [12],
'voting__xgb__learning_rate': [0.01],
'voting__xgb__alpha': [5],
'voting__xgb__gamma': [3],
'voting__xgb__lambda': [3],
'voting__xgb__subsample': [0.8],
'voting__xgb__colsample_bytree': [0.4],
}
]
X_train = X_train.drop(columns=['uid'] )<define_variables>
total = CROSS_VALIDATION * len(ParameterGrid(parameters))
display(f"Number of combination that will be run by the GridSearch: {total}" )
idxT = X_train.index[:3*len(X_train)//4]
idxV = X_train.index[3*len(X_train)//4:]<train_on_grid>
custom_scoring = make_scorer(
 score_func=lambda y, y_pred: mean_squared_error(y, y_pred, squared=False),
greater_is_better=False,
)
oof = np.zeros(len(X_train))
preds = np.zeros(len(X_test))
skf = GroupKFold(n_splits=6)
 for i,(idxT, idxV)in enumerate(skf.split(X_train, y_train, groups=X_train['DT_M'])) :
month = X_train.iloc[idxV]['DT_M'].iloc[0]
print('Fold',i,'withholding month',month)
 print(' rows of train =',len(idxT),'rows of holdout =',len(idxV))
clfz = xgb.XGBClassifier(
 n_estimators=5000,
max_depth=12,
learning_rate=0.02,
subsample=0.8,
colsample_bytree=0.4,
missing=-1,
eval_metric='auc',
tree_method='hist' 
)
 hz = clfz.fit(X_train[cols].iloc[idxT], y_train.iloc[idxT],
eval_set=[(X_train[cols].iloc[idxV],y_train.iloc[idxV])],
verbose=100, early_stopping_rounds=200)
 oof[idxV] += clfz.predict_proba(X_train[cols].iloc[idxV])[:,1]
preds += clfz.predict_proba(X_test[cols])[:,1]/skf.n_splits
print('
print('XGB96 OOF CV=',roc_auc_score(y_train,oof))<load_from_csv>
grid_search = GridSearchCV(
 pipeline,
param_grid=parameters,
cv=CROSS_VALIDATION,
scoring=custom_scoring,
n_jobs=-1,
verbose=True,
)
test_transaction = pd.read_csv('/kaggle/input/ieee-fraud-detection/test_transaction.csv')
 final_col = test_transaction['TransactionID']<save_to_csv>
grid_search.fit(X_train, y_train )
my_submission = pd.DataFrame({'TransactionID':final_col, 'isFraud': preds})
 my_submission.to_csv('./submission.csv', index=False)
 my_submission.head(5 )<import_modules>
display(abs(grid_search.best_score_))
display(grid_search.best_params_ )
print(tf.__version__ )<feature_engineering>
preds = grid_search.best_estimator_.predict(X_valid)
 mean_squared_error(y_valid, preds, squared=False )
TRAIN_PREFIX = '.. /input/fish-data/fish/train'
def load_annotations() :
boxes = dict()
for path in tqdm(glob('.. /input/fish-data/fish/boxes/*.json')) :
label = os.path.basename(path ).split('_', 1)[0]
with open(path)as src:
for annotation in json.load(src):
basename = os.path.basename(annotation['filename'])
 annotation['filename'] = os.path.join(
 TRAIN_PREFIX, label.upper() , basename)
 for rect in annotation['annotations']:
rect['x'] += rect['width'] / 2
rect['y'] += rect['height'] / 2
rect['class'] = label
if os.path.isfile(annotation['filename']):
boxes.setdefault(label, [] ).append(annotation)
 return boxes
def draw_boxes(annotation, rectangles=None, image_size=None):
def _draw(img, rectangles, scale_x, scale_y, color=(0, 255, 0)) :
for i, rect in enumerate(rectangles):
pt1 =(int(( rect['x'] - rect['width'] / 2)* scale_x),
int(( rect['y'] - rect['height'] / 2)* scale_y))
pt2 =(int(( rect['x'] + rect['width'] / 2)* scale_x),
int(( rect['y'] + rect['height'] / 2)* scale_y))
img = cv2.rectangle(img.copy() , pt1, pt2,
color=color, thickness=10)
 img = cv2.putText(img.copy() , annotation['annotations'][i]['class'], tuple(np.array(pt1)+[0,-7]), cv2.FONT_HERSHEY_SIMPLEX, 1, color, 4)
 return img
def __draw(img, rectangles, scale_x, scale_y, color=(0, 255, 0)) :
for i, rect in enumerate(rectangles):
pt1 =(int(( rect['x'] - rect['width'] / 2)* scale_x),
int(( rect['y'] - rect['height'] / 2)* scale_y))
pt2 =(int(( rect['x'] + rect['width'] / 2)* scale_x),
int(( rect['y'] + rect['height'] / 2)* scale_y))
img = cv2.rectangle(img.copy() , pt1, pt2,
color=color, thickness=3)
 img = cv2.putText(img.copy() , counts['class'][int(rect['label'])] + ': ' + str(rect['label']), tuple(np.array(pt1)+[0,-7]), cv2.FONT_HERSHEY_SIMPLEX, 1, color, 4)
 return img
scale_x, scale_y = 1., 1.
 img = cv2.imread(annotation['filename'], cv2.IMREAD_COLOR)[...,::-1]
if image_size is not None:
scale_x = 1.* image_size[0] / img.shape[1]
scale_y = 1.* image_size[1] / img.shape[0]
img = cv2.resize(img, image_size)
 img = _draw(img, annotation.get('annotations', []), scale_x, scale_y)
 if rectangles is not None:
img = __draw(img, rectangles, 1., 1.,(255, 0, 0))
return img<define_variables>
X_test = pd.read_csv(f"{data_dir}/test.csv" ).set_index('id' ).convert_dtypes()
display(X_test.shape)
 X_test.head(2 )
<create_dataframe><EOS> 
preds_test = grid_search.best_estimator_.predict(X_test)
 output = pd.DataFrame(
 {'Id': X_test.index, 'target': preds_test})
 output.to_csv(f"submission.csv", index=False )