<jupyter_start><jupyter_text>*** Importing libraries and setting up directoryLibraries used:    1. os: to handle directory structure    2. panda: to handle dataframes    3. munpy and scipy: for numeric and scintific calculation methods    4. matplotlib and seaborn: to plot and graphic visualization of data    5. sklearn: for various machine learning models such as KNN imputation, Parameter Search CV, Regression models    6. patsy: to define structure of model    7. xgboost: to improve accuracy through ensemble technique    8. joblib: to save model***<jupyter_code>import os
import pandas as pd
import numpy as np
import scipy as sp
from geopy.distance import geodesic, distance
import matplotlib as mpl
import seaborn as sns
from sklearn.impute import KNNImputer
from patsy import dmatrices
from statsmodels.stats.outliers_influence import variance_inflation_factor
from sklearn.model_selection import train_test_split
from sklearn import metrics as mtr
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import RandomizedSearchCV
from sklearn.linear_model import LinearRegression,Ridge,Lasso
from sklearn.ensemble import RandomForestRegressor
from sklearn.tree import DecisionTreeRegressor
import xgboost as xgb
import joblib

os.chdir('K:\\Data_Science\\Project_01\\Trials')<jupyter_output><empty_output><jupyter_text>***  Loading and getting an overview of train and test dataTrain and test csv files saved in different dataframes[dfTrain, dfTest]. ***<jupyter_code>dfTrain =  pd.read_csv('train_cab.csv')
dfTest = pd.read_csv('test.csv')
dfData = [dfTrain, dfTest]

for data in dfData:
    data.info()
dfTrain.describe(include = 'all')
dfTest.describe(include = 'all')<jupyter_output><empty_output><jupyter_text>*** Correcting data types of data frames* pickup_datetime saved as "datetime" type* fare amount saved as "numeric" type***<jupyter_code>dfTrain['fare_amount'] = pd.to_numeric(dfTrain['fare_amount'], errors = 'coerce')

for data in dfData:
    data['pickup_datetime'] = pd.to_datetime(data['pickup_datetime'], errors = 'coerce')
    data.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 16067 entries, 0 to 16066
Data columns (total 7 columns):
 #   Column             Non-Null Count  Dtype              
---  ------             --------------  -----              
 0   fare_amount        16042 non-null  float64            
 1   pickup_datetime    16066 non-null  datetime64[ns, UTC]
 2   pickup_longitude   16067 non-null  float64            
 3   pickup_latitude    16067 non-null  float64            
 4   dropoff_longitude  16067 non-null  float64            
 5   dropoff_latitude   16067 non-null  float64            
 6   passenger_count    16012 non-null  float64            
dtypes: datetime64[ns, UTC](1), float64(6)
memory usage: 878.8 KB
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 9914 entries, 0 to 9913
Data columns (total 6 columns):
 #   Column             Non-Null Count  Dtype              
---  ------             --------------  -----              
 0   pickup_datetime    9914 non-null   datetime64[ns, UTC]
[...]<jupyter_text>*** Removing nonsensical values from train data setremoved observations containing nonsensical values:1. unusual fare amount such as non-positive 2. unreal passenger count such as 0 or more than 63. and extreme geolocations dropped.***<jupyter_code>#removing data with NOK fare amount
dfTrain = dfTrain[~(dfTrain['fare_amount']<1)]

#removing data with NOK passenger count
dfTrain = dfTrain[~((dfTrain['passenger_count']<1) | (dfTrain['passenger_count']>6))]

#removing data with NOK pickup longitude
dfTrain = dfTrain[~((dfTrain['pickup_longitude']<-75.5) | (dfTrain['pickup_longitude']>-71.8))]

#removing data with NOK pickup lattitude
dfTrain = dfTrain[~((dfTrain['pickup_latitude']<39.5) | (dfTrain['pickup_latitude']>41.9))]

#removing data with NOK dropoff longitude
dfTrain = dfTrain[~((dfTrain['dropoff_longitude']<-75.5) | (dfTrain['dropoff_longitude']>-71.8))]

