Kaggle competition: New York City Taxi Fare Prediction
learn = Learner(databunch, six_conv_architecture, loss_func = nn.CrossEntropyLoss() , metrics=[accuracy] )<train_model>
df = pd.read_csv(".. /input/train.csv",nrows=1005000 )
learn.fit_one_cycle(15 )<predict_on_test>
test = pd.read_csv(".. /input/test.csv" )
preds, ids = learn.get_preds(DatasetType.Test)
 y = torch.argmax(preds, dim=1 )<create_dataframe>
testkey = test.key
submission = pd.DataFrame({ 'id': ids,'label': y } )<save_to_csv>
df = df.dropna(how = 'any', axis = 'rows' )
submission.to_csv(path_or_buf ="submission4.csv", index=False )<import_modules>
l = df[(df.pickup_latitude > 42.5)|(df.pickup_latitude < 40.0)|(df.dropoff_latitude > 42.5)|(df.dropoff_latitude < 40.0)|(df.pickup_longitude > -73.0)|(df.pickup_longitude < -75.0)|(df.dropoff_longitude > -73.0)|(df.dropoff_longitude < -75.0)].index
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from keras import Model, backend as K
from keras.layers import *
from keras.optimizers import SGD
from keras.callbacks import ModelCheckpoint, LambdaCallback
from keras.preprocessing.image import ImageDataGenerator
from keras.utils import to_categorical
from sklearn.model_selection import train_test_split<load_from_csv>
df = df.drop(l,axis=0 )
epochs = 30
batch_size = 128
dropout =.5
alpha = 1
df_train = pd.read_csv('.. /input/Kannada-MNIST/train.csv')
 df_test = pd.read_csv('.. /input/Kannada-MNIST/Dig-MNIST.csv')
 x_train, y_train = np.array(df_train.iloc[:, 1:]), np.array([i for i in df_train.iloc[:, 0].apply(to_categorical, args=(10,)) ])
 x_train, x_test, y_train, y_test = train_test_split(x_train, y_train, test_size=.3, random_state=1234)
 x_train = x_train.reshape(( x_train.shape[0], 28, 28, 1))
x_test = x_test.reshape(( x_test.shape[0], 28, 28, 1))
del df_train, df_test<normalization>
z = df[(df.fare_amount > 350.0)|(df.fare_amount < 0.0)|(df.passenger_count > 7.0)|(df.passenger_count < 0.0)].index
class VariableDropout(Layer):
def __init__(self, rate, noise_shape=None, seed=None, **kwargs):
self.rate = K.variable(min(1., max(0., rate)))
 self.noise_shape = noise_shape
self.seed = seed
super(VariableDropout, self ).__init__(**kwargs)
 def call(self, x, training=None):
def dropout() :
return K.dropout(x,
self.rate,
self._get_noise_shape(x),
seed=self.seed)
 return K.in_train_phase(dropout, x,
training=training)
 def compute_output_shape(self, input_shape):
return input_shape
def set_rate(self, rate):
K.set_value(self.rate, min(1, max(0., rate)))
 def _get_noise_shape(self, inputs):
if self.noise_shape is None:
return self.noise_shape
symbolic_shape = K.shape(inputs)
 noise_shape = [symbolic_shape[axis] if shape is None else shape
for axis, shape in enumerate(self.noise_shape)]
return tuple(noise_shape )<categorify>
df = df.drop(z,axis=0 )
def model() :
x = Input(( 28, 28, 1))
y = Conv2D(32, 3, activation='relu' )(x)
 y = BatchNormalization()(y)
 y = Conv2D(32, 4, strides=2, activation='relu' )(y)
 y = BatchNormalization()(y)
 y = Conv2D(64, 3, activation='relu' )(y)
 y = BatchNormalization()(y)
 y = Conv2D(64, 4, strides=2, activation='relu' )(y)
 y = BatchNormalization()(y)
 y = Conv2D(128, 3, activation='relu' )(y)
 y = BatchNormalization()(y)
 y = Conv2D(128, 2, activation='relu' )(y)
 y = BatchNormalization()(y)
 y = Flatten()(y)
 y = VariableDropout(dropout, seed=1234, name='dropout' )(y)
 y = Dense(256, activation='relu' )(y)
 y = Dense(10, activation='softmax' )(y)
 return Model(x, y)
 model = model()
