In [1]:
import h2o
import pandas
import pprint
import operator
import matplotlib
from tabulate import tabulate

In [2]:
# Connect to a cluster
h2o.init()


Warning: Version mismatch. H2O is version (unknown), but the python package is version UNKNOWN.
H2O cluster uptime: 9 minutes 43 seconds 331 milliseconds
H2O cluster version: (unknown)
H2O cluster name: spIdea
H2O cluster total nodes: 1
H2O cluster total memory: 12.44 GB
H2O cluster total cores: 8
H2O cluster allowed cores: 8
H2O cluster healthy: True
H2O Connection ip: 127.0.0.1
H2O Connection port: 54321

In [3]:
# set this to True if interactive (matplotlib) plots are desired
interactive = False
if not interactive: matplotlib.use('Agg', warn=False)
import matplotlib.pyplot as plt

In [4]:
from h2o.h2o import _locate # private function. used to find files within h2o git project directory.
# air_path = [_locate("bigdata/laptop/airlines_all.05p.csv")]
# air_path = [_locate("bigdata/laptop/flights-nyc/flights14.csv.zip")]
air_path = [_locate("smalldata/airlines/allyears2k_headers.zip")]

# ----------

# 1- Load data - 1 row per flight.  Has columns showing the origin,
# destination, departure and arrival time, carrier information, and
# whether the flight was delayed.
print "Import and Parse airlines data"
data = h2o.import_file(path=air_path)
data.describe()


Import and Parse airlines data

Parse Progress: [##################################################] 100%
Rows:43,978 Cols:31

Chunk compression summary:
chunk_type chunk_name count count_percentage size size_percentage
C0L Constant Integers 10 5.376344 800 B 0.1
C0D Constant Reals 23 12.365591 1.8 KB 0.1
CBS Bits 2 1.0752689 2.0 KB 0.1
CX0 Sparse Bits 10 5.376344 1.9 KB 0.1
C1 1-Byte Integers 40 21.505377 287.8 KB 18.564957
C1N 1-Byte Integers (w/o NAs) 19 10.215054 133.1 KB 8.58617
C1S 1-Byte Fractions 6 3.2258065 43.4 KB 2.8024976
C2 2-Byte Integers 76 40.860214 1.1 MB 69.628105
Frame distribution summary:
size number_of_rows number_of_chunks_per_column number_of_chunks
172.16.2.52:54321 1.5 MB 43978.0 6.0 186.0
mean 1.5 MB 43978.0 6.0 186.0
min 1.5 MB 43978.0 6.0 186.0
max 1.5 MB 43978.0 6.0 186.0
stddev 0 B 0.0 0.0 0.0
total 1.5 MB 43978.0 6.0 186.0

Year Month DayofMonth DayOfWeek DepTime CRSDepTime ArrTime CRSArrTime UniqueCarrier FlightNum TailNum ActualElapsedTime CRSElapsedTime AirTime ArrDelay DepDelay Origin Dest Distance TaxiIn TaxiOut Cancelled CancellationCode Diverted CarrierDelay WeatherDelay NASDelay SecurityDelay LateAircraftDelay IsArrDelayed IsDepDelayed
type int int int int int int int int enum int enum int int int int int enum enum int int int int enum int int int int int int enum enum
mins 1987.0 1.0 1.0 1.0 1.0 0.0 1.0 0.0 0.0 1.0 0.0 16.0 17.0 14.0 -63.0 -16.0 0.0 0.0 11.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
mean 1997.5 1.4090909090914.60107326393.820614852881345.846661381313.222861431504.634130381485.28916731NaN 818.842989677NaN 124.814529135 125.021562607 114.3161110919.3171119369810.0073906556NaN NaN 730.1821905655.3813680595314.16863418470.0246941652645NaN 0.002478511983264.04780029106 0.2893764692714.855031904180.01701556028217.62006045002 0.5557551503020.525057983537
maxs 2008.0 10.0 31.0 7.0 2400.0 2359.0 2400.0 2359.0 9.0 3949.0 3500.0 475.0 437.0 402.0 475.0 473.0 131.0 133.0 3365.0 128.0 254.0 1.0 3.0 1.0 369.0 201.0 323.0 14.0 373.0 1.0 1.0
sigma 6.344360901711.874711371349.175790425861.90501311913465.340899124476.251139993484.347487904492.750434123NaN 777.404369164NaN 73.9744416606 73.40159463 69.636329515129.840221962426.4388090429NaN NaN 578.43800823 4.201979939869.9050857472 0.155193141358 NaN 0.0497234872189 16.2057299045 4.41677989873 18.61977622150.403940182102 23.4875658741 0.4968872883430.499377380318
zeros 0 0 0 0 0 569 0 569 724 0 2 0 0 -8878 1514 6393 59 172 0 -8255 -8321 42892 81 43869 -23296 -21800 -23252 -21726 -23500 19537 20887
missing0 0 0 0 1086 0 1195 0 0 0 32 1195 13 16649 1195 1086 0 0 35 16026 16024 0 9774 0 35045 35045 35045 35045 35045 0 0
0 1987.0 10.0 14.0 3.0 741.0 730.0 912.0 849.0 PS 1451.0 NA 91.0 79.0 nan 23.0 11.0 SAN SFO 447.0 nan nan 0.0 NA 0.0 nan nan nan nan nan YES YES
1 1987.0 10.0 15.0 4.0 729.0 730.0 903.0 849.0 PS 1451.0 NA 94.0 79.0 nan 14.0 -1.0 SAN SFO 447.0 nan nan 0.0 NA 0.0 nan nan nan nan nan YES NO
2 1987.0 10.0 17.0 6.0 741.0 730.0 918.0 849.0 PS 1451.0 NA 97.0 79.0 nan 29.0 11.0 SAN SFO 447.0 nan nan 0.0 NA 0.0 nan nan nan nan nan YES YES
3 1987.0 10.0 18.0 7.0 729.0 730.0 847.0 849.0 PS 1451.0 NA 78.0 79.0 nan -2.0 -1.0 SAN SFO 447.0 nan nan 0.0 NA 0.0 nan nan nan nan nan NO NO
4 1987.0 10.0 19.0 1.0 749.0 730.0 922.0 849.0 PS 1451.0 NA 93.0 79.0 nan 33.0 19.0 SAN SFO 447.0 nan nan 0.0 NA 0.0 nan nan nan nan nan YES YES
5 1987.0 10.0 21.0 3.0 728.0 730.0 848.0 849.0 PS 1451.0 NA 80.0 79.0 nan -1.0 -2.0 SAN SFO 447.0 nan nan 0.0 NA 0.0 nan nan nan nan nan NO NO
6 1987.0 10.0 22.0 4.0 728.0 730.0 852.0 849.0 PS 1451.0 NA 84.0 79.0 nan 3.0 -2.0 SAN SFO 447.0 nan nan 0.0 NA 0.0 nan nan nan nan nan YES NO
7 1987.0 10.0 23.0 5.0 731.0 730.0 902.0 849.0 PS 1451.0 NA 91.0 79.0 nan 13.0 1.0 SAN SFO 447.0 nan nan 0.0 NA 0.0 nan nan nan nan nan YES YES
8 1987.0 10.0 24.0 6.0 744.0 730.0 908.0 849.0 PS 1451.0 NA 84.0 79.0 nan 19.0 14.0 SAN SFO 447.0 nan nan 0.0 NA 0.0 nan nan nan nan nan YES YES
9 1987.0 10.0 25.0 7.0 729.0 730.0 851.0 849.0 PS 1451.0 NA 82.0 79.0 nan 2.0 -1.0 SAN SFO 447.0 nan nan 0.0 NA 0.0 nan nan nan nan nan YES NO

In [5]:
# ----------

# 2- Data exploration and munging. Generate scatter plots 
# of various columns and plot fitted GLM model.

# Function to fit a GLM model and plot the fitted (x,y) values
def scatter_plot(data, x, y, max_points = 1000, fit = True):
    if(fit):
        lr = h2o.glm(x = data[[x]], y = data[y], family = "gaussian")
        coeff = lr.coef()
    df = data[[x,y]]
    runif = df[y].runif()
    df_subset = df[runif < float(max_points)/data.nrow]
    df_py = h2o.as_list(df_subset)
    
    if(fit): h2o.remove(lr._id)

    # If x variable is string, generate box-and-whisker plot
    if(df_py[x].dtype == "object"):
        if interactive: df_py.boxplot(column = y, by = x)
    # Otherwise, generate a scatter plot
    else:
        if interactive: df_py.plot(x = x, y = y, kind = "scatter")
    
    if(fit):
        x_min = min(df_py[x])
        x_max = max(df_py[x])
        y_min = coeff["Intercept"] + coeff[x]*x_min
        y_max = coeff["Intercept"] + coeff[x]*x_max
        plt.plot([x_min, x_max], [y_min, y_max], "k-")
    if interactive: plt.show()

scatter_plot(data, "Distance", "AirTime", fit = True)
scatter_plot(data, "UniqueCarrier", "ArrDelay", max_points = 5000, fit = False)


glm Model Build Progress: [##################################################] 100%
/Library/Python/2.7/site-packages/IPython/kernel/__main__.py:9: DeprecationWarning: `h2o.glm` is deprecated. Use the estimators sub module to build an H2OGeneralizedLinearEstimator.

In [6]:
# Group flights by month
grouped = data.group_by("Month")
bpd = grouped.count().sum("Cancelled").frame
bpd.show()
bpd.describe()
bpd.dim

# Convert columns to factors
data["Year"]      = data["Year"]     .asfactor()
data["Month"]     = data["Month"]    .asfactor()
data["DayOfWeek"] = data["DayOfWeek"].asfactor()
data["Cancelled"] = data["Cancelled"].asfactor()


Month sum_Cancelled nrow_Year
10 19 1999
1 1067 41979
Rows:2 Cols:3

Chunk compression summary:
chunk_type chunk_name count count_percentage size size_percentage
C1N 1-Byte Integers (w/o NAs) 1 33.333336 70 B 30.434782
C2 2-Byte Integers 1 33.333336 72 B 31.304348
C2S 2-Byte Fractions 1 33.333336 88 B 38.260868
Frame distribution summary:
size number_of_rows number_of_chunks_per_column number_of_chunks
172.16.2.52:54321 230 B 2.0 1.0 3.0
mean 230 B 2.0 1.0 3.0
min 230 B 2.0 1.0 3.0
max 230 B 2.0 1.0 3.0
stddev 0 B 0.0 0.0 0.0
total 230 B 2.0 1.0 3.0

Month sum_Cancelled nrow_Year
type int int int
mins 1.0 19.0 1999.0
mean 5.5 543.0 21989.0
maxs 10.0 1067.0 41979.0
sigma 6.36396103068741.047906684 28270.1291118
zeros 0 0 0
missing0 0 0
0 10.0 19.0 1999.0
1 1.0 1067.0 41979.0

In [8]:
# Calculate and plot travel time
hour1 = data["CRSArrTime"] / 100
mins1 = data["CRSArrTime"] % 100
arrTime = hour1*60 + mins1

hour2 = data["CRSDepTime"] / 100
mins2 = data["CRSDepTime"] % 100
depTime = hour2*60 + mins2

# TODO: Replace this once list comprehension is supported. See PUBDEV-1286.
# data["TravelTime"] = [x if x > 0 else None for x in (arrTime - depTime)]
data["TravelTime"] = (arrTime-depTime > 0).ifelse((arrTime-depTime), h2o.H2OFrame([None]*data.nrow))
scatter_plot(data, "Distance", "TravelTime")


Parse Progress: [##################################################] 100%

glm Model Build Progress: [##################################################] 100%
/Library/Python/2.7/site-packages/IPython/kernel/__main__.py:9: DeprecationWarning: `h2o.glm` is deprecated. Use the estimators sub module to build an H2OGeneralizedLinearEstimator.

In [9]:
# Impute missing travel times and re-plot
data.impute(column = "Distance", by = ["Origin", "Dest"])
scatter_plot(data, "Distance", "TravelTime")


glm Model Build Progress: [##################################################] 100%
/Library/Python/2.7/site-packages/IPython/kernel/__main__.py:9: DeprecationWarning: `h2o.glm` is deprecated. Use the estimators sub module to build an H2OGeneralizedLinearEstimator.

In [10]:
# ----------
# 3- Fit a model on train; using test as validation

# Create test/train split
s = data["Year"].runif()
train = data[s <= 0.75]
test  = data[s > 0.75]

# Set predictor and response variables
myY = "IsDepDelayed"
myX = ["Origin", "Dest", "Year", "UniqueCarrier", "DayOfWeek", "Month", "Distance", "FlightNum"]

# Simple GLM - Predict Delays
data_glm = h2o.glm(x           =train[myX],
                   y           =train[myY],
                   validation_x=test [myX],
                   validation_y=test [myY],
                   family      ="binomial",
                   standardize =True)

# Simple GBM
data_gbm = h2o.gbm(x              =train[myX],
                   y              =train[myY],
                   validation_x   =test [myX],
                   validation_y   =test [myY],
                   balance_classes=True,
                   ntrees         =3,
                   max_depth      =1,
                   distribution   ="bernoulli",
                   learn_rate     =0.1,
                   min_rows       =2)

# Complex GBM
data_gbm2 = h2o.gbm(x              =train[myX],
                    y              =train[myY],
                    validation_x   =test [myX],
                    validation_y   =test [myY],
                    balance_classes=True,
                    ntrees         =50,
                    max_depth      =5,
                    distribution   ="bernoulli",
                    learn_rate     =0.1,
                    min_rows       =2)

# Simple Random Forest
data_rf = h2o.random_forest(x              =train[myX],
                            y              =train[myY],
                            validation_x   =test [myX],
                            validation_y   =test [myY],
                            ntrees         =5,
                            max_depth      =2,
                            balance_classes=True)

# Complex Random Forest
data_rf2 = h2o.random_forest(x              =train[myX], 
                             y              =train[myY],
                             validation_x   =test [myX],
                             validation_y   =test [myY],
                             ntrees         =10,
                             max_depth      =5,
                             balance_classes=True)

# Deep Learning with 5 epochs
data_dl = h2o.deeplearning(x                   =train[myX],
                           y                   =train[myY],
                           validation_x        =test [myX],
                           validation_y        =test [myY],
                           hidden              =[10,10],
                           epochs              =5,
                           variable_importances=True,
                           balance_classes     =True,
                           loss                ="Automatic")


glm Model Build Progress: [##################################################] 100%
/Library/Python/2.7/site-packages/IPython/kernel/__main__.py:19: DeprecationWarning: `h2o.glm` is deprecated. Use the estimators sub module to build an H2OGeneralizedLinearEstimator.
/Library/Python/2.7/site-packages/IPython/kernel/__main__.py:31: DeprecationWarning: `h2o.gbm` is deprecated. Use the estimators sub module to build an H2OGradientBoostedEstimator.

gbm Model Build Progress: [##################################################] 100%

gbm Model Build Progress: [##################################################] 100%
/Library/Python/2.7/site-packages/IPython/kernel/__main__.py:43: DeprecationWarning: `h2o.gbm` is deprecated. Use the estimators sub module to build an H2OGradientBoostedEstimator.
/Library/Python/2.7/site-packages/IPython/kernel/__main__.py:52: DeprecationWarning: `h2o.random_forest` is deprecated. Use the estimators sub module to build an H2ORandomForestEstimator.

drf Model Build Progress: [##################################################] 100%

drf Model Build Progress: [##################################################] 100%
/Library/Python/2.7/site-packages/IPython/kernel/__main__.py:61: DeprecationWarning: `h2o.random_forest` is deprecated. Use the estimators sub module to build an H2ORandomForestEstimator.
/Library/Python/2.7/site-packages/IPython/kernel/__main__.py:72: DeprecationWarning: `h2o.deeplearning` is deprecated. Use the estimators sub module to build an H2ODeepLearningEstimator.

deeplearning Model Build Progress: [##################################################] 100%

In [11]:
# Variable importances from each algorithm
# Calculate magnitude of normalized GLM coefficients
glm_varimp = data_glm.coef_norm()
for k,v in glm_varimp.iteritems():
    glm_varimp[k] = abs(glm_varimp[k])
    
# Sort in descending order by magnitude
glm_sorted = sorted(glm_varimp.items(), key = operator.itemgetter(1), reverse = True)
table = tabulate(glm_sorted, headers = ["Predictor", "Normalized Coefficient"], tablefmt = "orgtbl")
print "Variable Importances:\n\n" + table

data_gbm.varimp()
data_rf.varimp()


Variable Importances:

| Predictor        |   Normalized Coefficient |
|------------------+--------------------------|
| Year.2008        |               2.32909    |
| Year.2003        |               1.58602    |
| Origin.LIH       |               1.52262    |
| Origin.MDW       |               1.51511    |
| Dest.LYH         |               1.49099    |
| Year.2007        |               1.46821    |
| Origin.HPN       |               1.43102    |
| Origin.TLH       |               1.42092    |
| Origin.LBB       |               1.40506    |
| Origin.CAE       |               1.39381    |
| Origin.ERI       |               1.31974    |
| UniqueCarrier.HP |               1.23449    |
| Origin.TRI       |               1.10814    |
| Origin.SAV       |               1.04866    |
| Origin.BTV       |               1.03496    |
| Year.2001        |               1.02026    |
| Origin.LEX       |               1.00824    |
| Year.2002        |               0.991165   |
| Dest.HTS         |               0.985988   |
| UniqueCarrier.TW |               0.977311   |
| Origin.CRP       |               0.94316    |
| Origin.MIA       |               0.94088    |
| Origin.ATL       |               0.901622   |
| Origin.HNL       |               0.878897   |
| Origin.SRQ       |               0.843496   |
| Origin.PBI       |               0.831153   |
| Dest.EYW         |               0.820526   |
| Year.1994        |               0.81828    |
| Origin.IAH       |               0.812985   |
| Origin.ACY       |               0.811903   |
| Year.2006        |               0.80505    |
| Origin.ORD       |               0.803985   |
| Dest.ICT         |               0.791497   |
| Dest.DAY         |               0.787073   |
| Year.2004        |               0.782058   |
| Origin.CHO       |               0.773375   |
| Origin.OGG       |               0.771396   |
| Origin.TYS       |               0.768322   |
| Dest.CHO         |               0.750908   |
| Origin.LYH       |               0.750908   |
| Origin.MAF       |               0.708144   |
| Dest.FLL         |               0.705287   |
| Dest.TOL         |               0.702964   |
| Origin.ALB       |               0.679838   |
| Origin.EYW       |               0.673705   |
| Origin.MSY       |               0.665379   |
| Origin.MLB       |               0.665287   |
| Origin.MRY       |               0.654559   |
| Origin.CMH       |               0.652942   |
| Dest.LBB         |               0.647455   |
| Year.1990        |               0.646123   |
| Origin.PSP       |               0.638068   |
| Origin.MYR       |               0.632499   |
| Dest.HRL         |               0.617397   |
| Dest.PSP         |               0.614558   |
| Origin.STL       |               0.609371   |
| Dest.ISP         |               0.604767   |
| Origin.AUS       |               0.602786   |
| Origin.TUL       |               0.593501   |
| Dest.GSO         |               0.592771   |
| Origin.ROA       |               0.589523   |
| Origin.PIT       |               0.588031   |
| Origin.CRW       |               0.58274    |
| Origin.GSO       |               0.578391   |
| Origin.JAX       |               0.570429   |
| Dest.PBI         |               0.569639   |
| Dest.LIH         |               0.566506   |
| Origin.TUS       |               0.545989   |
| Origin.GRR       |               0.54505    |
| Dest.OGG         |               0.544613   |
| Dest.TPA         |               0.543403   |
| UniqueCarrier.WN |               0.54302    |
| Origin.SLC       |               0.537536   |
| Origin.OMA       |               0.536219   |
| Dest.IND         |               0.526445   |
| Origin.BGM       |               0.512694   |
| Year.1996        |               0.505915   |
| Dest.SFO         |               0.502705   |
| Dest.FAY         |               0.502571   |
| Origin.LAX       |               0.50092    |
| Dest.BDL         |               0.480933   |
| Origin.PHL       |               0.467753   |
| Origin.FLL       |               0.467648   |
| Dest.OAJ         |               0.465443   |
| Dest.IAH         |               0.465309   |
| Origin.SYR       |               0.460764   |
| Origin.MCO       |               0.455878   |
| Origin.SDF       |               0.454177   |
| Dest.CRP         |               0.444166   |
| Origin.IND       |               0.439275   |
| Dest.SAT         |               0.435934   |
| Year.2005        |               0.427336   |
| UniqueCarrier.CO |               0.426534   |
| Origin.BOI       |               0.425995   |
| Origin.LGA       |               0.419963   |
| Origin.OKC       |               0.405915   |
| Origin.MEM       |               0.398872   |
| UniqueCarrier.PI |               0.393996   |
| Origin.OAK       |               0.393077   |
| Year.1997        |               0.38711    |
| Dest.BGM         |               0.385668   |
| Dest.MIA         |               0.384297   |
| Dest.BHM         |               0.381656   |
| Origin.PWM       |               0.381013   |
| Dest.STL         |               0.363011   |
| Dest.CLE         |               0.355422   |
| Origin.ELP       |               0.348199   |
| Origin.BDL       |               0.339765   |
| Dest.ALB         |               0.336874   |
| Dest.BUF         |               0.329391   |
| Origin.DFW       |               0.328583   |
| Origin.RNO       |               0.32767    |
| Origin.PDX       |               0.323056   |
| Origin.IAD       |               0.317755   |
| Year.1992        |               0.316751   |
| Origin.ABE       |               0.314883   |
| Dest.CHS         |               0.312367   |
| Origin.ROC       |               0.310369   |
| Origin.BHM       |               0.308438   |
| Origin.EGE       |               0.30736    |
| Origin.MCI       |               0.302764   |
| Origin.CLT       |               0.298978   |
| Year.1991        |               0.294257   |
| Dest.PWM         |               0.292541   |
| Dest.CAK         |               0.28878    |
| Year.1995        |               0.287829   |
| Dest.EUG         |               0.277207   |
| Origin.BWI       |               0.27248    |
| Origin.SAN       |               0.269065   |
| Dest.PNS         |               0.269013   |
| Origin.SWF       |               0.267978   |
| Origin.ISP       |               0.264563   |
| Origin.STX       |               0.262968   |
| UniqueCarrier.US |               0.261129   |
| Origin.BUF       |               0.260776   |
| Dest.FNT         |               0.259389   |
| Dest.FAT         |               0.259131   |
| Dest.SBN         |               0.25807    |
| Origin.DEN       |               0.257953   |
| Dest.AVL         |               0.252749   |
| Dest.SEA         |               0.250817   |
| Dest.CMH         |               0.250197   |
| Origin.CLE       |               0.248514   |
| Dest.PHX         |               0.247179   |
| Dest.RIC         |               0.246128   |
| DayOfWeek.5      |               0.244885   |
| Origin.BUR       |               0.243037   |
| Dest.MAF         |               0.23684    |
| Origin.DAY       |               0.236086   |
| Origin.DSM       |               0.234868   |
| Dest.ACY         |               0.22949    |
| Origin.MKE       |               0.226907   |
| UniqueCarrier.AA |               0.221893   |
| Origin.BOS       |               0.22173    |
| Origin.TPA       |               0.22126    |
| Dest.ORF         |               0.217326   |
| Dest.CAE         |               0.214417   |
| Origin.JFK       |               0.211024   |
| Dest.EWR         |               0.204522   |
| Year.1987        |               0.204327   |
| Month.10         |               0.204327   |
| Dest.GEG         |               0.202752   |
| Dest.GRR         |               0.202414   |
| Dest.BTV         |               0.201767   |
| Origin.SFO       |               0.197402   |
| Origin.ONT       |               0.195393   |
| Dest.OKC         |               0.193209   |
| Origin.RIC       |               0.192843   |
| Origin.MSP       |               0.192299   |
| Dest.AUS         |               0.189116   |
| Year.1999        |               0.188015   |
| DayOfWeek.2      |               0.186682   |
| Dest.MCO         |               0.18663    |
| Dest.LAX         |               0.185541   |
| Origin.SJC       |               0.185346   |
| Year.1993        |               0.184239   |
| Dest.ABQ         |               0.183639   |
| Dest.JAX         |               0.182933   |
| Origin.PHX       |               0.176828   |
| Origin.HRL       |               0.168418   |
| Dest.OAK         |               0.166368   |
| DayOfWeek.6      |               0.166242   |
| Year.1989        |               0.16571    |
| Dest.MCI         |               0.164261   |
| Dest.BWI         |               0.157173   |
| Dest.BUR         |               0.151132   |
| Dest.DFW         |               0.149876   |
| Distance         |               0.141787   |
| Dest.LAS         |               0.139533   |
| Dest.IAD         |               0.136137   |
| Dest.OMA         |               0.130675   |
| Origin.RDU       |               0.128901   |
| Origin.PVD       |               0.126506   |
| Origin.BNA       |               0.126127   |
| Dest.PVD         |               0.120904   |
| Dest.CVG         |               0.120002   |
| Year.1998        |               0.119616   |
| Dest.SNA         |               0.118181   |
| Dest.MHT         |               0.1154     |
| Origin.SMF       |               0.1152     |
| Dest.CLT         |               0.11334    |
| Dest.PDX         |               0.112725   |
| Dest.ATL         |               0.112372   |
| Dest.KOA         |               0.111871   |
| Origin.ORF       |               0.109896   |
| Dest.SLC         |               0.105991   |
| Month.1          |               0.105322   |
| Year.2000        |               0.103907   |
| DayOfWeek.4      |               0.103593   |
| Dest.TUL         |               0.101233   |
| Dest.HOU         |               0.101205   |
| Dest.LGA         |               0.0986903  |
| Dest.MDW         |               0.0984498  |
| Origin.SJU       |               0.0933868  |
| Dest.RNO         |               0.09181    |
| Dest.ORD         |               0.0876702  |
| Origin.ICT       |               0.0875178  |
| Dest.TUS         |               0.0871986  |
| Dest.ABE         |               0.085817   |
| Dest.HNL         |               0.0833162  |
| Dest.SAN         |               0.0805696  |
| Origin.COS       |               0.0793754  |
| Dest.PHL         |               0.0769116  |
| Dest.DTW         |               0.0735575  |
| DayOfWeek.7      |               0.0702998  |
| Origin.DAL       |               0.068293   |
| UniqueCarrier.DL |               0.0671625  |
| Dest.SYR         |               0.0645953  |
| Origin.SCK       |               0.062638   |
| Origin.JAN       |               0.0620125  |
| Dest.STT         |               0.0574638  |
| Dest.DCA         |               0.0543437  |
| Dest.BNA         |               0.0516433  |
| UniqueCarrier.UA |               0.0500982  |
| Dest.RSW         |               0.0499947  |
| Origin.EWR       |               0.0468324  |
| Origin.SNA       |               0.0468281  |
| Origin.SEA       |               0.0438454  |
| Origin.ABQ       |               0.0405749  |
| Origin.KOA       |               0.0403563  |
| Dest.MDT         |               0.0387199  |
| Dest.RDU         |               0.0343743  |
| Dest.HPN         |               0.0326688  |
| FlightNum        |               0.0314507  |
| Dest.DEN         |               0.0275815  |
| Origin.DCA       |               0.0273375  |
| DayOfWeek.3      |               0.0259223  |
| Dest.ONT         |               0.025527   |
| Dest.JFK         |               0.0236515  |
| Dest.BOS         |               0.0235103  |
| Dest.PIT         |               0.019948   |
| Dest.DAL         |               0.0198705  |
| Origin.DTW       |               0.0185183  |
| Dest.ELM         |               0.0167056  |
| Year.1988        |               0.016543   |
| Origin.LAS       |               0.0160909  |
| Origin.AVP       |               0.0156962  |
| Origin.PHF       |               0.0137917  |
| Intercept        |               0.0131142  |
| Dest.UCA         |               0.0101947  |
| Origin.LAN       |               0.00957813 |
| Dest.COS         |               0.00811531 |
| Dest.SJC         |               0.00792548 |
| Dest.MSY         |               0.00733655 |
| Dest.MSP         |               0.00661902 |
| Origin.MHT       |               0.0050723  |
| Origin.CVG       |               0.00391065 |
| Origin.CHS       |               0.00238382 |
| Origin.MDT       |               0.00115688 |
| Dest.LIT         |               0          |
| Dest.ORH         |               0          |
| Origin.ANC       |               0          |
| Dest.SRQ         |               0          |
| Dest.MRY         |               0          |
| Dest.SJU         |               0          |
| Origin.RSW       |               0          |
| Origin.SAT       |               0          |
| Dest.DSM         |               0          |
| Dest.MKE         |               0          |
| Origin.LIT       |               0          |
| Dest.SWF         |               0          |
| Origin.UCA       |               0          |
| Origin.HOU       |               0          |
| Origin.AMA       |               0          |
| Dest.ERI         |               0          |
| Dest.ROC         |               0          |
| Dest.GSP         |               0          |
| Origin.MFR       |               0          |
| Dest.JAN         |               0          |
| Origin.STT       |               0          |
| Dest.ELP         |               0          |
| Dest.CHA         |               0          |
| Origin.SBN       |               0          |
| Origin.GNV       |               0          |
| Dest.AMA         |               0          |
| Dest.LEX         |               0          |
| Dest.BOI         |               0          |
| Dest.ROA         |               0          |
| UniqueCarrier.PS |               0          |
| Dest.ILM         |               0          |
| Dest.SDF         |               0          |
| Dest.PHF         |               0          |
| Dest.ANC         |               0          |
| Dest.AVP         |               0          |
| DayOfWeek.1      |               0          |
| Dest.SMF         |               0          |
| Origin.GEG       |               0          |
| Dest.MYR         |               0          |
| Dest.SCK         |               0          |
| Origin.BIL       |               0          |

Variable Importances:
variable relative_importance scaled_importance percentage
Year 1205.6 1.0 1.0
Origin 0.0 0.0 0.0
Dest 0.0 0.0 0.0
UniqueCarrier 0.0 0.0 0.0
DayOfWeek 0.0 0.0 0.0
Month 0.0 0.0 0.0
Distance 0.0 0.0 0.0
FlightNum 0.0 0.0 0.0
Variable Importances:
variable relative_importance scaled_importance percentage
Year 1211.3 1.0 0.5
Origin 544.0 0.4 0.2
Dest 343.0 0.3 0.1
UniqueCarrier 159.0 0.1 0.1
FlightNum 67.9 0.1 0.0
DayOfWeek 0.0 0.0 0.0
Month 0.0 0.0 0.0
Distance 0.0 0.0 0.0

In [12]:
# Model performance of GBM model on test data
data_gbm2.model_performance(test)


ModelMetricsBinomial: gbm
** Reported on test data. **

MSE: 0.20601352191
R^2: 0.174364205256
LogLoss: 0.598874249831
AUC: 0.741511687629
Gini: 0.483023375259

Confusion Matrix (Act/Pred) for max f1 @ threshold = 0.38952159377:
NO YES Error Rate
NO 2351.0 2936.0 0.5553 (2936.0/5287.0)
YES 888.0 4883.0 0.1539 (888.0/5771.0)
Total 3239.0 7819.0 0.3458 (3824.0/11058.0)
Maximum Metrics: Maximum metrics at their respective thresholds

metric threshold value idx
max f1 0.4 0.7 268.0
max f2 0.2 0.8 372.0
max f0point5 0.6 0.7 168.0
max accuracy 0.5 0.7 202.0
max precision 1.0 1.0 0.0
max absolute_MCC 0.5 0.4 190.0
max min_per_class_accuracy 0.5 0.7 204.0
Out[12]: