In [1]:
import h2o
import pandas
import pprint
import operator
import matplotlib
from h2o.estimators.glm import H2OGeneralizedLinearEstimator
from h2o.estimators.gbm import H2OGradientBoostingEstimator
from h2o.estimators.random_forest import H2ORandomForestEstimator
from h2o.estimators.deeplearning import H2ODeepLearningEstimator
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: 11 minutes 35 seconds 932 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 = H2OGeneralizedLinearEstimator(family = "gaussian")
        lr.train(x=x, y=y, training_frame=data)
        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%

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 [7]:
# 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")


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-26afa1c5b480> in <module>()
     10 # TODO: Replace this once list comprehension is supported. See PUBDEV-1286.
     11 # data["TravelTime"] = [x if x > 0 else None for x in (arrTime - depTime)]
---> 12 data["TravelTime"] = (arrTime-depTime > 0).ifelse((arrTime-depTime), h2o.H2OFrame.fromPython([[None] * data.nrow]))
     13 scatter_plot(data, "Distance", "TravelTime")

AttributeError: type object 'H2OFrame' has no attribute 'fromPython'

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


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

In [18]:
# ----------
# 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 = H2OGeneralizedLinearEstimator(family="binomial", standardize=True)
data_glm.train(x               =myX,
               y               =myY,
               training_frame  =train,
               validation_frame=test)

# Simple GBM
data_gbm = H2OGradientBoostingEstimator(balance_classes=True,
                                        ntrees         =3,
                                        max_depth      =1,
                                        distribution   ="bernoulli",
                                        learn_rate     =0.1,
                                        min_rows       =2)

data_gbm.train(x               =myX,
               y               =myY,
               training_frame  =train,
               validation_frame=test)

# Complex GBM
data_gbm2 = H2OGradientBoostingEstimator(balance_classes=True,
                                         ntrees         =50,
                                         max_depth      =5,
                                         distribution   ="bernoulli",
                                         learn_rate     =0.1,
                                         min_rows       =2)

data_gbm2.train(x               =myX,
                y               =myY,
                training_frame  =train,
                validation_frame=test)

# Simple Random Forest
data_rf = H2ORandomForestEstimator(ntrees         =5,
                                   max_depth      =2,
                                   balance_classes=True)

data_rf.train(x               =myX,
              y               =myY,
              training_frame  =train,
              validation_frame=test)

# Complex Random Forest
data_rf2 = H2ORandomForestEstimator(ntrees         =10,
                                    max_depth      =5,
                                    balance_classes=True)

data_rf2.train(x               =myX,
               y               =myY,
               training_frame  =train,
               validation_frame=test)

# Deep Learning with 5 epochs
data_dl = H2ODeepLearningEstimator(hidden              =[10,10],
                                   epochs              =5,
                                   variable_importances=True,
                                   balance_classes     =True,
                                   loss                ="Automatic")

data_dl.train(x               =myX,
              y               =myY,
              training_frame  =train,
              validation_frame=test)


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

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

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

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

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

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

