pyFTSex/tutorial/pyFTS/developer/Severiano_et_al_HighOrderFTS.ipynb
2024-08-15 12:15:32 +04:00

221 KiB
Raw Blame History

High Order Fuzzy Time Series

Severiano, S. A. Jr; Silva, P. C. L.; Sadaei, H. J.; Guimarães, F. G. Very Short-term Solar Forecasting using Fuzzy Time Series. 2017 IEEE International Conference on Fuzzy Systems. DOI10.1109/FUZZ-IEEE.2017.8015732

Installing Dependencies

In [3]:
!pip install -U git+https://github.com/petroniocandido/pyFTS/
!pip install dill
Collecting git+https://github.com/petroniocandido/pyFTS/
  Cloning https://github.com/petroniocandido/pyFTS/ to /tmp/pip-req-build-vcsdyoq1
Building wheels for collected packages: pyFTS
  Running setup.py bdist_wheel for pyFTS ... - \ done
  Stored in directory: /tmp/pip-ephem-wheel-cache-es_qve_2/wheels/e3/3e/f8/37b9ff061c854fc9e0fa76a2b7d69265bca326b1ab452b0f7b
Successfully built pyFTS
Installing collected packages: pyFTS
Successfully installed pyFTS-1.2.2
In [5]:

Collecting dill
  Downloading https://files.pythonhosted.org/packages/91/a0/19d4d31dee064fc553ae01263b5c55e7fb93daff03a69debbedee647c5a0/dill-0.2.7.1.tar.gz (64kB)
    100% |████████████████████████████████| 71kB 2.5MB/s 
Building wheels for collected packages: dill
  Running setup.py bdist_wheel for dill ... - done
  Stored in directory: /content/.cache/pip/wheels/99/c4/ed/1b64d2d5809e60d5a3685530432f6159d6a9959739facb61f2
Successfully built dill
Installing collected packages: dill
Successfully installed dill-0.2.7.1

Common Imports

In [6]:
import matplotlib.pylab as plt
from pyFTS.benchmarks import benchmarks as bchmk
from pyFTS.models import hofts

from pyFTS.common import Transformations
tdiff = Transformations.Differential(1)

%matplotlib notebook
/usr/local/lib/python3.6/dist-packages/statsmodels/compat/pandas.py:56: FutureWarning: The pandas.core.datetools module is deprecated and will be removed in a future version. Please use the pandas.tseries module instead.
  from pandas.core import datetools

Data Loading

In [7]:
from pyFTS.data import Enrollments

enrollments = Enrollments.get_data()

Exploring the partitioning effects on original data

In [8]:
tmp = bchmk.simpleSearch_RMSE(enrollments, enrollments, hofts.HighOrderFTS, range(1,20), [1, 2, 3], tam=[10, 5])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-fd17c65014be> in <module>()
----> 1 tmp = bchmk.simpleSearch_RMSE(enrollments, enrollments, hofts.HighOrderFTS, range(1,20), [1, 2, 3], tam=[10, 5])

/usr/local/lib/python3.6/dist-packages/pyFTS/benchmarks/benchmarks.py in simpleSearch_RMSE(train, test, model, partitions, orders, save, file, tam, plotforecasts, elev, azim, intervals, parameters, partitioner, transformation, indexer)
   1021         sets = partitioner(data=train, npart=p, transformation=transformation).sets
   1022         for oc, o in enumerate(orders, start=0):
-> 1023             fts = model("q = " + str(p) + " n = " + str(o))
   1024             fts.append_transformation(transformation)
   1025             fts.train(train, sets=sets, order=o, parameters=parameters)

TypeError: __init__() takes 1 positional argument but 2 were given

Exploring the partitioning effects on transformed data

In [0]:
tmp = bchmk.simpleSearch_RMSE(enrollments, enrollments, hofts.HighOrderFTS, range(1,20), [1, 2, 3], 
                              transformation=tdiff, tam=[10, 5])

Comparing the partitioning schemas

In [9]:
from pyFTS.partitioners import Grid, Util as pUtil

fuzzy_sets = Grid.GridPartitioner(data=enrollments, npart=12)
fuzzy_sets2 = Grid.GridPartitioner(data=enrollments, npart=5, transformation=tdiff)

pUtil.plot_partitioners(enrollments, [fuzzy_sets,fuzzy_sets2])

Fitting a model on original data

In [11]:
model1 = hofts.HighOrderFTS(partitioner=fuzzy_sets)
model1.fit(enrollments, order=3)

print(model1)
High Order FTS:
A1,A2,A2 -> A3,A4
A1,A2,A3 -> A3,A4
A1,A3,A2 -> A3,A4
A1,A3,A3 -> A3,A4
A10,A10,A10 -> A8,A9
A10,A10,A9 -> A8,A9
A10,A9,A10 -> A8,A9
A10,A9,A9 -> A8,A9
A2,A2,A2 -> A3,A4
A2,A2,A3 -> A3,A4,A5
A2,A2,A4 -> A4,A5
A2,A3,A2 -> A3,A4
A2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-> A10,A9
A6,A9,A10 -> A10,A9
A6,A9,A9 -> A10,A9
A7,A5,A4 -> A4,A5
A7,A5,A5 -> A4,A5
A7,A6,A4 -> A4,A5
A7,A6,A5 -> A4,A5
A7,A6,A6 -> A4,A5
A7,A7,A5 -> A4,A5
A7,A7,A6 -> A4,A5
A7,A8,A10 -> A10,A9
A7,A8,A9 -> A10,A9
A7,A9,A10 -> A10,A9
A7,A9,A9 -> A10,A9
A8,A10,A10 -> A10,A9
A8,A10,A9 -> A10,A9
A8,A9,A10 -> A10,A9
A8,A9,A9 -> A10,A9
A9,A10,A10 -> A10,A8,A9
A9,A10,A9 -> A10,A8,A9
A9,A9,A10 -> A10,A8,A9
A9,A9,A9 -> A10,A8,A9

