Kaggle competition: Santander Customer Transaction Prediction
<SOS> metric: AUC Kaggle data source: santander-customer-transaction-prediction<categorify>
sns.set_style('whitegrid')
 print(os.listdir(".. /input"))
train = train[train.pickup_latitude.between(40.5, 40.9)]
train = train[train.dropoff_latitude.between(40.5, 40.9)]
train = train[train.pickup_longitude.between(-74.3, -73)]
train = train[train.dropoff_longitude.between(-74.3, -73)]<count_missing_values>
%%time
train = pd.read_csv('.. /input/train.csv' )
train.isnull().mean().sum() , test.isnull().mean().sum()<sort_values>
test_df = pd.read_csv('.. /input/test.csv' )
corr = train.corr() ['fare_amount']
corr.sort_values(ascending = False )<feature_engineering>
X = train.drop(["ID_code", "target"], axis=1)
 Y = train["target"]
X_test = test_df.drop(["ID_code"], axis=1 )
train['latitude_abs'] =(train.pickup_latitude - train.dropoff_latitude ).abs()
train['longitude_abs'] =(train.pickup_longitude - train.dropoff_longitude ).abs()
test['latitude_abs'] =(test.pickup_latitude - test.dropoff_latitude ).abs()
test['longitude_abs'] =(test.pickup_longitude - test.dropoff_longitude ).abs()<train_model>
def augment(x,y,t=2):
xs,xn = [],[]
for i in range(t):
mask = y>0
x1 = x[mask].copy()
ids = np.arange(x1.shape[0])
 for c in range(x1.shape[1]):
np.random.shuffle(ids)
 x1[:,c] = x1[ids][:,c]
xs.append(x1)
 for i in range(t//2):
mask = y==0
x1 = x[mask].copy()
ids = np.arange(x1.shape[0])
 for c in range(x1.shape[1]):
np.random.shuffle(ids)
 x1[:,c] = x1[ids][:,c]
xn.append(x1)
 xs = np.vstack(xs)
 xn = np.vstack(xn)
 ys = np.ones(xs.shape[0])
 yn = np.zeros(xn.shape[0])
 x = np.vstack([x,xs,xn])
 y = np.concatenate([y,ys,yn])
 return x,y
PI = 3.14159265359
deg_to_rad = PI/180.0
earth_rad = 6371.009
def distance_bn_coordinates(lat1, log1, lat2, log2):
delta_lat =(lat2 - lat1)*deg_to_rad
delta_log =(log2 - log1)*deg_to_rad
mean_lat = deg_to_rad *(lat2 + lat1)/2.0
dist = earth_rad * np.sqrt(
 np.power(delta_lat,2.0)+
np.power(np.cos(mean_lat)*delta_log,2.0)
)
 return dist<compute_test_metric>
n_fold = 15
folds = StratifiedKFold(n_splits=n_fold, shuffle=True, random_state=42 )
def calculate_euclidean_distance(lat1, log1, lat2, log2):
lat_sq = np.power(( lat2-lat1), 2.0)
 log_sq = np.power(( log2-log1), 2.0)
 return np.sqrt(lat_sq+log_sq )<compute_test_metric>
params = {'num_leaves': 13,
'min_data_in_leaf': 80,
'min_sum_hessian_in_leaf': 10.0,
'objective': 'binary',
'boost_from_average': False,
'max_depth': -1,
'learning_rate': 0.0083,
'boost': 'gbdt',
'bagging_freq': 5,
'tree_learner': "serial",
'bagging_fraction': 0.335,
'feature_fraction': 0.041,
'metric': 'auc',
'num_threads': 8}
def haversine_distance(lat1, lon1, lat2, lon2):
R = 3959.87433
dLat = radians(lat2 - lat1)
 dLon = radians(lon2 - lon1)
 lat1 = radians(lat1)
 lat2 = radians(lat2)
 a = sin(dLat/2)**2 + cos(lat1)*cos(lat2)*sin(dLon/2)**2
c = 2*asin(sqrt(a))
return R * c<feature_engineering>
prediction = np.zeros(len(X_test))
oof = np.zeros(len(X))
for fold_n,(train_index, valid_index)in enumerate(folds.split(X,Y)) :
print('Fold', fold_n, 'started at', time.ctime())
 X_train, X_valid = X.iloc[train_index], X.iloc[valid_index]
y_train, y_valid = Y.iloc[train_index], Y.iloc[valid_index]
X_tr, y_tr = augment(X_train.values, y_train.values)
 X_tr = pd.DataFrame(X_tr)
 train_data = lgb.Dataset(X_tr, label=y_tr)
 valid_data = lgb.Dataset(X_valid, label=y_valid)
 model = lgb.train(params,train_data,num_boost_round=1000000,
valid_sets = [train_data, valid_data],verbose_eval=3000,early_stopping_rounds = 4000)
 oof[valid_index] = model.predict(X.iloc[valid_index], num_iteration=model.best_iteration)
 prediction += model.predict(X_test, num_iteration=model.best_iteration)/15
print("CV score: {:<8.5f}".format(roc_auc_score(Y, oof)) )
use_feature = True
if use_feature == True:
train['distance'] = distance_bn_coordinates(train.pickup_latitude ,train.pickup_longitude,
train.dropoff_latitude ,train.dropoff_longitude)
 test['distance'] = distance_bn_coordinates(test.pickup_latitude ,test.pickup_longitude,
test.dropoff_latitude ,test.dropoff_longitude )<feature_engineering>
sub = pd.DataFrame({"ID_code": test_df.ID_code.values})
 sub["target"] = prediction
sub.to_csv("submission.csv", index=False )