In [19]:
# 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.2275     |
| Origin.HPN       |               1.65336    |
| Origin.MDW       |               1.62308    |
| Year.2003        |               1.59114    |
| Dest.LYH         |               1.58882    |
| Origin.LEX       |               1.58225    |
| Year.2007        |               1.35973    |
| Origin.LIH       |               1.24787    |
| Origin.HNL       |               1.2209     |
| Origin.TLH       |               1.18049    |
| Origin.ERI       |               1.16758    |
| Origin.SRQ       |               1.12207    |
| UniqueCarrier.HP |               1.11691    |
| Origin.CAE       |               1.0982     |
| Origin.OGG       |               1.0819     |
| Year.2001        |               1.07256    |
| Origin.TRI       |               1.04899    |
| Origin.MYR       |               1.03663    |
| Year.2002        |               0.996461   |
| Origin.ATL       |               0.969041   |
| Origin.PSP       |               0.949241   |
| Origin.GRR       |               0.947258   |
| Origin.ROA       |               0.94287    |
| Dest.HTS         |               0.936352   |
| UniqueCarrier.TW |               0.921902   |
| Year.2004        |               0.898567   |
| Dest.PSP         |               0.895299   |
| Dest.ICT         |               0.8874     |
| Origin.OKC       |               0.862971   |
| Origin.BTV       |               0.860685   |
| Origin.IAH       |               0.848429   |
| Dest.DAY         |               0.842686   |
| Dest.CAE         |               0.841802   |
| Year.2006        |               0.836756   |
| Origin.SAV       |               0.819502   |
| Origin.ORD       |               0.808572   |
| Origin.PBI       |               0.796989   |
| Year.1994        |               0.764688   |
| Origin.MLB       |               0.756815   |
| Origin.JAX       |               0.729777   |
| Origin.LYH       |               0.727461   |
| Dest.CHO         |               0.727461   |
| Dest.PBI         |               0.716583   |
| Dest.TOL         |               0.713648   |
| Year.1996        |               0.690888   |
| Dest.PNS         |               0.689612   |
| Dest.FLL         |               0.684016   |
| Origin.SYR       |               0.672188   |
| Origin.TYS       |               0.664401   |
| Origin.LBB       |               0.659649   |
| Origin.MIA       |               0.643939   |
| Dest.LIH         |               0.638818   |
| Origin.CRP       |               0.633152   |
| Origin.EYW       |               0.632021   |
| Origin.OMA       |               0.630335   |
| Dest.CRP         |               0.626277   |
| Dest.GSO         |               0.625201   |
| Origin.AUS       |               0.623901   |
| Origin.ALB       |               0.622646   |
| Dest.CAK         |               0.619225   |
| Origin.DSM       |               0.614182   |
| Origin.BOI       |               0.612563   |
| Origin.CMH       |               0.610468   |
| Dest.OGG         |               0.609105   |
| Origin.ABE       |               0.603223   |
| Origin.STL       |               0.596553   |
| Origin.MAF       |               0.592643   |
| Origin.GSO       |               0.572995   |
| Origin.PIT       |               0.572245   |
| Dest.KOA         |               0.569394   |
| Year.1997        |               0.559696   |
| Origin.FLL       |               0.55443    |
| Origin.EGE       |               0.548196   |
| Origin.CHO       |               0.527513   |
| Origin.BUF       |               0.518716   |
| Dest.SFO         |               0.514501   |
| Year.1990        |               0.514277   |
| Origin.STX       |               0.513656   |
| Origin.DFW       |               0.50983    |
| Year.2005        |               0.509044   |
| Origin.MRY       |               0.508098   |
| Dest.GEG         |               0.507461   |
| Origin.RNO       |               0.490191   |
| Origin.MSY       |               0.478458   |
| UniqueCarrier.WN |               0.474916   |
| Origin.LAX       |               0.466962   |
| Origin.ROC       |               0.457296   |
| Origin.IND       |               0.457098   |
| Origin.BDL       |               0.448797   |
| Dest.CHS         |               0.445441   |
| Origin.MKE       |               0.442115   |
| Dest.TPA         |               0.441168   |
| Dest.BUF         |               0.431423   |
| Origin.MCI       |               0.430957   |
| Origin.CRW       |               0.430383   |
| Dest.UCA         |               0.41825    |
| Dest.ABQ         |               0.413122   |
| Origin.PHL       |               0.411756   |
| Dest.STL         |               0.406148   |
| Origin.SDF       |               0.404947   |
| Origin.LGA       |               0.398562   |
| UniqueCarrier.PI |               0.396953   |
| Origin.DAY       |               0.394878   |
| Origin.MCO       |               0.393791   |
| Origin.SAN       |               0.386797   |
| Dest.IAH         |               0.374325   |
| Dest.IND         |               0.367184   |
| Dest.MKE         |               0.358601   |
| Origin.TUL       |               0.355715   |
| Dest.BDL         |               0.352087   |
| Dest.PVD         |               0.35156    |
| Dest.BTV         |               0.341781   |
| Origin.MSP       |               0.336283   |
| Origin.OAK       |               0.334478   |
| Origin.BGM       |               0.333016   |
| Dest.ROC         |               0.329125   |
| UniqueCarrier.CO |               0.326535   |
| Dest.CLE         |               0.324059   |
| Dest.ELM         |               0.323227   |
| Year.1992        |               0.322615   |
| Origin.PWM       |               0.318531   |
| Dest.FAY         |               0.315016   |
| Dest.HRL         |               0.314797   |
| Origin.BOS       |               0.313955   |
| Dest.SNA         |               0.312157   |
| Year.1991        |               0.311134   |
| Origin.BWI       |               0.309089   |
| Dest.HPN         |               0.303715   |
| Dest.MCO         |               0.302181   |
| UniqueCarrier.US |               0.302145   |
| Dest.SWF         |               0.287815   |
| Dest.SAT         |               0.287655   |
| Origin.SWF       |               0.285187   |
| Year.1995        |               0.283958   |
| Origin.MEM       |               0.278675   |
| Dest.AUS         |               0.275089   |
| DayOfWeek.5      |               0.269415   |
| Origin.SBN       |               0.261982   |
| Dest.CMH         |               0.261746   |
| Dest.SEA         |               0.259097   |
| Dest.ISP         |               0.258144   |
| Origin.BUR       |               0.25813    |
| Dest.ALB         |               0.256221   |
| Origin.UCA       |               0.254124   |
| Origin.CLT       |               0.253083   |
| Dest.ORF         |               0.252015   |
| Dest.OAJ         |               0.251626   |
| Origin.ELP       |               0.250258   |
| Dest.RIC         |               0.247783   |
| Dest.PWM         |               0.245666   |
| Dest.STT         |               0.245357   |
| Origin.SLC       |               0.236774   |
| Origin.JFK       |               0.23662    |
| Origin.TUS       |               0.236556   |
| Year.1987        |               0.232402   |
| Month.10         |               0.232402   |
| Origin.CLE       |               0.231119   |
| Dest.ILM         |               0.228868   |
| Dest.JAX         |               0.224722   |
| UniqueCarrier.AA |               0.224693   |
| Dest.LAS         |               0.219216   |
| Dest.BUR         |               0.217384   |
| Origin.SFO       |               0.216126   |
| Origin.DEN       |               0.216035   |
| Dest.MYR         |               0.214981   |
| Dest.GRR         |               0.21137    |
| Dest.SMF         |               0.211231   |
| Dest.COS         |               0.209247   |
| Dest.CLT         |               0.205425   |
| Dest.DFW         |               0.204676   |
| Dest.BGM         |               0.201743   |
| Origin.LIT       |               0.199517   |
| Origin.BHM       |               0.192509   |
| DayOfWeek.2      |               0.191144   |
| Origin.MDT       |               0.184875   |
| Dest.IAD         |               0.171728   |
| Origin.ACY       |               0.169522   |
| Distance         |               0.163557   |
| Dest.LBB         |               0.16073    |
| Origin.TPA       |               0.155183   |
| Origin.MFR       |               0.150086   |
| Dest.ATL         |               0.147874   |
| Year.2000        |               0.147091   |
| Dest.FAT         |               0.146204   |
| DayOfWeek.4      |               0.143545   |
| Dest.OAK         |               0.131122   |
| Dest.SJC         |               0.130707   |
| Dest.MIA         |               0.130348   |
| Origin.IAD       |               0.129749   |
| Origin.PHX       |               0.126804   |
| Year.1999        |               0.123166   |
| Origin.EWR       |               0.122524   |
| DayOfWeek.6      |               0.121043   |
| Dest.SDF         |               0.119076   |
| Dest.PDX         |               0.117449   |
| Dest.RNO         |               0.115122   |
| Dest.PHL         |               0.114881   |
| Dest.DAL         |               0.111471   |
| Year.1989        |               0.110836   |
| UniqueCarrier.UA |               0.110227   |
| Dest.ABE         |               0.109149   |
| Origin.SJC       |               0.108557   |
| Origin.COS       |               0.108529   |
| Year.1993        |               0.10379    |
| Month.1          |               0.102827   |
| Dest.ORD         |               0.10004    |
| Dest.BWI         |               0.0970778  |
| Dest.SAN         |               0.0943769  |
| Dest.OMA         |               0.0924889  |
| Origin.ICT       |               0.0922715  |
| Dest.SLC         |               0.0887606  |
| Dest.EYW         |               0.088619   |
| Dest.DCA         |               0.0873498  |
| Dest.DEN         |               0.0790191  |
| Dest.MCI         |               0.0786633  |
| Dest.MAF         |               0.0785132  |
| Dest.AVP         |               0.0782321  |
| Dest.BOS         |               0.0772358  |
| Origin.ABQ       |               0.0737992  |
| Origin.CHS       |               0.0736405  |
| Origin.SMF       |               0.0670507  |
| Dest.LGA         |               0.0661394  |
| Dest.TUS         |               0.0646304  |
| Dest.LAX         |               0.0646072  |
| Origin.CVG       |               0.0632093  |
| Origin.BNA       |               0.0629494  |
| Dest.RSW         |               0.0624705  |
| Dest.LIT         |               0.062223   |
| Origin.AVP       |               0.0579933  |
| Dest.SYR         |               0.0570599  |
| Dest.BHM         |               0.0487471  |
| Origin.GEG       |               0.047366   |
| Origin.PHF       |               0.0451668  |
| FlightNum        |               0.0434175  |
| Dest.BNA         |               0.0405668  |
| Year.1988        |               0.0387791  |
| Dest.PHX         |               0.0337794  |
| Dest.PIT         |               0.0304121  |
| Dest.DTW         |               0.030221   |
| DayOfWeek.3      |               0.0292536  |
| Origin.DCA       |               0.0273642  |
| Dest.SJU         |               0.0267403  |
| Dest.HOU         |               0.0257996  |
| Dest.SBN         |               0.023946   |
| Origin.SNA       |               0.0220876  |
| UniqueCarrier.PS |               0.0219856  |
| DayOfWeek.7      |               0.020087   |
| Origin.PDX       |               0.0188203  |
| Intercept        |               0.0181939  |
| Dest.CVG         |               0.0163624  |
| Dest.MDT         |               0.0154982  |
| Origin.SEA       |               0.0149     |
| Origin.RDU       |               0.0129438  |
| Dest.EUG         |               0.0112746  |
| Origin.JAN       |               0.0112455  |
| Dest.MSP         |               0.0106434  |
| Dest.GSP         |               0.00908574 |
| Origin.DAL       |               0.00744431 |
| Origin.HRL       |               0.00738513 |
| Origin.ORF       |               0.00730436 |
| Origin.HOU       |               0.0061481  |
| Dest.HNL         |               0.00608665 |
| Origin.ONT       |               0.00545128 |
| Origin.LAS       |               0.00479972 |
| Dest.MDW         |               0.00444781 |
| Dest.EWR         |               0.00308016 |
| Dest.OKC         |               0          |
| Dest.ORH         |               0          |
| Origin.ANC       |               0          |
| Dest.SRQ         |               0          |
| Dest.MRY         |               0          |
| UniqueCarrier.DL |               0          |
| Dest.MSY         |               0          |
| Origin.RSW       |               0          |
| Origin.SAT       |               0          |
| Dest.DSM         |               0          |
| Year.1998        |               0          |
| Origin.SCK       |               0          |
| Origin.ISP       |               0          |
| Origin.RIC       |               0          |
| Origin.LAN       |               0          |
| Origin.KOA       |               0          |
| Dest.JFK         |               0          |
| Origin.AMA       |               0          |
| Dest.ERI         |               0          |
| Dest.ACY         |               0          |
| Dest.FNT         |               0          |
| Origin.DTW       |               0          |
| Dest.TUL         |               0          |
| Origin.MHT       |               0          |
| Dest.JAN         |               0          |
| Origin.STT       |               0          |
| Dest.ELP         |               0          |
| Dest.CHA         |               0          |
| Origin.PVD       |               0          |
| Origin.SJU       |               0          |
| Origin.GNV       |               0          |
| Dest.AMA         |               0          |
| Dest.LEX         |               0          |
| Dest.BOI         |               0          |
| Dest.ROA         |               0          |
| Dest.AVL         |               0          |
| Dest.PHF         |               0          |
| Dest.ANC         |               0          |
| Dest.RDU         |               0          |
| DayOfWeek.1      |               0          |
| Dest.MHT         |               0          |
| Dest.ONT         |               0          |
| Dest.SCK         |               0          |
| Origin.BIL       |               0          |