#removing data with NOK dropoff lattitude
dfTrain = dfTrain[~((dfTrain['dropoff_latitude']<39.5) | (dfTrain['dropoff_latitude']>41.9))]

dfTrain.describe()<jupyter_output><empty_output><jupyter_text>*** Visualizing distribution of the data in train dataset* violin plot used for distribution of individual variables * pairwise plot used for correlation of each pair of variables***<jupyter_code>sns.set(rc={'figure.figsize':(8.4,5.5)})
sns.violinplot(y = dfTrain['passenger_count'])
sns.set(rc={'figure.figsize':(8.4,5.5)})
sns.violinplot(y =dfTrain['fare_amount'])
sns.set(rc={'figure.figsize':(8.4,5.5)})
sns.violinplot(y = dfTrain['pickup_latitude'])
sns.set(rc={'figure.figsize':(8.4,5.5)})
sns.violinplot(y = dfTrain['pickup_longitude'])
sns.set(rc={'figure.figsize':(8.4,5.5)})
sns.violinplot(y = dfTrain['dropoff_latitude'])
sns.set(rc={'figure.figsize':(8.4,5.5)})
sns.violinplot(y =dfTrain['dropoff_longitude'])
num_var=['fare_amount','pickup_longitude','pickup_latitude','dropoff_longitude','dropoff_latitude', 'passenger_count']
sns.pairplot(data=dfTrain[num_var],kind='reg',dropna=True)<jupyter_output><empty_output><jupyter_text>*** Deriving new and usefull features for analysisnew features created to better explain model***features based on geographic coordinates<jupyter_code>def dist(row):
    pickup = (row['pickup_latitude'], row['pickup_longitude'])
    dropoff = (row['dropoff_latitude'], row['dropoff_longitude'])
    return geodesic(pickup, dropoff).km

def len_trip(row):
    if (row['geodesic'] >= 15):
        return 'long'
    elif (row['geodesic'] < 15):
        return 'short'

dfTrain['geodesic'] = dfTrain.apply(dist, axis =1)
dfTest['geodesic'] = dfTest.apply(dist, axis = 1)

dfTrain['trip'] = dfTrain.apply(len_trip, axis =1)
dfTest['trip'] = dfTest.apply(len_trip, axis =1)<jupyter_output><empty_output><jupyter_text>features based on pickup date_time<jupyter_code>def season(row):
    month = row['pickup_datetime'].month
    if (month >2) & (month <= 5):
        return 'spring'
    elif (month >5) & (month <=8 ):
        return 'summer'
    elif (month >8) & (month <= 11):
         return'fall'
    elif (month > 11)|(month <= 2) :
        return 'winter'

def shift(row):
    hour = row['pickup_datetime'].hour
    if (hour > 2) & (hour <=8):
        return 'dawn'
    elif (hour > 8) & (hour <= 14):
        return 'morning'
    elif (hour > 14) & (hour <= 20):
        return 'evening'
    elif (hour > 20) | (hour <= 2):
        return 'night'
    
def weekDay(row):
    day = row['pickup_datetime'].dayofweek
    if (day >= 5):
        return 'weekend'
    elif (day < 5):
        return 'weekday'
    


#feature derivation from datetime
dfTrain['year'] = dfTrain.apply(lambda row: row['pickup_datetime'].year, axis =1)
dfTest['year'] = dfTest.apply(lambda row: row['pickup_datetime'].year, axis =1)

dfTrain['month'] = dfTrain.apply(lambda row: row['pickup_datetime'].month, axis =1)
dfTest['month'] = dfTest.apply(lambda row: row['pickup_datetime'].month, axis =1)

dfTrain['dayofwk'] = dfTrain.apply(lambda row: row['pickup_datetime'].dayofweek, axis =1)
dfTest['dayofwk'] = dfTest.apply(lambda row: row['pickup_datetime'].dayofweek, axis =1)

dfTrain['hour'] = dfTrain.apply(lambda row: row['pickup_datetime'].hour, axis =1)
dfTest['hour'] = dfTest.apply(lambda row: row['pickup_datetime'].hour, axis =1)

#new variables for seasonal surcharges
dfTrain['season'] = dfTrain.apply(season, axis =1)
dfTest['season'] = dfTest.apply(season, axis =1)

dfTrain['weekday'] = dfTrain.apply(weekDay, axis =1)
dfTest['weekday'] = dfTest.apply(weekDay, axis =1)

dfTrain['shift'] = dfTrain.apply(shift, axis =1)
dfTest['shift'] = dfTest.apply(shift, axis =1)<jupyter_output><empty_output><jupyter_text>*** Cleaning data train datamissing values and outliers for each attribute checked***<jupyter_code>miss_val = pd.DataFrame(dfTrain.isnull().sum())
miss_val = miss_val.rename(columns = {'index': 'Variables', 0: 'Missing Count'})
miss_val['Missing Pcent'] = (miss_val['Missing Count']/len(dfTrain))*100
miss_val<jupyter_output><empty_output><jupyter_text>*** Treating missing valuesIn first step, some observation with missing values dropped with insignificant loss in data***<jupyter_code>dfTrain.loc[(dfTrain['geodesic']<0.05), 'geodesic'] = np.nan
dfTrain = dfTrain.dropna(subset = ['fare_amount', 'pickup_datetime', 'geodesic']).reset_index(drop = True)<jupyter_output><empty_output><jupyter_text>*** Replaceing outliers with NaN to impute values laterIn second step, outlier values also replaced with NaN and masked as missing values***<jupyter_code>def replaceOutlier(col):
    Q1 = np.percentile(dfTrain[col], 25)
    Q3 = np.percentile(dfTrain[col], 75)
    Max_Bound = Q3 + 1.5*(Q3-Q1)
    Min_Bound = Q1 - 1.5*(Q3-Q1)
    
    dfTrain.loc[((dfTrain[col]<Min_Bound)|(dfTrain[col]>Max_Bound)), col] = np.nan
    return


to_modify = ['fare_amount', 'geodesic']

for var in to_modify:
    replaceOutlier(var)

dfTrain.describe()<jupyter_output><empty_output><jupyter_text>*** Imputing missing valuesAfter replacing outliers with missing vallues, all missing values are imputed. Two methods checked for imputing missing values* With statistical mean: for imputing missing values first mean of data checked* With KNN Imputation: for KNN imputation a subset of data created***<jupyter_code>dfImputer = dfTrain[['fare_amount', 'geodesic', 'year', 'month', 'hour', 'dayofwk', 'passenger_count']].copy()
dfImputer.isnull().sum()<jupyter_output><empty_output><jupyter_text>of the subset a sample taken for validation.<jupyter_code>dfImputTrial = dfImputer.sample(frac = 0.02).copy().dropna().reset_index(drop = True)
dfImputValid = dfImputTrial.copy()
ranInd = np.random.randint(0, high = len(dfImputTrial), size = 5)
dfImputTrial.isnull().sum()<jupyter_output><empty_output><jupyter_text>random values set to NaN to validate KNN Imputation<jupyter_code>for i in ranInd:
    dfImputTrial['passenger_count'].loc[i] = np.nan
    dfImputTrial['geodesic'].loc[i] = np.nan
    dfImputTrial['fare_amount'].loc[i] = np.nan<jupyter_output><empty_output><jupyter_text>With statistical mean: for imputing missing values first mean of data checked<jupyter_code>print(dfImputer['passenger_count'].mean(), dfImputer['fare_amount'].mean(), dfImputer['geodesic'].mean())<jupyter_output>1.6530262557821356 8.94937127024727 2.4787213725522497<jupyter_text>imputed values crosschecked with true values<jupyter_code>naImputer = KNNImputer(n_neighbors = 5)
