Kaggle competition: New York City Taxi Fare Prediction
df_train = pd.read_csv(DATAPATH/'train.csv')
 df_train['fn'] = df_train.index<normalization>
train = pd.read_csv(".. /input/new-york-city-taxi-fare-prediction/train.csv", nrows = 1000000)
 test = pd.read_csv(".. /input/new-york-city-taxi-fare-prediction/test.csv" )
class PixelImageItemList(ImageList):
def open(self,fn):
img_pixel = self.inner_df.loc[self.inner_df['fn'] == int(fn[2:])].values[0,1:785]
img_pixel = img_pixel.reshape(28,28)
 return vision.Image(pil2tensor(img_pixel,np.float32 ).div_(255))<feature_engineering>
train.isnull().sum().sort_values(ascending=False )
tfms=get_transforms(do_flip=False)
 with_tta =([],tfms[0] )<categorify>
test.isnull().sum().sort_values(ascending=False )
bs = 1024
data =(src.transform(tfms=with_tta)
.databunch(num_workers=2,bs=bs)
.normalize() )<choose_model_class>
train = train.drop(train[train.isnull().any(1)].index, axis = 0 )
best_architecture = nn.Sequential(
 conv_layer(1,32,stride=1,ks=3),
conv_layer(32,32,stride=1,ks=3),
conv_layer(32,32,stride=2,ks=5),
nn.Dropout(0.4),
conv_layer(32,64,stride=1,ks=3),
conv_layer(64,64,stride=1,ks=3),
conv_layer(64,64,stride=2,ks=5),
nn.Dropout(0.4),
Flatten() ,
nn.Linear(3136, 128),
relu(inplace=True),
nn.BatchNorm1d(128),
nn.Dropout(0.4),
nn.Linear(128,10),
nn.Softmax(-1)
 )<choose_model_class>
train = train.drop(train[train['fare_amount']<0].index, axis=0)
 train.shape
learn = Learner(data, best_architecture, loss_func = nn.CrossEntropyLoss() , metrics=[accuracy] ).mixup()<train_model>
train[train['passenger_count']>6]
learn.fit_one_cycle(50,1e-2 )<load_from_csv>
train = train.drop(train[train['passenger_count']==208].index, axis = 0 )
df_test = pd.read_csv(DATAPATH/'test.csv')
 df_test.rename(columns={'id':'label'}, inplace=True)
 df_test['fn'] = df_test.index
test_set = PixelImageItemList.from_df(df_test,path='.',cols='fn' )<feature_engineering>
train[train['pickup_latitude']<-90]
learn.data.add_test(test_set )<predict_on_test>
train[train['pickup_latitude']>90]
preds, _ = learn.get_preds(DatasetType.Test)
 preds.unsqueeze_(1);preds.shape<predict_on_test>
train = train.drop(((train[train['pickup_latitude']<-90])|(train[train['pickup_latitude']>90])).index, axis=0 )
num_preds = 14
for x in range(num_preds):
new_preds, _ = learn.get_preds(DatasetType.Test)
 new_preds.unsqueeze_(1);preds.shape
preds = torch.cat(( preds,new_preds),1 )<prepare_output>
train[train['pickup_longitude']<-180]
indv_preds = torch.argmax(preds, dim=2);indv_preds.shape<create_dataframe>
train[train['pickup_longitude']>180]
submission = pd.DataFrame({ 'id': np.arange(0,len(winner)) ,'label': winner } )<save_to_csv>
train = train.drop(((train[train['pickup_longitude']<-180])|(train[train['pickup_longitude']>180])).index, axis=0 )
submission.to_csv(path_or_buf ="submission.csv", index=False )<import_modules>
train[train['dropoff_latitude']<-90]
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt<load_from_csv>
train[train['dropoff_latitude']>90]
train = pd.read_csv(".. /input/Kannada-MNIST/train.csv")
 test = pd.read_csv(".. /input/Kannada-MNIST/test.csv" )<prepare_x_and_y>
train = train.drop(((train[train['dropoff_latitude']<-90])|(train[train['dropoff_latitude']>90])).index, axis=0 )
y = train["label"]
train.drop("label",axis=1,inplace=True)
 test.drop("id",axis=1,inplace=True )<feature_engineering>
