pyFTS.common package¶
Module contents¶
Submodules¶
pyFTS.common.Composite module¶
Composite Fuzzy Sets
- class pyFTS.common.Composite.FuzzySet(name, superset=False, **kwargs)[source]¶
Bases:
pyFTS.common.FuzzySet.FuzzySet
Composite Fuzzy Set
- append(mf, parameters)[source]¶
Adds a new function to composition
- Parameters
mf –
parameters –
- Returns
pyFTS.common.FLR module¶
This module implements functions for Fuzzy Logical Relationship generation
- class pyFTS.common.FLR.FLR(LHS, RHS)[source]¶
Bases:
object
Fuzzy Logical Relationship
Represents a temporal transition of the fuzzy set LHS on time t for the fuzzy set RHS on time t+1.
- LHS¶
Left Hand Side fuzzy set
- RHS¶
Right Hand Side fuzzy set
- class pyFTS.common.FLR.IndexedFLR(index, LHS, RHS)[source]¶
Bases:
pyFTS.common.FLR.FLR
Season Indexed Fuzzy Logical Relationship
- index¶
seasonal index
- pyFTS.common.FLR.generate_high_order_recurrent_flr(fuzzyData)[source]¶
Create a ordered FLR set from a list of fuzzy sets with recurrence
- Parameters
fuzzyData – ordered list of fuzzy sets
- Returns
ordered list of FLR
- pyFTS.common.FLR.generate_indexed_flrs(sets, indexer, data, transformation=None, alpha_cut=0.0)[source]¶
Create a season-indexed ordered FLR set from a list of fuzzy sets with recurrence
- Parameters
sets – fuzzy sets
indexer – seasonality indexer
data – original data
- Returns
ordered list of FLR
pyFTS.common.FuzzySet module¶
- class pyFTS.common.FuzzySet.FuzzySet(name: str, mf, parameters: list, centroid: float, alpha: float = 1.0, **kwargs)[source]¶
Bases:
object
Fuzzy Set
- Z¶
Partition function in respect to the membership function
- membership(x)[source]¶
Calculate the membership value of a given input
- Parameters
x – input value
- Returns
membership value of x at this fuzzy set
- mf¶
The membership function
- partition_function(uod=None, nbins=100)[source]¶
Calculate the partition function over the membership function.
- Parameters
uod –
nbins –
- Returns
- transform(x)[source]¶
Preprocess the data point for non native types
- Parameters
x –
- Returns
return a native type value for the structured type
- variable¶
In multivariate time series, indicate for which variable this fuzzy set belogs
- pyFTS.common.FuzzySet.fuzzyfy(data, partitioner, **kwargs)[source]¶
A general method for fuzzyfication.
- Parameters
data – input value to be fuzzyfied
partitioner – a trained pyFTS.partitioners.Partitioner object
kwargs – dict, optional arguments
alpha_cut – the minimal membership value to be considered on fuzzyfication (only for mode=’sets’)
method – the fuzzyfication method (fuzzy: all fuzzy memberships, maximum: only the maximum membership)
mode – the fuzzyfication mode (sets: return the fuzzy sets names, vector: return a vector with the membership
values for all fuzzy sets, both: return a list with tuples (fuzzy set, membership value) ) :returns a list with the fuzzyfied values, depending on the mode
- pyFTS.common.FuzzySet.fuzzyfy_instance(inst, fuzzy_sets: dict, ordered_sets: Optional[list] = None)[source]¶
Calculate the membership values for a data point given fuzzy sets
- Parameters
inst – data point
fuzzy_sets – a dictionary where the key is the fuzzy set name and the value is the fuzzy set object.
ordered_sets – a list with the fuzzy sets names ordered by their centroids.
- Returns
array of membership values
- pyFTS.common.FuzzySet.fuzzyfy_instances(data: list, fuzzy_sets: dict, ordered_sets=None) list [source]¶
Calculate the membership values for a data point given fuzzy sets
- Parameters
inst – data point
fuzzy_sets – a dictionary where the key is the fuzzy set name and the value is the fuzzy set object.
ordered_sets – a list with the fuzzy sets names ordered by their centroids.
- Returns
array of membership values
- pyFTS.common.FuzzySet.fuzzyfy_series(data, fuzzy_sets, method='maximum', alpha_cut=0.0, ordered_sets=None)[source]¶
- pyFTS.common.FuzzySet.get_fuzzysets(inst, fuzzy_sets: dict, ordered_sets: Optional[list] = None, alpha_cut: float = 0.0) list [source]¶
Return the fuzzy sets which membership value for a inst is greater than the alpha_cut
- Parameters
inst – data point
fuzzy_sets – a dictionary where the key is the fuzzy set name and the value is the fuzzy set object.
ordered_sets – a list with the fuzzy sets names ordered by their centroids.
alpha_cut – Minimal membership to be considered on fuzzyfication process
- Returns
array of membership values
- pyFTS.common.FuzzySet.get_maximum_membership_fuzzyset(inst, fuzzy_sets, ordered_sets=None) pyFTS.common.FuzzySet.FuzzySet [source]¶
Fuzzify a data point, returning the fuzzy set with maximum membership value
- Parameters
inst – data point
fuzzy_sets – a dictionary where the key is the fuzzy set name and the value is the fuzzy set object.
ordered_sets – a list with the fuzzy sets names ordered by their centroids.
- Returns
fuzzy set with maximum membership
pyFTS.common.Membership module¶
Membership functions for Fuzzy Sets
- pyFTS.common.Membership.bellmf(x, parameters)[source]¶
Bell shaped membership function
- Parameters
x –
parameters –
- Returns
- pyFTS.common.Membership.gaussmf(x, parameters)[source]¶
Gaussian fuzzy membership function
- Parameters
x – data point
parameters – a list with 2 real values (mean and variance)
- Returns
the membership value of x given the parameters
- pyFTS.common.Membership.sigmf(x, parameters)[source]¶
Sigmoid / Logistic membership function
- Parameters
x –
parameters – an list with 2 real values (smoothness and midpoint)
:return
- pyFTS.common.Membership.singleton(x, parameters)[source]¶
Singleton membership function, a single value fuzzy function
- Parameters
x –
parameters – a list with one real value
:returns
pyFTS.common.SortedCollection module¶
- class pyFTS.common.SortedCollection.SortedCollection(iterable=(), key=None)[source]¶
Bases:
object
Sequence sorted by a key function.
SortedCollection() is much easier to work with than using bisect() directly. It supports key functions like those use in sorted(), min(), and max(). The result of the key function call is saved so that keys can be searched efficiently.
Instead of returning an insertion-point which can be hard to interpret, the five find-methods return a specific item in the sequence. They can scan for exact matches, the last item less-than-or-equal to a key, or the first item greater-than-or-equal to a key.
Once found, an item’s ordinal position can be located with the index() method. New items can be added with the insert() and insert_right() methods. Old items can be deleted with the remove() method.
The usual sequence methods are provided to support indexing, slicing, length lookup, clearing, copying, forward and reverse iteration, contains checking, item counts, item removal, and a nice looking repr.
Finding and indexing are O(log n) operations while iteration and insertion are O(n). The initial sort is O(n log n).
The key function is stored in the ‘key’ attibute for easy introspection or so that you can assign a new key function (triggering an automatic re-sort).
In short, the class was designed to handle all of the common use cases for bisect but with a simpler API and support for key functions.
>>> from pprint import pprint >>> from operator import itemgetter
>>> s = SortedCollection(key=itemgetter(2)) >>> for record in [ ... ('roger', 'young', 30), ... ('angela', 'jones', 28), ... ('bill', 'smith', 22), ... ('david', 'thomas', 32)]: ... s.insert(record)
>>> pprint(list(s)) # show records sorted by age [('bill', 'smith', 22), ('angela', 'jones', 28), ('roger', 'young', 30), ('david', 'thomas', 32)]
>>> s.find_le(29) # find oldest person aged 29 or younger ('angela', 'jones', 28) >>> s.find_lt(28) # find oldest person under 28 ('bill', 'smith', 22) >>> s.find_gt(28) # find youngest person over 28 ('roger', 'young', 30)
>>> r = s.find_ge(32) # find youngest person aged 32 or older >>> s.index(r) # get the index of their record 3 >>> s[3] # fetch the record at that index ('david', 'thomas', 32)
>>> s.key = itemgetter(0) # now sort by first name >>> pprint(list(s)) [('angela', 'jones', 28), ('bill', 'smith', 22), ('david', 'thomas', 32), ('roger', 'young', 30)]
- property key¶
key function
pyFTS.common.Util module¶
Common facilities for pyFTS
- pyFTS.common.Util.current_milli_time()¶
- pyFTS.common.Util.load_obj(file)[source]¶
Load to memory an object stored filesystem. This function depends on Dill package
- Parameters
file – file name where the object is stored
- Returns
object
- pyFTS.common.Util.persist_env(file)[source]¶
Persist an entire environment on file. This function depends on Dill package
- Parameters
file – file name to store the environment
- pyFTS.common.Util.persist_obj(obj, file)[source]¶
Persist an object on filesystem. This function depends on Dill package
- Parameters
obj – object on memory
file – file name to store the object
- pyFTS.common.Util.plot_compared_intervals_ahead(original, models, colors, distributions, time_from, time_to, intervals=True, save=False, file=None, tam=[20, 5], resolution=None, cmap='Blues', linewidth=1.5)[source]¶
Plot the forecasts of several one step ahead models, by point or by interval
- Parameters
original – Original time series data (list)
models – List of models to compare
colors – List of models colors
distributions – True to plot a distribution
time_from – index of data poit to start the ahead forecasting
time_to – number of steps ahead to forecast
interpol – Fill space between distribution plots
save – Save the picture on file
file – Filename to save the picture
tam – Size of the picture
resolution –
cmap – Color map to be used on distribution plot
option – Distribution type to be passed for models
- Returns
- pyFTS.common.Util.plot_density_rectange(ax, cmap, density, fig, resolution, time_from, time_to)[source]¶
Auxiliar function to plot_compared_intervals_ahead
- pyFTS.common.Util.plot_distribution(ax, cmap, probabilitydist, fig, time_from, reference_data=None)[source]¶
Plot forecasted ProbabilityDistribution objects on a matplotlib axis
- Parameters
ax – matplotlib axis
cmap – matplotlib colormap name
probabilitydist – list of ProbabilityDistribution objects
fig – matplotlib figure
time_from – starting time (on x axis) to begin the plots
reference_data –
- Returns
- pyFTS.common.Util.plot_distribution2(probabilitydist, data, **kwargs)[source]¶
Plot distributions in y-axis over the time (x-axis)
- Parameters
probabilitydist – the forecasted probability distributions to plot
data – the original test sample
start_at – the time index (inside of data) to start to plot the probability distributions
ax – a matplotlib axis. If no value was provided a new axis is created.
cmap – a matplotlib colormap name, the default value is ‘Blues’
quantiles – the list of quantiles intervals to plot, e. g. [.05, .25, .75, .95]
median – a boolean value indicating if the median value will be plot.
- pyFTS.common.Util.plot_distribution_tiled(distributions, data=None, rows=5, cols=5, index=None, axis=None, size=[10, 20])[source]¶
Plot one distribution individually in each axis, with probability in y-axis and UoD on x-axis
- Parameters
distributions –
data –
rows –
cols –
index –
axis –
size –
- Returns
- pyFTS.common.Util.plot_interval(axis, intervals, order, label, color='red', typeonlegend=False, ls='-', linewidth=1)[source]¶
Plot forecasted intervals on matplotlib
- Parameters
axis – matplotlib axis
intervals – list of forecasted intervals
order – order of the model that create the forecasts
label – figure label
color – matplotlib color name
typeonlegend –
ls – matplotlib line style
linewidth – matplotlib width
- Returns
- pyFTS.common.Util.plot_interval2(intervals, data, **kwargs)[source]¶
Plot forecasted intervals on matplotlib
- Parameters
intervals – list of forecasted intervals
data – the original test sample
start_at – the time index (inside of data) to start to plot the intervals
label – figure label
color – matplotlib color name
typeonlegend –
ls – matplotlib line style
linewidth – matplotlib width
- pyFTS.common.Util.plot_rules(model, size=[5, 5], axis=None, rules_by_axis=None, columns=1)[source]¶
Plot the FLRG rules of a FTS model on a matplotlib axis
- Parameters
model – FTS model
size – figure size
axis – matplotlib axis
rules_by_axis – number of rules plotted by column
columns – number of columns
- Returns
- pyFTS.common.Util.show_and_save_image(fig, file, flag, lgd=None)[source]¶
Show and image and save on file
- Parameters
fig – Matplotlib Figure object
file – filename to save the picture
flag – if True the image will be saved
lgd – legend
- pyFTS.common.Util.sliding_window(data, windowsize, train=0.8, inc=0.1, **kwargs)[source]¶
Sliding window method of cross validation for time series
- Parameters
data – the entire dataset
windowsize – window size
train – percentual of the window size will be used for training the models
inc – percentual of data used for slide the window
- Returns
window count, training set, test set
pyFTS.common.flrg module¶
- class pyFTS.common.flrg.FLRG(order, **kwargs)[source]¶
Bases:
object
Fuzzy Logical Relationship Group
Group a set of FLR’s with the same LHS. Represents the temporal patterns for time t+1 (the RHS fuzzy sets) when the LHS pattern is identified on time t.
- LHS¶
Left Hand Side of the rule
- RHS¶
Right Hand Side of the rule
- get_lower(sets)[source]¶
Returns the lower bound value for the RHS fuzzy sets
- Parameters
sets – fuzzy sets
- Returns
lower bound value
- get_membership(data, sets)[source]¶
Returns the membership value of the FLRG for the input data
- Parameters
data – input data
sets – fuzzy sets
- Returns
the membership value
- get_midpoint(sets)[source]¶
Returns the midpoint value for the RHS fuzzy sets
- Parameters
sets – fuzzy sets
- Returns
the midpoint value
- get_upper(sets)[source]¶
Returns the upper bound value for the RHS fuzzy sets
- Parameters
sets – fuzzy sets
- Returns
upper bound value
- order¶
Number of lags on LHS
pyFTS.common.fts module¶
- class pyFTS.common.fts.FTS(**kwargs)[source]¶
Bases:
object
Fuzzy Time Series object model
- apply_inverse_transformations(data, params=None, **kwargs)[source]¶
Apply the data transformations for data postprocessing
- Parameters
data – input data
params – transformation parameters
updateUoD –
kwargs –
- Returns
postprocessed data
- apply_transformations(data, params=None, updateUoD=False, **kwargs)[source]¶
Apply the data transformations for data preprocessing
- Parameters
data – input data
params – transformation parameters
updateUoD –
kwargs –
- Returns
preprocessed data
- benchmark_only: bool¶
A boolean value indicating a façade for external (non-FTS) model used on benchmarks or ensembles.
- clone_parameters(model)[source]¶
Import the parameters values from other model
- Parameters
model – a model to clone the parameters
- fit(ndata, **kwargs)[source]¶
Fit the model’s parameters based on the training data.
- Parameters
ndata – training time series data
kwargs –
num_batches – split the training data in num_batches to save memory during the training process
save_model – save final model on disk
batch_save – save the model between each batch
file_path – path to save the model
distributed – boolean, indicate if the training procedure will be distributed in a dispy cluster
nodes – a list with the dispy cluster nodes addresses
- forecast(data, **kwargs) list [source]¶
Point forecast one step ahead
- Parameters
data – time series data with the minimal length equal to the max_lag of the model
kwargs – model specific parameters
- Returns
a list with the forecasted values
- forecast_ahead(data, steps, **kwargs) list [source]¶
Point forecast from 1 to H steps ahead, where H is given by the steps parameter
- Parameters
data – time series data with the minimal length equal to the max_lag of the model
steps – the number of steps ahead to forecast (default: 1)
start_at – in the multi step forecasting, the index of the data where to start forecasting (default: 0)
- Returns
a list with the forecasted values
- forecast_ahead_distribution(data, steps, **kwargs) list [source]¶
Probabilistic forecast from 1 to H steps ahead, where H is given by the steps parameter
- Parameters
data – time series data with the minimal length equal to the max_lag of the model
steps – the number of steps ahead to forecast
start_at – in the multi step forecasting, the index of the data where to start forecasting (default: 0)
- Returns
a list with the forecasted Probability Distributions
- forecast_ahead_interval(data, steps, **kwargs) list [source]¶
Interval forecast from 1 to H steps ahead, where H is given by the steps parameter
- Parameters
data – time series data with the minimal length equal to the max_lag of the model
steps – the number of steps ahead to forecast
start_at – in the multi step forecasting, the index of the data where to start forecasting (default: 0)
- Returns
a list with the forecasted intervals
- forecast_ahead_multivariate(data, steps, **kwargs) pandas.core.frame.DataFrame [source]¶
Multivariate forecast n step ahead
- Parameters
data – Pandas dataframe with one column for each variable and with the minimal length equal to the max_lag of the model
steps – the number of steps ahead to forecast
start_at – in the multi step forecasting, the index of the data where to start forecasting (default: 0)
- Returns
a Pandas Dataframe object representing the forecasted values for each variable
- forecast_distribution(data, **kwargs) list [source]¶
Probabilistic forecast one step ahead
- Parameters
data – time series data with the minimal length equal to the max_lag of the model
kwargs – model specific parameters
- Returns
a list with probabilistic.ProbabilityDistribution objects representing the forecasted Probability Distributions
- forecast_interval(data, **kwargs) list [source]¶
Interval forecast one step ahead
- Parameters
data – time series data with the minimal length equal to the max_lag of the model
kwargs – model specific parameters
- Returns
a list with the prediction intervals
- forecast_multivariate(data, **kwargs) pandas.core.frame.DataFrame [source]¶
Multivariate forecast one step ahead
- Parameters
data – Pandas dataframe with one column for each variable and with the minimal length equal to the max_lag of the model
kwargs – model specific parameters
- Returns
a Pandas Dataframe object representing the forecasted values for each variable
- forecast_step(data, step, **kwargs) list [source]¶
Point forecast for H steps ahead, where H is given by the step parameter
- Parameters
data – time series data with the minimal length equal to the max_lag of the model
step – the forecasting horizon (default: 1)
start_at – in the multi step forecasting, the index of the data where to start forecasting (default: 0)
- Returns
a list with the forecasted values
- fuzzy(data) dict [source]¶
Fuzzify a data point
- Parameters
data – data point
- Returns
maximum membership fuzzy set
- get_UoD() set [source]¶
Returns the interval of the known bounds of the universe of discourse (UoD), i. e., the known minimum and maximum values of the time series.
- Returns
A set with the lower and the upper bounds of the UoD
- has_interval_forecasting: bool¶
A boolean value indicating if the model supports interval forecasting, default: False
- has_point_forecasting: bool¶
A boolean value indicating if the model supports point forecasting, default: True
- has_probability_forecasting: bool¶
A boolean value indicating if the model support probabilistic forecasting, default: False
- has_seasonality: bool¶
A boolean value indicating if the model supports seasonal indexers, default: False
- indexer¶
An pyFTS.models.seasonal.Indexer object for indexing the time series data
- is_clustered: bool¶
A boolean value indicating if the model support multivariate time series (Pandas DataFrame), but works like a monovariate method, default: False
- is_high_order: bool¶
A boolean value indicating if the model support orders greater than 1, default: False
- is_multivariate: bool¶
A boolean value indicating if the model support multivariate time series (Pandas DataFrame), default: False
- log: pd.DataFrame¶
- max_lag: int¶
A integer indicating the largest lag used by the model. This value also indicates the minimum number of past lags needed to forecast a single step ahead
- min_order: int¶
In high order models, this integer value indicates the minimal order supported for the model, default: 1
- offset() int [source]¶
Returns the number of lags to skip in the input test data in order to synchronize it with the forecasted values given by the predict function. This is necessary due to the order of the model, among other parameters.
- Returns
An integer with the number of lags to skip
- original_max: float¶
A float with the upper limit of the Universe of Discourse, the maximal value found on training data
- original_min: float¶
A float with the lower limit of the Universe of Discourse, the minimal value found on training data
- partitioner: partitioner.Partitioner¶
A pyFTS.partitioners.Partitioner object with the Universe of Discourse partitioner used on the model. This is a mandatory dependecy.
- predict(data, **kwargs)[source]¶
Forecast using trained model
- Parameters
data – time series with minimal length to the order of the model
type – the forecasting type, one of these values: point(default), interval, distribution or multivariate.
steps_ahead – The forecasting path H, i. e., tell the model to forecast from t+1 to t+H.
step_to – The forecasting step H, i. e., tell the model to forecast to t+H for each input sample
start_at – in the multi step forecasting, the index of the data where to start forecasting (default value: 0)
distributed – boolean, indicate if the forecasting procedure will be distributed in a dispy cluster (default value: False)
nodes – a list with the dispy cluster nodes addresses
explain – try to explain, step by step, the one-step-ahead point forecasting result given the input data. (default value: False)
generators – for multivariate methods on multi step ahead forecasting, generators is a dict where the keys are the dataframe columun names (except the target_variable) and the values are lambda functions that accept one value (the actual value of the variable) and return the next value or trained FTS models that accept the actual values and forecast new ones.
- Returns
a numpy array with the forecasted data
- train(data, **kwargs)[source]¶
Method specific parameter fitting
- Parameters
data – training time series data
kwargs – Method specific parameters
- transformations: list[transformation.Transformation]¶
A list with the data transformations (common.Transformations) applied on model pre and post processing, default: []