Skip to main content

spinesTS, a powerful timeseries toolsets.

Project description

spinesTS

Time Series forecasting toolsets

Install

pip install spinesTS

spinesTS Modules

  • base: Model base class
  • data: Built-in datasets and data wrapper classes
  • feature_generator: Feature generation functions
  • metrics: Model performance measurement function
  • ml_model: Machine learning models
  • nn: neural network models
  • pipeline: Model fitting and prediction pipeline
  • plotting: Visualization of model prediction results
  • preprocessing: data preprocessing
  • utils: Tool functions set
  • layers: Neural network layer

Tutorials

Getting started

# simple demo to predict Electric data
from sklearn.preprocessing import StandardScaler
from lightgbm import LGBMRegressor
import matplotlib.pyplot as plt

from spinesTS.pipeline import Pipeline
from spinesTS.data import LoadElectricDataSets
from spinesTS.ml_model import MultiOutputRegressor
from spinesTS.preprocessing import split_series
from spinesTS.plotting import plot2d


# load data
df = LoadElectricDataSets()

# split data
x_train, x_test, y_train, y_test = split_series(
    x_seq=df['value'], 
    y_seq=df['value'],  # The sequence of parameter y_seq is cut based on parameter x_seq
    # sliding window size, every 30 before days to predict after days
    window_size=30, 
    # predict after 30 days
    pred_steps=30, 
    train_size=0.8
)

print(f"x_train shape is {x_train.shape}, "
      f"x_test shape is {x_test.shape}," 
      f"y_train shape is {y_train.shape},"
      f"y_test shape is {y_test.shape}")

# Assemble the model using Pipeline class
model = Pipeline([
    ('sc', StandardScaler()),
    ('model', MultiOutputRegressor(LGBMRegressor(random_state=2022)))
])
print("Model successfully initialization...")

# fitting model
model.fit(x_train, y_train, eval_set=(x_test, y_test), verbose=0)
print(f"r2_score is {model.score(x_test, y_test)}")

# plot the predicted results
fig = plot2d(y_test, model.predict(x_test), figsize=(20, 10), 
       eval_slices='[:30]', labels=['y_test', 'y_pred'])
plt.show()
[output]:
x_train shape is (270, 30), x_test shape is (68, 30),y_train shape is (270, 30),y_test shape is (68, 30)
Model successfully initialization...
r2_score is 0.8186046606725977

model prediction image

Using nn module

StackingRNN

import matplotlib.pyplot as plt

from spinesTS.data import LoadElectricDataSets
from spinesTS.preprocessing import split_series
from spinesTS.plotting import plot2d
from spinesTS.nn import StackingRNN
from spinesTS.metrics import r2_score, mean_absolute_error, mean_absolute_percentage_error


# load data
df = LoadElectricDataSets()

# split data
x_train, x_test, y_train, y_test = split_series(
    x_seq=df['value'], 
    y_seq=df['value'],
    # sliding window size, every 128 before days to predict after days
    window_size=128, 
    # predict after 24 days goods incoming
    pred_steps=24, 
    train_size=0.8
)

print(f"x_train shape is {x_train.shape}, "
      f"x_test shape is {x_test.shape}," 
      f"y_train shape is {y_train.shape},"
      f"y_test shape is {y_test.shape}")

# model initialization
model = StackingRNN(in_features=128, out_features=24, 
                    random_seed=42, loss_fn='mae', 
                    learning_rate=0.001, dropout=0.1, diff_n=1, 
                    stack_num=2, bidirectional=True, device='cpu')

model.fit(x_train, y_train, eval_set=(x_test[:-2], y_test[:-2]), batch_size=32,
             min_delta=0, patience=100, epochs=3000, verbose=False, lr_scheduler=None)
y_pred_cs = model.predict(x_test[-2:])
print(f"r2: {r2_score(y_test[-2:].T, y_pred_cs.T)}")
print(f"mae: {mean_absolute_error(y_test[-2:], y_pred_cs)}")
print(f"mape: {mean_absolute_percentage_error(y_test[-2:], y_pred_cs)}")
a = plot2d(y_test[-2:], y_pred_cs, eval_slices='[-1]', labels=['y_test', 'y_pred'], figsize=(20, 6))
plt.show()

GAUNet

import matplotlib.pyplot as plt

from spinesTS.data import LoadElectricDataSets
from spinesTS.preprocessing import split_series
from spinesTS.plotting import plot2d
from spinesTS.nn import GAUNet
from spinesTS.metrics import r2_score, mean_absolute_error, mean_absolute_percentage_error


# load data
df = LoadElectricDataSets()

# split data
x_train, x_test, y_train, y_test = split_series(
    x_seq=df['value'], 
    y_seq=df['value'],
    # sliding window size, every 128 before days to predict after days
    window_size=128, 
    # predict after 24 days 
    pred_steps=24, 
    train_size=0.8
)

print(f"x_train shape is {x_train.shape}, "
      f"x_test shape is {x_test.shape}," 
      f"y_train shape is {y_train.shape},"
      f"y_test shape is {y_test.shape}")

# model initialization
model = GAUNet(in_features=128, out_features=24, 
               random_seed=42, flip_features=False, 
               learning_rate=0.001, level=5, device='cpu')

model.fit(x_train, y_train, eval_set=(x_test[:-2], y_test[:-2]), batch_size=32,
             min_delta=0, patience=100, epochs=3000, verbose=False, lr_scheduler='ReduceLROnPlateau')
y_pred_cs = model.predict(x_test[-2:])
print(f"r2: {r2_score(y_test[-2:].T, y_pred_cs.T)}")
print(f"mae: {mean_absolute_error(y_test[-2:], y_pred_cs)}")
print(f"mape: {mean_absolute_percentage_error(y_test[-2:], y_pred_cs)}")
a = plot2d(y_test[-2:], y_pred_cs, eval_slices='[-1]', labels=['y_test', 'y_pred'], figsize=(20, 6))
plt.show()

Time2VecNet

import matplotlib.pyplot as plt

from spinesTS.data import LoadElectricDataSets
from spinesTS.preprocessing import split_series
from spinesTS.plotting import plot2d
from spinesTS.nn import Time2VecNet
from spinesTS.metrics import r2_score, mean_absolute_error, mean_absolute_percentage_error


# load data
df = LoadElectricDataSets()

# split data
x_train, x_test, y_train, y_test = split_series(
    x_seq=df['value'], 
    y_seq=df['value'],
    # sliding window size, every 128 before days to predict after days
    window_size=128, 
    # predict after 24 days 
    pred_steps=24, 
    train_size=0.8
)

print(f"x_train shape is {x_train.shape}, "
      f"x_test shape is {x_test.shape}," 
      f"y_train shape is {y_train.shape},"
      f"y_test shape is {y_test.shape}")

# model initialization
model = Time2VecNet(in_features=128, out_features=24, 
               random_seed=42, flip_features=False, 
               learning_rate=0.001, device='cpu')

model.fit(x_train, y_train, eval_set=(x_test[:-2], y_test[:-2]), batch_size=32,
             min_delta=0, patience=100, epochs=3000, verbose=False, lr_scheduler='CosineAnnealingLR')
y_pred_cs = model.predict(x_test[-2:])
print(f"r2: {r2_score(y_test[-2:].T, y_pred_cs.T)}")
print(f"mae: {mean_absolute_error(y_test[-2:], y_pred_cs)}")
print(f"mape: {mean_absolute_percentage_error(y_test[-2:], y_pred_cs)}")
a = plot2d(y_test[-2:], y_pred_cs, eval_slices='[-1]', labels=['y_test', 'y_pred'], figsize=(20, 6))
plt.show()

Using ml_model module

MultiStepRegressor

from lightgbm import LGBMRegressor
import matplotlib.pyplot as plt

from spinesTS.data import LoadElectricDataSets
from spinesTS.ml_model import MultiStepRegressor
from spinesTS.preprocessing import split_series
from spinesTS.plotting import plot2d


# load data
df = LoadElectricDataSets()

# split data
x_train, x_test, y_train, y_test = split_series(
    df['value'], 
    df['value'],
    # sliding window size, every 30 before days to predict after days
    window_size=30, 
    # predict after 30 days 
    pred_steps=30, 
    train_size=0.8
)

print(f"x_train shape is {x_train.shape}, "
      f"x_test shape is {x_test.shape}," 
      f"y_train shape is {y_train.shape},"
      f"y_test shape is {y_test.shape}")

# model initialization
model = MultiStepRegressor(LGBMRegressor(random_state=2022))
print("Model successfully initialization...")

# fitting model
model.fit(x_train, y_train, eval_set=(x_test, y_test), verbose=0)
print(f"r2_score is {model.score(x_test, y_test)}")

# plot the predicted results
fig = plot2d(y_test, model.predict(x_test), figsize=(20, 10), 
       eval_slices='[:30]', labels=['y_test', 'y_pred'])
plt.show()

MultiOutputRegressor

from lightgbm import LGBMRegressor
import matplotlib.pyplot as plt

from spinesTS.data import LoadElectricDataSets
from spinesTS.ml_model import MultiOutputRegressor
from spinesTS.preprocessing import split_series
from spinesTS.plotting import plot2d


# load data
df = LoadElectricDataSets()

# split data
x_train, x_test, y_train, y_test = split_series(
    df['value'], 
    df['value'],
    # sliding window size, every 30 before days to predict after days
    window_size=30, 
    # predict after 30 days 
    pred_steps=30, 
    train_size=0.8
)

