Kaggle competition: New York City Taxi Fare Prediction
<SOS> metric: RMSE Kaggle data source: new-york-city-taxi-fare-prediction<predict_on_test>
warnings.filterwarnings('ignore')
 pd.set_option('display.float_format', lambda x: '%.4f' % x)
 TRAIN_PATH = '.. /input/new-york-city-taxi-fare-prediction/train.csv'
TEST_PATH = '.. /input/new-york-city-taxi-fare-prediction/test.csv'
preds, ids = learn.get_preds(DatasetType.Test)
 y = torch.argmax(preds, dim=1 )<save_to_csv>
test = pd.read_csv(TEST_PATH)
 print('Null values:',test.isnull().sum().sum())
 test.head()
submission = pd.DataFrame({ 'id': ids,'label': y })
 submission.to_csv(path_or_buf ="submission.csv", index=False )<load_from_csv>
df_temp = pd.read_csv(TRAIN_PATH, nrows=100000)
 profile = pandas_profiling.ProfileReport(df_temp, title="Profile Report", minimal=True, progress_bar=False)
 profile.to_notebook_iframe()
train=pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')
 test=pd.read_csv('/kaggle/input/Kannada-MNIST/Dig-MNIST.csv')
 submission_set = pd.read_csv(".. /input/Kannada-MNIST/test.csv" ).iloc[:,1:]
train_data=train.drop('label',axis=1)
 train_targets=train['label']
test_images=test.drop('label',axis=1)
 test_labels=test['label']
train_images, val_images, train_labels, val_labels = train_test_split(train_data,
train_targets,
test_size=0.2)
 train_images.reset_index(drop=True, inplace=True)
 train_labels.reset_index(drop=True, inplace=True)
 val_images.reset_index(drop=True, inplace=True)
 val_labels.reset_index(drop=True, inplace=True)
 test_images.reset_index(drop=True, inplace=True)
 test_labels.reset_index(drop=True, inplace=True)
 print("Train Set")
 print(train_images.shape)
 print(train_labels.shape)
 print("Validation Set")
 print(val_images.shape)
 print(val_labels.shape)
 print("Validation 2")
 print(test_images.shape)
 print(test_labels.shape)
 print("Submission")
 print(submission_set.shape )<count_values>
def clean_df(df):
df['pickup_datetime'] = df['pickup_datetime'].str.slice(0, 15)
 df['pickup_datetime'] = pd.to_datetime(df['pickup_datetime'], utc=True, format='%Y-%m-%d %H:%M')
 df = df.assign(rev=df.dropoff_latitude<df.dropoff_longitude)
 idx =(df['rev'] == 1)
 df.loc[idx,['dropoff_longitude','dropoff_latitude']] = df.loc[idx,['dropoff_latitude','dropoff_longitude']].values
df.loc[idx,['pickup_longitude','pickup_latitude']] = df.loc[idx,['pickup_latitude','pickup_longitude']].values
criteria =(
 " 0 < fare_amount <= 500"
" and 0 < passenger_count <= 6 "
" and -75 <= pickup_longitude <= -72 "
" and -75 <= dropoff_longitude <= -72 "
" and 40 <= pickup_latitude <= 42 "
" and 40 <= dropoff_latitude <= 42 " 
)
 df =(df 
.dropna() 
.query(criteria)
.reset_index() 
.drop(columns=['rev', 'index'])
)
 return df
def load_df(nrows=None, features=None):
cols = [
'fare_amount', 'pickup_datetime','pickup_longitude', 'pickup_latitude',
'dropoff_longitude', 'dropoff_latitude', 'passenger_count'
]
df_as_list = []
for df_chunk in pd.read_csv(TRAIN_PATH, usecols=cols, nrows=nrows, chunksize=5000000):
df_chunk = clean_df(df_chunk)
 if features == 'explore':
df_chunk = exploration_features(df_chunk)
 elif features == 'model':
df_chunk = modelling_features(df_chunk)
 else:
df_chunk = df_chunk.drop(columns='pickup_datetime')
 df_as_list.append(df_chunk)
 df = pd.concat(df_as_list)
 return df
print("Train Distribution")
 print(train_labels.value_counts(normalize = True))
print("
Submission Distribution")
 print(test_labels.value_counts(normalize = True))<prepare_x_and_y>
def get_split_sets(train):
x = train.drop(columns=['fare_amount'])
 y = train['fare_amount'].values
x_train, x_val, y_train, y_val = train_test_split(x, y, test_size=0.1, random_state=0)
 return x_train, x_val, y_train, y_val
def lin_model(x_train, x_val, y_train, y_val):
model = LinearRegression()
model.fit(x_train, y_train)
 pred = model.predict(x_val)
 rmse = np.sqrt(mean_squared_error(y_val, pred))
return model, rmse, pred
def knn_model(x_train, x_val, y_train, y_val, neighbors):
min_rmse = 1000
for n in neighbors:
knn = KNeighborsRegressor(n_neighbors=n)
 knn.fit(x_train, y_train)
 pred = knn.predict(x_val)
 rmse = np.sqrt(mean_squared_error(y_val, pred))
if rmse < min_rmse:
min_rmse = rmse
model = knn
best_pred = pred
print('Neighbours', n, 'RMSE', rmse)
 return model, min_rmse, best_pred
def lgbm_model(params,x_train, x_val, y_train, y_val):
lgbm_train = lgbm.Dataset(x_train, y_train, silent=True)
 lgbm_val = lgbm.Dataset(x_val, y_val, silent=True)
 model = lgbm.train(params=params, train_set=lgbm_train, valid_sets=lgbm_val, verbose_eval=100)
 pred = model.predict(x_val, num_iteration=model.best_iteration)
 rmse = np.sqrt(mean_squared_error(y_val, pred))
return model, rmse, pred
IMGSIZE = 28
train_trans = transforms.Compose(( [
transforms.ToPILImage() ,
transforms.RandomCrop(IMGSIZE),
transforms.RandomAffine(degrees=5, translate=(0.1, 0.1)) ,
transforms.ToTensor() ,
]))
val_trans = transforms.Compose(( [
transforms.ToPILImage() ,
transforms.ToTensor() ,
]))
class KannadaDataSet(torch.utils.data.Dataset):
def __init__(self, images, labels,transforms = None):
self.X = images
self.y = labels
self.transforms = transforms
def __len__(self):
return(len(self.X))
def __getitem__(self, i):
data = self.X.iloc[i,:]
data = np.array(data ).astype(np.uint8 ).reshape(IMGSIZE,IMGSIZE,1)
 if self.transforms:
data = self.transforms(data)
 if self.y is not None:
return(data, self.y[i])
 else:
return data<create_dataframe>
train = load_df(1000000)
 x_train, x_val, y_train, y_val= get_split_sets(train)
 test = pd.read_csv(TEST_PATH)
 x_test = test.drop(columns=['key'] )
batch_size = 128
train_data = KannadaDataSet(train_images, train_labels, train_trans)
 val_data = KannadaDataSet(val_images, val_labels, val_trans)
 test_data = KannadaDataSet(test_images, test_labels, val_trans)
 submission_data = KannadaDataSet(submission_set, None, val_trans)
 train_loader = torch.utils.data.DataLoader(train_data,
batch_size=batch_size,
shuffle=True)
 val_loader = torch.utils.data.DataLoader(val_data,
batch_size=batch_size,
shuffle=False)
 test_loader = torch.utils.data.DataLoader(test_data,
batch_size=batch_size,
shuffle=False)
 submission_loader = torch.utils.data.DataLoader(submission_data,
batch_size=batch_size,
shuffle=False)
 classes =('0', '1', '2', '3', '4', '5', '6', '7', '8', '9' )<choose_model_class>
lin_init_model, lin_init_rmse, lin_init_pred = lin_model(x_train, x_val, y_train, y_val )
class Net(nn.Module):
def __init__(self, dropout = 0.40):
super(Net, self ).__init__()
self.dropout = dropout
self.conv1 = nn.Conv2d(in_channels=1, out_channels=64, kernel_size=5, stride=1, padding=2)
 self.conv1_bn = nn.BatchNorm2d(num_features=64)
 self.conv1_1 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=5, stride=1, padding=2)
 self.conv1_1_bn = nn.BatchNorm2d(num_features=64)
 self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
 self.d2_1 = nn.Dropout2d(p=self.dropout)
 self.conv2 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1)
 self.conv2_bn = nn.BatchNorm2d(num_features=128)
 self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
 self.d2_2 = nn.Dropout2d(p=self.dropout)
 self.conv3 = nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, stride=1, padding=1)
 self.conv3_bn = nn.BatchNorm2d(num_features=256)
 self.pool3 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
 self.d2_3 = nn.Dropout2d(p=self.dropout)
 self.fc1 = nn.Linear(256 * 3 * 3, 512)
 self.d1_1 = nn.Dropout(p=self.dropout)
 self.fc2 = nn.Linear(in_features=512, out_features=256)
 self.d1_2 = nn.Dropout(p=self.dropout)
 self.fc3 = nn.Linear(in_features=256, out_features=128)
 self.d1_3 = nn.Dropout(p=self.dropout)
 self.out = nn.Linear(in_features=128, out_features=10)
 def forward(self, x):
