Created
July 27, 2018 19:07
-
-
Save llSourcell/b713da1e06e844be6414659ee180f6c5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Code to read csv file into colaboratory: | |
| !pip install -U -q PyDrive | |
| from pydrive.auth import GoogleAuth | |
| from pydrive.drive import GoogleDrive | |
| from google.colab import auth | |
| from oauth2client.client import GoogleCredentials | |
| import pandas as pd | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| plt.rcParams['figure.figsize'] = [16, 10] | |
| import seaborn as sns | |
| from sklearn.decomposition import PCA | |
| from sklearn.cluster import MiniBatchKMeans | |
| import datetime as dt | |
| import xgboost as xgb | |
| from sklearn.model_selection import train_test_split | |
| # 노트북 안에 그래프가 표시되도록 | |
| %matplotlib inline | |
| # 한글폰트 사용 시 그래프에서 마이너스 폰트 깨지는 문제에 대한 대처 | |
| plt.rcParams['axes.unicode_minus'] = False | |
| # 1. Authenticate and create the PyDrive client. | |
| auth.authenticate_user() | |
| gauth = GoogleAuth() | |
| gauth.credentials = GoogleCredentials.get_application_default() | |
| drive = GoogleDrive(gauth) | |
| ###THEN | |
| #how long was the trip? | |
| train['log_trip_duration'] = np.log(train['trip_duration'].values + 1) | |
| plt.hist(train['log_trip_duration'].values, bins=100) | |
| plt.xlabel('log(trip_duration)') | |
| plt.ylabel('number of train records') | |
| plt.show() | |
| #how much overlap? | |
| N = 100000 # number of sample rows in plots | |
| city_long_border = (-74.03, -73.75) | |
| city_lat_border = (40.63, 40.85) | |
| fig, ax = plt.subplots(ncols=2, sharex=True, sharey=True) | |
| ax[0].scatter(train['pickup_longitude'].values[:N], | |
| train['pickup_latitude'].values[:N], | |
| color='blue', s=1, label='train', alpha=0.1) | |
| ax[1].scatter(test['pickup_longitude'].values[:N], | |
| test['pickup_latitude'].values[:N], | |
| color='green', s=1, label='test', alpha=0.1) | |
| fig.suptitle('Train and test area complete overlap.') | |
| ax[0].legend(loc=0) | |
| ax[0].set_ylabel('latitude') | |
| ax[0].set_xlabel('longitude') | |
| ax[1].set_xlabel('longitude') | |
| ax[1].legend(loc=0) | |
| plt.ylim(city_lat_border) | |
| plt.xlim(city_long_border) | |
| plt.show() | |
| #train model | |
| feature_names = list(train.columns) | |
| print(np.setdiff1d(train.columns, test.columns)) | |
| do_not_use_for_training = ['id', 'log_trip_duration', | |
| 'pickup_datetime', 'dropoff_datetime', | |
| 'trip_duration', 'check_trip_duration', | |
| 'pickup_date', 'avg_speed_h', 'avg_speed_m', | |
| 'pickup_lat_bin', 'pickup_long_bin', | |
| 'center_lat_bin', 'center_long_bin', | |
| 'pickup_dt_bin', 'pickup_datetime_group'] | |
| feature_names = [f for f in train.columns if f not in do_not_use_for_training] | |
| y = np.log(train['trip_duration'].values + 1) | |
| Xtr, Xv, ytr, yv = train_test_split(train[feature_names].values, y, test_size=0.2, random_state=1987) | |
| dtrain = xgb.DMatrix(Xtr, label=ytr) | |
| dvalid = xgb.DMatrix(Xv, label=yv) | |
| dtest = xgb.DMatrix(test[feature_names].values) | |
| watchlist = [(dtrain, 'train'), (dvalid, 'valid')] | |
| xgb_pars = {'min_child_weight': 50, | |
| 'eta': 0.3, | |
| 'colsample_bytree': 0.3, | |
| 'max_depth': 10, | |
| 'subsample': 0.8, | |
| 'lambda': 1., | |
| 'nthread': 4, | |
| 'booster' : 'gbtree', | |
| 'silent': 1, | |
| 'eval_metric': 'rmse', | |
| 'objective': 'reg:linear'} | |
| model = xgb.train(xgb_pars, dtrain, 60, watchlist, early_stopping_rounds=50,\ | |
| maximize=False, verbose_eval=10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment