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
Support Classifiers Parameter call
Multi-layer Perceptron 'MLPClassifier'
K-Nearest Neighbors 'KNeighborsClassifier'
Gaussian Naive Bayes 'GaussianNB'
Decision Tree 'DecisionTreeClassifier'
Support Vector Classification 'SVC'
Radial-basis Function Kernel 'RBF'
Logistic Regression 'LogisticRegression'
Quadratic Discriminant Analysis 'QuadraticDiscriminantAnalysis'
AdaBoost classifier 'AdaBoostClassifier'
Random Forest 'RandomForestClassifier'
LightGBM 'LGBM'

Symbolic machine learning prediction

Import the package

>>> from slearn import symbolicML

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

The parameters for the chosen classifier follow the same as the scikit-learn library, so just 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 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 = 50

You can select the available classifiers and symbolic representation method (currently we support SAX and ABBA) for prediction. Similarly, 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=3, 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=3, 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.3.tar.gz (10.6 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: slearn-0.0.3.tar.gz
  • Upload date:
  • Size: 10.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.3.tar.gz
Algorithm Hash digest
SHA256 abcc7ad299131ecf85aa243e9c22661086e058747140bbb30dc5fbfcd278c7cb
MD5 3be29bab884f7cbf6d228bb993334fbb
BLAKE2b-256 5fe9718fb8428e4bea10594430d4846b87d7c0fb50cb8a88568f7427375495f0

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