Kaggle competition: New York City Taxi Fare Prediction
train = pd.read_csv(".. /input/Kannada-MNIST/train.csv")
 test = pd.read_csv(".. /input/Kannada-MNIST/test.csv" )<prepare_x_and_y>
from geopy.distance import great_circle
Y = train["label"]
X = train.drop(labels = ["label"],axis = 1 )<feature_engineering>
warnings.filterwarnings('ignore' )
X = X / 255.0
test = test / 255.0<set_options>
train = pd.read_csv('.. /input/train.csv',nrows=10_000_000 )
%matplotlib inline<categorify>
train = train.loc[(train['fare_amount'] > 0)&(train['fare_amount'] < 200)]
train = train.loc[(train['pickup_longitude'] > -150)&(train['pickup_longitude'] < 0)]
train = train.loc[(train['pickup_latitude'] > 0)&(train['pickup_latitude'] < 80)]
train = train.loc[(train['dropoff_longitude'] > -150)&(train['dropoff_longitude'] < 0)]
train = train.loc[(train['dropoff_latitude'] > 0)&(train['dropoff_longitude'] < 80)]
train = train.loc[train['passenger_count'] <= 8]
Y = to_categorical(Y, num_classes = 10 )<split>
test = pd.read_csv('.. /input/test.csv' )
X_train, X_val, Y_train, Y_val = train_test_split(X, Y, test_size = 0.1, random_state=42 )<choose_model_class>
def haversine(lon1, lat1, lon2, lat2):

 lon1 = np.radians(lon1)
 lat1 = np.radians(lat1)
 lon2 = np.radians(lon2)
 lat2 = np.radians(lat2)
 dlon = lon2 - lon1