model.compile(SGD(momentum=.9, nesterov=True), 'categorical_crossentropy', metrics=['acc'])
 model.summary()<normalization>
def distlatlong(lon1,lat1,lon2,lat2):
lat1=radians(lat1)
 lat2=radians(lat2)
 lon1 = radians(lon1)
 lon2 = radians(lon2)
 dlon = lon2 - lon1
dlat = lat2 - lat1
a =(sin(dlat/2)) **2 + cos(lat1)* cos(lat2)*(sin(dlon/2)) **2
c = 2 * atan2(sqrt(a), sqrt(1-a))
distance = 6373.0 * c
return distance
train_idg = ImageDataGenerator(rotation_range=.25*alpha,
width_shift_range=.2*alpha,
height_shift_range=.2*alpha,
zoom_range=.2*alpha,
preprocessing_function=lambda x: x/127.5-1)
 train_idg = train_idg.flow(x_train,
y_train,
batch_size,
shuffle=False,
seed=1234)
 test_idg = ImageDataGenerator(preprocessing_function=lambda x: x/127.5-1)
 test_idg = test_idg.flow(x_test,
y_test,
batch_size=batch_size,
shuffle=False )<train_model>
df['dist'] = [distlatlong(df.pickup_longitude[i],df.pickup_latitude[i],df.dropoff_longitude[i],df.dropoff_latitude[i])for i in df.index]
history = model.fit_generator(train_idg,
len(train_idg),
epochs,
callbacks=[adjust_alpha, model_checkpoint],
validation_data=test_idg,
validation_steps=len(test_idg),
shuffle=False ).history<load_from_csv>
test['dist'] = [distlatlong(test.pickup_longitude[i],test.pickup_latitude[i],test.dropoff_longitude[i],test.dropoff_latitude[i])for i in test.index]
model.load_weights('Weights.h5')
 sample_submission = pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv')
 df_test = pd.read_csv('.. /input/Kannada-MNIST/test.csv')
 df_test = np.array(df_test.iloc[:, 1:] ).reshape(( df_test.shape[0], 28, 28, 1)) /127.5-1<save_to_csv>
df['pickup_datetime'] = pd.to_datetime(df['pickup_datetime'] )
for i in range(df_test.shape[0]):
sample_submission['label'][i] = np.argmax(model.predict(df_test[i:i+1]))
sample_submission.to_csv('submission.csv', index=False )<load_from_csv>
test['pickup_datetime'] = pd.to_datetime(test['pickup_datetime'] )
train_df=pd.read_csv(".. /input/Kannada-MNIST/train.csv")
 test_df=pd.read_csv(".. /input/Kannada-MNIST/test.csv")
 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>
df['latenights'] = [1 if df.pickup_datetime[i].hour < 5 else 0 for i in df.index]
test['latenights'] = [1 if test.pickup_datetime[i].hour < 5 else 0 for i in test.index]
x_train,x_val,y_train,y_val=train_test_split(x_train, y_train, test_size=0.2 )<define_search_model>
df['weekday'] = [1 if df.pickup_datetime[i].weekday() > 4 else 0 for i in df.index]
test['weekday'] = [1 if test.pickup_datetime[i].weekday() > 4 else 0 for i in test.index]
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>
df['year'] = [df.pickup_datetime[i].year for i in df.index]
checkpoint=ModelCheckpoint('bestweights.hdf5', monitor='val_acc', verbose=1, save_best_only=True, mode="max")
 epochs=80
lr=0.002
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>
test['year'] = [test.pickup_datetime[i].year for i in test.index]
results=model.predict(x_test )<prepare_output>
df['day'] = [df.pickup_datetime[i].day for i in df.index]
results=np.argmax(results, axis=1 )<create_dataframe>
test['day'] = [test.pickup_datetime[i].day for i in test.index]
dg=pd.DataFrame()
dg['id']=list(test_df.values[0:,0] )<feature_engineering>
feat = df.drop(['key','pickup_datetime'],axis=1)
 test = test.drop(['key','pickup_datetime'],axis=1 )
