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

1.0 MiB

Open In Colab

Exploring the Universe of Discourse Partitioners

Environment Setup

Library install/update

In [1]:
# !pip install -U --force-reinstall --no-deps -e git+https://git.athene.tech/sam/pyFTS.git#egg=pyFTS

External libraries import

In [2]:
import warnings
warnings.filterwarnings('ignore')


import matplotlib.pylab as plt

%pylab inline
%pylab is deprecated, use %matplotlib inline and import the required libraries.
Populating the interactive namespace from numpy and matplotlib

Common pyFTS imports

In [3]:
from pyFTS.partitioners import KMeans, Grid, FCM, Huarng, Entropy, Util as pUtil
from pyFTS.common import Membership as mf
from pyFTS.benchmarks import benchmarks as bchmk
from pyFTS.data import Enrollments
2024-08-08 12:33:54.147886: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:485] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
2024-08-08 12:33:54.168752: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:8454] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
2024-08-08 12:33:54.175031: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1452] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
2024-08-08 12:33:54.191648: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX512F FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-08-08 12:33:55.150180: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT

Common data transformations

In [4]:
from pyFTS.common import Transformations

tdiff = Transformations.Differential(1)

Dataset

Data Loading

In [5]:
from pyFTS.data import TAIEX

dataset = TAIEX.get_data()
dataset_diff = tdiff.apply(dataset)

Visualization

In [6]:
fig, ax = plt.subplots(nrows=2, ncols=1, figsize=[10,5])
ax[0].plot(dataset)
ax[1].plot(dataset_diff)
Out[6]:
[<matplotlib.lines.Line2D at 0x7048949da350>]

Exploring partitioning schemes

Same method with different membership functions

In [7]:
part = pUtil.explore_partitioners(dataset, 20, methods=[Grid.GridPartitioner], 
                                  mf=[mf.trimf, mf.trapmf, mf.gaussmf])
In [8]:
# for p in part:
#     print(p)

Same mathod with different membership functions and transformation

In [9]:
part = pUtil.explore_partitioners(dataset, 10, methods=[Grid.GridPartitioner], 
                                  mf=[mf.trimf, mf.trapmf, mf.gaussmf], transformation=tdiff)
In [10]:
# for p in part:
#     print(p)

Several different mathods

In [13]:
# part = pUtil.explore_partitioners(dataset, 10, methods=[Huarng.HuarngPartitioner], 
#                                   mf=[mf.trimf], transformation=tdiff)
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
Cell In[13], line 1
----> 1 part = pUtil.explore_partitioners(dataset, 10, methods=[Huarng.HuarngPartitioner], 
      2                                   mf=[mf.trimf], transformation=tdiff)

File ~/Projects/Disser/pyFTSex/.venv/src/pyfts/pyFTS/partitioners/Util.py:88, in explore_partitioners(data, npart, methods, mf, transformation, size, save, file)
     86 for p in methods:
     87     for m in mf:
---> 88         obj = p(data=data, npart=npart, func=m, transformation=transformation)
     89         obj.name = obj.name  + " - " + obj.membership_function.__name__
     90         objs.append(obj)

File ~/Projects/Disser/pyFTSex/.venv/src/pyfts/pyFTS/partitioners/Huarng.py:17, in HuarngPartitioner.__init__(self, **kwargs)
     16 def __init__(self, **kwargs):
---> 17     super(HuarngPartitioner, self).__init__(name="Huarng", **kwargs)

File ~/Projects/Disser/pyFTSex/.venv/src/pyfts/pyFTS/partitioners/partitioner.py:76, in Partitioner.__init__(self, **kwargs)
     73 _max = np.nanmax(ndata)
     74 self.max = float(_max * (1 + self.upper_margin) if _max > 0 else _max * (1 - self.upper_margin))
---> 76 self.sets = self.build(ndata)
     78 self.partitions = len(self.sets)
     80 if self.ordered_sets is None and self.setnames is not None:

File ~/Projects/Disser/pyFTSex/.venv/src/pyfts/pyFTS/partitioners/Huarng.py:40, in HuarngPartitioner.build(self, data)
     38 npart = math.ceil(dlen / base)
     39 partition = math.ceil(self.min)
---> 40 for c in range(npart):
     41     _name = self.get_name(c)
     42     if self.membership_function == Membership.trimf:

File ~/Projects/Disser/pyFTSex/.venv/src/pyfts/pyFTS/partitioners/Huarng.py:40, in HuarngPartitioner.build(self, data)
     38 npart = math.ceil(dlen / base)
     39 partition = math.ceil(self.min)
---> 40 for c in range(npart):
     41     _name = self.get_name(c)
     42     if self.membership_function == Membership.trimf:

File _pydevd_bundle/pydevd_cython.pyx:1457, in _pydevd_bundle.pydevd_cython.SafeCallWrapper.__call__()

File _pydevd_bundle/pydevd_cython.pyx:701, in _pydevd_bundle.pydevd_cython.PyDBFrame.trace_dispatch()

File _pydevd_bundle/pydevd_cython.pyx:1395, in _pydevd_bundle.pydevd_cython.PyDBFrame.trace_dispatch()

File _pydevd_bundle/pydevd_cython.pyx:1344, in _pydevd_bundle.pydevd_cython.PyDBFrame.trace_dispatch()

File _pydevd_bundle/pydevd_cython.pyx:312, in _pydevd_bundle.pydevd_cython.PyDBFrame.do_wait_suspend()

File ~/Projects/Disser/pyFTSex/.venv/lib/python3.11/site-packages/debugpy/_vendored/pydevd/pydevd.py:2070, in PyDB.do_wait_suspend(self, thread, frame, event, arg, exception_type)
   2067             from_this_thread.append(frame_custom_thread_id)
   2069     with self._threads_suspended_single_notification.notify_thread_suspended(thread_id, thread, stop_reason):
-> 2070         keep_suspended = self._do_wait_suspend(thread, frame, event, arg, suspend_type, from_this_thread, frames_tracker)
   2072 frames_list = None
   2074 if keep_suspended:
   2075     # This means that we should pause again after a set next statement.

File ~/Projects/Disser/pyFTSex/.venv/lib/python3.11/site-packages/debugpy/_vendored/pydevd/pydevd.py:2106, in PyDB._do_wait_suspend(self, thread, frame, event, arg, suspend_type, from_this_thread, frames_tracker)
   2103         self._call_input_hook()
   2105     self.process_internal_commands()
-> 2106     time.sleep(0.01)
   2108 self.cancel_async_evaluation(get_current_thread_id(thread), str(id(frame)))
   2110 # process any stepping instructions

KeyboardInterrupt: 
In [14]:
part = pUtil.explore_partitioners(dataset, 10, methods=[Grid.GridPartitioner, 
                                                        KMeans.KMeansPartitioner, 
                                                        FCM.FCMPartitioner,
                                                        Entropy.EntropyPartitioner], 
                                  mf=[mf.trimf])
In [15]:
for p in part:
    print(p)
Grid - trimf:
A0: trimf([2223.737111111111, 3126.4335, 4029.129888888889])
A1: trimf([3126.4335, 4029.129888888889, 4931.826277777778])
A2: trimf([4029.129888888889, 4931.826277777778, 5834.522666666668])
A3: trimf([4931.826277777778, 5834.522666666668, 6737.219055555557])
A4: trimf([5834.522666666668, 6737.219055555556, 7639.915444444445])
A5: trimf([6737.219055555557, 7639.915444444446, 8542.611833333334])
A6: trimf([7639.915444444445, 8542.611833333334, 9445.308222222224])
A7: trimf([8542.611833333332, 9445.308222222222, 10348.004611111111])
A8: trimf([9445.308222222222, 10348.004611111111, 11250.701000000001])
A9: trimf([10348.004611111111, 11250.701000000001, 12153.39738888889])

KMeans - trimf:
A1: trimf([3126.434, 4446.984, 5115.59])
A2: trimf([4446.984, 5115.59, 5866.252])
A3: trimf([5115.59, 5866.252, 6468.86])
A4: trimf([5866.252, 6468.86, 7071.561])
A5: trimf([6468.86, 7071.561, 7622.299])
A6: trimf([7071.561, 7622.299, 8067.048])
A7: trimf([7622.299, 8067.048, 8537.641])
A8: trimf([8067.048, 8537.641, 9032.461])
A9: trimf([8537.641, 9032.461, 9632.606])
A10: trimf([9032.461, 9632.606, 11250.701])

FCM - trimf:
A1: trimf([3126.434, 4889.001, 6010.956])
A2: trimf([4889.001, 6010.956, 6169.123])
A3: trimf([6010.956, 6169.123, 6397.746])
A4: trimf([6169.123, 6397.746, 6990.173])
A5: trimf([6397.746, 6990.173, 7181.347])
A6: trimf([6990.173, 7181.347, 7567.393])
A7: trimf([7181.347, 7567.393, 7818.631])
A8: trimf([7567.393, 7818.631, 8336.612])
A9: trimf([7818.631, 8336.612, 9318.522])
A10: trimf([8336.612, 9318.522, 11250.701])

Entropy - trimf:
A0: trimf([3126.4335, 5107.275, 5918.92])
A1: trimf([5107.275, 5918.92, 6495.625])
A2: trimf([5918.92, 6495.625, 7199.905])
A3: trimf([6495.625, 7199.905, 7754.915])
A4: trimf([7199.905, 7754.915, 8233.685])
A5: trimf([7754.915, 8233.685, 8852.515])
A6: trimf([8233.685, 8852.515, 11250.701000000001])