Kaggle competition: New York City Taxi Fare Prediction
cat_pred =cb_model.predict(final_X_test )<save_to_csv>
plt.style.use('dark_background')
 sns.set_style("darkgrid")
output3 = pd.DataFrame({"id":test_data.id, "target":cat_pred})
 output3.to_csv('cat.csv', index=False )<save_to_csv>
%%time
train_path = '.. /input/train.csv'
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())
 train_df = pd.read_csv(train_path, usecols=cols, dtype=traintypes, nrows=2_000_000 )
results =(y_pred_lgb + y_xgb_pred + cat_pred)/ 3
output = pd.DataFrame({"id":test_data.id, "target":results})
 output.to_csv('submission.csv', index=False )<load_from_csv>
%%time
train_df.to_feather('nyc_taxi_data_raw.feather' )
train = pd.read_csv(".. /input/tabular-playground-series-jan-2021/train.csv")
 test = pd.read_csv(".. /input/tabular-playground-series-jan-2021/test.csv")
 sub = pd.read_csv(".. /input/tabular-playground-series-jan-2021/sample_submission.csv")
 train<drop_column>
df_train = pd.read_feather('nyc_taxi_data_raw.feather')
train = train.drop(["id"], axis=1)
 features = [c for c in train.columns if "cont" in c]
test = test.drop("id", axis=1)
 test<load_pretrained>
len(df_train[df_train.fare_amount > 0] )
fe = dict(
 rankgauss = True,
stats = True,
gaussmix = False,
pca = True,
tsne = True,
umap = True,
drop_original = False,
)<concatenate>
df_train = df_train[df_train.fare_amount>=0]
all_data = pd.concat([train, test], axis=0, ignore_index=True)
 targets = all_data.target[:300000]
all_data = all_data.drop("target", axis=1)
 COLS = [c for c in all_data.columns if "cont" in c]
all_data<normalization>
df_train.isnull().sum()
if fe["stats"]:
for stats in tqdm.tqdm(["sum", "var", "mean", "median", "std", "kurt", "skew"]):
all_data["cont_" + stats] = getattr(all_data[COLS], stats )(axis = 1)
 all_data<normalization>
df_train = df_train.dropna(how = 'any', axis = 'rows')
sys.path.append(".. /input/rank-gauss")
 if fe["rankgauss"]:
scaler = GaussRankScaler()
rankgauss_feat = scaler.fit_transform(all_data[COLS])
 rankgauss_df = pd.DataFrame(rankgauss_feat, columns=[f"rankgauss_{i}" for i in range(rankgauss_feat.shape[1])])
 all_data = pd.concat([all_data, rankgauss_df], axis=1)
 all_data<predict_on_test>
df_test = pd.read_csv('.. /input/test.csv')
 df_test.head(5 )
if fe["gaussmix"]:
def get_gmm_class_feature(feat, n):
gmm = GaussianMixture(n_components=n, random_state=42)
 gmm.fit(all_data[feat].values.reshape(-1, 1))
all_data[f'{feat}_class'] = gmm.predict(all_data[feat].values.reshape(-1, 1))
get_gmm_class_feature('cont1', 4)
 get_gmm_class_feature('cont2', 10)
 get_gmm_class_feature('cont3', 6)
 get_gmm_class_feature('cont4', 4)
 get_gmm_class_feature('cont5', 3)
 get_gmm_class_feature('cont6', 2)
 get_gmm_class_feature('cont7', 3)
 get_gmm_class_feature('cont8', 4)
 get_gmm_class_feature('cont9', 4)
 get_gmm_class_feature('cont10', 8)
 get_gmm_class_feature('cont11', 5)
 get_gmm_class_feature('cont12', 4)
 get_gmm_class_feature('cont13', 6)
 get_gmm_class_feature('cont14', 6)
 CLASS_COLS = [c for c in all_data.columns if "_class" in c]
CLASS_COLS_IDX = []
for c in CLASS_COLS:
CLASS_COLS_IDX.append(all_data.columns.get_loc(c))
assert len(CLASS_COLS)> 0
all_data<categorify>
df_train['pickup_datetime'] = pd.to_datetime(df_train['pickup_datetime'],format="%Y-%m-%d %H:%M:%S UTC" )
if fe["pca"]:
pca = PCA(n_components = 0.9, random_state = 42 ).fit(all_data[COLS])
 pca_feat = pca.transform(all_data[COLS])
 pca_df = pd.DataFrame(pca_feat, columns = [f"pca_cont{i}" for i in range(pca.n_components_)])
 all_data = pd.concat([all_data, pca_df], axis=1)
 PCA_COLS = [c for c in all_data.columns if "pca" in c]