train[train['dropoff_latitude']<-180]|train[train['dropoff_latitude']>180]
x_train = train.values
test = test.values
x_train = x_train/255
test = test/255<categorify>
train['key'] = pd.to_datetime(train['key'])
 train['pickup_datetime'] = pd.to_datetime(train['pickup_datetime'] )
y_train = pd.get_dummies(y )<split>
test['key'] = pd.to_datetime(test['key'])
 test['pickup_datetime'] = pd.to_datetime(test['pickup_datetime'] )
x_train, x_test, y_train, y_test = train_test_split(x_train,y_train,test_size=0.2,random_state=42,stratify=y_train )<import_modules>
pip install haversine
from keras.models import Sequential
from keras.layers import Dense, Conv2D, AveragePooling2D, Flatten, Dropout,MaxPool2D,BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau
from keras.optimizers import RMSprop<choose_model_class>
def haversine_distance(lat1, long1, lat2, long2):
data = [train, test]
for i in data:
R = 6371
phi1 = np.radians(i[lat1])
 phi2 = np.radians(i[lat2])
 delta_phi = np.radians(i[lat2]-i[lat1])
 delta_lambda = np.radians(i[long2]-i[long1])
 a = np.sin(delta_phi / 2.0)** 2 + np.cos(phi1)* np.cos(phi2)* np.sin(delta_lambda / 2.0)** 2
c = 2 * np.arctan2(np.sqrt(a), np.sqrt(1-a))
d =(R * c)
 i['H_Distance'] = d
return d
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same', activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same', activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))<choose_model_class>
haversine_distance('pickup_latitude', 'pickup_longitude', 'dropoff_latitude', 'dropoff_longitude' )
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0 )<choose_model_class>
data = [train,test]
for i in data:
i['Year'] = i['pickup_datetime'].dt.year
i['Month'] = i['pickup_datetime'].dt.month
i['Date'] = i['pickup_datetime'].dt.day
i['Day of Week'] = i['pickup_datetime'].dt.dayofweek
i['Hour'] = i['pickup_datetime'].dt.hour
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )<choose_model_class>
train.sort_values(['H_Distance','fare_amount'], ascending=False )
checkpoint=ModelCheckpoint('bestweights.hdf5', monitor='val_acc', verbose=1, save_best_only=True, mode="max" )<choose_model_class>
pearsonr(train['H_Distance'], train['fare_amount'] )
model.compile(optimizer=optimizer,loss='categorical_crossentropy',metrics=['acc'] )<choose_model_class>
bins_0 = train.loc[(train['H_Distance'] == 0), ['H_Distance']]
bins_1 = train.loc[(train['H_Distance'] > 0)&(train['H_Distance'] <= 10),['H_Distance']]
bins_2 = train.loc[(train['H_Distance'] > 10)&(train['H_Distance'] <= 50),['H_Distance']]
bins_3 = train.loc[(train['H_Distance'] > 50)&(train['H_Distance'] <= 100),['H_Distance']]
bins_4 = train.loc[(train['H_Distance'] > 100)&(train['H_Distance'] <= 200),['H_Distance']]
bins_5 = train.loc[(train['H_Distance'] > 200)&(train['H_Distance'] <= 300),['H_Distance']]
bins_6 = train.loc[(train['H_Distance'] > 300),['H_Distance']]
bins_0['bins'] = '0'
bins_1['bins'] = '0-10'
bins_2['bins'] = '11-50'
bins_3['bins'] = '51-100'
bins_4['bins'] = '100-200'
bins_5['bins'] = '201-300'
bins_6['bins'] = '>300'
dist_bins =pd.concat([bins_0,bins_1,bins_2,bins_3,bins_4,bins_5,bins_6])
 dist_bins.columns
train_gen = ImageDataGenerator(rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.1 )<train_model>
Counter(dist_bins['bins'] )
history= model.fit_generator(train_gen.flow(x_train,y_train,batch_size=86),
steps_per_epoch=x_train.shape[0]//86,
validation_data=(x_test,y_test),
validation_steps=x_test.shape[0]//86,
epochs=30,callbacks=[checkpoint,learning_rate_reduction ] )<load_pretrained>
train.loc[(( train['pickup_latitude']==0)&(train['pickup_longitude']==0)) &(( train['dropoff_latitude']!=0)&(train['dropoff_longitude']!=0)) &(train['fare_amount']==0)]
model.load_weights("bestweights.hdf5" )<save_to_csv>
train = train.drop(train.loc[(( train['pickup_latitude']==0)&(train['pickup_longitude']==0)) &(( train['dropoff_latitude']!=0)&(train['dropoff_longitude']!=0)) &(train['fare_amount']==0)].index, axis=0 )
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 submission = pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv')
 submission['label'] = results
submission.to_csv('submission.csv', index=False)
 submission.head()<load_from_csv>
test.loc[(( test['pickup_latitude']==0)&(test['pickup_longitude']==0)) &(( test['dropoff_latitude']!=0)&(test['dropoff_longitude']!=0)) ]
train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')
 test = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')
 sub = pd.read_csv('/kaggle/input/Kannada-MNIST/sample_submission.csv')
 print('Our train set have {} rows and {} columns'.format(train.shape[0], train.shape[1]))
print('Our test set have {} rows and {} columns'.format(test.shape[0], test.shape[1]))<count_missing_values>
train.loc[(( train['pickup_latitude']!=0)&(train['pickup_longitude']!=0)) &(( train['dropoff_latitude']==0)&(train['dropoff_longitude']==0)) &(train['fare_amount']==0)]
train.isnull().sum().sum()<split>
train = train.drop(train.loc[(( train['pickup_latitude']!=0)&(train['pickup_longitude']!=0)) &(( train['dropoff_latitude']==0)&(train['dropoff_longitude']==0)) &(train['fare_amount']==0)].index, axis=0 )
def preprocessing(train, test):
IMG_SIZE = 28
img_train = train.drop(['label'], axis = 1 ).values.reshape(-1, IMG_SIZE, IMG_SIZE, 1 ).astype('float32')
 img_test = test.drop(['id'], axis = 1 ).values.reshape(-1, IMG_SIZE, IMG_SIZE, 1 ).astype('float32')
 img_y = train['label'].values
img_train /= 255
img_test /= 255
x_train, x_val, y_train, y_val = train_test_split(img_train, img_y, test_size = 0.20)
 print('Our transformed train set have the following dimension: ', x_train.shape)
 print('Our transformed valid set have the following dimension: ', x_val.shape)
 return img_test, x_train, x_val, y_train, y_val<choose_model_class>
test.loc[(( test['pickup_latitude']!=0)&(test['pickup_longitude']!=0)) &(( test['dropoff_latitude']==0)&(test['dropoff_longitude']==0)) ]
n_classes = train['label'].value_counts().count()
def build_model(input_shape=(28, 28, 1), classes = n_classes):
input_layer = Input(shape=input_shape)
 x = Conv2D(16,(3,3), strides=1, padding="same", name="conv1" )(input_layer)
 x = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer="uniform", name="batch1" )(x)
 x = Activation('relu',name='relu1' )(x)
 x = Dropout(0.1 )(x)
 x = Conv2D(32,(3,3), strides=1, padding="same", name="conv2" )(x)
 x = BatchNormalization(momentum=0.15, epsilon=1e-5, gamma_initializer="uniform", name="batch2" )(x)
 x = Activation('relu',name='relu2' )(x)
 x = Dropout(0.15 )(x)
 x = MaxPooling2D(pool_size=2, strides=2, padding="same", name="max2" )(x)
 x = Conv2D(64,(5,5), strides=1, padding ="same", name="conv3" )(x)
 x = BatchNormalization(momentum=0.17, epsilon=1e-5, gamma_initializer="uniform", name="batch3" )(x)
 x = Activation('relu', name="relu3" )(x)
 x = MaxPooling2D(pool_size=2, strides=2, padding="same", name="max3" )(x)
 x = Conv2D(128,(5,5), strides=1, padding="same", name="conv4" )(x)
 x = BatchNormalization(momentum=0.15, epsilon=1e-5, gamma_initializer="uniform", name="batch4" )(x)
 x = Activation('relu', name="relu4" )(x)
 x = Dropout(0.17 )(x)
 x = Conv2D(64,(3,3), strides=1, padding="same", name="conv5" )(x)
 x = BatchNormalization(momentum=0.15, epsilon=1e-5, gamma_initializer="uniform", name="batch5" )(x)
 x = Activation('relu', name='relu5' )(x)
 x = Dropout(0.2 )(x)
 x = Conv2D(32,(3,3), strides=1, padding="same", name="conv6" )(x)
 x = BatchNormalization(momentum=0.15, epsilon=1e-5, gamma_initializer="uniform", name="batch6" )(x)
 x = Activation('relu', name="relu6" )(x)
 x = Dropout(0.05 )(x)
 x = Flatten()(x)
 x = Dense(50, name="Dense1" )(x)
 x = Activation('relu', name='relu7' )(x)
 x = Dropout(0.05 )(x)
 x = Dense(25, name="Dense2" )(x)
 x = Activation('relu', name='relu8' )(x)
 x = Dropout(0.03 )(x)
 x = Dense(classes, name="Dense3" )(x)
 x = Activation('softmax' )(x)
 model = Model(inputs=input_layer, outputs=x)
 return model<choose_model_class>
high_distance = train.loc[(train['H_Distance']>200)&(train['fare_amount']!=0)]
def trng_lr_ck_opt(modelname):
train_generator = ImageDataGenerator(rotation_range = 8,
zoom_range = 0.28,
width_shift_range = 0.25,
height_shift_range = 0.25)
 learning_rate_reduction = ReduceLROnPlateau(monitor = 'val_accuracy', patience = 5, verbose = 1, factor = 0.5, min_le = 0.000001)
 checkpoint = ModelCheckpoint(modelname+'.hdf5', monitor = 'val_accuracy', verbose = 1, save_best_only = True)
 return train_generator, learning_rate_reduction, checkpoint
def compile_model() :
optimizer = Adam(lr = 0.001)
 model = build_model(input_shape =(28, 28, 1), classes = n_classes)
 model.compile(loss = 'sparse_categorical_crossentropy', optimizer = optimizer, metrics = ['accuracy'])
 return model
def train_and_evaluate(train, test, batch_size, epochs):
img_test, x_train, x_val, y_train, y_val = preprocessing(train, test)
 model = compile_model()
train_generator, learning_rate_reduction, checkpoint = trng_lr_ck_opt('bestmodel')
 history = model.fit_generator(train_generator.flow(x_train, y_train, batch_size = batch_size),
steps_per_epoch = x_train.shape[0] // batch_size,
epochs = epochs,
validation_data =(x_val, y_val),
callbacks = [checkpoint, learning_rate_reduction])
 return img_test, history
BATCH_SIZE = 64
EPOCHS = 40
img_test, history = train_and_evaluate(train, test, BATCH_SIZE, EPOCHS )<prepare_x_and_y>
high_distance['H_Distance'] = high_distance.apply(
 lambda row:(row['fare_amount'] - 2.50)/1.56,
axis=1
)
def train_and_evaluate_kfold(train, test, batch_size, epochs):
NFOLDS = 5
folds = KFold(n_splits=NFOLDS)
 IMG_SIZE = 28
img_test = test.drop(['id'], axis = 1 ).values.reshape(-1, IMG_SIZE, IMG_SIZE, 1 ).astype('float32')/ 255
img_y = train['label'].values
train = train.drop(['label'], axis = 1)
 oof = np.zeros([train.shape[0], n_classes])
 preds = np.zeros([test.shape[0], n_classes])
 for fold_n,(train_index, val_index)in enumerate(folds.split(train)) :
