Kaggle competition: New York City Taxi Fare Prediction
<SOS> metric: RMSE Kaggle data source: new-york-city-taxi-fare-prediction<import_modules>
import math
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from collections import Counter
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier, GradientBoostingClassifier, ExtraTreesClassifier, VotingClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV, cross_val_score, StratifiedKFold, learning_curve<choose_model_class>
df_train = pd.read_csv('.. /input/train.csv', nrows=2000000, parse_dates=['pickup_datetime'])
 df_test = pd.read_csv('.. /input/test.csv', parse_dates=['pickup_datetime'] )
random_state = 2
classifiers = []
classifiers.append(SVC(random_state=random_state, max_iter=1000))
classifiers.append(DecisionTreeClassifier(random_state=random_state))
classifiers.append(AdaBoostClassifier(DecisionTreeClassifier(random_state=random_state),random_state=random_state,learning_rate=0.1))
classifiers.append(RandomForestClassifier(random_state=random_state))
classifiers.append(ExtraTreesClassifier(random_state=random_state))
classifiers.append(GradientBoostingClassifier(random_state=random_state))
classifiers.append(MLPClassifier(random_state=random_state, max_iter=1000))
classifiers.append(KNeighborsClassifier())
 classifiers.append(LogisticRegression(random_state = random_state, max_iter=1000))
classifiers.append(LinearDiscriminantAnalysis() )<compute_train_metric>
df_train['datatype'], df_test['datatype'] = 'training', 'testing'
df_test.insert(1, 'fare_amount', 0)
 df_data = pd.concat([df_train, df_test], ignore_index=True )
cv_results = []
for clf in classifiers:
cv_results.append(cross_val_score(clf, X_train, y = Y_train, cv = kfold, scoring = 'accuracy')) 
<choose_model_class>
def distance(lat1: float, lon1: float, lat2: float, lon2: float)-> float:

 angle = 0.017453292519943295
x = 0.5 - np.cos(( lat2 - lat1)* angle)/ 2 + np.cos(lat1 * angle)* np.cos(lat2 * angle)*(1 - np.cos(( lon2 - lon1)* angle)) / 2
return 0.6213712 * 12742 * np.arcsin(np.sqrt(x))
Scores = []
Params = []
Algorithm = []
GB = GradientBoostingClassifier()
GB_param = {'loss' : ["deviance"],
'n_estimators' : [100,200,300],
'learning_rate': [0.1, 0.05, 0.01],
'max_depth': [4, 8],
'min_samples_leaf': [100,150],
'max_features': [0.3, 0.1]
}
grid_GB = GridSearchCV(GB, param_grid = GB_param, scoring = 'accuracy', cv = kfold, n_jobs = -1, verbose = 1)
 grid_GB.fit(X_train, Y_train)
 Scores.append(grid_GB.best_score_)
 Params.append(grid_GB.best_params_)
 Algorithm.append(GB.__class__.__name__ )<train_on_grid>
df_data['fare_amount'] = np.log1p(df_data['fare_amount'] )
LDA = LinearDiscriminantAnalysis()
LDA_param = {"solver" : ["svd"],
"tol" : [0.0001,0.0002,0.0003]}
grid_LDA = GridSearchCV(LDA, param_grid = LDA_param, cv = kfold, n_jobs = -1, verbose = 1, scoring = 'accuracy')
 grid_LDA.fit(X_train, Y_train)
 Scores.append(grid_LDA.best_score_)
 Params.append(grid_LDA.best_params_)
 Algorithm.append(LDA.__class__.__name__ )<train_on_grid>
df_data['year'] = df_data['pickup_datetime'].dt.year
df_data['quarter'] = df_data['pickup_datetime'].dt.quarter
df_data['month'] = df_data['pickup_datetime'].dt.month
df_data['weekofyear'] = df_data['pickup_datetime'].dt.weekofyear
df_data['weekday'] = df_data['pickup_datetime'].dt.weekday
df_data['dayofweek'] = df_data['pickup_datetime'].dt.dayofweek
df_data['hour'] = df_data['pickup_datetime'].dt.hour
LR = LogisticRegression(max_iter=1000)
 LR_params = {
'penalty':['l1', 'l2'],
'C': np.logspace(0,4,10)
 }
grid_LR = GridSearchCV(LR, param_grid = LR_params, cv = kfold, n_jobs = 1, verbose = 1, scoring = 'accuracy')
 grid_LR.fit(X_train, Y_train)
 Scores.append(grid_LR.best_score_)
 Params.append(grid_LR.best_params_)
 Algorithm.append(LR.__class__.__name__ )<train_on_grid>
df_data['distance_euclidean'] = distance(df_data['pickup_latitude'], df_data['pickup_longitude'], \
df_data['dropoff_latitude'], df_data['dropoff_longitude'])
 df_data['distance_latitude'] = df_data['dropoff_latitude'] - df_data['pickup_latitude']