x = self.conv1(x)
 x = self.conv1_bn(x)
 x = F.relu(x)
 x = self.conv1_1(x)
 x = self.conv1_1_bn(x)
 x = F.relu(x)
 x = self.d2_1(x)
 x = self.pool1(x)
 x = self.conv2(x)
 x = self.conv2_bn(x)
 x = F.relu(x)
 x = self.d2_2(x)
 x = self.pool2(x)
 x = self.conv3(x)
 x = self.conv3_bn(x)
 x = F.relu(x)
 x = self.d2_3(x)
 x = self.pool3(x)
 x = x.view(-1, 256 * 3 * 3)
 x = F.relu(self.fc1(x))
x = self.d1_1(x)
 x = F.relu(self.fc2(x))
x = self.d1_2(x)
 x = F.relu(self.fc3(x))
x = self.d1_3(x)
 x = self.out(x)
 return F.log_softmax(x, dim=-1)
 net = Net().to(device)
 net<find_best_params>
k_choices = [10,20,30,40,50,60]
knn_cols = ['pickup_longitude','pickup_latitude','dropoff_longitude','dropoff_latitude']
knn_init, knn_init_rmse, knn_init_pred = knn_model(x_train[knn_cols], x_val[knn_cols], y_train, y_val, k_choices )
def find_lr(trn_loader, init_value = 1e-8, final_value=10., beta = 0.98):
num = len(trn_loader)-1
mult =(final_value / init_value)**(1/num)
 lr = init_value
optimizer.param_groups[0]['lr'] = lr
avg_loss = 0.
 best_loss = 0.
 batch_num = 0
losses = []
log_lrs = []
for data in trn_loader:
batch_num += 1
inputs = data[0].to(device)
 labels = data[1].to(device)
 optimizer.zero_grad()
outputs = net(inputs)
 loss = criterion(outputs, labels)
 avg_loss = beta * avg_loss +(1-beta)*loss.item()
smoothed_loss = avg_loss /(1 - beta**batch_num)
 if batch_num > 1 and smoothed_loss > 4 * best_loss:
return log_lrs, losses
if smoothed_loss < best_loss or batch_num==1:
best_loss = smoothed_loss
losses.append(smoothed_loss)
 log_lrs.append(math.log10(lr))
loss.backward()
optimizer.step()
lr *= mult
optimizer.param_groups[0]['lr'] = lr
return log_lrs, losses<choose_model_class>
lgbm_params = {
'objective': 'regression',
'boosting': 'gbdt',
'num_leaves': 400,
'learning_rate': 0.1,
'max_bin': 3000,
'num_rounds': 5000,
'early_stopping_rounds': 100,
'metric' : 'rmse'
}
lgbm_init_model, lgbm_init_rmse, lgbm_init_pred = lgbm_model(lgbm_params, x_train, x_val, y_train, y_val )
net = Net().to(device)
 criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(net.parameters() , lr=1e-1)
 logs,losses = find_lr(trn_loader = train_loader)
 plt.plot(logs[10:-5],losses[10:-5] )<init_hyperparams>
print('Linear Regression RMSE', lin_init_rmse)
 print('KNN RMSE', knn_init_rmse)
 print('LightGBM RMSE', lgbm_init_rmse )
net = Net().to(device)
 EPOCHS = 30
nn_output = []
optimizer = optim.Adam(net.parameters() , lr=4e-3)
 criterion = nn.CrossEntropyLoss()
def get_num_correct(preds, labels):
return preds.argmax(dim=1 ).eq(labels ).sum().item()
for epoch in range(EPOCHS):
epoch_loss = 0
epoch_correct = 0
net.train()
for data in train_loader:
X = data[0].to(device)
 y = data[1].to(device)
 net.zero_grad()
output = net(X)
 tloss = criterion(output, y)
 tloss.backward()
optimizer.step()
epoch_loss += tloss.item()
epoch_correct += get_num_correct(output, y)
 net.eval()
val_loss = 0
val_correct = 0
test_loss = 0
test_correct = 0
with torch.no_grad() :
for data in val_loader:
X = data[0].to(device)
 y = data[1].to(device)
 preds = net(X)
 vloss = criterion(preds, y)
 val_correct += get_num_correct(preds, y)
 val_loss += vloss.item()
for data in test_loader:
X = data[0].to(device)
 y = data[1].to(device)
 preds = net(X)
 tstloss = criterion(preds, y)
 test_correct += get_num_correct(preds, y)
 test_loss += tstloss.item()
tmp_nn_output = [epoch + 1,EPOCHS,
epoch_loss/len(train_loader.dataset),epoch_correct/len(train_loader.dataset)*100,
val_loss/len(val_loader.dataset), val_correct/len(val_loader.dataset)*100,
test_loss/len(test_loader.dataset), test_correct/len(test_loader.dataset)*100
]
nn_output.append(tmp_nn_output)
 print('Epoch [{}/{}] train loss: {:.6f} acc: {:.3f} - valid loss: {:.6f} acc: {:.3f} - Test loss: {:.6f} acc: {:.3f}' 
.format(*tmp_nn_output))<create_dataframe>
init_preds_ave =(lgbm_init_pred+knn_init_pred)/2
rmse = np.sqrt(mean_squared_error(y_val, init_preds_ave))
print('Combined RMSE: ', rmse )
pd_results = pd.DataFrame(nn_output,
columns = ['epoch','total_epochs','train_loss','train_acc','valid_loss','valid_acc','test_loss','test_acc'] 
)
 display(pd_results)
 print("Best Epoch: {}".format(pd_results.loc[pd_results.valid_acc.idxmax() ]['epoch']))<categorify>
def distance(lon1,lat1,lon2,lat2):
az12,az21,dist = Geod(ellps='WGS84' ).inv(lon1,lat1,lon2,lat2)
 return dist
def direction(lon1,lat1,lon2,lat2):
az12,az21,dist = Geod(ellps='WGS84' ).inv(lon1,lat1,lon2,lat2)
 return az12
def shared_features(df):

 rows = len(df)
 nyc_long, nyc_lat = [-74.001541]*rows, [40.724944]*rows
jfk_long, jfk_lat = [-73.785937]*rows, [40.645494]*rows
lga_long, lga_lat = [-73.872067]*rows, [40.774071]*rows
nla_long, nla_lat = [-74.177721]*rows, [40.690764]*rows
chp_long, chp_lat = [-73.137393]*rows, [41.366138]*rows
exp_long, exp_lat = [-74.0375]*rows, [40.736]*rows
pickup_long = df.pickup_longitude.tolist()
pickup_lat = df.pickup_latitude.tolist()
dropoff_long = df.dropoff_longitude.tolist()
dropoff_lat = df.dropoff_latitude.tolist()
df = df.assign(
 year=df.pickup_datetime.dt.year,
dayofyear=df.pickup_datetime.dt.dayofyear,
weekday=df.pickup_datetime.dt.dayofweek,
time=(df.pickup_datetime.dt.hour+df.pickup_datetime.dt.minute/5),
distance=distance(pickup_long, pickup_lat, dropoff_long, dropoff_lat),
direction=direction(pickup_long, pickup_lat, dropoff_long, dropoff_lat),
pickup_dist_nyc=pd.Series(distance(pickup_long, pickup_lat, nyc_long, nyc_lat)) ,
dropoff_dist_nyc=pd.Series(distance(dropoff_long, dropoff_lat, nyc_long, nyc_lat)) ,
pickup_dist_jfk=pd.Series(distance(pickup_long, pickup_lat, jfk_long, jfk_lat)) ,
dropoff_dist_jfk=pd.Series(distance(dropoff_long, dropoff_lat, jfk_long, jfk_lat)) ,
pickup_dist_lga=pd.Series(distance(pickup_long, pickup_lat, lga_long, lga_lat)) ,
dropoff_dist_lga=pd.Series(distance(dropoff_long, dropoff_lat, lga_long, lga_lat)) ,
pickup_dist_nla=pd.Series(distance(pickup_long, pickup_lat, nla_long, nla_lat)) ,
dropoff_dist_nla=pd.Series(distance(dropoff_long, dropoff_lat, nla_long, nla_lat)) ,
pickup_dist_chp=pd.Series(distance(pickup_long, pickup_lat, chp_long, chp_lat)) ,
dropoff_dist_chp=pd.Series(distance(dropoff_long, dropoff_lat, chp_long, chp_lat)) ,
pickup_dist_exp=pd.Series(distance(pickup_long, pickup_lat, exp_long, exp_lat)) ,
dropoff_dist_exp=pd.Series(distance(dropoff_long, dropoff_lat, exp_long, exp_lat)) 
)
 return df
def exploration_features(df):

 df = shared_features(df)
 df =(
 df 
.assign(
 hour=df.pickup_datetime.dt.hour,
close_to_airport='No',
fare_per_km=df.fare_amount*1000/df.distance,
direction_bucket = pd.cut(df.direction, np.linspace(-180, 180, 37)) ,
pickup_long_bucket=pd.cut(df.pickup_longitude, bins=2550, labels=False),
pickup_lat_bucket=pd.cut(df.pickup_latitude, bins=2200, labels=False),
dropoff_long_bucket=pd.cut(df.dropoff_longitude, bins=2550, labels=False),
dropoff_lat_bucket=pd.cut(df.dropoff_latitude, bins=2200, labels=False),
pickup_long_bucket_big=pd.cut(df.pickup_longitude, bins=255, labels=False),
pickup_lat_bucket_big=pd.cut(df.pickup_latitude, bins=220, labels=False),
dropoff_long_bucket_big=pd.cut(df.dropoff_longitude, bins=255, labels=False),
dropoff_lat_bucket_big=pd.cut(df.dropoff_latitude, bins=220, labels=False)
)
.drop(columns='pickup_datetime')
.query("0 < distance")
)
 df.loc[(( df['pickup_dist_jfk']<1500)|(df['dropoff_dist_jfk']<1500)) , 'close_to_airport'] = 'JFK'
df.loc[(( df['pickup_dist_lga']<1500)|(df['dropoff_dist_lga']<1500)) , 'close_to_airport'] = 'LaGuardia'
df.loc[(( df['pickup_dist_nla']<1500)|(df['dropoff_dist_nla']<1500)) , 'close_to_airport'] = 'Newark'
return df
num_classes = len(classes)
 net.eval()
predictions = torch.LongTensor().to(device)
 for images, _ in val_loader:
images = images.to(device)
 preds = net(images)
 predictions = torch.cat(( predictions, preds.argmax(dim=1)) , dim=0)
 cmt = torch.zeros(num_classes, num_classes, dtype=torch.int32)
 for i in range(len(val_labels)) :
cmt[val_labels[i], predictions[i]] += 1<predict_on_test>
train = load_df(5000000, features='explore' )
net.eval()
predictions = torch.LongTensor().to(device)
 for images in submission_loader:
images = images.to(device)
 preds = net(images)
 predictions = torch.cat(( predictions, preds.argmax(dim=1)) , dim=0 )<save_to_csv>
ks = scipy.stats.ks_2samp(
 train.where(train.distance > 50 ).dropna() ['fare_amount'],
train.where(train.distance <= 50 ).dropna() ['fare_amount'] 
)
 print('p-value:', ks[1] )
submission = pd.read_csv(".. /input/Kannada-MNIST/sample_submission.csv")
 submission['label'] = predictions.cpu().numpy()
submission.to_csv("predictions.csv", index=False )<load_from_csv>
long_trips = train[train.distance>75000].fare_amount.count()
print(long_trips, 'trips over 75km.' )
train=pd.read_csv("/kaggle/input/Kannada-MNIST/train.csv")
 test=pd.read_csv("/kaggle/input/Kannada-MNIST/test.csv" )<prepare_x_and_y>
def modelling_features(df):
df = shared_features(df)
 df = df.assign(
 sin_time=np.sin(2*np.pi*df['time']/24),
cos_time=np.cos(2*np.pi*df['time']/24),
sin_direction=np.sin(2*np.pi*df['direction']/360),
cos_direction=np.cos(2*np.pi*df['direction']/360),
sin_dayofyear=np.sin(2*np.pi*df['dayofyear']/365),
cos_dayofyear=np.cos(2*np.pi*df['dayofyear']/365),
sin_weekday=np.sin(2*np.pi*df['weekday']/6),
cos_weekday=np.cos(2*np.pi*df['weekday']/6),
direction_bucket=pd.cut(df['direction'], bins=37, labels=False)
 ).drop(columns=['pickup_datetime', 'time', 'direction', 'weekday', 'dayofyear'])
 return df
ids=test.id
test=test.drop(["id"],axis=1)
 train_result=train.label
train=train.drop(["label"],axis=1)
 test=test.iloc[:,:].values
test=test.reshape(-1,28,28,1)
 y=train_result.iloc[:].values
x=train.iloc[:,:].values
x=x.reshape(-1,28,28,1)
 x, test= x/255.0,test/255.0<set_options>
train = load_df(10000000, features='model')
 test['pickup_datetime'] = test['pickup_datetime'].str.slice(0, 15)
 test['pickup_datetime'] = pd.to_datetime(test['pickup_datetime'], utc=True, format='%Y-%m-%d %H:%M')
 test = modelling_features(test)
 train =(train 
.query(f'{test.pickup_longitude.min() -0.1} <= pickup_longitude <= {test.pickup_longitude.max() +0.1}')
.query(f'{test.pickup_latitude.min() -0.1} <= pickup_latitude <= {test.pickup_latitude.max() +0.1}')
.query(f'{test.dropoff_longitude.min() -0.1} <= dropoff_longitude <= {test.dropoff_longitude.max() +0.1}')
.query(f'{test.dropoff_latitude.min() -0.1} <= dropoff_latitude <= {test.dropoff_latitude.max() +0.1}')
)
 x_train, x_val, y_train, y_val = get_split_sets(train)
 x_train['fare_per_km'] = y_train*1000/(x_train.distance+5)
 fares_by_direction = x_train.query('5 < distance' ).groupby('direction_bucket')['fare_per_km'].mean()