naImputer.fit(dfImputTrial)
naImputed = naImputer.transform(dfImputTrial)
print('IP', 'VP', 'IF', 'VF', 'IG', 'VG')
for i in ranInd:    
    print((naImputed[i, 6].round()),'|', dfImputValid['passenger_count'].loc[i],'||',
          (naImputed[i, 0].round(decimals = 2)),'|', dfImputValid['fare_amount'].loc[i].round(decimals = 1), '||',
          (naImputed[i, 1].round(decimals = 2)),'|', dfImputValid['geodesic'].loc[i].round(decimals = 1))<jupyter_output>IP VP IF VF IG VG
1.0 | 2.0 || 7.8 | 4.5 || 2.13 | 1.0
2.0 | 1.0 || 8.62 | 17.0 || 2.13 | 4.8
2.0 | 1.0 || 6.62 | 12.0 || 1.13 | 3.6
3.0 | 1.0 || 7.62 | 9.5 || 1.51 | 2.9
2.0 | 1.0 || 9.22 | 4.5 || 3.01 | 1.1<jupyter_text>std deviation checked before and after imputation<jupyter_code>print(np.std(naImputed[:,6].round()), np.std(naImputed[:,0]), np.std(naImputed[:,1]))
print(dfImputValid['passenger_count'].std(),dfImputValid['fare_amount'].std(),dfImputValid['geodesic'].std())<jupyter_output>1.3874280758538362 4.081993798048414 1.5065464202356897
1.3900615918031953 4.136580069157395 1.5191336009200622<jupyter_text>imputing values with KNN Imputation for original data<jupyter_code>theImputer = KNNImputer(n_neighbors = 5)
theImputer.fit(dfImputer)
theImputed = theImputer.transform(dfImputer)

dfTrain['passenger_count'] = theImputed[:,6].round()
dfTrain['fare_amount'] = theImputed[:,0]
dfTrain['geodesic'] = theImputed[:,1]
dfTrain.isnull().sum()<jupyter_output><empty_output><jupyter_text>*** Understanding relation between variablesdistribution and correlation after missing value and oulier correction checked***<jupyter_code>sns.set(rc={'figure.figsize':(8.4,5.5)})
sns.violinplot(y =dfTrain['fare_amount'])
sns.set(rc={'figure.figsize':(8.4,5.5)})
sns.violinplot(y =dfTrain['geodesic'])<jupyter_output><empty_output><jupyter_text>***If any variables are correleated is checked with help of heatmap of correlation coeficient<jupyter_code>sns.heatmap(dfTrain[['fare_amount', 'geodesic', 'hour', 'dayofwk','month', 'year']].corr(), square=True, linewidths=0.5,linecolor='w',annot=True)
sns.jointplot(x='fare_amount',y='geodesic',data=dfTrain, kind = 'reg').annotate(sp.stats.pearsonr)<jupyter_output>C:\Users\ASUS\anaconda3\lib\site-packages\seaborn\axisgrid.py:1840: UserWarning: JointGrid annotation is deprecated and will be removed in a future release.
  warnings.warn(UserWarning(msg))<jupyter_text>*** Transforming variables to suit modelcategorical varibles transformed with one-hot coding.old variables dropped from the data set.one dummy variable of each category dropped to avoid multi-colinearity problem.***<jupyter_code>categorical = ['passenger_count', 'year', 'season', 'weekday', 'shift', 'trip']
integral = ['passenger_count', 'year']

dfTrain[integral] = dfTrain[integral].apply(np.int64)
dfTest[integral] = dfTest[integral].apply(np.int64)

    
#dummy variables created
for var in categorical:
    dfDummy1 = pd.get_dummies(dfTrain[var], prefix = var)
    dfTrain = dfTrain.join(dfDummy1)
    
    dfDummy2 = pd.get_dummies(dfTest[var], prefix = var)
    dfTest = dfTest.join(dfDummy2)   

#old variables dropped    
dfTrain = dfTrain.drop(['pickup_datetime', 'pickup_longitude', 'pickup_latitude', 'dropoff_longitude', 'dropoff_latitude', 
                  'passenger_count', 'year', 'season', 'weekday', 'month', 'shift', 'trip', 'dayofwk', 'hour'], axis =1)
dfTest = dfTest.drop(['pickup_datetime', 'pickup_longitude', 'pickup_latitude', 'dropoff_longitude', 'dropoff_latitude', 
                  'passenger_count', 'year', 'season', 'weekday', 'month', 'shift', 'trip', 'dayofwk', 'hour'], axis =1)


#one dummy variable dropped
dfTrain = dfTrain.drop(['passenger_count_1', 'year_2009', 'season_fall', 'weekday_weekday', 'shift_dawn', 'trip_short'], axis =1)
dfTest = dfTest.drop(['passenger_count_1', 'year_2009', 'season_fall', 'weekday_weekday', 'shift_dawn', 'trip_short'], axis =1)<jupyter_output><empty_output><jupyter_text>*** Checking multicolinearity in datawith help of VIF, multicolinearity in data is checked.* if VIF = 1        -> No colinearity in any variables.* if 1  Moderate corinearity* if VIF > 5        -> High colinearityif multiple variables have more than 5 VIF, one with highest VIF will be dropped***<jupyter_code>features = " + ".join(dfTrain.columns)
features = features.replace("fare_amount + ", "")

target, attributes = dmatrices("fare_amount ~ " + features, dfTrain, return_type='dataframe')
vif = pd.DataFrame()
vif['VIF'] = [variance_inflation_factor(attributes.values, i) for i in range(attributes.shape[1])]
vif['INPUTS'] = attributes.columns
vif<jupyter_output><empty_output><jupyter_text>*** Building models and checking performance* train data split to validate performance.* sevral functions defined to visualize and calculate model statistics***<jupyter_code>X = dfTrain.drop(['fare_amount'], axis = 1).values
Y = dfTrain['fare_amount'].values

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.3, random_state=35)
def scores(y_true, y_pred):
    
    mape = np.mean(np.abs((y_true - y_pred)/y_true))*100

    r2 = mtr.r2_score(y_true, y_pred)

    rms = np.sqrt(mtr.mean_squared_error(y_true, y_pred))

    return [mape, r2, rms]

def model_scores(model):
    
    y_trPred = model.predict(X_train)
    trScore = scores(Y_train, y_trPred)
    
    y_tsPred = model.predict(X_test)
    tsScore = scores(Y_test, y_tsPred)
    
    data = {'train' : trScore, 'test' : tsScore}
    dfScore = pd.DataFrame(data, index = ['MAPE', 'R2', 'RMSE'])
    
    print(dfScore)
    return dfScore

def model_plot(model):
    
    coefModel = model.coef_

    mpl.pyplot.plot(range(len(dfTest.columns)), coefModel)
    mpl.pyplot.xticks(range(len(dfTest.columns)), dfTest.columns.values, rotation=75)
    
    return

def tree_plot(model):
    
    coefModel = model.feature_importances_

    mpl.pyplot.plot(range(len(dfTest.columns)), coefModel)
    mpl.pyplot.xticks(range(len(dfTest.columns)), dfTest.columns.values, rotation=75)
    
    return<jupyter_output><empty_output><jupyter_text>Linear Regression Model<jupyter_code>lin_reg_trial = LinearRegression()

param_grid = {'fit_intercept': [True, False], 'normalize': [True, False], 'copy_X': [True, False]}

LinRegCV = GridSearchCV(lin_reg_trial, param_grid, cv=5, scoring='r2')
LinRegCV.fit(X, Y)

print("Optimum Parameters: {}".format(LinRegCV.best_params_))
print("Optimum score     : {}".format(LinRegCV.best_score_))
lin_reg_model = LinearRegression(copy_X = True, fit_intercept = True, normalize = True)
lin_reg_model.fit(X_train,Y_train)

Yp_linreg = lin_reg_model.predict(X_test)

scoreLinReg = model_scores(lin_reg_model)

model_plot(lin_reg_model)<jupyter_output>train       test
MAPE  17.448193  17.876085
R2     0.727850   0.742516
RMSE   2.112474   2.112242<jupyter_text>Ridge Regression Model<jupyter_code>ridge_reg_trial = Ridge()