Variable Importances:
variable relative_importance scaled_importance percentage
Year 1208.1 1.0 1.0
Month 0.0 0.0 0.0
DayOfWeek 0.0 0.0 0.0
UniqueCarrier 0.0 0.0 0.0
FlightNum 0.0 0.0 0.0
Origin 0.0 0.0 0.0
Dest 0.0 0.0 0.0
Distance 0.0 0.0 0.0
Variable Importances:
variable relative_importance scaled_importance percentage
Year 1047.7 1.0 0.5
Origin 742.6 0.7 0.3
Dest 223.7 0.2 0.1
FlightNum 188.9 0.2 0.1
Distance 14.8 0.0 0.0
DayOfWeek 8.5 0.0 0.0
Month 0.0 0.0 0.0
UniqueCarrier 0.0 0.0 0.0

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


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

MSE: 0.206759868103
R^2: 0.17060728992
LogLoss: 0.600570494543
AUC: 0.738661980377
Gini: 0.477323960754

Confusion Matrix (Act/Pred) for max f1 @ threshold = 0.397200694931:
NO YES Error Rate
NO 2439.0 2742.0 0.5292 (2742.0/5181.0)
YES 953.0 4811.0 0.1653 (953.0/5764.0)
Total 3392.0 7553.0 0.3376 (3695.0/10945.0)
Maximum Metrics: Maximum metrics at their respective thresholds

metric threshold value idx
max f1 0.4 0.7 267.0
max f2 0.2 0.8 378.0
max f0point5 0.6 0.7 178.0
max accuracy 0.5 0.7 208.0
max precision 1.0 1.0 0.0
max absolute_MCC 0.6 0.4 178.0
max min_per_class_accuracy 0.5 0.7 202.0
Out[20]: