text_plain_output_1.png
from sklearn.ensemble import GradientBoostingRegressor

from sklearn.ensemble import RandomForestRegressor

from sklearn.linear_model import LinearRegression

from sklearn.metrics import mean_squared_error

from sklearn.metrics import mean_squared_log_error

from sklearn.tree import DecisionTreeRegressor

import numpy as np

import pandas as pd

import xgboost as xgb

df = pd.read_csv('../input/train.csv')

df_ = df.drop(['id', 'vendor_id', 'store_and_fwd_flag'], axis=1)

time = df_.set_index('trip_duration')
cols = ['passenger_count', 'Turno', 'Dia', 'Mês', 'Dia_Semana']

df_.drop(['dropoff_datetime', 'pickup_datetime', 'Hora', 'Data'], axis=1, inplace=True)

def distancia(pickup_lat, pickup_lon, dropoff_lat, dropoff_lon):
    R_terra = 6371
    inicio_lat, inicio_lon, fim_lat, fim_lon = map(np.radians, [pickup_lat, pickup_lon, dropoff_lat, dropoff_lon])
    dlat = fim_lat - inicio_lat
    dlon = fim_lon - inicio_lon
    d = np.sin(dlat / 2.0) ** 2 + np.cos(inicio_lat) * np.cos(fim_lat) * np.sin(dlon / 2.0) ** 2
    return 2 * R_terra * np.arcsin(np.sqrt(d))

df_['Duração'] = df_['trip_duration']
df_.drop(['trip_duration'], axis=1, inplace=True)

X = df_.values[:, :-1]
y = np.log(df_.values[:, -1] + 1)

models = []
models.append(('Linear', LinearRegression()))
models.append(('GBooting', GradientBoostingRegressor()))
models.append(('RFR', RandomForestRegressor(n_estimators=10)))
models.append(('DTR', DecisionTreeRegressor()))

def rmsle(y_pred, y_test):
    assert len(ytest) == len(ypred)
    return np.sqrt(np.mean((np.log1p(y_pred) - np.log1p(y_test)) ** 2))

rmse_calc = []
rmsle = []
for nome, model in models:
    model.fit(X_train, y_train)
    y_pred = model.predict(X_test)
    rmse_calc.append((nome, np.sqrt(mean_squared_error(y_test, y_pred))))
    rmsle.append((nome, np.sqrt(mean_squared_log_error(y_test, y_pred))))

dtrain = xgb.DMatrix(X_train, label=y_train)

dtest = xgb.DMatrix(X_test, label=y_test)

watchlist = [(dtrain, 'train'), (dtest, 'valid')]
xgb_pars = {'min_child_weight': 10, 'eta': 0.03, 'colsample_bytree': 0.3, 'max_depth': 10, 'subsample': 0.8, 'lambda': 0.5, 'nthread': -1, 'booster': 'gbtree', 'silent': 1, 'eval_metric': 'rmse', 'objective': 'reg:linear'}

model = xgb.train(xgb_pars, dtrain, 1000, watchlist, early_stopping_rounds=90, maximize=False, verbose_eval=100)

y_pred = model.predict(dtest)

rmse_xgb = np.sqrt(mean_squared_error(y_test, y_pred))

rmsle_xgb = np.sqrt(mean_squared_log_error(y_test, y_pred))

rmsle_xgb