assert len(PCA_COLS)> 0
all_data<create_dataframe>
df_test['pickup_datetime'] = pd.to_datetime(df_test['pickup_datetime'],format="%Y-%m-%d %H:%M:%S UTC" )
if fe["tsne"]:
tsne_components = 2
perplexity = [5, 10, 15, 20, 25]
for per in perplexity:
tsne = TSNE(n_components = tsne_components, perplexity = per, n_neighbors = 3.5 * per)
 tsne_feat = tsne.fit_transform(all_data[COLS])
 tsne_df = pd.DataFrame(tsne_feat, columns=[f"tsne_{per}_{i}" for i in range(tsne_components)])
 all_data = pd.concat([all_data, tsne_df], axis = 1)
 TSNE_COLS = [c for c in all_data.columns if "tsne" in c]
all_data<categorify>
def add_new_date_time_features(dataset):
dataset['hour'] = dataset.pickup_datetime.dt.hour
dataset['day'] = dataset.pickup_datetime.dt.day
dataset['month'] = dataset.pickup_datetime.dt.month
dataset['year'] = dataset.pickup_datetime.dt.year
dataset['day_of_week'] = dataset.pickup_datetime.dt.dayofweek
return dataset
if fe["umap"]:
umap_components = 10
umap = UMAP(n_components = umap_components)
 umap_feat = umap.fit_transform(all_data[COLS])
 umap_df = pd.DataFrame(umap_feat, columns=[f"umap{i}" for i in range(umap_components)])
 all_data = pd.concat([all_data, umap_df], axis=1)
 UMAP_COLS = [c for c in all_data.columns if "umap" in c]
assert len(UMAP_COLS)> 0
all_data<drop_column>
df_train = add_new_date_time_features(df_train)
 df_test = add_new_date_time_features(df_test )
if fe["drop_original"]:
all_data = all_data.drop(COLS, axis=1 )<split>
def calculate_abs_different(df):
df['abs_diff_longitude'] =(df.dropoff_longitude - df.pickup_longitude ).abs()
df['abs_diff_latitude'] =(df.dropoff_latitude - df.pickup_latitude ).abs()
calculate_abs_different(df_train)
 calculate_abs_different(df_test )
dtypes = {c: "int8" for c in train.columns if "_class" in c}
train = train.astype(dtypes)
 X_train, X_val, y_train, y_val = train_test_split(train, targets, test_size=0.1, random_state=42)
 train_pool = Pool(X_train, y_train, cat_features = [c for c in train.columns if "_class" in c])
 val_pool = Pool(X_val, y_val, cat_features = [c for c in train.columns if "_class" in c] )<train_model>
def convert_different_miles(df):
df['abs_diff_longitude'] = df.abs_diff_longitude*50
df['abs_diff_latitude'] = df.abs_diff_latitude*69
convert_different_miles(df_train)
 convert_different_miles(df_test )
ITERATIONS = 5000
MAX_EVALS = 150
def objective_func(params, train_pool, val_pool):
model = CatBoostRegressor(iterations = ITERATIONS,task_type="GPU", devices='0:1', grow_policy="Lossguide",
loss_function = "RMSE", custom_metric = "RMSE", eval_metric="RMSE", verbose = 1000, **params)
 model.fit(train_pool, eval_set = val_pool, early_stopping_rounds = 200, plot=False)
 loss = model.get_best_score()
return {"loss": loss["validation"]["RMSE"], "status": STATUS_OK}
space = {
"learning_rate": hp.loguniform("learning_rate", np.log(0.01), np.log(1)) ,
"max_depth": hp.quniform("max_depth", 6, 11, 1),
"l2_leaf_reg": hp.quniform("l2_leaf_reg", 1, 5, 0.9),
"bagging_temperature": hp.quniform("bagging_temperature", 0, 4, 0.9),
"min_data_in_leaf": hp.quniform("min_data_in_leaf", 1, 42, 1),
"max_leaves": hp.quniform("max_leaves", 2**4-1, 2**6-1, 1),
}
fn = partial(objective_func, train_pool=train_pool, val_pool=val_pool)
 best_params = fmin(fn = fn, space=space, algo=tpe.suggest, max_evals = MAX_EVALS)
 model = CatBoostRegressor(iterations = ITERATIONS,task_type="GPU", devices='0:1', grow_policy="Lossguide",
loss_function = "RMSE", custom_metric = "RMSE", eval_metric="RMSE", verbose = 200, **best_params)
 model.fit(train_pool, eval_set = val_pool, early_stopping_rounds = 200, plot=True )<predict_on_test>