param_grid = {'alpha':np.logspace(-4, 0, 75), 'normalize':[True,False], 'max_iter':range(100,1000,100)}

RidgeRegCV = GridSearchCV(ridge_reg_trial, param_grid, cv=5, scoring='r2')
RidgeRegCV.fit(X, Y)

print("Optimum Parameters: {}".format(RidgeRegCV.best_params_))
print("Optimum score     : {}".format(RidgeRegCV.best_score_))
ridge_reg_model = Ridge(alpha = 1.0, max_iter = 100, normalize = False)
ridge_reg_model.fit(X_train,Y_train)

Yp_ridgeReg = ridge_reg_model.predict(X_test)

scoreRidgeReg = model_scores(ridge_reg_model)

model_plot(ridge_reg_model)<jupyter_output>train       test
MAPE  17.447869  17.875432
R2     0.727850   0.742517
RMSE   2.112475   2.112241<jupyter_text>Lasso Regression Model<jupyter_code>lasso_reg_trial = Lasso()

param_grid = {'alpha':np.logspace(-4, 0, 75), 'normalize':[True,False], 'max_iter':range(100,1000,100)}

LassoRegCV = GridSearchCV(lasso_reg_trial, param_grid, cv=5, scoring='r2')
LassoRegCV.fit(X, Y)

print("Optimum Parameters: {}".format(LassoRegCV.best_params_))
print("Optimum score     : {}".format(LassoRegCV.best_score_))
lasso_reg_model = Lasso(alpha = 0.0004452958509942655, max_iter = 100, normalize = False)
lasso_reg_model.fit(X_train,Y_train)

Yp_lassoReg = lasso_reg_model.predict(X_test)

scoreLassoReg = model_scores(lasso_reg_model)

model_plot(lasso_reg_model)<jupyter_output>train       test
MAPE  17.445332  17.874590
R2     0.727846   0.742497
RMSE   2.112492   2.112322<jupyter_text>Decision Tree Regression Model<jupyter_code>detree_reg_trial = DecisionTreeRegressor()

param_grid = {'criterion': ['mse', 'mae'], 'max_depth': range(2,10,2), 'min_samples_split': range(2,10,2)}

DeTreeRegCV = RandomizedSearchCV(detree_reg_trial, param_grid, cv=5, scoring='r2')
DeTreeRegCV.fit(X, Y)

print("Optimum Parameters: {}".format(DeTreeRegCV.best_params_))
print("Optimum score     : {}".format(DeTreeRegCV.best_score_))
detree_reg_model = DecisionTreeRegressor(min_samples_split = 4, max_depth = 6, criterion = 'mse')
detree_reg_model.fit(X_train,Y_train)

Yp_detreeReg = detree_reg_model.predict(X_test)

scoreDetreeReg = model_scores(detree_reg_model)

tree_plot(detree_reg_model)<jupyter_output>train       test
MAPE  17.536055  18.474367
R2     0.729539   0.727468
RMSE   2.105909   2.173092<jupyter_text>Random Forest Regression Model<jupyter_code>randfr_reg_trial = RandomForestRegressor()

param_grid = {'n_estimators': range(100,500,100), 'max_depth': range(5,20,3), 'min_samples_leaf':range(2,5,1), 'max_features':['auto','sqrt','log2'], 'bootstrap': [True, False], 'min_samples_split': range(2,5,1)}

RandFrRegCV = RandomizedSearchCV(randfr_reg_trial, param_grid, cv=5)
RandFrRegCV.fit(X,Y)

print("Optimum Parameters: {}".format(RandFrRegCV.best_params_))
print("Optimum score     : {}".format(RandFrRegCV.best_score_))
rforest_reg_model = RandomForestRegressor(n_estimators = 200, min_samples_split = 3, min_samples_leaf = 4, max_features = 'auto', max_depth = 14, bootstrap = True)
rforest_reg_model.fit(X_train,Y_train)

Yp_rforestReg = rforest_reg_model.predict(X_test)

