Compare commits

...

16 Commits

15 changed files with 98 additions and 35 deletions

57
.gitignore vendored Normal file
View File

@ -0,0 +1,57 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
# C extensions
*.so
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
# Translations
*.mo
*.pot
# Django stuff:
*.log
# Sphinx documentation
docs/_build/
# PyBuilder
target/

View File

@ -104,7 +104,7 @@ pyFTS/models/seasonal/common.py
pyFTS/models/seasonal/msfts.py
pyFTS/models/seasonal/partitioner.py
pyFTS/models/seasonal/sfts.py
pyFTS/partitioners/CMeans.py
pyFTS/partitioners/KMeans.py
pyFTS/partitioners/Entropy.py
pyFTS/partitioners/FCM.py
pyFTS/partitioners/Grid.py

View File

@ -38,7 +38,7 @@ Fuzzy Time Series (FTS) are non parametric methods for time series forecasting b
2. **Universe of Discourse Partitioning**: This is the most important step. Here, the range of values of the numerical time series *Y(t)* will be splited in overlapped intervals and for each interval will be created a Fuzzy Set. This step is performed by pyFTS.partition module and its classes (for instance GridPartitioner, EntropyPartitioner, etc). The main parameters are:
- the number of intervals
- which fuzzy membership function (on [pyFTS.common.Membership](https://github.com/PYFTS/pyFTS/blob/master/pyFTS/common/Membership.py))
- partition scheme ([GridPartitioner](https://github.com/PYFTS/pyFTS/blob/master/pyFTS/partitioners/Grid.py), [EntropyPartitioner](https://github.com/PYFTS/pyFTS/blob/master/pyFTS/partitioners/Entropy.py)[3], [FCMPartitioner](https://github.com/PYFTS/pyFTS/blob/master/pyFTS/partitioners/FCM.py), [CMeansPartitioner](https://github.com/PYFTS/pyFTS/blob/master/pyFTS/partitioners/CMeans.py), [HuarngPartitioner](https://github.com/PYFTS/pyFTS/blob/master/pyFTS/partitioners/Huarng.py)[4])
- partition scheme ([GridPartitioner](https://github.com/PYFTS/pyFTS/blob/master/pyFTS/partitioners/Grid.py), [EntropyPartitioner](https://github.com/PYFTS/pyFTS/blob/master/pyFTS/partitioners/Entropy.py)[3], [FCMPartitioner](https://github.com/PYFTS/pyFTS/blob/master/pyFTS/partitioners/FCM.py), [KMeansPartitioner](https://github.com/PYFTS/pyFTS/blob/master/pyFTS/partitioners/KMeans.py), [HuarngPartitioner](https://github.com/PYFTS/pyFTS/blob/master/pyFTS/partitioners/Huarng.py)[4])
Check out the jupyter notebook on [notebooks/Partitioners.ipynb](https://github.com/PYFTS/notebooks/blob/master/Partitioners.ipynb) for sample codes.

View File

@ -28,10 +28,10 @@ pyFTS.partitioners.Class module
:undoc-members:
:show-inheritance:
pyFTS.partitioners.CMeans module
pyFTS.partitioners.KMeans module
--------------------------------
.. automodule:: pyFTS.partitioners.CMeans
.. automodule:: pyFTS.partitioners.KMeans
:members:
:undoc-members:
:show-inheritance:

View File

@ -1,5 +1,5 @@
from pyFTS.common.transformations.transformation import Transformation
from pandas import datetime
# from pandas import datetime
from sklearn.linear_model import LinearRegression
import numpy as np
import pandas as pd

View File

@ -146,7 +146,7 @@ class ClusteredMVFTS(mvfts.MVFTS):
new_data_point[self.target_variable.data_label] = tmp.expected_value()
sample = sample.append(new_data_point, ignore_index=True)
sample = pd.concat([sample, pd.DataFrame([new_data_point])], ignore_index=True)
return ret[-steps:]
@ -199,7 +199,7 @@ class ClusteredMVFTS(mvfts.MVFTS):
for k in np.arange(0, steps):
sample = ret.iloc[k:self.order+k]
tmp = self.forecast_multivariate(sample, **kwargs)
ret = ret.append(tmp, ignore_index=True)
ret = pd.concat([ret, pd.DataFrame([tmp])], ignore_index=True)
return ret

View File

@ -211,7 +211,7 @@ class MVFTS(fts.FTS):
new_data_point[self.target_variable.data_label] = tmp
ndata = ndata.append(new_data_point, ignore_index=True)
ndata = pd.concat([ndata, pd.DataFrame([new_data_point])], ignore_index=True)
return ret[-steps:]
@ -307,8 +307,8 @@ class MVFTS(fts.FTS):
new_data_point_lo[self.target_variable.data_label] = min(tmp_lo)
new_data_point_up[self.target_variable.data_label] = max(tmp_up)
lo = lo.append(new_data_point_lo, ignore_index=True)
up = up.append(new_data_point_up, ignore_index=True)
lo = pd.concat([lo, pd.DataFrame([new_data_point_lo])], ignore_index=True)
up = pd.concat([up, pd.DataFrame([new_data_point_up])], ignore_index=True)
return ret[-steps:]

View File

@ -69,7 +69,7 @@ class WeightedMVFTS(mvfts.MVFTS):
self.shortname = "WeightedMVFTS"
self.name = "Weighted Multivariate FTS"
self.has_classification = True
self.class_weigths : dict = kwargs.get("class_weights", {})
self.class_weights : dict = kwargs.get("class_weights", {})
def generate_flrg(self, flrs):
@ -102,7 +102,7 @@ class WeightedMVFTS(mvfts.MVFTS):
for k,v in _flrg.RHS.items():
classification[k] += (v / _flrg.count) * mb
classification = activation(classification, self.class_weigths)
classification = activation(classification, self.class_weights)
ret.append(classification)

View File

@ -36,7 +36,7 @@ def fuzzy_cmeans(k, data, size, m, deltadist=0.001):
centroids = [data[rnd.randint(0, data_length - 1)] for kk in range(0, k)]
# Membership table
membership_table = np.zeros((k, data_length)) #[[0 for kk in range(0, k)] for xx in range(0, data_length)]
membership_table = np.zeros((data_length, k))
mean_change = 1000
@ -50,12 +50,12 @@ def fuzzy_cmeans(k, data, size, m, deltadist=0.001):
inst_count = 0
for instance in data:
dist_groups = np.zeros(k) #[0 for xx in range(0, k)]
dist_groups = np.zeros(k)
for group_count, group in enumerate(centroids):
dist_groups[group_count] = fuzzy_distance(group, instance)
dist_groups_total = functools.reduce(operator.add, [xk for xk in dist_groups])
# dist_groups_total = functools.reduce(operator.add, [xk for xk in dist_groups])
for grp in range(0, k):
if dist_groups[grp] == 0:

View File

@ -19,13 +19,13 @@ class HuarngPartitioner(partitioner.Partitioner):
def build(self, data):
diff = Transformations.Differential(1)
data2 = diff.apply(data)
davg = np.abs( np.mean(data2) / 2 )
divs = np.abs( np.mean(data2) / 2 )
if davg <= 1.0:
if divs <= 1.0:
base = 0.1
elif 1 < davg <= 10:
elif 1 < divs <= 10:
base = 1.0
elif 10 < davg <= 100:
elif 10 < divs <= 100:
base = 10
else:
base = 100

View File

@ -14,15 +14,15 @@ def distance(x, y):
return math.sqrt(tmp)
def c_means(k, dados, tam):
# Inicializa as centróides escolhendo elementos aleatórios dos conjuntos
def k_means(k, dados, tam):
# Инициализирует центроиды, выбирая случайные элементы из множества
centroides = [dados[rnd.randint(0, len(dados)-1)] for kk in range(0, k)]
grupos = [-1 for x in range(0, len(dados))]
it_semmodificacao = 0
# para cada instância
# для каждого экземпляра
iteracoes = 0
while iteracoes < 1000 and it_semmodificacao < 10:
inst_count = 0
@ -31,7 +31,7 @@ def c_means(k, dados, tam):
for instancia in dados:
# verifica a distância para cada centroide
# проверяет расстояние до каждого центроида
grupo_count = 0
dist = 10000
@ -41,7 +41,7 @@ def c_means(k, dados, tam):
tmp = distance(instancia, grupo)
if tmp < dist:
dist = tmp
# associa a a centroide de menor distância à instância
# ассоциирует центроид с наименьшим расстоянием до экземпляра
grupos[inst_count] = grupo_count
grupo_count = grupo_count + 1
@ -55,7 +55,7 @@ def c_means(k, dados, tam):
else:
it_semmodificacao = 0
# atualiza cada centroide com base nos valores médios de todas as instâncias à ela associadas
# обновляет каждый центроид на основе средних значений всех связанных с ним экземпляров
grupo_count = 0
for grupo in centroides:
total_inst = functools.reduce(operator.add, [1 for xx in grupos if xx == grupo_count], 0)
@ -77,21 +77,21 @@ def c_means(k, dados, tam):
return centroides
class CMeansPartitioner(partitioner.Partitioner):
class KMeansPartitioner(partitioner.Partitioner):
def __init__(self, **kwargs):
super(CMeansPartitioner, self).__init__(name="CMeans", **kwargs)
super(KMeansPartitioner, self).__init__(name="KMeans", **kwargs)
def build(self, data):
sets = {}
kwargs = {'type': self.type, 'variable': self.variable}
centroides = c_means(self.partitions, data, 1)
centroides = k_means(self.partitions, data, 1)
centroides.append(self.max)
centroides.append(self.min)
centroides = list(set(centroides))
centroides.sort()
for c in np.arange(1, len(centroides) - 1):
for c in range(1, len(centroides) - 1):
_name = self.get_name(c)
sets[_name] = FuzzySet.FuzzySet(_name, Membership.trimf,
[round(centroides[c - 1], 3), round(centroides[c], 3), round(centroides[c + 1], 3)],

View File

@ -18,19 +18,20 @@ all_methods = [Grid.GridPartitioner, Entropy.EntropyPartitioner, FCM.FCMPartitio
mfs = [Membership.trimf, Membership.gaussmf, Membership.trapmf]
def plot_sets(data, sets: dict, titles : list, size=[12, 10], save=False, file=None, axis=None):
def plot_sets(sets: dict, titles : list, size=[12, 10], save=False, file=None, axis=None):
"""
Plot all fuzzy sets in a Partitioner
"""
num = len(sets)
num_cols_plot = 1
if axis is None:
fig, axes = plt.subplots(nrows=num, ncols=1,figsize=size)
for k in np.arange(0,num):
fig, axes = plt.subplots(nrows=num, ncols=num_cols_plot, figsize=size, squeeze=False)
for k in range(num):
ticks = []
x = []
ax = axes[k] if axis is None else axis
ax = axes[k, num_cols_plot-1] if axis is None else axis
ax.set_title(titles[k])
ax.set_ylim([0, 1.1])
for key in sets[k].keys():
@ -54,7 +55,7 @@ def plot_sets(data, sets: dict, titles : list, size=[12, 10], save=False, file=N
Util.show_and_save_image(fig, file, save)
def plot_partitioners(data, objs, tam=[12, 10], save=False, file=None, axis=None):
def plot_partitioners(objs, tam=[12, 10], save=False, file=None, axis=None):
sets = [k.sets for k in objs]
titles = [k.name for k in objs]
plot_sets(sets, titles, tam, save, file, axis)

View File

@ -1,6 +1,7 @@
from pyFTS.common import FuzzySet, Membership
import numpy as np
from scipy.spatial import KDTree
import warnings
class Partitioner(object):
@ -45,6 +46,9 @@ class Partitioner(object):
if kwargs.get('preprocess',True):
data = kwargs.get('data',[None])
if isinstance(data, np.ndarray) and len(data.shape) > 1:
warnings.warn(f"An ndarray of dimension greater than 1 is used. shape.len(): {len(data.shape)}")
if self.indexer is not None:
ndata = self.indexer.get_data(data)

View File

@ -11,7 +11,7 @@ from mpl_toolkits.mplot3d import Axes3D
import datetime
import pandas as pd
from pyFTS.partitioners import Grid, CMeans, FCM, Entropy
from pyFTS.partitioners import Grid, KMeans, FCM, Entropy
from pyFTS.common import FLR, FuzzySet, Membership, Transformations, Util, fts
from pyFTS import sfts
from pyFTS.models import msfts

View File

@ -26,6 +26,7 @@ setuptools.setup(
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Intended Audience :: Science/Research',
'Intended Audience :: Developers',
'Intended Audience :: Education',