Skip to main content

A package linking symbolic representation with sklearn for time series prediction

Project description

slearn

Build Status PyPI version License: MIT

A package linking symbolic representation with sklearn for time series prediction

Install the slearn package simply by

$ pip install slearn

Symbolic machine learning prediction

Then, import the package

>>> from slearn import symbolicML, slearn

We can predict any symbolic sequence by choosing the classifiers available in scikit-learn.

>>> string = 'aaaabbbccd'
>>> sbml = symbolicML(classifier_name="MLPClassifier", gap=3, random_seed=0, verbose=0)
>>> x, y = sbml._encoding(string)
>>> pred = sbml.forecasting(x, y, step=5, hidden_layer_sizes=(10,10), learning_rate_init=0.1)
>>> print(pred)
['d', 'b', 'a', 'b', 'b'] # the prediction

Also, you can use it by passing into parameters of dictionary form

>>> string = 'aaaabbbccd'
>>> sbml = symbolicML(classifier_name="MLPClassifier", gap=3, random_seed=0, verbose=0)
>>> x, y = sbml._encoding(string)
>>> params = {'hidden_layer_sizes':(10,10), 'activation':'relu', 'learning_rate_init':0.1}
>>> pred = sbml.forecasting(x, y, step=5, **params)
>>> print(pred)
['d', 'b', 'a', 'b', 'b'] # the prediction

But ensure that parameters are existing in the scikit-learn classifiers.

Prediction with symbolic representation

Load libraries.

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from slearn.slearn import *

time_series = pd.read_csv("Amazon.csv") # load the required dataset, here we use Amazon stock daily close price.
ts = time_series.Close.values

Set the number of symbols you would like to predict.

step = 200

You can select the available classifiers for prediction, and the parameters of the chosen classifier follow the same as the scikit-learn library. We usually deploy ABBA symbolic representation, since it achieves better forecasting against SAX.

Use Gaussian Naive Bayes method:

sl = slearn(series=ts, method='ABBA', 
            gap=10, step=step,
            tol=0.01, alpha=0.2, 
            form='numeric', classifier_name="GaussianNB",
            random_seed=1, verbose=1)
sklearn_params = {'var_smoothing':0.001}
abba_nb_pred = sl.predict(**sklearn_params)

Use neural network models method:

sl = slearn(series=ts, method='ABBA',
            gap=10, step=step,
            tol=0.01, alpha=0.2, 
            form='numeric', classifier_name="MLPClassifier",
            random_seed=1, verbose=1)
sklearn_params = {'hidden_layer_sizes':(20,80), 'learning_rate_init':0.1}
abba_nn_pred = sl.predict(**sklearn_params)

We can plot the prediction,

sns.set_theme(style="whitegrid")
plt.figure(figsize=(25, 9))
sns.set(font_scale=2, style="whitegrid")
sns.lineplot(x=np.arange(0, len(ts)), y= ts, color='c', linewidth=6, label='Time series')
sns.lineplot(x=np.arange(len(ts), len(ts)+min_len), y=abba_nb_pred[:min_len], color='tomato', linewidth=6, label='Prediction (ABBA - GaussianNB)')
sns.lineplot(x=np.arange(len(ts), len(ts)+min_len), y=abba_nn_pred[:min_len], color='darkgreen', linewidth=6, label='Prediction (ABBA - MLPClassifier)')
plt.tight_layout()
plt.tick_params(axis='both', labelsize=25)
plt.show()

original image

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

slearn-0.0.2.tar.gz (9.6 kB view details)

Uploaded Source

File details

Details for the file slearn-0.0.2.tar.gz.

File metadata

  • Download URL: slearn-0.0.2.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.2

File hashes

Hashes for slearn-0.0.2.tar.gz
Algorithm Hash digest
SHA256 42be4402a102bc13ce1ebb3f98aeb764412df14b9b188adc1cab093aff991473
MD5 79f69f003e98ea7d00ea1eddf0ed9494
BLAKE2b-256 e610f8c9c1c7f02035f7078413bc93a214fbcda7236c308b2410b5ae5e15a44f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page