df_data['distance_longitude'] = df_data['dropoff_longitude'] - df_data['pickup_longitude']
RF = RandomForestClassifier()
RF_params = {"max_depth": [None],
"max_features": [1, 3, 10],
"min_samples_split": [2, 3, 10],
"min_samples_leaf": [1, 3, 10],
"bootstrap": [False],
"n_estimators" :[100,300],
"criterion": ["gini"]
}
grid_RF = GridSearchCV(RF, param_grid = RF_params, scoring = 'accuracy', cv = kfold, n_jobs = -1, verbose = 1)
 grid_RF.fit(X_train, Y_train)
 Scores.append(grid_RF.best_score_)
 Params.append(grid_RF.best_params_)
 Algorithm.append(RF.__class__.__name__ )<create_dataframe>
nyc =(40.7128, -74.0060)
 jfk =(40.6413, -73.7781)
 ewr =(40.6895, -74.1745)
 df_data['distance_pickup_to_nyc'] = distance(df_data['pickup_latitude'], df_data['pickup_longitude'], nyc[0], nyc[1])
 df_data['distance_pickup_to_jfk'] = distance(df_data['pickup_latitude'], df_data['pickup_longitude'], jfk[0], jfk[1])
 df_data['distance_pickup_to_ewr'] = distance(df_data['pickup_latitude'], df_data['pickup_longitude'], ewr[0], ewr[1])
 df_data['distance_dropoff_to_nyc'] = distance(df_data['dropoff_latitude'], df_data['dropoff_longitude'], nyc[0], nyc[1])
 df_data['distance_dropoff_to_jfk'] = distance(df_data['dropoff_latitude'], df_data['dropoff_longitude'], jfk[0], jfk[1])
 df_data['distance_dropoff_to_ewr'] = distance(df_data['dropoff_latitude'], df_data['dropoff_longitude'], ewr[0], ewr[1] )
tuned = pd.DataFrame({
'Algorithm':Algorithm,
'Score':Scores,
'Best Parameters':Params
})
 tuned<filter>
df_data['fare_per_mile'] = df_data['fare_amount'] / df_data['distance_euclidean']
df_data['fare_per_mile'] = df_data['fare_per_mile'].apply(lambda x: 0 if x == float('inf')else x)
 df_data['fare_per_mile'] = df_data['fare_per_mile'].fillna(0 )
tuned[tuned['Algorithm'] == 'LinearDiscriminantAnalysis']['Best Parameters'].values<choose_model_class>
df_data = df_data.dropna()
RF = RandomForestClassifier(bootstrap= False,
criterion='gini',
max_depth=None,
max_features= 3,
min_samples_leaf= 10,
min_samples_split= 10,
n_estimators= 100)
 LDA = LinearDiscriminantAnalysis(solver='svd', tol=0.0001)
 GB = GradientBoostingClassifier(learning_rate = 0.1, loss = 'deviance', max_depth= 4, max_features= 0.1,
min_samples_leaf= 100, n_estimators= 300 )<import_modules>
df_data = pd.get_dummies(df_data, columns=['datatype'], drop_first=True )
from sklearn.ensemble import StackingClassifier<choose_model_class>
x = df_data[df_data['datatype_training'] == 1].drop(['key', 'pickup_datetime', 'fare_amount', 'fare_per_mile', 'datatype_training'], axis=1)
 y = df_data[df_data['datatype_training'] == 1]['fare_amount']
def get_stacking() :
base = list()
base.append(( 'rf', RF))
base.append(( 'lda', LDA))
base.append(( 'GB', GB))
meta = LogisticRegression()
model = StackingClassifier(estimators=base,final_estimator=meta, cv = 5)
 return model 
<import_modules>
x_train, x_validate, y_train, y_validate = train_test_split(x, y, test_size=0.25, random_state=58 )
from sklearn.model_selection import RepeatedStratifiedKFold<choose_model_class>
model_forestreg = RandomForestRegressor(n_estimators=10, max_depth=20, min_samples_split=1000, random_state=58)
 model_forestreg.fit(x_train, y_train)
 model_forestreg_ypredict = model_forestreg.predict(x_validate)
 model_forestreg_rmse = mean_squared_error(y_validate, model_forestreg_ypredict)** 0.5
model_forestreg_cvscores = np.sqrt(np.abs(cross_val_score(model_forestreg, x, y, cv=5, scoring='neg_mean_squared_error')))
 print('random forest regression
root mean squared error: %0.4f, cross validation score: %0.4f(+/- %0.4f)' %(model_forestreg_rmse, model_forestreg_cvscores.mean() , 2 * model_forestreg_cvscores.std()))
def get_models() :
models = dict()
models['rf'] = RF
models['lda'] = LDA
models['GB'] = GB
models['stacking'] = get_stacking()
return models<compute_train_metric>
final_model = model_forestreg
x_test = df_data[df_data['datatype_training'] == 0].drop(['key', 'pickup_datetime', 'fare_amount', 'fare_per_mile', 'datatype_training'], axis=1)
 y_test = pd.DataFrame(np.expm1(final_model.predict(x_test)) , columns=['fare_amount'], index=df_data.loc[df_data['datatype_training'] == 0, 'key'] )
<compute_test_metric><EOS> 
out = pd.DataFrame({'key': y_test.index, 'fare_amount': y_test['fare_amount']})
 out.to_csv('submission.csv', index=False )