meas_ang = 0.506
def add_distance(df):
df['Euclidean'] =(df.abs_diff_latitude**2 + df.abs_diff_longitude**2)**0.5
df['delta_manh_long'] =(df.Euclidean*np.sin(np.arctan(df.abs_diff_longitude / df.abs_diff_latitude)-meas_ang)).abs()
df['delta_manh_lat'] =(df.Euclidean*np.cos(np.arctan(df.abs_diff_longitude / df.abs_diff_latitude)-meas_ang)).abs()
df['distance'] = df.delta_manh_long + df.delta_manh_lat
df.drop(['abs_diff_longitude', 'abs_diff_latitude','Euclidean', 'delta_manh_long', 'delta_manh_lat'], axis=1, inplace=True)
 add_distance(df_train)
 add_distance(df_test )
sub["target"] = model.predict(test)
 sub<save_to_csv>
def calculate_direction(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 = pickup_lon - dropoff_lon
a = np.arctan2(np.sin(dlon * np.cos(dropoff_lat)) ,np.cos(pickup_lat)* np.sin(dropoff_lat)- np.sin(pickup_lat)* np.cos(dropoff_lat)* np.cos(dlon))
return a
sub.to_csv("submission.csv", index=False )<import_modules>
train_df['direction'] = calculate_direction(train_df['pickup_latitude'].values, train_df['pickup_longitude'].values,
train_df['dropoff_latitude'].values , train_df['dropoff_longitude'].values)
 df_test['direction'] = calculate_direction(df_test['pickup_latitude'].values, df_test['pickup_longitude'].values,
df_test['dropoff_latitude'].values , df_test['dropoff_longitude'].values )
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns<load_from_csv>
train_df['pickup_latitude'].apply(lambda x: np.radians(x))
train_df['pickup_longitude'].apply(lambda x: np.radians(x))
train_df['dropoff_latitude'].apply(lambda x: np.radians(x))
train_df['dropoff_longitude'].apply(lambda x: np.radians(x))
df_test['pickup_latitude'].apply(lambda x: np.radians(x))
df_test['pickup_longitude'].apply(lambda x: np.radians(x))
df_test['dropoff_latitude'].apply(lambda x: np.radians(x))
df_test['dropoff_longitude'].apply(lambda x: np.radians(x))
train_df = pd.read_csv('.. /input/tabular-playground-series-jan-2021/train.csv')
 test_df = pd.read_csv('.. /input/tabular-playground-series-jan-2021/test.csv')
 sample_sub = pd.read_csv('.. /input/tabular-playground-series-jan-2021/sample_submission.csv' )<import_modules>
df_train.drop(columns=['pickup_datetime'], inplace=True)
 y = df_train['fare_amount']
df_train = df_train.drop(columns=['fare_amount'] )
from sklearn.model_selection import train_test_split<import_modules>
x_train,x_test,y_train,y_test = train_test_split(df_train,y,random_state=123,test_size=0.1 )
from sklearn.model_selection import train_test_split<prepare_x_and_y>
params = {
'boosting_type':'gbdt',
'objective': 'regression',
'nthread': 4,
'num_leaves': 31,
'learning_rate': 0.1,
'max_depth': -1,
'subsample': 0.8,
'bagging_fraction' : 1,
'max_bin' : 10000 ,
'bagging_freq': 10,
'metric': 'rmse',
'zero_as_missing': True,
'num_rounds':50000
}
X = train_df.drop('target', axis=1)
 y = train_df['target']<split>
train_set = lgbm.Dataset(x_train, y_train, silent=False,categorical_feature=['year','month','day','day_of_week'])
 valid_set = lgbm.Dataset(x_test, y_test, silent=False,categorical_feature=['year','month','day','day_of_week'])
 model = lgbm.train(params, train_set = train_set, num_boost_round=10000,early_stopping_rounds=1000,verbose_eval=500, valid_sets=valid_set )
X_train, X_val, y_train, y_val = train_test_split(X, y,test_size=0.2, random_state=3 )<choose_model_class>
test_key = df_test['key']
df_test.drop(columns=["pickup_datetime",'key'], axis=1, inplace=True )
lgb_model = LGBMRegressor(n_estimators = 493,metric='rmse',
reg_alpha = 2, reg_lambda=10, colsample_bytree=0.8 )<train_model>
prediction = model.predict(df_test, num_iteration = model.best_iteration)
<predict_on_test><EOS> 
submission = pd.DataFrame({
"key": test_key,
"fare_amount": prediction
})
 submission.to_csv('taxi_fare_submission.csv',index=False )