fuzzyclust.models.ModifiedGG#

class fuzzyclust.models.ModifiedGG(n_clusters=3, m=2.0, antecedent_features=None, gamma=0.0, var_floor=1e-06, tol=0.0001, max_iter=300, init='kmeans++', n_init=3, restart_criterion='likelihood', random_state=None, verbose=0)[source]#

Bases: BaseFuzzyClusterer

Modified Gath–Geva clustering: clusters are interpretable Takagi–Sugeno rules.

Each cluster is described by (a) Gaussian membership functions on the individual input variables (an axis-parallel Gaussian, so the cluster is directly a rule antecedent and no projection error arises), (b) a local linear model \(\hat y = \mathbf{a}_i^T\mathbf{x} + b_i\) with the variance of its error, and (c) a prior probability \(\alpha_i\). The distance of a sample to a cluster is the reciprocal of the probability that the cluster generated the input and the output, so the alternating optimization is the EM identification of a mixture of local linear regression models, written as a Gath–Geva type fuzzy clustering [1]. Vector-valued outputs (MIMO systems) are handled with a full error covariance matrix per rule [2].

Parameters:
n_clustersint, default=3

Number of clusters = rules \(c\).

mfloat, default=2.0

Fuzziness exponent of the membership update. m = 2 reproduces the EM algorithm (memberships are the posterior cluster probabilities).

antecedent_featuresarray_like of int, optional

Column indices of X used in the rule antecedents (scheduling variables). The local linear models always use all columns. None uses every input.

gammafloat, default=0.0

Variance regularization \(\gamma\in[0,1]\): the membership-function variances are shrunk toward the feature variances, \(\sigma^2_{ij}\leftarrow(1-\gamma)\sigma^2_{ij}+\gamma\,\mathrm{var}(x_j)\), the diagonal analog of the covariance regularization of GG; it prevents rules from collapsing onto a few samples.

var_floorfloat, default=1e-6

Lower bound of the membership-function variances relative to the variance of the corresponding feature (avoids collapsing clusters).

tolfloat, default=1e-4
max_iterint, default=300
init{“random”, “kmeans++”} or ndarray, default=”kmeans++”

As in BaseFuzzyClusterer; passing the membership matrix of a fitted FCM/GK model is also a good choice because the exponential distance is sensitive to the initial partition.

n_initint, default=3

Number of random restarts (ignored when init is an array). The c-means functional \(J\) is not comparable between runs of an exponential-distance algorithm, so the kept run is chosen by restart_criterion.

restart_criterion{“likelihood”, “error”}, default=”likelihood”

"likelihood" keeps the run with the largest log-likelihood \(\sum_k\log\sum_i 1/D^2_{ik}\) of the data under the mixture of local models (the quantity the EM algorithm maximizes); "error" keeps the run whose Takagi–Sugeno output has the smallest sum of squared errors on the training data. The two can disagree: a rule with a very small error variance on part of the data raises the likelihood without improving the global fit.

random_stateint, RandomState instance or None, default=None
verboseint, default=0
Attributes:
cluster_centers_ndarray of shape (n_clusters, n_antecedent)

Centers \(v_{ij}\) of the Gaussian membership functions.

input_variances_ndarray of shape (n_clusters, n_antecedent)

Variances \(\sigma^2_{ij}\) of the membership functions.

local_models_ndarray of shape (n_clusters, n_features + 1, n_outputs)

Parameters \(\boldsymbol\theta_i=[\mathbf{a}_i^T\, b_i]^T\) of the local linear models (intercept in the last row).

coef_ndarray of shape (n_clusters, n_features) or (n_clusters, n_outputs, n_features)
intercept_ndarray of shape (n_clusters,) or (n_clusters, n_outputs)
error_covariances_ndarray of shape (n_clusters, n_outputs, n_outputs)

Weighted covariance of the local model errors \(\mathbf{F}^{yy}_i\).

error_variances_ndarray of shape (n_clusters,) or (n_clusters, n_outputs)

Diagonal of error_covariances_ (\(\sigma_i^2\) for one output).

priors_ndarray of shape (n_clusters,)

Prior probabilities \(\alpha_i\).

rule_weights_ndarray of shape (n_clusters,)

\(w_i=\alpha_i\prod_j (2\pi\sigma^2_{ij})^{-1/2}\).

log_likelihood_float

\(\sum_k\log\sum_i 1/D^2_{ik}\) of the kept run.

restart_scores_ndarray of shape (n_init,)

Value of the restart criterion of every run (higher is better).

membership_, labels_, distances_, objective_, objective_history_, n_iter_

See BaseFuzzyClusterer.

n_outputs_int

Notes

With \(\mathbf{z}_k=[\mathbf{x}_k^T\,y_k]^T\) the distance is

\[\frac{1}{D^2_{ik}} = \alpha_i \prod_{j=1}^{n}\frac{1}{\sqrt{2\pi\sigma^2_{ij}}} \exp\!\Big(-\tfrac12\frac{(x_{jk}-v_{ij})^2}{\sigma^2_{ij}}\Big)\cdot \frac{1}{\sqrt{2\pi\sigma_i^2}} \exp\!\Big(-\frac{(y_k-\mathbf{a}_i^T\mathbf{x}_k-b_i)^2}{2\sigma_i^2}\Big),\]

and the parameters are updated with the memberships as weights:

\[\mathbf{v}_i=\frac{\sum_k\mu_{ik}\mathbf{x}_k}{\sum_k\mu_{ik}},\quad \sigma^2_{ij}=\frac{\sum_k\mu_{ik}(x_{jk}-v_{ij})^2}{\sum_k\mu_{ik}},\quad \boldsymbol\theta_i=(\mathbf{X}_e^T\boldsymbol\Phi_i\mathbf{X}_e)^{-1}\mathbf{X}_e^T\boldsymbol\Phi_i\mathbf{y},\quad \alpha_i=\frac1N\sum_k\mu_{ik},\]

where \(\boldsymbol\Phi_i=\operatorname{diag}(\mu_{i1},\dots,\mu_{iN})\) and \(\sigma_i^2\) is the \(\mu\)-weighted mean squared residual (for several outputs the weighted error covariance \(\mathbf{F}^{yy}_i\) and the multivariate Gaussian density replace \(\sigma_i^2\)). The partition is updated with the usual \(\mu_{ik}=1/\sum_j (D_{ik}/D_{jk})^{2/(m-1)}\); for \(m=2\) the memberships equal the posterior probabilities of the EM algorithm. All densities are evaluated in the log domain.

Unlike the clusterers of fuzzyclust.cluster, predict() returns the Takagi–Sugeno model output, not a hard label: the rules are aggregated with the normalized firing strengths \(\beta_i(\mathbf{x})=w_i\prod_j A_{ij}(x_j)\) of the Gaussian antecedents (see firing_strengths()). Hard labels of training samples remain available as labels_.

References

[1]

J. Abonyi, R. Babuška, F. Szeifert, “Modified Gath–Geva fuzzy clustering for identification of Takagi–Sugeno fuzzy models”, IEEE Trans. Systems, Man and Cybernetics B, 32(5), 612–621, 2002.

[2]

J. Abonyi, F. Szeifert, “Fuzzy clustering for the identification of Takagi–Sugeno fuzzy models of MIMO dynamical systems”, 2002.

Examples

>>> from fuzzyclust.models import ModifiedGG, make_sinc
>>> X, y = make_sinc(n_samples=200, random_state=0)
>>> mgg = ModifiedGG(n_clusters=4, random_state=0).fit(X, y)
>>> mgg.predict(X).shape
(200,)
>>> list(mgg.describe_rules().columns)
['rule', 'weight', 'prior', 'x1 center', 'x1 sigma', 'coef x1', 'intercept', 'error sigma']
describe_rules(feature_names=None, output_names=None)[source]#

Readable rule base: one row per rule (and per output for MIMO models).

Parameters:
feature_namessequence of str, optional

Defaults to feature_names_in_ or x1, x2, ....

output_namessequence of str, optional
Returns:
pandas.DataFrame

Columns rule, weight, prior, the center and standard deviation of every antecedent set (<name> center, <name> sigma), the consequent coefficients (coef <name>) and intercept.

firing_strengths(X, normalize=True)[source]#

Rule firing strengths \(\beta_i(\mathbf{x})=w_i\prod_j A_{ij}(x_j)\).

Parameters:
Xarray_like of shape (n_samples, n_features)
normalizebool, default=True

Return \(\beta_i/\sum_j\beta_j\) (rows sum to one). These are the posterior cluster probabilities given the input only.

Returns:
betandarray of shape (n_samples, n_clusters)
fit(X, y=None)[source]#

Cluster the input–output data and identify the Takagi–Sugeno rules.

Parameters:
Xarray_like of shape (n_samples, n_features)

Regressors (inputs).

yarray_like of shape (n_samples,) or (n_samples, n_outputs)

Output(s); required (the default None only exists for API compatibility and raises a ValueError).

Returns:
selfobject
fit_predict(X, y=None, **kwargs)[source]#

Fit and return the hard cluster labels of the training samples.

fit_transform(X, y=None, **fit_params)[source]#

Fit and return the antecedent distances transform(X) of the training inputs.

local_predict(X)[source]#

Outputs of the individual local linear models, shape (n_samples, n_clusters[, n_outputs]).

predict(X)[source]#

Takagi–Sugeno model output.

\[\hat y=\frac{\sum_i\beta_i(\mathbf{x})(\mathbf{a}_i^T\mathbf{x}+b_i)}{\sum_i\beta_i(\mathbf{x})}\]
Returns:
y_hatndarray of shape (n_samples,) or (n_samples, n_outputs)
predict_proba(X, y=None)[source]#

Cluster memberships of (new) samples.

Without y the memberships are the normalized firing strengths of the antecedents, \(p(\eta_i\mid\mathbf{x})\). With y the output-error term enters as during fitting and the memberships are \(p(\eta_i\mid\mathbf{x},y)\), i.e. the partition the clustering would produce for these input–output pairs.

Returns:
Undarray of shape (n_samples, n_clusters)
property rules_#

Rule base as a DataFrame (see describe_rules()).

score(X, y)[source]#

Negative clustering objective \(-J\) of the pairs (X, y) (higher is better).

transform(X, y=None)[source]#

Distances \(D_{ik}\) of samples to the rules.

Without y only the antecedent term enters (the distance a new input has to the rule); with y the output-error term is included as during fitting.

Examples using fuzzyclust.models.ModifiedGG#

Takagi–Sugeno model of a static nonlinearity: clusters become rules

Takagi–Sugeno model of a static nonlinearity: clusters become rules