print(f"x_train shape is {x_train.shape}, "
      f"x_test shape is {x_test.shape}," 
      f"y_train shape is {y_train.shape},"
      f"y_test shape is {y_test.shape}")

# model initialization
model = MultiOutputRegressor(LGBMRegressor(random_state=2022))
print("Model successfully initialization...")

# fitting model
model.fit(x_train, y_train, eval_set=(x_test, y_test), verbose=0)
print(f"r2_score is {model.score(x_test, y_test)}")

# plot the predicted results
fig = plot2d(y_test, model.predict(x_test), figsize=(20, 10), 
       eval_slices='[:30]', labels=['y_test', 'y_pred'])
plt.show()

WideGBRT

from lightgbm import LGBMRegressor
import matplotlib.pyplot as plt

from spinesTS.data import LoadElectricDataSets
from spinesTS.ml_model import GBRTPreprocessing, WideGBRT
from spinesTS.plotting import plot2d


# load data
df = LoadElectricDataSets()

# split data and generate new features
gbrt_processor = GBRTPreprocessing(in_features=128, out_features=30, 
                                   target_col='value', train_size=0.8, date_col='date',
                                   differential_n=1  # The order of data differentiation.
                                   )
gbrt_processor.fit(df)

x_train, x_test, y_train, y_test = gbrt_processor.transform(df)

print(f"x_train shape is {x_train.shape}, "
      f"x_test shape is {x_test.shape}," 
      f"y_train shape is {y_train.shape},"
      f"y_test shape is {y_test.shape}")

# model initialization
model = WideGBRT(model=LGBMRegressor(random_state=2022))
print("Model successfully initialization...")

# fitting model
model.fit(x_train, y_train, eval_set=(x_test, y_test), verbose=0)
print(f"r2_score is {model.score(x_test, y_test)}")

# plot the predicted results
fig = plot2d(y_test, model.predict(x_test), figsize=(20, 10), 
       eval_slices='[:30]', labels=['y_test', 'y_pred'])
plt.show()

Using Data module

from spinesTS.data import *
series_data = BuiltInSeriesData(print_file_list=True)
+---+----------------------+----------------------------------------------+
|   | ds name              | columns                                      |
+---+----------------------+----------------------------------------------+
| 0 | ETTh1                | date, HUFL, HULL, MUFL, MULL, LUFL, LULL, OT |
| 1 | ETTh2                | date, HUFL, HULL, MUFL, MULL, LUFL, LULL, OT |
| 2 | ETTm1                | date, HUFL, HULL, MUFL, MULL, LUFL, LULL, OT |
| 3 | ETTm2                | date, HUFL, HULL, MUFL, MULL, LUFL, LULL, OT |
| 4 | Electric_Production  | date, value                                  |
| 5 | Messages_Sent        | date, ta, tb, tc                             |
| 6 | Messages_Sent_Hour   | date, hour, ta, tb, tc                       |
| 7 | Supermarket_Incoming | date, goods_cnt                              |
| 8 | Web_Sales            | date, type_a, type_b, sales_cnt              |
+---+----------------------+----------------------------------------------+
# select one dataset
df_a = series_data['ETTh1']  # series_data[0], it works, too
print(type(df_a))  # <class 'spinesTS.data._data_base.DataTS'>

# Because DataTS inherit from pandas DataFrame, it has all the functionality of pandas DataFrame
df_a.head() ,df_a.tail(), df_a.shape

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

spinesTS-0.3.16.tar.gz (8.8 MB view details)

Uploaded Source

Built Distribution

spinesTS-0.3.16-py3-none-any.whl (8.9 MB view details)

Uploaded Python 3

File details

Details for the file spinesTS-0.3.16.tar.gz.

File metadata

  • Download URL: spinesTS-0.3.16.tar.gz
  • Upload date:
  • Size: 8.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for spinesTS-0.3.16.tar.gz
Algorithm Hash digest
SHA256 8849243b5e01f18b34dcc138611be335a3aa56316200321eea00bde88ca2d649
MD5 c860c73ea97dfa88272cf65733d49cf4
BLAKE2b-256 d03c7fa62abe401e51f99d07bd3c0fec88bba7f941f8035a6d676f4b037476cb

See more details on using hashes here.

File details

Details for the file spinesTS-0.3.16-py3-none-any.whl.

File metadata

  • Download URL: spinesTS-0.3.16-py3-none-any.whl
  • Upload date:
  • Size: 8.9 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for spinesTS-0.3.16-py3-none-any.whl
Algorithm Hash digest
SHA256 dfb153c0b467ef2e7386cb503a67c710089bbf8dba87c31cafbb47f29ae9ed05
MD5 3c057557c548d2cffee9ada19b900cbc
BLAKE2b-256 ec889b0776c862eaf9ff8fa36221106b4ae98cc4a311cb70ca5c47322c92a3fd

See more details on using hashes here.

Supported by

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