scoreRForestReg = model_scores(rforest_reg_model)

tree_plot(rforest_reg_model)<jupyter_output>train       test
MAPE  13.759257  17.931695
R2     0.830670   0.742405
RMSE   1.666303   2.112698<jupyter_text>Improving Accuracy through xgboost<jupyter_code>dData = xgb.DMatrix(data=X,label=Y)
dTrain = xgb.DMatrix(X_train, label=Y_train)
dTest = xgb.DMatrix(X_test)

params = {"objective":"reg:squarederror",'colsample_bytree': 0.5,'learning_rate': 0.1, 'max_depth': 5, 'alpha': 10}
cv_results = xgb.cv(dtrain=dData, params=params, nfold=5, num_boost_round=75, early_stopping_rounds=10, metrics="rmse", as_pandas=True, seed=123)

print((cv_results["test-rmse-mean"]).tail(1))
xgb_reg_trial = xgb.XGBRegressor()

param_grid = {'n_estimators': range(100,500,100),  'reg_alpha':np.logspace(-3, 0, 30), 'colsample_bytree': np.arange(0.1,1,0.2), 'learning_rate': np.arange(0.1, 1, 0.1)}
XGBRegCV = RandomizedSearchCV(xgb_reg_trial, param_grid, cv=5)
XGBRegCV.fit(X,Y)

print("Optimum Parameters: {}".format(XGBRegCV.best_params_))
print("Optimum score     : {}".format(XGBRegCV.best_score_))
xgb_reg_model = xgb.XGBRegressor(reg_alpha = 0.11721022975334805, n_estimators = 100, learning_rate = 0.1, max_depth = 5, colsample_bytree = 0.90000000000000001)
xgb_reg_model.fit(X_train,Y_train)

Yp_xgbReg = xgb_reg_model.predict(X_test)

scoreRForestReg = model_scores(xgb_reg_model)

tree_plot(xgb_reg_model)<jupyter_output>train       test
MAPE  15.873809  17.403499
R2     0.775923   0.752668
RMSE   1.916841   2.070184<jupyter_text>*** Training entire train data set and predicting values for test dataset***<jupyter_code>xgb_reg_final = xgb.XGBRegressor(reg_alpha = 0.11721022975334805, n_estimators = 100, learning_rate = 0.1, max_depth = 5, colsample_bytree = 0.90000000000000001)
xgb_reg_final.fit(X, Y)

Y_Check = xgb_reg_final.predict(X)

testScores = scores(Y, Y_Check)
pd.DataFrame(testScores, index = ['MAPE', 'R2', 'RMSE'])<jupyter_output>C:\Users\ASUS\anaconda3\lib\site-packages\xgboost\data.py:112: UserWarning: Use subset (sliced data) of np.ndarray is not recommended because it will generate extra copies and increase memory consumption
  warnings.warn(<jupyter_text>***Predicing fare amount for test dataset and saving output and final model.***<jupyter_code>Y_Out = xgb_reg_final.predict(dfTest.values)

pd.DataFrame({'fare_amount' : Y_Out}).to_csv("cabfare_xgb_output_py.csv")

joblib.dump(xgb_reg_final, 'cabfare_xgbmodel_py.pkl')<jupyter_output>C:\Users\ASUS\anaconda3\lib\site-packages\xgboost\data.py:112: UserWarning: Use subset (sliced data) of np.ndarray is not recommended because it will generate extra copies and increase memory consumption
  warnings.warn(
{'fraction_non_alphanumeric': 0.14014921488678755, 'fraction_numerical': 0.04480784245684046, 'mean_word_length': 5.653679653679654, 'pattern_counts': {'":': 1, '<': 128, '<?xml version=': 0, '>': 129, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '30', 'dir': 'jupyter-structured-clean-dedup', 'id': '478168', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 9125, 'n_tokens_neox': 8281, 'n_words': 1687, 'path': '02_Code/CabFare_Prediction_Project_Py.ipynb', 'repo_name': 'sh99-git/NY-Taxi-fare-prediction', 'size': '23054'}