Skip to content

Commit

Permalink
Merge pull request #79 from bjherger/ts_example
Browse files Browse the repository at this point in the history
Timeseries example
  • Loading branch information
bjherger committed Oct 26, 2018
2 parents 1ea4f96 + 8751da4 commit 48b3c61
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 8 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion examples/example_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
def main():
# TODO Load data
# TODO Heuristic data transformations
# TODO Train /test split
# TODO List out variable types
# TODO Create and fit Automater
# TODO Create and fit keras (deep learning) model.
# TODO List out which components are supplied by Automaterl
# TODO List out which components are supplied by Automater
# TODO Inverse transform model output, to get usable results
# TODO Save all results

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ def main():

# Load data
observations = lib.load_lending_club()
# observations = lib.load_lending_club(test_run=False)
print('Observation columns: {}'.format(list(observations.columns)))
print('Class balance:\n {}'.format(observations['loan_status'].value_counts()))


# Heuristic data transformations
for var in ['int_rate', 'revol_util']:

Expand All @@ -38,6 +36,7 @@ def main():
'application_type', 'disbursement_method']
text_vars = ['desc', 'purpose', 'title']

# Train /test split
train_observations, test_observations = train_test_split(observations)
train_observations = train_observations.copy()
test_observations = test_observations.copy()
Expand Down
61 changes: 61 additions & 0 deletions examples/numerical_response_stocks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import logging

from keras import Model
from keras.layers import Dense
from sklearn.model_selection import train_test_split

from keras_pandas import lib
from keras_pandas.Automater import Automater


def main():
# Load data
observations = lib.load_instanbul_stocks(as_ts=True)
print('Observation columns: {}'.format(list(observations.columns)))

# Heuristic data transformations

# Train /test split
train_observations, test_observations = train_test_split(observations)
train_observations = train_observations.copy()
test_observations = test_observations.copy()

# List out variable types
timeseries_vars = ['ise_lagged', 'ise.1_lagged', 'sp_lagged', 'dax_lagged']
numerical_vars = ['ise']

# Create and fit Automater
auto = Automater(numerical_vars=numerical_vars, timeseries_vars=timeseries_vars,
response_var='ise')
auto.fit(train_observations)

# Create and fit keras (deep learning) model.
# The auto.transform, auto.input_nub, auto.input_layers, auto.output_nub, and auto.loss are provided by
# keras-pandas, and everything else is core Keras

x = auto.input_nub
x = Dense(16)(x)
x = Dense(16, activation='relu')(x)
x = Dense(16)(x)
x = auto.output_nub(x)

model = Model(inputs=auto.input_layers, outputs=x)
model.compile(optimizer='adam', loss=auto.loss)

train_X, train_y = auto.transform(train_observations)
model.fit(train_X, train_y)

# Inverse transform model output, to get usable results
test_X, test_y = auto.transform(test_observations)
test_y_pred = model.predict(test_X)
test_observations[auto.response_var + '_pred'] = auto.inverse_transform_output(test_y_pred)
print('Predictions: {}'.format(test_observations[auto.response_var + '_pred']))

# TODO Save all results

pass


if __name__ == '__main__':
logging.getLogger().setLevel(logging.INFO)
main()
14 changes: 9 additions & 5 deletions tests/testexamples.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
from examples import numerical_lending_club, categorical_lending_club, categorical_mushrooms, categorical_titanic
from examples import numerical_response_lending_club, categorical_response_lending_club, categorical_response_mushrooms, \
categorical_response_titanic, numerical_response_stocks
from tests.testbase import TestBase
import numpy


class TestExamples(TestBase):
def test_numerical_lending_club(self):
numpy.random.seed(0)
numerical_lending_club.main()
numerical_response_lending_club.main()

def test_categorical_lending_club(self):
numpy.random.seed(0)
categorical_lending_club.main()
categorical_response_lending_club.main()

def test_categorical_mushrooms(self):
numpy.random.seed(0)
categorical_mushrooms.main()
categorical_response_mushrooms.main()

def test_categorical_titanic(self):
numpy.random.seed(0)
categorical_titanic.main()
categorical_response_titanic.main()

def test_numerical_stocks(self):
numpy.random.seed(0)
numerical_response_stocks.main()

0 comments on commit 48b3c61

Please sign in to comment.