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]:
# imports and inits
import h2o
from h2o.estimators.deeplearning import H2ODeepLearningEstimator
h2o.init()
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import pandas as pd
In [2]:
train = h2o.import_file('../data/train.csv')
X = [name for name in train.columns if name != 'label']
In [3]:
sdae = H2ODeepLearningEstimator(
epochs=3,
hidden=[250, 50, 2, 50, 250], # 5 layers, 2 in the middle for viz
activation='rectifier',
l2=0.2, # L2 for numeric stability and can help with plotting rectifier activations
adaptive_rate=True,
sparse=True, # handles data w/ many zeros more efficiently
seed=88888, # enables exact reproducibility
reproducible=True, # slow
ignore_const_cols=True,
autoencoder=True)
sdae.train(x=X, training_frame=train)
In [4]:
deep_features = sdae.deepfeatures(train, 2)
deep_features_pandas = deep_features.as_data_frame()
deep_features_pandas['label'] = train['label'].as_data_frame()
deep_features_pandas.head()
Out[4]:
In [5]:
x_label = 'DF.L3.C1'
y_label = 'DF.L3.C2'
samp = 10000 # show first 10000
# figure size
plt.figure(figsize=(10,10), dpi = 300)
# baseline marker size
s = 100
# plot each number with different visual attributes
_1 = plt.scatter(deep_features_pandas[deep_features_pandas.label == 1].loc[0:samp:, x_label],
deep_features_pandas[deep_features_pandas.label == 1].loc[0:samp, y_label],
color='c', s=s/2, alpha=.15)
_7 = plt.scatter(deep_features_pandas[deep_features_pandas.label == 7].loc[0:samp:, x_label],
deep_features_pandas[deep_features_pandas.label == 7].loc[0:samp, y_label],
color='#99ff33', s=2*s, marker='h', alpha=.15)
# legend
_ = plt.legend([_1, _7],
['1','7'],
bbox_to_anchor=(1.05, 0.0),
loc=3, borderaxespad=0.)
In [6]:
# h2o anomoly function calculates row-wise reconstrunction MSE
reconstruction_mse = sdae.anomaly(train[train['label'] == 1])
# use pandas to sort reconstrunction MSE
pandas_reconstruction_mse = reconstruction_mse.as_data_frame().sort_values(by='Reconstruction.MSE', ascending=False)
print(pandas_reconstruction_mse.head())
top = pandas_reconstruction_mse.idxmax().values[0]
In [7]:
ones = train[train['label'] == 1].as_data_frame().as_matrix()
pixels = ones[top, 1:]
pixels = np.array(pixels, dtype='uint8')
pixels = pixels.reshape((28, 28))
plt.imshow(pixels, cmap='gray')
plt.show()
In [8]:
# h2o anomoly function calculates row-wise reconstrunction MSE
reconstruction_mse = sdae.anomaly(train[train['label'] == 7])
# use pandas to sort reconstrunction MSE
pandas_reconstruction_mse = reconstruction_mse.as_data_frame().sort_values(by='Reconstruction.MSE', ascending=False)
print(pandas_reconstruction_mse.head())
top = pandas_reconstruction_mse.idxmax().values[0]
In [9]:
sevens = train[train['label'] == 7].as_data_frame().as_matrix()
pixels = sevens[top, 1:]
pixels = np.array(pixels, dtype='uint8')
pixels = pixels.reshape((28, 28))
plt.imshow(pixels, cmap='gray')
plt.show()
In [10]:
# shutdown h2o
h2o.cluster().shutdown(prompt=True)