print('Training fold {}'.format(fold_n + 1))
x_train, x_val = train.iloc[train_index], train.iloc[val_index]
y_train, y_val = img_y[train_index], img_y[val_index]
x_train = x_train.values.reshape(-1, IMG_SIZE, IMG_SIZE, 1 ).astype('float32')/ 255
x_val = x_val.values.reshape(-1, IMG_SIZE, IMG_SIZE, 1 ).astype('float32')/ 255
model = compile_model()
train_generator, learning_rate_reduction, checkpoint = trng_lr_ck_opt('model_fold_{}'.format(fold_n + 1))
history = model.fit_generator(train_generator.flow(x_train, y_train, batch_size = batch_size),
steps_per_epoch = x_train.shape[0] // batch_size,
epochs = epochs,
validation_data =(x_val, y_val),
callbacks = [checkpoint, learning_rate_reduction])
 model.load_weights('model_fold_{}'.format(fold_n + 1)+ '.hdf5')
 preds += model.predict(img_test)/ NFOLDS
oof[val_index] = model.predict(x_val)
 oof = np.argmax(oof, axis = 1)
 preds = np.argmax(preds, axis = 1)
 print('-'*20)
 print('Out of fold cv score: ', metrics.accuracy_score(img_y, oof))
return preds, oof
preds, oof = train_and_evaluate_kfold(train, test, BATCH_SIZE, EPOCHS )<save_to_csv>
train.update(high_distance )
sub['label'] = preds
sub.to_csv('cnn_5kfold_baseline.csv', index = False )<import_modules>
train[train['H_Distance']==0]
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt<import_modules>
train[(train['H_Distance']==0)&(train['fare_amount']==0)]
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, BatchNormalization
from keras.layers import Conv2D, MaxPooling2D
from keras.utils import to_categorical
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau
from keras.applications.resnet50 import ResNet50<load_from_csv>
train = train.drop(train[(train['H_Distance']==0)&(train['fare_amount']==0)].index, axis = 0 )
train_df = pd.read_csv('.. /input/Kannada-MNIST/train.csv')
 test_df = pd.read_csv('.. /input/Kannada-MNIST/test.csv')
 sample_sub = pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv')
 dig_mnist = pd.read_csv('.. /input/Kannada-MNIST/Dig-MNIST.csv' )<categorify>
rush_hour = train.loc[(((train['Hour']>=6)&(train['Hour']<=20)) &(( train['Day of Week']>=1)&(train['Day of Week']<=5)) &(train['H_Distance']==0)&(train['fare_amount'] < 2.5)) ]
rush_hour
train_data = np.array(train_df.iloc[:, 1:])
 val_data = np.array(dig_mnist.iloc[:, 1:])
 test_data = np.array(test_df.iloc[:, 1:])
 train_labels = to_categorical(train_df.iloc[:, 0])
 val_labels = to_categorical(dig_mnist.iloc[:, 0] )<train_model>
train=train.drop(rush_hour.index, axis=0 )
rows, cols = 28, 28
train_data = train_data.reshape(train_data.shape[0], rows, cols, 1)
 val_data = val_data.reshape(val_data.shape[0], rows, cols, 1)
 test_data = test_data.reshape(test_data.shape[0], rows, cols, 1 )<data_type_conversions>
non_rush_hour = train.loc[(((train['Hour']<6)|(train['Hour']>20)) &(( train['Day of Week']>=1)&(train['Day of Week']<=5)) &(train['H_Distance']==0)&(train['fare_amount'] < 3.0)) ]
non_rush_hour
train_data = train_data.astype('float32')
 val_data = val_data.astype('float32')
 test_data = test_data.astype('float32' )<feature_engineering>
weekends = train.loc[(( train['Day of Week']==0)|(train['Day of Week']==6)) &(train['H_Distance']==0)&(train['fare_amount'] < 3.0)]
weekends
train_data /= 255.0
val_data /= 255.0
test_data /= 255.0<define_variables>
train.loc[(train['H_Distance']!=0)&(train['fare_amount']==0)]
batch_size = 64
epochs = 5
input_shape =(rows, cols, 1 )<choose_model_class>
scenario_3 = train.loc[(train['H_Distance']!=0)&(train['fare_amount']==0)]
def baseline_model() :
model = Sequential()
model.add(Conv2D(64,(3,3), strides=1, padding="same", activation='relu', input_shape=input_shape))
model.add(BatchNormalization())
 model.add(Conv2D(64,(3,3), strides=1, padding="same", activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=2, strides=2, padding="same"))
model.add(Dropout(0.15))
model.add(Conv2D(128,(3,3), strides=1, padding="same", activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(128,(3,3), strides=1, padding="same", activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=2, strides=2, padding="same"))
model.add(Dropout(0.15))
model.add(Conv2D(256,(3,3), strides=1, padding="same", activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(256,(3,3), strides=1, padding="same", activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=2, strides=2, padding="same"))
model.add(Dropout(0.15))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(Dense(10, activation='softmax'))
return model<choose_model_class>
scenario_3.sort_values('H_Distance', ascending=False )
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'] )<init_hyperparams>
scenario_3['fare_amount'] = scenario_3.apply(
 lambda row:(( row['H_Distance'] * 1.56)+ 2.50), axis=1
)
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=8,
zoom_range = 0.15,
width_shift_range=0.15,
height_shift_range=0.15,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(train_data )<choose_model_class>
train.loc[(train['H_Distance']==0)&(train['fare_amount']!=0)]
learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )<train_model>
scenario_4 = train.loc[(train['H_Distance']==0)&(train['fare_amount']!=0)]
history = model.fit_generator(datagen.flow(train_data, train_labels, batch_size=batch_size),
epochs = epochs, validation_data =(val_data, val_labels),
steps_per_epoch=train_data.shape[0] // batch_size,
callbacks=[learning_rate_reduction] )<predict_on_test>
scenario_4.loc[(scenario_4['fare_amount']<=3.0)&(scenario_4['H_Distance']==0)]
preds = model.predict_classes(test_data )<prepare_output>
scenario_4.loc[(scenario_4['fare_amount']>3.0)&(scenario_4['H_Distance']==0)]
sample_sub.label = preds<save_to_csv>
scenario_4_sub = scenario_4.loc[(scenario_4['fare_amount']>3.0)&(scenario_4['H_Distance']==0)]
sample_sub.to_csv('SubmissionFile.csv', index=False )<import_modules>
scenario_4_sub['H_Distance'] = scenario_4_sub.apply(
 lambda row:(( row['fare_amount']-2.50)/1.56), axis=1
)
print('TF version',tf.__version__ )<define_variables>
train = train.drop(['key','pickup_datetime'], axis = 1)
 test = test.drop(['key','pickup_datetime'], axis = 1 )
MAX_LEN = 140
PATH = '.. /input/tf-roberta/'
tokenizer = tokenizers.ByteLevelBPETokenizer(
 vocab_file=PATH+'vocab-roberta-base.json',
merges_file=PATH+'merges-roberta-base.txt',
lowercase=True,
add_prefix_space=True 
)
 sentiment_id = {'positive': 1313, 'negative': 2430, 'neutral': 7974}
train = pd.read_csv('.. /input/tweet-sentiment-extraction/train.csv' ).fillna('')
 train.head()<define_variables>
x_train = train.iloc[:,train.columns!='fare_amount']
y_train = train['fare_amount'].values
x_test = test
ct = train.shape[0]
input_ids = np.ones(( ct,MAX_LEN),dtype='int32')
 attention_mask = np.zeros(( ct,MAX_LEN),dtype='int32')
 token_type_ids = np.zeros(( ct,MAX_LEN),dtype='int32')
 start_tokens = np.zeros(( ct,MAX_LEN),dtype='int32')
 end_tokens = np.zeros(( ct,MAX_LEN),dtype='int32')
 for k in range(train.shape[0]):
text1 = " "+" ".join(train.loc[k,'text'].split())
 text2 = " ".join(train.loc[k,'selected_text'].split())
 idx = text1.find(text2)
 chars = np.zeros(( len(text1)))
 chars[idx:idx+len(text2)]=1
if text1[idx-1]==' ': chars[idx-1] = 1
enc = tokenizer.encode(text1)
 offsets = []; idx=0
for t in enc.ids:
w = tokenizer.decode([t])
 offsets.append(( idx,idx+len(w)))
 idx += len(w)
 toks = []
for i,(a,b)in enumerate(offsets):
sm = np.sum(chars[a:b])
 if sm>0: toks.append(i)
 s_tok = sentiment_id[train.loc[k,'sentiment']]
input_ids[k,:len(enc.ids)+5] = [0] + enc.ids + [2,2] + [s_tok] + [2]
attention_mask[k,:len(enc.ids)+5] = 1
if len(toks)>0:
start_tokens[k,toks[0]+1] = 1
end_tokens[k,toks[-1]+1] = 1<feature_engineering>
import lightgbm as lgbm
test = pd.read_csv('.. /input/tweet-sentiment-extraction/test.csv' ).fillna('')
 ct = test.shape[0]
input_ids_t = np.ones(( ct,MAX_LEN),dtype='int32')
 attention_mask_t = np.zeros(( ct,MAX_LEN),dtype='int32')
 token_type_ids_t = np.zeros(( ct,MAX_LEN),dtype='int32')
 for k in range(test.shape[0]):
text1 = " "+" ".join(test.loc[k,'text'].split())
 enc = tokenizer.encode(text1)
 s_tok = sentiment_id[test.loc[k,'sentiment']]
input_ids_t[k,:len(enc.ids)+5] = [0] + enc.ids + [2,2] + [s_tok] + [2]
attention_mask_t[k,:len(enc.ids)+5] = 1<load_pretrained>
params = {
'boosting_type':'gbdt',
'objective': 'regression',
'nthread': -1,
'verbose': 0,
'num_leaves': 31,
'learning_rate': 0.05,
'max_depth': -1,
'subsample': 0.8,
'subsample_freq': 1,
'colsample_bytree': 0.6,
'reg_aplha': 1,
'reg_lambda': 0.001,
'metric': 'rmse',
'min_split_gain': 0.5,
'min_child_weight': 1,
'min_child_samples': 10,
'scale_pos_weight':1
}
<load_pretrained>
train_set = lgbm.Dataset(x_train, y_train, silent=True)
 train_set
<load_pretrained>
model = lgbm.train(params, train_set = train_set, num_boost_round=300 )
<load_pretrained>
pred_test_y = model.predict(x_test, num_iteration = model.best_iteration )
def build_model() :
ids = tf.keras.layers.Input(( MAX_LEN,), dtype=tf.int32)
 att = tf.keras.layers.Input(( MAX_LEN,), dtype=tf.int32)
 tok = tf.keras.layers.Input(( MAX_LEN,), dtype=tf.int32)
 config = RobertaConfig.from_pretrained(PATH+'config-roberta-base.json')
 bert_model = TFRobertaModel.from_pretrained(PATH+'pretrained-roberta-base.h5',config=config)
 x = bert_model(ids,attention_mask=att,token_type_ids=tok)
 x1 = tf.keras.layers.Dropout(0.15 )(x[0])
 x1 = tf.keras.layers.Conv1D(512, 2,padding='same' )(x1)
 x1 = tf.keras.layers.LeakyReLU()(x1)
 x1 = tf.keras.layers.Conv1D(128, 2,padding='same' )(x1)
 x1 = tf.keras.layers.Dense(1 )(x1)
 x1 = tf.keras.layers.Flatten()(x1)
 x1 = tf.keras.layers.Activation('softmax' )(x1)
 x2 = tf.keras.layers.Dropout(0.15 )(x[0])
 x2 = tf.keras.layers.Conv1D(512, 2, padding='same' )(x2)
 x2 = tf.keras.layers.LeakyReLU()(x2)
 x2 = tf.keras.layers.Conv1D(128, 2, padding='same' )(x2)
 x2 = tf.keras.layers.Dense(1 )(x2)
 x2 = tf.keras.layers.Flatten()(x2)
 x2 = tf.keras.layers.Activation('softmax' )(x2)
 model = tf.keras.models.Model(inputs=[ids, att, tok], outputs=[x1,x2])
 optimizer = tf.keras.optimizers.Adam(learning_rate=3e-5)
 model.compile(loss='categorical_crossentropy', optimizer=optimizer)
 return model<string_transform>
submission = pd.read_csv('.. /input/new-york-city-taxi-fare-prediction/sample_submission.csv')
 submission['fare_amount'] = pred_test_y
submission.to_csv('s.csv', index=False)
 submission.head(20 )