dg['label']=results<save_to_csv>
feat = pd.concat([feat,pd.get_dummies(feat.year)],axis=1)
 test = pd.concat([test,pd.get_dummies(test.year)],axis=1 )
dg.to_csv("submission.csv", index=False )<import_modules>
feat = feat.drop('year',axis=1 )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
import gc
from datetime import datetime
from sklearn.model_selection import train_test_split
from keras.utils import to_categorical
from keras import backend as K
from keras.layers import Dense, Dropout,Flatten
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.models import Sequential
from keras.layers import Dropout
from keras.layers.core import Activation
from keras.layers.normalization import BatchNormalization<load_from_csv>
test = test.drop('year',axis=1 )
train = pd.read_csv('.. /input/Kannada-MNIST/train.csv')
 test = pd.read_csv('.. /input/Kannada-MNIST/test.csv')
 valid = pd.read_csv('.. /input/Kannada-MNIST/Dig-MNIST.csv')
 sub = pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv' )<categorify>
label = feat['fare_amount']
train_arr = train.values[0:,1:]
labels = train.values[0:,0]
labels = to_categorical(labels )<split>
feat = feat.drop('fare_amount',axis=1 )
valid_arr = valid.values
valid_arrx = valid_arr[0:,1:]
valid_arry = valid_arr[0:,0]<categorify>
xtr,xts,ytr,yts = train_test_split(feat,label )
valid_arrx = valid_arrx/255.0
valid_arrx = valid_arrx.reshape(valid_arr.shape[0], 28, 28, 1)
 valid_arry = to_categorical(valid_arry )<choose_model_class>
xgbtrain = xgboost.DMatrix(xtr,ytr)
 xgbtest = xgboost.DMatrix(xts,yts)
 xgbfinaltest = xgboost.DMatrix(test )
model = Sequential()
model.add(Conv2D(32,(3,3),input_shape=train_arr.shape[1:], activation='relu', padding='same'))
model.add(MaxPooling2D(( 2,2)))
 model.add(Dropout(0.2))
model.add(Conv2D(64,(3,3), activation='relu', padding='same'))
model.add(MaxPooling2D(( 2,2)))
 model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
 model.summary()<train_model>
params={'eval_metric':'rmse','objective':'reg:linear'}
history = model.fit(train_arr, labels, validation_data=(valid_arrx,valid_arry), epochs=15, batch_size=128 )<define_variables>
xgbmodel = xgboost.train(params,
dtrain=xgbtrain,num_boost_round=350,
early_stopping_rounds=30,evals=[(xgbtest,'test')] )
test = test.values
test_arr = test[0:,1:]
id_samples = test[0:,0]
test_arr = test_arr / 255.0
test_arr = test_arr.reshape(( test_arr.shape[0],28,28,1))<predict_on_test>
pred = xgbmodel.predict(xgbfinaltest )
y_pred = model.predict_classes(test_arr )<save_to_csv>
pred = pred.round(2 )
sub = pd.DataFrame()
sub['id'] = list(id_samples)
 sub['label'] = y_pred
sub.head()
sub.to_csv('submission.csv',index = False )<import_modules>
finalset = pd.DataFrame({'key':testkey,'fare_amount':pred} )
print(tf.__version__ )<load_from_csv>
finalset = finalset[['key','fare_amount']]
train = pd.read_csv("/kaggle/input/Kannada-MNIST/train.csv")
 validation = pd.read_csv("/kaggle/input/Kannada-MNIST/Dig-MNIST.csv")
 train = train.append(validation)
 test = pd.read_csv("/kaggle/input/Kannada-MNIST/test.csv")
 sample_submission = pd.read_csv("/kaggle/input/Kannada-MNIST/sample_submission.csv" )<prepare_x_and_y>
finalset.to_csv('finaloutput.csv',index=False )