dlat = lat2 - lat1
a = np.sin(dlat/2)**2 + np.cos(lat1)* np.cos(lat2)* np.sin(dlon/2)**2
c = 2 * np.arcsin(np.sqrt(a))
r = 6371
return c * r
Permute, TimeDistributed, Bidirectional,GRU
hidden_unit = 32
kernel_size = 3
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'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))
 model.add(Dropout(0.25))
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.25))
model.add(Conv2D(filters = 256, kernel_size =(3,3),padding = 'Same' , activation ='relu'))
model.add(Conv2D(filters = 256, kernel_size =(3,3),padding = 'Same' , activation ='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(1,2), strides=(1,2)))
 model.add(Dropout(0.25))
model.add(TimeDistributed(Flatten()))
model.add(Bidirectional(GRU(hidden_unit,return_sequences=True)))
 model.add(Flatten())
 model.add(Dense(256 , activation ='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Dense(10, activation = "softmax"))<train_model>
def add_travel_distance_vector_features(df):
df['distance'] = haversine(df.dropoff_longitude,df.dropoff_latitude,df.pickup_longitude,df.pickup_latitude)
 add_travel_distance_vector_features(train)
 add_travel_distance_vector_features(test )
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train )<choose_model_class>
optimizer = RMSprop(lr=0.005, rho=0.9, epsilon=1e-08, decay=0.0)
 model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"])
 learning_rate = ReduceLROnPlateau(monitor='val_accuracy',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001)
 epochs = 50
batch_size = 128<train_model>
train.isnull().sum()
history = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_val,Y_val),
verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=[learning_rate] )<predict_on_test>
train.isnull().sum()
results = model.predict(x_test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="label" )<load_from_csv>
train.dropna(how = 'any', axis = 'rows', inplace=True )
submission = pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv')
 submission['label'] = results<save_to_csv>
train['key'] = pd.to_datetime(train.key)
 test['key'] = pd.to_datetime(test.key )
submission.to_csv("submission.csv",index=False )<load_from_csv>
train['hour'] = train['key'].dt.hour
test['hour'] = test['key'].dt.hour
train['day'] = train['key'].dt.day
test['day'] = test['key'].dt.day
train['month'] = train['key'].dt.month
test['month'] = test['key'].dt.month
train['year'] = train['key'].dt.year
test['year'] = test['key'].dt.year
train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')
 test = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv' )<set_options>
train['daysinmonth'] = train['key'].dt.daysinmonth
test['daysinmonth'] = test['key'].dt.daysinmonth
train['weekofyear'] = train['key'].dt.weekofyear
test['weekofyear'] = test['key'].dt.weekofyear
train['dayofweek'] = train['key'].dt.dayofweek
test['dayofweek'] = test['key'].dt.dayofweek
train['dayofyear'] = train['key'].dt.dayofyear
test['dayofyear'] = test['key'].dt.dayofyear
train['quarter'] = train['key'].dt.quarter
test['quarter'] = test['key'].dt.quarter
%matplotlib inline
np.random.seed(2)
 sns.set(style='white', context='notebook', palette='deep' )<drop_column>
train.key = train.key.values.astype(np.int64)
 test.key = test.key.values.astype(np.int64 )
train_df = train
test_df = test<data_type_conversions>
train.pop('pickup_datetime')
 test.pop('pickup_datetime')
 train.pop('key')
 test.pop('key' )
x_train=train_df.drop(["label"], axis=1 ).values.astype('float32')
 y_train=train_df["label"].values.astype("int32")
 x_test=test_df.drop(["id"],axis=1 ).values.astype('float32' )<split>
train['longitude_distance'] = abs(train['pickup_longitude'] - train['dropoff_longitude'])
 train['latitude_distance'] = abs(train['pickup_latitude'] - train['dropoff_latitude'])
 test['longitude_distance'] = abs(test['pickup_longitude'] - test['dropoff_longitude'])
 test['latitude_distance'] = abs(test['pickup_latitude'] - test['dropoff_latitude'])
 train['distance_travelled_sin'] = np.sin(( train['longitude_distance'] ** 2 * train['latitude_distance'] ** 2)**.5)
 test['distance_travelled_sin'] = np.sin(( test['longitude_distance'] ** 2 * test['latitude_distance'] ** 2)**.5 )
x_train,x_val,y_train,y_val=train_test_split(x_train, y_train, test_size=0.2)
<define_search_model>
y = train.pop('fare_amount')
 X = train
X=Input(shape=[28,28,1])
 x=Conv2D(16,(3,3), strides=1, padding="same", name="conv1" )(X)
 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(10, name="Dense3" )(x)
 x=Activation('softmax' )(x)
 model=Model(inputs=X, outputs=x )<train_model>
from sklearn.preprocessing import StandardScaler
checkpoint=ModelCheckpoint('bestweights.hdf5', monitor='val_accuracy', verbose=1, save_best_only=True, mode="max")
 epochs=80
lr=0.001
optimizer=Adam(lr=lr, decay=lr/(epochs*1.5))
model.compile(optimizer=optimizer, loss="sparse_categorical_crossentropy",metrics=["accuracy"])
 datagenerator=ImageDataGenerator(rotation_range=9, zoom_range=0.25, width_shift_range=0.25, height_shift_range=0.25)
 datagenerator.fit(x_train)
 batch_size=64
history=model.fit_generator(datagenerator.flow(x_train, y_train, batch_size=batch_size), epochs=epochs, validation_data=(x_val, y_val), verbose=2,steps_per_epoch=x_train.shape[0]//batch_size, callbacks=[checkpoint])
 model.load_weights("bestweights.hdf5" )<predict_on_test>
scaler = StandardScaler()
results = model.predict(x_test )<prepare_output>
X = scaler.fit_transform(X)
 test = scaler.transform(test )
results = np.argmax(results, axis = 1 )<create_dataframe>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.001, random_state=124 )
fin=pd.DataFrame()
fin['id']=list(test_df.values[0:,0])
 fin['label']=results<save_to_csv>
from keras.layers import Dense, Input,Dropout,LeakyReLU,LSTM,BatchNormalization
from keras.models import Model
from keras import backend as Backend
import keras.activations as act
fin.to_csv('submission.csv' ,index = False )<import_modules>
def rmse(y_true, y_pred):
return Backend.sqrt(Backend.mean(Backend.square(y_pred - y_true), axis=-1))
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename)) 
<define_variables>
BASE_PATH = '/kaggle/input/Kannada-MNIST/'
np.random.seed(1973 )<load_from_csv>
def lstm(n):
model_in = Input(shape=(1,n))
model = LSTM(100 )(model_in)
 model = Dropout(0.2 )(model)
 model = Dense(1,activation=act.selu )(model)
 model = Model(model_in,model)
 model.compile(loss='mse', optimizer='nadam',metrics=[rmse])
 return model
train_src_df = pd.read_csv(filepath_or_buffer=BASE_PATH+'train.csv')
 test_src_df = pd.read_csv(filepath_or_buffer=BASE_PATH+'test.csv')
 dm_src_df = pd.read_csv(filepath_or_buffer=BASE_PATH+'Dig-MNIST.csv')
 submission = pd.read_csv(filepath_or_buffer=BASE_PATH+'sample_submission.csv' )<set_options>
history = model.fit(X_train,y_train,batch_size=10000,epochs=100,verbose=1,validation_data=(X_test,y_test))
train_datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
zca_epsilon=1e-06,
rotation_range=15,
width_shift_range=0.2,
height_shift_range=0.2,
brightness_range=[0.7,1.0],
shear_range=0.2,
zoom_range=0.2,
channel_shift_range=0.0,
fill_mode='constant',
cval=0.0,
horizontal_flip=False,
vertical_flip=False,
rescale=1./255,
preprocessing_function=None,
data_format='channels_last',
validation_split=0.0,
dtype='float32')
 test_datagen = ImageDataGenerator(rescale=1./255)
 train_datagen.fit(X_train)
<normalization>
model.evaluate(X_test,y_test )
def gelu(x):

 cdf = 0.5 *(1.0 + tf.tanh(
(np.sqrt(2 / np.pi)*(x + 0.044715 * tf.pow(x, 3)))))
 return x * cdf 
<choose_model_class>
pres = model.predict(test )
def create_model(fig_size=28, channel_num=1, class_num=10, activation=relu):
model = Sequential()
model.add(Conv2D(64,(5, 5), padding='same', activation=activation, input_shape=(fig_size, fig_size, channel_num)))
 model.add(Dropout(0.5))
model.add(Conv2D(64,(5, 5), padding='same', activation=activation))
model.add(Dropout(0.5))
model.add(MaxPooling2D(2))
model.add(Conv2D(128,(3, 3), padding='same', activation=activation))
model.add(Dropout(0.5))
model.add(Conv2D(128,(3, 3), padding='same', activation=activation))
model.add(Dropout(0.5))
model.add(MaxPooling2D(2))
model.add(Conv2D(256,(3, 3), padding='same', activation=activation))
model.add(Dropout(0.5))
model.add(Conv2D(256,(3, 3), padding='same', activation=activation))
model.add(Dropout(0.5))
model.add(Conv2D(256,(3, 3), padding='same', activation=activation))
model.add(Dropout(0.5))
model.add(MaxPooling2D(2))
model.add(Conv2D(1024,(1, 1), activation=activation))
model.add(Dropout(0.5))
model.add(DepthwiseConv2D(( 3, 3), padding='valid', activation=activation))
model.add(Dropout(0.5))
model.add(Flatten())
 model.add(Dense(512, activation=activation))
model.add(Dropout(0.5))
model.add(Dense(512, activation=activation))
model.add(Dropout(0.5))
model.add(Dense(class_num, activation='softmax'))
model.compile(optimizer=Adam() ,
loss='categorical_crossentropy',
metrics=['accuracy'])
 return model<choose_model_class>
test = pd.read_csv('.. /input/test.csv' )
rlr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=3, min_lr=1.0e-8, verbose=1)
 mcp = ModelCheckpoint(filepath='weights.hdf5', verbose=1, save_best_only=True )<define_variables>
submission = pd.DataFrame(
 {'key': test.key, 'fare_amount': pres.reshape(9914)},
columns = ['key', 'fare_amount'] )
<train_model><EOS> 
submission.to_csv('submission.csv', index = False )