Kaggle competition: New York City Taxi Fare Prediction
test2['open_channels'] = submit['open_channels'].astype(int )<import_modules>
import tensorflow as tf
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import itertools
from keras.utils.np_utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from keras.optimizers import RMSprop
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau<set_options>
traintypes = {'fare_amount': 'float32',
'pickup_datetime': 'str',
'pickup_longitude': 'float32',
'pickup_latitude': 'float32',
'dropoff_longitude': 'float32',
'dropoff_latitude': 'float32',
'passenger_count': 'uint8'}
cols = list(traintypes.keys() )
%matplotlib inline
sns.set()<load_from_csv>
NROWS = 12000000
test_df = pd.read_csv(TEST_PATH)
 train_df = pd.read_csv(TRAIN_PATH, usecols=cols,nrows = NROWS, dtype=traintypes )
train = pd.read_csv("/kaggle/input/Kannada-MNIST/train.csv")
 test = pd.read_csv("/kaggle/input/Kannada-MNIST/test.csv")
 test.columns<prepare_x_and_y>
Y = train["label"]
X = train.drop(labels = ["label"],axis = 1)
 testX = test.drop(labels = ["id"],axis = 1)
 g = sns.countplot(Y)
 Y.value_counts().sort_values()<drop_column>
train_df = train_df.dropna(how = 'any', axis = 'rows' )
del train 
<load_from_csv>
def clean_df(df):
return df[(df.fare_amount > 0)& 
(df.pickup_longitude > -80)&(df.pickup_longitude < -70)& 
(df.pickup_latitude > 35)&(df.pickup_latitude < 45)& 
(df.dropoff_longitude > -80)&(df.dropoff_longitude < -70)& 
(df.dropoff_latitude > 35)&(df.dropoff_latitude < 45)& 
(df.passenger_count > 0)&(df.passenger_count < 10)]
X = X / 255.0
testX = testX / 255.0<categorify>
def sphere_dist(pickup_lat, pickup_lon, dropoff_lat, dropoff_lon):

 R_earth = 6371
pickup_lat, pickup_lon, dropoff_lat, dropoff_lon = map(np.radians,
[pickup_lat, pickup_lon,
dropoff_lat, dropoff_lon])
 dlat = dropoff_lat - pickup_lat
dlon = dropoff_lon - pickup_lon
a = np.sin(dlat/2.0)**2 + np.cos(pickup_lat)* np.cos(dropoff_lat)* np.sin(dlon/2.0)**2
return 2 * R_earth * np.arcsin(np.sqrt(a))
def add_airport_dist(dataset):

 jfk_coord =(40.639722, -73.778889)
 ewr_coord =(40.6925, -74.168611)
 lga_coord =(40.77725, -73.872611)
 pickup_lat = dataset['pickup_latitude']
dropoff_lat = dataset['dropoff_latitude']
pickup_lon = dataset['pickup_longitude']
dropoff_lon = dataset['dropoff_longitude']
pickup_jfk = sphere_dist(pickup_lat, pickup_lon, jfk_coord[0], jfk_coord[1])
 dropoff_jfk = sphere_dist(jfk_coord[0], jfk_coord[1], dropoff_lat, dropoff_lon)
 pickup_ewr = sphere_dist(pickup_lat, pickup_lon, ewr_coord[0], ewr_coord[1])
 dropoff_ewr = sphere_dist(ewr_coord[0], ewr_coord[1], dropoff_lat, dropoff_lon)
 pickup_lga = sphere_dist(pickup_lat, pickup_lon, lga_coord[0], lga_coord[1])
 dropoff_lga = sphere_dist(lga_coord[0], lga_coord[1], dropoff_lat, dropoff_lon)
 dataset['jfk_dist'] = pd.concat([pickup_jfk, dropoff_jfk], axis=1 ).min(axis=1)
 dataset['ewr_dist'] = pd.concat([pickup_ewr, dropoff_ewr], axis=1 ).min(axis=1)
 dataset['lga_dist'] = pd.concat([pickup_lga, dropoff_lga], axis=1 ).min(axis=1)
 return dataset
def add_datetime_info(dataset):
dataset['pickup_datetime'] = pd.to_datetime(dataset['pickup_datetime'],format="%Y-%m-%d %H:%M:%S UTC")
 dataset['hour'] = dataset.pickup_datetime.dt.hour
dataset['day'] = dataset.pickup_datetime.dt.day
dataset['month'] = dataset.pickup_datetime.dt.month
dataset['weekday'] = dataset.pickup_datetime.dt.weekday
dataset['year'] = dataset.pickup_datetime.dt.year
return dataset
Y = to_categorical(Y, num_classes = 10 )<prepare_x_and_y>
def transform_features(df):
df = add_datetime_info(df)
 df = add_airport_dist(df)
 df['distance'] = sphere_dist(df['pickup_latitude'], df['pickup_longitude'],
df['dropoff_latitude'] , df['dropoff_longitude'])
 df.drop(columns=['pickup_datetime'], inplace=True)
 return df
Y<split>
train_df = transform_features(train_df)
 test_df = transform_features(test_df)
 test_df.drop(columns=['key'], inplace=True )
X_train, X_val, Y_train, Y_val = train_test_split(X, Y, test_size = 0.2, shuffle=True )<choose_model_class>
train_df_y = train_df.fare_amount.copy()
train_df_X = train_df[test_df.columns]
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>
dtrain = lgb.Dataset(train_df_X, label = train_df_y, free_raw_data = False )
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"])
 learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy',
patience=3,
factor=0.3,
verbose=1,
min_lr=0.00001 )<define_variables>
print("Light Gradient Boosting Regressor: ")
 lgbm_params = {
'task': 'train',
'boosting_type': 'gbdt',
'objective': 'regression',
'metric': 'rmse',
'learning_rate' :'0.03',
'num_leaves':'31',
'max_depth' : '-1',
'subsample' :'.8',
'colsample_bytree' : '0.6',
'min_split_gain' : '0.5',
'min_child_weight' : '1',
'min_child_samples' :'10',
'scale_pos_weight' : '1',
'num_threads' : '4',
'seed' : '0',
'eval_freq' : '50'
}
folds = KFold(n_splits=5, shuffle=True, random_state=1)
 fold_preds = np.zeros(testshape[0])
 oof_preds = np.zeros(trainshape[0])
 dtrain.construct()
modelstart = time.time()
for trn_idx, val_idx in folds.split(train_df_X):
clf = lgb.train(
 params=lgbm_params,
train_set=dtrain.subset(trn_idx),
valid_sets=dtrain.subset(val_idx),
num_boost_round=10000,
early_stopping_rounds=125,
verbose_eval=500 
)
 oof_preds[val_idx] = clf.predict(dtrain.data.iloc[val_idx])
 fold_preds += clf.predict(test_df)/ folds.n_splits
print(mean_squared_error(train_df_y.iloc[val_idx], oof_preds[val_idx])**.5)
 print("Model Runtime: %0.2f Minutes"%(( time.time() - modelstart)/60))
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
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 )<train_model>
test_df = pd.read_csv(TEST_PATH )
epochs = 30
batch_size = 96
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_reduction] )<predict_on_test>
result = pd.DataFrame({'key':test_df['key'], 'fare_amount':fold_preds})
 result.head()
result.to_csv('taxi-fare-prediction.csv', index=False )