x_train['adj_dist'] = [fares_by_direction[i] for i in x_train.direction_bucket]*x_train.distance/fares_by_direction.max()
x_val['adj_dist'] = [fares_by_direction[i] for i in x_val.direction_bucket]*x_val.distance/fares_by_direction.max()
test['adj_dist'] = [fares_by_direction[i] for i in test.direction_bucket]*test.distance/fares_by_direction.max()
x_train = x_train.drop(columns=['fare_per_km', 'direction_bucket'])
 x_val = x_val.drop(columns=['direction_bucket'])
 x_test = test.drop(columns=['key', 'direction_bucket'] )
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.25,
width_shift_range=0.25,
height_shift_range=0.25 )<choose_model_class>
lin_final_model, lin_final_rmse, lin_final_pred = lin_model(x_train, x_val, y_train, y_val )
Sequential=keras.Sequential
Conv2D=keras.layers.Conv2D
MaxPool2D=keras.layers.MaxPool2D
Dense=keras.layers.Dense
Flatten=keras.layers.Flatten
Dropout=keras.layers.Dropout
BatchNormalization=keras.layers.BatchNormalization<choose_model_class>
knn_cols = ['pickup_longitude','pickup_latitude','dropoff_longitude','dropoff_latitude']
k_choices = [18,24,30,40]
knn_final_model, knn_final_rmse, knn_final_pred = knn_model(x_train[knn_cols], x_val[knn_cols], y_train, y_val, k_choices )
def generate_model() :
model=Sequential()
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'same',
activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'same',
activation ='relu', input_shape =(28,28,1)))
 model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))
 model.add(Dropout(0.3))
model.add(Conv2D(filters = 128, kernel_size =(3,3),padding = 'same',
activation ='relu'))
model.add(Conv2D(filters = 128, kernel_size =(3,3),padding = 'same',
activation ='relu'))
model.add(Conv2D(filters = 128, kernel_size =(3,3),padding = 'same',
activation ='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))
 model.add(Dropout(0.3))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.3))
model.add(Dense(10, activation = "softmax"))
model.compile(keras.optimizers.Adam(0.0001),
loss="sparse_categorical_crossentropy",
metrics=["accuracy"] 
)
 return model<split>
lgbm_params = {
'objective': 'regression',
'boosting': 'gbdt',
'reg_sqrt': True,
'learning_rate': 0.03,
'num_leaves': 1200,
'max_depth': -1,
'max_bin': 5000,
'num_rounds': 5000,
'early_stopping_round': 50,
'metric': 'rmse'
}
lgbm_final_model, lgbm_final_rmse, lgbm_final_pred = lgbm_model(lgbm_params, x_train, x_val, y_train, y_val )
x_train, x_test, y_train, y_test=train_test_split(x,y,test_size=0.2 )<train_model>
print('Linear Regression RMSE', lin_final_rmse)
 print('KNN RMSE', knn_final_rmse)
 print('LightGBM RMSE', lgbm_final_rmse )
model=generate_model()
history = model.fit_generator(datagen.flow(x_train,y_train, batch_size=40),
epochs = 45,
validation_data=(x_test,y_test),
validation_steps=int(x_test.shape[0]/40),
verbose = 1, steps_per_epoch=x_train.shape[0] / 40 )<save_to_csv>
d = {}
for a in np.linspace(0,1,101):
final_preds_ave =(lgbm_final_pred*(1-a)+ knn_final_pred * a)
 rmse = np.sqrt(mean_squared_error(y_val, final_preds_ave))
d[a] = rmse
alpha = min(d, key=d.get)
 print('Best weight to give KNN: ', alpha )
predictions=model.predict_classes(test)
 output=pd.DataFrame({"id":ids, "label":predictions})
 output.to_csv("submission.csv",index=False )<load_from_csv>
lgbm_test_pred = lgbm_final_model.predict(x_test, num_iteration=lgbm_final_model.best_iteration)
 knn_test_pred = knn_final_model.predict(x_test[['pickup_longitude','pickup_latitude','dropoff_longitude','dropoff_latitude']])
 submission_pred =(lgbm_test_pred*(1-alpha)+ knn_test_pred * alpha)
 submission = pd.DataFrame({'key': test.key, 'fare_amount': submission_pred})
 submission.to_csv('submission_10_10_20_comb.csv', index=False )