Copyright (C) 2017 J. Patrick Hall, jphall@gwu.edu
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
In [1]:
import h2o
from h2o.estimators.glm import H2OGeneralizedLinearEstimator
import operator
In [2]:
h2o.init()
In [3]:
# location of clean file
path = '../data/loan_clean.csv'
In [4]:
# define input variable measurement levels
# strings automatically parsed as enums (nominal)
# numbers automatically parsed as numeric
col_types = {'bad_loan': 'enum'}
In [5]:
frame = h2o.import_file(path=path, col_types=col_types) # multi-threaded import
In [6]:
frame.describe()
In [7]:
# split into training, validation and test
train, test = frame.split_frame([0.7])
In [8]:
# assign target and inputs for linear regression
y = 'STD_IMP_REP_loan_amnt'
X = [name for name in frame.columns if name not in ['id', '_WARN_', y]]
In [9]:
print(y)
print(X)
In [10]:
# elastic net regularized regression
# - Gaussian family, i.e. squared loss, for linear regression
# - L1 for variable selection
# - L2 for handling multicollinearity
# - IRLS for handling outliers
# - with lamba parameter tuning for variable selection
# initialize
loan_glm = H2OGeneralizedLinearEstimator(family='gaussian',
model_id='loan_glm1',
solver='IRLSM',
standardize=True,
lambda_search=True)
# train
loan_glm.train(X, y, training_frame=train)
# print trained model info
loan_glm.model_performance()
# view detailed results at http://host:ip/flow/index.html
Out[10]:
In [11]:
# range of target
test[y].max() - test[y].min()
Out[11]:
In [12]:
# measure train and test MSE
print(loan_glm.rmse(train=True))
print(loan_glm.model_performance(test_data=test).rmse())
In [13]:
# print non-zero model parameters
for name, val in sorted(loan_glm.coef().items(), key=operator.itemgetter(1)):
if val != 0.0:
print(name, ': ', val)
In [14]:
# plot top frame values
yhat_frame = test.cbind(loan_glm.predict(test))
print(yhat_frame[0:10, [y, 'predict']])
# plot sorted predictions
%matplotlib inline
yhat_frame_df = yhat_frame[[y, 'predict']].as_data_frame()
yhat_frame_df.sort_values(by=y, inplace=True)
yhat_frame_df.reset_index(inplace=True, drop=True)
ax = yhat_frame_df.plot(title='Ranked Predictions Plot', y='predict')
_ = yhat_frame_df.plot(y=y, ax=ax)
In [15]:
h2o.cluster().shutdown(prompt=True)