Fitting a model on transformed data

In [0]:
model2 = hofts.HighOrderFTS("FTS Diff", partitioner=fuzzy_sets2)
model2.append_transformation(tdiff)
model2.fit(enrollments, order=3)

print(model2)
[ 17:43:16] Start training
[ 17:43:16] Finish training
High Order FTS:
A0,A1,A2 -> A2,A3
A0,A1,A3 -> A2,A3
A0,A2,A2 -> A2,A3
A0,A2,A3 -> A2,A3,A4
A0,A2,A4 -> A4
A0,A3,A3 -> A4
A0,A3,A4 -> A4
A1,A0,A1 -> A2,A3
A1,A0,A2 -> A2,A3
A1,A1,A0 -> A1,A2
A1,A1,A1 -> A1,A2,A3
A1,A1,A2 -> A2,A3
A1,A1,A3 -> A2,A3
A1,A1,A4 -> A2,A3
A1,A2,A0 -> A1,A2
A1,A2,A1 -> A1,A2
A1,A2,A2 -> A1,A2,A3,A4
A1,A2,A3 -> A2,A3,A4
A1,A2,A4 -> A2,A3,A4
A1,A3,A1 -> A1,A2
A1,A3,A2 -> A1,A2,A3,A4
A1,A3,A3 -> A2,A3,A4
A1,A3,A4 -> A4
A1,A4,A2 -> A2,A3
A1,A4,A3 -> A2,A3
A2,A0,A1 -> A2,A3
A2,A0,A2 -> A2,A3,A4
A2,A0,A3 -> A3,A4
A2,A1,A0 -> A1,A2
A2,A1,A1 -> A0,A1,A2,A3,A4
A2,A1,A2 -> A0,A1,A2,A3,A4
A2,A1,A3 -> A1,A2,A3,A4
A2,A1,A4 -> A2,A3
A2,A2,A0 -> A1,A2,A3
A2,A2,A1 -> A0,A1,A2,A3,A4
A2,A2,A2 -> A0,A1,A2,A3,A4
A2,A2,A3 -> A1,A2,A3,A4
A2,A2,A4 -> A2,A3
A2,A3,A0 -> A2,A3
A2,A3,A1 -> A1,A2,A3
A2,A3,A2 -> A1,A2,A3,A4
A2,A3,A3 -> A1,A2,A3,A4
A2,A3,A4 -> A2,A3
A2,A4,A2 -> A1,A2,A3
A2,A4,A3 -> A1,A2,A3
A2,A4,A4 -> A2,A3
A3,A0,A2 -> A3,A4
A3,A0,A3 -> A3,A4
A3,A1,A1 -> A0,A1,A3,A4
A3,A1,A2 -> A0,A1,A2,A3,A4
A3,A1,A3 -> A1,A2,A3,A4
A3,A2,A0 -> A2,A3
A3,A2,A1 -> A0,A1,A2,A3,A4
A3,A2,A2 -> A0,A1,A2,A3,A4
A3,A2,A3 -> A0,A1,A2
A3,A3,A0 -> A2,A3
A3,A3,A1 -> A1,A2,A3
A3,A3,A2 -> A0,A1,A2
A3,A3,A3 -> A0,A1,A2
A3,A3,A4 -> A2,A3
A3,A4,A2 -> A1,A2,A3,A4
A3,A4,A3 -> A1,A2,A3,A4
A3,A4,A4 -> A2,A3
A4,A2,A1 -> A1,A2
A4,A2,A2 -> A0,A1,A2
A4,A2,A3 -> A0,A1,A2,A3
A4,A2,A4 -> A2,A3
A4,A3,A1 -> A1,A2
A4,A3,A2 -> A0,A1,A2
A4,A3,A3 -> A0,A1,A2,A3
A4,A3,A4 -> A2,A3
A4,A4,A2 -> A3,A4
A4,A4,A3 -> A3,A4

Using the models

In [0]:
model1.predict(enrollments)
Out[0]:
[14923.233333333337,
 15319.950000000004,
 15319.950000000004,
 16113.383333333339,
 16113.383333333339,
 17700.250000000007,
 17303.53333333334,
 16113.383333333339,
 15319.950000000004,
 15319.950000000004,
 15319.950000000004,
 16113.383333333339,
 16113.383333333339,
 17700.250000000007,
 17303.53333333334,
 19287.116666666676,
 19287.116666666676,
 18890.40000000001,
 18890.40000000001,
 18890.40000000001]
In [0]:
model2.predict(enrollments)
Out[0]:
[12941.02,
 14511.2,
 15028.14,
 15126.2,
 15171.14,
 15182.08,
 16375.14,
 16734.2,
 16944.38,
 15248.2,
 15806.32,
 14960.2,
 14978.2,
 15058.02,
 16427.14,
 17718.14,
 18538.14,
 19390.260000000002,
 19152.2,
 18691.2]

Comparing the models

In [0]:
bchmk.plot_compared_series(enrollments, [model1, model2], bchmk.colors, intervals=False)
In [0]:
bchmk.print_point_statistics(enrollments, [model1, model2])
Model		& Order     & RMSE		& SMAPE      & Theil's U		\\ 
HOFTSFTS		& 3		& 466.65		& 1.06		& 0.76	\\ 
HOFTSFTS Diff		& 3		& 1029.5		& 2.66		& 1.68	\\ 

Residual Analysis

In [0]:
from pyFTS.benchmarks import ResidualAnalysis as ra

ra.plot_residuals(enrollments, [model1, model2])
In [0]:

In [0]: