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:
BaseFuzzyClustererModified 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_clusters
int, default=3 Number of clusters = rules \(c\).
- m
float, default=2.0 Fuzziness exponent of the membership update.
m = 2reproduces the EM algorithm (memberships are the posterior cluster probabilities).- antecedent_featuresarray_like
ofint,optional Column indices of
Xused in the rule antecedents (scheduling variables). The local linear models always use all columns.Noneuses every input.- gamma
float, 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_floor
float, default=1e-6 Lower bound of the membership-function variances relative to the variance of the corresponding feature (avoids collapsing clusters).
- tol
float, default=1e-4 - max_iter
int, default=300 - init{“random”, “kmeans++”}
orndarray, 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_init
int, default=3 Number of random restarts (ignored when
initis an array). The c-means functional \(J\) is not comparable between runs of an exponential-distance algorithm, so the kept run is chosen byrestart_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_state
int,RandomStateinstanceorNone, default=None - verbose
int, default=0
- n_clusters
- Attributes:
- cluster_centers_
ndarrayofshape(n_clusters,n_antecedent) Centers \(v_{ij}\) of the Gaussian membership functions.
- input_variances_
ndarrayofshape(n_clusters,n_antecedent) Variances \(\sigma^2_{ij}\) of the membership functions.
- local_models_
ndarrayofshape(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_
ndarrayofshape(n_clusters,n_features)or(n_clusters,n_outputs,n_features) - intercept_
ndarrayofshape(n_clusters,)or(n_clusters,n_outputs) - error_covariances_
ndarrayofshape(n_clusters,n_outputs,n_outputs) Weighted covariance of the local model errors \(\mathbf{F}^{yy}_i\).
- error_variances_
ndarrayofshape(n_clusters,)or(n_clusters,n_outputs) Diagonal of
error_covariances_(\(\sigma_i^2\) for one output).- priors_
ndarrayofshape(n_clusters,) Prior probabilities \(\alpha_i\).
- rule_weights_
ndarrayofshape(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_
ndarrayofshape(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
- cluster_centers_
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 (seefiring_strengths()). Hard labels of training samples remain available aslabels_.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:
- Returns:
pandas.DataFrameColumns
rule,weight,prior, the center and standard deviation of every antecedent set (<name> center,<name> sigma), the consequent coefficients (coef <name>) andintercept.
- firing_strengths(X, normalize=True)[source]#
Rule firing strengths \(\beta_i(\mathbf{x})=w_i\prod_j A_{ij}(x_j)\).
- Parameters:
- Xarray_like
ofshape(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.
- Xarray_like
- Returns:
- beta
ndarrayofshape(n_samples,n_clusters)
- beta
- fit(X, y=None)[source]#
Cluster the input–output data and identify the Takagi–Sugeno rules.
- Parameters:
- Xarray_like
ofshape(n_samples,n_features) Regressors (inputs).
- yarray_like
ofshape(n_samples,)or(n_samples,n_outputs) Output(s); required (the default
Noneonly exists for API compatibility and raises aValueError).
- Xarray_like
- Returns:
- self
object
- self
- 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_hat
ndarrayofshape(n_samples,)or(n_samples,n_outputs)
- y_hat
- predict_proba(X, y=None)[source]#
Cluster memberships of (new) samples.
Without
ythe memberships are the normalized firing strengths of the antecedents, \(p(\eta_i\mid\mathbf{x})\). Withythe 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:
- U
ndarrayofshape(n_samples,n_clusters)
- U
- property rules_#
Rule base as a DataFrame (see
describe_rules()).
Examples using fuzzyclust.models.ModifiedGG#
Takagi–Sugeno model of a static nonlinearity: clusters become rules