From 6aa2a6c92e4dde97250d4ca0fbb805bcb8204154 Mon Sep 17 00:00:00 2001 From: matheus_cascalho Date: Mon, 16 Nov 2020 22:37:04 -0300 Subject: [PATCH] treinamento da rede --- pyFTS/models/multivariate/som.py | 63 ++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 pyFTS/models/multivariate/som.py diff --git a/pyFTS/models/multivariate/som.py b/pyFTS/models/multivariate/som.py new file mode 100644 index 0000000..f1faf67 --- /dev/null +++ b/pyFTS/models/multivariate/som.py @@ -0,0 +1,63 @@ +""" +Kohonen Self Organizing Maps for Fuzzy Time Series +""" +import pandas as pd +import SimpSOM as sps +from pyFTS.models.multivariate import wmvfts +from typing import Tuple + + +class SOMFTS: + def __init__(self, + grid_dimension: Tuple, + **kwargs): + # SOM attributes + self.net: sps.somNet = None + self.data: pd.DataFrame = None + self.grid_dimension: Tuple = grid_dimension + self.pbc = kwargs.get('PBC', True) + + # fts attributes + self.fts_method = kwargs.get('fts_method', wmvfts.WeightedMVFTS) + self.order = kwargs.get('order', 2) + self.is_trained = False + + # debug attributes + self.name = 'Kohonen Self Organizing Maps FTS' + self.shortname = 'SOM-FTS' + + def __repr__(self): + status = "is trained" if self.is_trained else "not trained" + return f'{self.name}-{status}' + + def __str__(self): + return self.name + + def __del__(self): + del self.net + + def train(self, + data: pd.DataFrame, + percentage_train: float = .7, + leaning_rate: float = 0.01, + epochs: int = 10000): + self.data = data + limit = len(self.data) * percentage_train + train = data[:limit] + x, y = self.grid_dimension + self.net = sps.somNet(x, y, train, self.pbc) + self.net.train(startLearnRate=leaning_rate, + epochs=epochs) + + def save_net(self, + filename: str = "SomNet trained"): + self.net.save(filename) + + def show_grid(self, + graph_type: str = 'nodes_graph', + **kwargs): + if graph_type == 'nodes_graph': + colnum = kwargs.get('colnum', 0) + self.net.nodes_graph(colnum=colnum) + else: + self.net.diff_graph() \ No newline at end of file