From: <lm...@us...> - 2012-07-09 13:17:26
|
Revision: 10739 http://octave.svn.sourceforge.net/octave/?rev=10739&view=rev Author: lmarkov Date: 2012-07-09 13:17:11 +0000 (Mon, 09 Jul 2012) Log Message: ----------- A inst/gustafson_kessel_demo_1.m A inst/gustafson_kessel_demo_2.m A inst/partition_coeff.m A inst/fcm.m A inst/xie_beni_index.m A inst/gustafson_kessel.m A inst/private/square_distance_matrix.m A inst/private/fcm_init_prototype.m A inst/private/fcm_update_cluster_centers.m A inst/private/fcm_compute_convergence_criterion.m A inst/private/fcm_update_membership_fcn.m A inst/private/fcm_compute_objective_fcn.m A inst/fcm_demo_1.m A inst/partition_entropy.m A inst/fcm_demo_2.m Added Paths: ----------- trunk/octave-forge/main/fuzzy-logic-toolkit/inst/fcm.m trunk/octave-forge/main/fuzzy-logic-toolkit/inst/fcm_demo_1.m trunk/octave-forge/main/fuzzy-logic-toolkit/inst/fcm_demo_2.m trunk/octave-forge/main/fuzzy-logic-toolkit/inst/gustafson_kessel.m trunk/octave-forge/main/fuzzy-logic-toolkit/inst/gustafson_kessel_demo_1.m trunk/octave-forge/main/fuzzy-logic-toolkit/inst/gustafson_kessel_demo_2.m trunk/octave-forge/main/fuzzy-logic-toolkit/inst/partition_coeff.m trunk/octave-forge/main/fuzzy-logic-toolkit/inst/partition_entropy.m trunk/octave-forge/main/fuzzy-logic-toolkit/inst/private/fcm_compute_convergence_criterion.m trunk/octave-forge/main/fuzzy-logic-toolkit/inst/private/fcm_compute_objective_fcn.m trunk/octave-forge/main/fuzzy-logic-toolkit/inst/private/fcm_init_prototype.m trunk/octave-forge/main/fuzzy-logic-toolkit/inst/private/fcm_update_cluster_centers.m trunk/octave-forge/main/fuzzy-logic-toolkit/inst/private/fcm_update_membership_fcn.m trunk/octave-forge/main/fuzzy-logic-toolkit/inst/private/square_distance_matrix.m trunk/octave-forge/main/fuzzy-logic-toolkit/inst/xie_beni_index.m Added: trunk/octave-forge/main/fuzzy-logic-toolkit/inst/fcm.m =================================================================== --- trunk/octave-forge/main/fuzzy-logic-toolkit/inst/fcm.m (rev 0) +++ trunk/octave-forge/main/fuzzy-logic-toolkit/inst/fcm.m 2012-07-09 13:17:11 UTC (rev 10739) @@ -0,0 +1,204 @@ +## Copyright (C) 2012 L. Markowsky <lm...@us...> +## +## This file is part of the fuzzy-logic-toolkit. +## +## The fuzzy-logic-toolkit is free software; you can redistribute it +## and/or modify it under the terms of the GNU General Public License +## as published by the Free Software Foundation; either version 3 of +## the License, or (at your option) any later version. +## +## The fuzzy-logic-toolkit is distributed in the hope that it will be +## useful, but WITHOUT ANY WARRANTY; without even the implied warranty +## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with the fuzzy-logic-toolkit; see the file COPYING. If not, +## see <http://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{cluster_centers} =} fcm (@var{input_data}, @var{num_clusters}) +## @deftypefnx {Function File} {@var{cluster_centers} =} fcm (@var{input_data}, @var{num_clusters}, @var{options}) +## @deftypefnx {Function File} {@var{cluster_centers} =} fcm (@var{input_data}, @var{num_clusters}, [@var{m}, @var{max_iterations}, @var{epsilon}, @var{display_intermediate_results}]) +## @deftypefnx {Function File} {[@var{cluster_centers}, @var{soft_partition}, @var{obj_fcn_history}] =} fcm (@var{input_data}, @var{num_clusters}) +## @deftypefnx {Function File} {[@var{cluster_centers}, @var{soft_partition}, @var{obj_fcn_history}] =} fcm (@var{input_data}, @var{num_clusters}, @var{options}) +## @deftypefnx {Function File} {[@var{cluster_centers}, @var{soft_partition}, @var{obj_fcn_history}] =} fcm (@var{input_data}, @var{num_clusters}, [@var{m}, @var{max_iterations}, @var{epsilon}, @var{display_intermediate_results}]) +## +## Return the soft partition of a set of unlabeled data points. +## Also, if @var{display_intermediate_results} is true, display intermediate +## results after each iteration. +## +## The required arguments to fcm are: +## @itemize @w +## @item +## @var{input_data} - a matrix of input data points; each row corresponds to one point +## @item +## @var{num_clusters} - the number of clusters to form +## @end itemize +## +## The optional arguments to fcm are: +## @itemize @w +## @item +## @var{m} - the parameter (exponent) in the objective function; default = 2.0 +## @item +## @var{max_iterations} - the maximum number of iterations before stopping; default = 100 +## @item +## @var{epsilon} - the stopping criteria; default = 1e-5 +## @item +## @var{display_intermediate_results} - if 1, display results after each iteration, and if 0, do not; default = 1 +## @end itemize +## +## The default values are used if any of the optional arguments are missing or +## evaluate to NaN. +## +## The return values are: +## @itemize @w +## @item +## @var{cluster_centers} - a matrix of the cluster centers; each row corresponds to one point +## @item +## @var{soft_partition} - a constrained soft partition matrix +## @item +## @var{obj_fcn_history} - the values of the objective function after each iteration +## @end itemize +## +## Three important matrices used in the calculation are X (the input points +## to be clustered), V (the cluster centers), and Mu (the membership of each +## data point in each cluster). Each row of X and V denotes a single point, +## and Mu(i, j) denotes the membership degree of input point X(j, :) in the +## cluster having center V(i, :). +## +## X is identical to the required argument @var{input_data}; V is identical +## to the output @var{cluster_centers}; and Mu is identical to the output +## @var{soft_partition}. +## +## If n denotes the number of input points and k denotes the number of +## clusters to be formed, then X, V, and Mu have the dimensions: +## +## @example +## @group +## 1 2 ... #features +## 1 [ ] +## X = input_data = 2 [ ] +## ... [ ] +## n [ ] +## @end group +## @end example +## +## @example +## @group +## 1 2 ... #features +## 1 [ ] +## V = cluster_centers = 2 [ ] +## ... [ ] +## k [ ] +## @end group +## @end example +## +## @example +## @group +## 1 2 ... n +## 1 [ ] +## Mu = soft_partition = 2 [ ] +## ... [ ] +## k [ ] +## @end group +## @end example +## +## @seealso{fcm_demo_1, fcm_demo_2, gustafson_kessel, gustafson_kessel_demo_1, gustafson_kessel_demo_2, partition_coeff, partition_entropy, xie_beni_index} +## +## @end deftypefn + +## Author: L. Markowsky +## Keywords: fuzzy-logic-toolkit fuzzy partition clustering fcm +## Directory: fuzzy-logic-toolkit/inst/ +## Filename: fcm.m +## Last-Modified: 8 July 2012 + +function [cluster_centers, soft_partition, obj_fcn_history] = ... + fcm (input_data, num_clusters, options = [2.0, 100, 1e-5, 1]) + + ## If fcm was called with an incorrect number of arguments, or the + ## arguments do not have the correct type, print an error message and halt. + + if ((nargin != 2) && (nargin != 3)) + puts ("Type 'help fcm' for more information.\n"); + error ("fcm requires 2 or 3 arguments\n"); + elseif (!is_real_matrix (input_data)) + puts ("Type 'help fcm' for more information.\n"); + error ("fcm's first argument must be matrix of real numbers\n"); + elseif (!(is_int (num_clusters) && (num_clusters > 1))) + puts ("Type 'help fcm' for more information.\n"); + error ("fcm's second argument must be an integer greater than 1\n"); + elseif (!(isreal (options) && isvector (options))) + puts ("Type 'help fcm' for more information.\n"); + error ("fcm's third (optional) argument must be a vector of real numbers\n"); + endif + + ## Assign options to the more readable variable names: m, max_iterations, + ## epsilon, and display_intermediate_results. If options are missing or + ## NaN (not a number), use the default values. + + default_options = [2.0, 100, 1e-5, 1]; + + for i = 1 : 4 + if ((length (options) < i) || isna (options(i)) || isnan (options(i))) + options(i) = default_options(i); + endif + endfor + + m = options(1); + max_iterations = options(2); + epsilon = options(3); + display_intermediate_results = options(4); + + ## Call a private function to compute the output. + + [cluster_centers, soft_partition, obj_fcn_history] = ... + fcm_private (input_data, num_clusters, m, max_iterations, epsilon, + display_intermediate_results); +endfunction + +##------------------------------------------------------------------------------ +## Note: This function (fcm_private) is an implementation of Figure 13.4 in +## Fuzzy Logic: Intelligence, Control and Information, by J. Yen and +## R. Langari, Prentice Hall, 1999, page 380 (International Edition) +## and Algorithm 4.1 in Fuzzy and Neural Control, by Robert Babuska, +## November 2009, p. 63. +##------------------------------------------------------------------------------ + +function [V, Mu, obj_fcn_history] = ... + fcm_private (X, k, m, max_iterations, epsilon, display_intermediate_results) + + ## Initialize the prototype and the calculation. + V = fcm_init_prototype (X, k); + obj_fcn_history = zeros (max_iterations); + convergence_criterion = epsilon + 1; + iteration = 0; + + ## Calculate a few numbers here to reduce redundant computation. + k = rows (V); + n = rows (X); + sqr_dist = square_distance_matrix (X, V); + + ## Loop until the objective function is within tolerance or the maximum + ## number of iterations has been reached. + while (convergence_criterion > epsilon && ++iteration <= max_iterations) + V_previous = V; + Mu = fcm_update_membership_fcn (V, X, m, k, n, sqr_dist); + Mu_m = Mu .^ m; + V = fcm_update_cluster_centers (Mu_m, X, k); + sqr_dist = square_distance_matrix (X, V); + obj_fcn_history(iteration) = fcm_compute_objective_fcn (Mu_m, sqr_dist); + if (display_intermediate_results) + printf ("Iteration count = %d, Objective fcn = %8.6f\n", ... + iteration, obj_fcn_history(iteration)); + endif + convergence_criterion = fcm_compute_convergence_criterion (V, V_previous); + endwhile + + ## Remove extraneous entries from the tail of the objective function history. + if (convergence_criterion <= epsilon) + obj_fcn_history = obj_fcn_history(1 : iteration); + endif + +endfunction Added: trunk/octave-forge/main/fuzzy-logic-toolkit/inst/fcm_demo_1.m =================================================================== --- trunk/octave-forge/main/fuzzy-logic-toolkit/inst/fcm_demo_1.m (rev 0) +++ trunk/octave-forge/main/fuzzy-logic-toolkit/inst/fcm_demo_1.m 2012-07-09 13:17:11 UTC (rev 10739) @@ -0,0 +1,90 @@ +## Copyright (C) 2012 L. Markowsky <lm...@us...> +## +## This file is part of the fuzzy-logic-toolkit. +## +## The fuzzy-logic-toolkit is free software; you can redistribute it +## and/or modify it under the terms of the GNU General Public License +## as published by the Free Software Foundation; either version 3 of +## the License, or (at your option) any later version. +## +## The fuzzy-logic-toolkit is distributed in the hope that it will be +## useful, but WITHOUT ANY WARRANTY; without even the implied warranty +## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with the fuzzy-logic-toolkit; see the file COPYING. If not, +## see <http://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {Script File} {} fcm_demo_1 +## Use the Fuzzy C-Means algorithm to classify unlabeled data points and +## evaluate the quality of the resulting clusters. +## +## This demo: +## @itemize @minus +## @item +## classifies a small set of unlabeled data points using the Fuzzy C-Means +## algorithm into two fuzzy clusters +## @item +## calculates and prints (on standard output) three validity measures: +## the partition coefficient, the partition entropy, and the Xie-Beni +## validity index +## @item +## plots the input points together with the cluster centers +## @end itemize +## +## For a description of the data structures used in the script, see the +## documentation for fcm. +## +## @seealso{fcm, fcm_demo_2, partition_coeff, partition_entropy, xie_beni_index, gustafson_kessel, gustafson_kessel_demo_1, gustafson_kessel_demo_2} +## +## @end deftypefn + +## Author: L. Markowsky +## Keywords: fuzzy-logic-toolkit fuzzy partition clustering fcm demo +## Directory: fuzzy-logic-toolkit/inst/ +## Filename: fcm_demo_1.m +## Last-Modified: 8 July 2012 + +##------------------------------------------------------------------------------ +## Note: The input_data is taken from Chapter 13, Example 17 in +## Fuzzy Logic: Intelligence, Control and Information, by J. Yen and +## R. Langari, Prentice Hall, 1999, page 381 (International Edition). +##------------------------------------------------------------------------------ + +## Use fcm to classify the data in matrix x. +input_data = [2 12; 4 9; 7 13; 11 5; 12 7; 14 4] +number_of_clusters = 2 +[cluster_centers, soft_partition, obj_fcn_history] = ... + fcm (input_data, number_of_clusters) + +## Calculate and print the three validity measures. +printf ("Partition Coefficient: %f\n", partition_coeff (soft_partition)); +printf ("Partition Entropy (with a = 2): %f\n", partition_entropy (soft_partition, 2)); +printf ("Xie-Beni Index: %f\n\n", xie_beni_index (input_data, cluster_centers, soft_partition)); + +## Plot the data points as small blue x's. +figure ('NumberTitle', 'off', 'Name', 'FCM Demo 1'); +for i = 1 : rows (input_data) + plot (input_data(i, 1), input_data(i, 2), 'LineWidth', 2, 'marker', 'x', ... + 'color', 'b'); + hold on; +endfor + +## Plot the cluster centers as larger red *'s. +for i = 1 : number_of_clusters + plot (cluster_centers(i, 1), cluster_centers(i, 2), 'LineWidth', 4, ... + 'marker', '*', 'color', 'r'); + hold on; +endfor + +## Make the figure look a little better: +## -- scale and label the axes +## -- show gridlines +xlim ([0 15]); +ylim ([0 15]); +xlabel ('Feature 1'); +ylabel ('Feature 2'); +grid +hold Added: trunk/octave-forge/main/fuzzy-logic-toolkit/inst/fcm_demo_2.m =================================================================== --- trunk/octave-forge/main/fuzzy-logic-toolkit/inst/fcm_demo_2.m (rev 0) +++ trunk/octave-forge/main/fuzzy-logic-toolkit/inst/fcm_demo_2.m 2012-07-09 13:17:11 UTC (rev 10739) @@ -0,0 +1,121 @@ +## Copyright (C) 2012 L. Markowsky <lm...@us...> +## +## This file is part of the fuzzy-logic-toolkit. +## +## The fuzzy-logic-toolkit is free software; you can redistribute it +## and/or modify it under the terms of the GNU General Public License +## as published by the Free Software Foundation; either version 3 of +## the License, or (at your option) any later version. +## +## The fuzzy-logic-toolkit is distributed in the hope that it will be +## useful, but WITHOUT ANY WARRANTY; without even the implied warranty +## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with the fuzzy-logic-toolkit; see the file COPYING. If not, +## see <http://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {Script File} {} fcm_demo_2 +## Use the Fuzzy C-Means algorithm to classify unlabeled data points and +## evaluate the quality of the resulting clusters. +## +## The demo: +## @itemize @minus +## @item +## classifies three-dimensional unlabeled data points using the Fuzzy +## C-Means algorithm into three fuzzy clusters +## @item +## calculates and prints (on standard output) three validity measures: +## the partition coefficient, the partition entropy, and the Xie-Beni +## validity index +## @item +## plots the input points together with the cluster centers +## @end itemize +## +## For a description of the data structures used in the script, see the +## documentation for fcm. +## +## @seealso{fcm, fcm_demo_1, partition_coeff, partition_entropy, xie_beni_index, gustafson_kessel, gustafson_kessel_demo_1, gustafson_kessel_demo_2} +## +## @end deftypefn + +## Author: L. Markowsky +## Keywords: fuzzy-logic-toolkit fuzzy partition clustering fcm demo +## Directory: fuzzy-logic-toolkit/inst/ +## Filename: fcm_demo_2.m +## Last-Modified: 8 July 2012 + +##------------------------------------------------------------------------------ +## Note: The input_data was selected to form three areas of different shapes. +##------------------------------------------------------------------------------ + +## Use fcm to classify the data in matrix x. +input_data = [1 11 5; 1 12 6; 1 13 5; 2 11 7; 2 12 6; 2 13 7; 3 11 6; 3 12 5; + 3 13 7; 1 1 10; 1 3 9; 2 2 11; 3 1 9; 3 3 10; 3 5 11; 4 4 9; + 4 6 8; 5 5 8; 5 7 9; 6 6 10; 9 10 12; 9 12 13; 9 13 14; 10 9 13; + 10 13 12; 11 10 14; 11 12 13; 12 6 12; 12 7 15; 12 9 15; 14 6 14; + 14 8 13] +number_of_clusters = 3 +[cluster_centers, soft_partition, obj_fcn_history] = ... + fcm (input_data, number_of_clusters, [NaN NaN NaN 0]) + +## Calculate and print the three validity measures. +printf ("Partition Coefficient: %f\n", partition_coeff (soft_partition)); +printf ("Partition Entropy (with a = 2): %f\n", partition_entropy (soft_partition, 2)); +printf ("Xie-Beni Index: %f\n\n", xie_beni_index (input_data, cluster_centers, soft_partition)); + +## Plot the data points in two dimensions (using features 1 and 2) +## as small blue x's. +figure ('NumberTitle', 'off', 'Name', 'FCM Demo 2'); +for i = 1 : rows (input_data) + plot (input_data(i, 1), input_data(i, 2), 'LineWidth', 2, 'marker', 'x', ... + 'color', 'b'); + hold on; +endfor + +## Plot the cluster centers in two dimensions (using features 1 and 2) +## as larger red *'s. +for i = 1 : number_of_clusters + plot (cluster_centers(i, 1), cluster_centers(i, 2), 'LineWidth', 4, ... + 'marker', '*', 'color', 'r'); + hold on; +endfor + +## Make the figure look a little better: +## -- scale and label the axes +## -- show gridlines +xlim ([0 15]); +ylim ([0 15]); +xlabel ('Feature 1'); +ylabel ('Feature 2'); +grid +hold + +## Plot the data points in two dimensions (using features 1 and 3) +## as small blue x's. +figure ('NumberTitle', 'off', 'Name', 'FCM Demo 2'); +for i = 1 : rows (input_data) + plot (input_data(i, 1), input_data(i, 3), 'LineWidth', 2, 'marker', 'x', ... + 'color', 'b'); + hold on; +endfor + +## Plot the cluster centers in two dimensions (using features 1 and 3) +## as larger red *'s. +for i = 1 : number_of_clusters + plot (cluster_centers(i, 1), cluster_centers(i, 3), 'LineWidth', 4, ... + 'marker', '*', 'color', 'r'); + hold on; +endfor + +## Make the figure look a little better: +## -- scale and label the axes +## -- show gridlines +xlim ([0 15]); +ylim ([0 15]); +xlabel ('Feature 1'); +ylabel ('Feature 3'); +grid +hold Added: trunk/octave-forge/main/fuzzy-logic-toolkit/inst/gustafson_kessel.m =================================================================== --- trunk/octave-forge/main/fuzzy-logic-toolkit/inst/gustafson_kessel.m (rev 0) +++ trunk/octave-forge/main/fuzzy-logic-toolkit/inst/gustafson_kessel.m 2012-07-09 13:17:11 UTC (rev 10739) @@ -0,0 +1,272 @@ +## Copyright (C) 2012 L. Markowsky <lm...@us...> +## +## This file is part of the fuzzy-logic-toolkit. +## +## The fuzzy-logic-toolkit is free software; you can redistribute it +## and/or modify it under the terms of the GNU General Public License +## as published by the Free Software Foundation; either version 3 of +## the License, or (at your option) any later version. +## +## The fuzzy-logic-toolkit is distributed in the hope that it will be +## useful, but WITHOUT ANY WARRANTY; without even the implied warranty +## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with the fuzzy-logic-toolkit; see the file COPYING. If not, +## see <http://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{cluster_centers} =} gustafson_kessel (@var{input_data}, @var{num_clusters}) +## @deftypefnx {Function File} {@var{cluster_centers} =} gustafson_kessel (@var{input_data}, @var{num_clusters}, @var{cluster_volume}) +## @deftypefnx {Function File} {@var{cluster_centers} =} gustafson_kessel (@var{input_data}, @var{num_clusters}, @var{cluster_volume}, @var{options}) +## @deftypefnx {Function File} {@var{cluster_centers} =} gustafson_kessel (@var{input_data}, @var{num_clusters}, @var{cluster_volume}, [@var{m}, @var{max_iterations}, @var{epsilon}, @var{display_intermediate_results}]) +## @deftypefnx {Function File} {[@var{cluster_centers}, @var{soft_partition}, @var{obj_fcn_history}] =} gustafson_kessel (@var{input_data}, @var{num_clusters}) +## @deftypefnx {Function File} {[@var{cluster_centers}, @var{soft_partition}, @var{obj_fcn_history}] =} gustafson_kessel (@var{input_data}, @var{num_clusters}, @var{cluster_volume}) +## @deftypefnx {Function File} {[@var{cluster_centers}, @var{soft_partition}, @var{obj_fcn_history}] =} gustafson_kessel (@var{input_data}, @var{num_clusters}, @var{cluster_volume}, @var{options}) +## @deftypefnx {Function File} {[@var{cluster_centers}, @var{soft_partition}, @var{obj_fcn_history}] =} gustafson_kessel (@var{input_data}, @var{num_clusters}, @var{cluster_volume}, [@var{m}, @var{max_iterations}, @var{epsilon}, @var{display_intermediate_results}]) +## +## Return the soft partition of a set of unlabeled data points. +## Also, if @var{display_intermediate_results} is true, display intermediate +## results after each iteration. +## +## The required arguments to gustafson_kessel are: +## @itemize @w +## @item +## @var{input_data} - a matrix of input data points; each row corresponds to one point +## @item +## @var{num_clusters} - the number of clusters to form +## @end itemize +## +## The third (optional) argument to gustafson_kessel is a vector of cluster volumes. +## If omitted, a vector of 1's will be used as the default. +## +## The fourth (optional) argument to gustafson_kessel is a vector consisting of: +## @itemize @w +## @item +## @var{m} - the parameter (exponent) in the objective function; default = 2.0 +## @item +## @var{max_iterations} - the maximum number of iterations before stopping; default = 100 +## @item +## @var{epsilon} - the stopping criteria; default = 1e-5 +## @item +## @var{display_intermediate_results} - if 1, display results after each iteration, and if 0, do not; default = 1 +## @end itemize +## +## The default values are used if any of the four elements of the vector are missing or +## evaluate to NaN. +## +## The return values are: +## @itemize @w +## @item +## @var{cluster_centers} - a matrix of the cluster centers; each row corresponds to one point +## @item +## @var{soft_partition} - a constrained soft partition matrix +## @item +## @var{obj_fcn_history} - the values of the objective function after each iteration +## @end itemize +## +## Three important matrices used in the calculation are X (the input points +## to be clustered), V (the cluster centers), and Mu (the membership of each +## data point in each cluster). Each row of X and V denotes a single point, +## and Mu(i, j) denotes the membership degree of input point X(j, :) in the +## cluster having center V(i, :). +## +## X is identical to the required argument @var{input_data}; V is identical +## to the output @var{cluster_centers}; and Mu is identical to the output +## @var{soft_partition}. +## +## If n denotes the number of input points and k denotes the number of +## clusters to be formed, then X, V, and Mu have the dimensions: +## +## @example +## @group +## 1 2 ... #features +## 1 [ ] +## X = input_data = 2 [ ] +## ... [ ] +## n [ ] +## @end group +## @end example +## +## @example +## @group +## 1 2 ... #features +## 1 [ ] +## V = cluster_centers = 2 [ ] +## ... [ ] +## k [ ] +## @end group +## @end example +## +## @example +## @group +## 1 2 ... n +## 1 [ ] +## Mu = soft_partition = 2 [ ] +## ... [ ] +## k [ ] +## @end group +## @end example +## +## @seealso{fcm, fcm_demo_1, fcm_demo_2, gustafson_kessel_demo_1, gustafson_kessel_demo_2, partition_coeff, partition_entropy, xie_beni_index} +## +## @end deftypefn + +## Author: L. Markowsky +## Keywords: fuzzy-logic-toolkit fuzzy partition clustering gustafson_kessel +## Directory: fuzzy-logic-toolkit/inst/ +## Filename: gustafson_kessel.m +## Last-Modified: 8 July 2012 + +function [cluster_centers, soft_partition, obj_fcn_history] = ... + gustafson_kessel (input_data, num_clusters, cluster_volume = [], options = [2.0, 100, 1e-5, 1]) + + ## If gustafson_kessel was called with an incorrect number of arguments, or the + ## arguments do not have the correct type, print an error message and halt. + + if ((nargin < 2) || (nargin > 4)) + puts ("Type 'help gustafson_kessel' for more information.\n"); + error ("gustafson_kessel requires 2, 3, or 4 arguments\n"); + elseif (!is_real_matrix (input_data)) + puts ("Type 'help gustafson_kessel' for more information.\n"); + error ("gustafson_kessel's first argument must be matrix of real numbers\n"); + elseif (!(is_int (num_clusters) && (num_clusters > 1))) + puts ("Type 'help gustafson_kessel' for more information.\n"); + error ("gustafson_kessel's second argument must be an integer greater than 1\n"); + elseif (!(isequal (cluster_volume, []) || ... + (isreal (cluster_volume) && isvector (cluster_volume)))) + puts ("Type 'help gustafson_kessel' for more information.\n"); + error ("gustafson_kessel's third (optional) argument must be a vector of real numbers\n"); + elseif (!(isreal (options) && isvector (options))) + puts ("Type 'help gustafson_kessel' for more information.\n"); + error ("gustafson_kessel's fourth (optional) argument must be a vector of real numbers\n"); + endif + + ## If the cluster volume matrix was not entered, create a default value + ## (a vector of 1's). + + if (isequal (cluster_volume, [])) + cluster_volume = ones (1, num_clusters); + endif + + ## Assign options to the more readable variable names: m, max_iterations, + ## epsilon, and display_intermediate_results. If options are missing or + ## NaN (not a number), use the default values. + + default_options = [2.0, 100, 1e-5, 1]; + + for i = 1 : 4 + if ((length (options) < i) || isna (options(i)) || isnan (options(i))) + options(i) = default_options(i); + endif + endfor + + m = options(1); + max_iterations = options(2); + epsilon = options(3); + display_intermediate_results = options(4); + + ## Call a private function to compute the output. + + [cluster_centers, soft_partition, obj_fcn_history] = ... + gustafson_kessel_private (input_data, num_clusters, cluster_volume, m, ... + max_iterations, epsilon, display_intermediate_results); +endfunction + +##------------------------------------------------------------------------------ +## Function: gustafson_kessel_private +## Purpose: Classify unlabeled data points using the Gustafson-Kessel +## algorithm. +## Note: This function (gustafson_kessel_private) is an implementation of +## Algorithm 4.2 in Fuzzy and Neural Control, by Robert Babuska, +## November 2009, p. 69. +##------------------------------------------------------------------------------ + +function [V, Mu, obj_fcn_history] = ... + gustafson_kessel_private (X, k, cluster_volume, m, max_iterations, epsilon, display_intermediate_results) + + ## Initialize the prototype and the calculation. + V = fcm_init_prototype (X, k); + obj_fcn_history = zeros (max_iterations); + convergence_criterion = epsilon + 1; + iteration = 0; + + ## Calculate a few numbers here to reduce redundant computation. + k = rows (V); + n = rows (X); + sqr_dist = square_distance_matrix (X, V); + + ## Loop until the objective function is within tolerance or the maximum + ## number of iterations has been reached. + while (convergence_criterion > epsilon && ++iteration <= max_iterations) + V_previous = V; + Mu = fcm_update_membership_fcn (V, X, m, k, n, sqr_dist); + Mu_m = Mu .^ m; + V = fcm_update_cluster_centers (Mu_m, X, k); + sqr_dist = gk_square_distance_matrix (X, V, Mu_m, cluster_volume); + obj_fcn_history(iteration) = fcm_compute_objective_fcn (Mu_m, sqr_dist); + if (display_intermediate_results) + printf ("Iteration count = %d, Objective fcn = %8.6f\n", ... + iteration, obj_fcn_history(iteration)); + endif + convergence_criterion = fcm_compute_convergence_criterion (V, V_previous); + endwhile + + ## Remove extraneous entries from the tail of the objective function history. + if (convergence_criterion <= epsilon) + obj_fcn_history = obj_fcn_history(1 : iteration); + endif + +endfunction + +##------------------------------------------------------------------------------ +## Function: gk_square_distance_matrix +##------------------------------------------------------------------------------ + +function sqr_dist = gk_square_distance_matrix (X, V, Mu_m, cluster_volume) + + k = rows (V); + n = rows (X); + num_features = columns (X); + sqr_dist = zeros (k, n); + + for i = 1 : k + Vi = V(i, :); + covariance_matrix = compute_covariance_matrix (X, V, Mu_m, i); + + for j = 1 : n + Vi_to_Xj = X(j, :) - Vi; + A = cluster_volume(i) * ... + det (covariance_matrix) ^ (1.0 / num_features) * ... + inv (covariance_matrix); + sqr_dist(i, j) = sum (Vi_to_Xj .* (A * Vi_to_Xj')'); + + endfor + + endfor + +endfunction + +##------------------------------------------------------------------------------ +## Function: compute_covariance_matrix +##------------------------------------------------------------------------------ + +function covariance_matrix = compute_covariance_matrix (X, V, Mu_m, i) + + num_features = columns (V); + n = rows (X); + num = zeros (num_features); + denom = 0.0; + Vi = V(i, :); + + for j = 1 : n + Vi_to_Xj = X(j, :) - Vi; + num += Mu_m(i, j) * Vi_to_Xj' * Vi_to_Xj; + denom += Mu_m(i, j); + endfor + + covariance_matrix = num / denom; + +endfunction + Added: trunk/octave-forge/main/fuzzy-logic-toolkit/inst/gustafson_kessel_demo_1.m =================================================================== --- trunk/octave-forge/main/fuzzy-logic-toolkit/inst/gustafson_kessel_demo_1.m (rev 0) +++ trunk/octave-forge/main/fuzzy-logic-toolkit/inst/gustafson_kessel_demo_1.m 2012-07-09 13:17:11 UTC (rev 10739) @@ -0,0 +1,90 @@ +## Copyright (C) 2012 L. Markowsky <lm...@us...> +## +## This file is part of the fuzzy-logic-toolkit. +## +## The fuzzy-logic-toolkit is free software; you can redistribute it +## and/or modify it under the terms of the GNU General Public License +## as published by the Free Software Foundation; either version 3 of +## the License, or (at your option) any later version. +## +## The fuzzy-logic-toolkit is distributed in the hope that it will be +## useful, but WITHOUT ANY WARRANTY; without even the implied warranty +## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with the fuzzy-logic-toolkit; see the file COPYING. If not, +## see <http://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {Script File} {} gustafson_kessel_demo_1 +## Use the Gustafson-Kessel algorithm to classify unlabeled data points and +## evaluate the quality of the resulting clusters. +## +## The demo: +## @itemize @minus +## @item +## classifies a small set of unlabeled data points using the Gustafson-Kessel +## algorithm into two fuzzy clusters +## @item +## calculates and prints (on standard output) three validity measures: +## the partition coefficient, the partition entropy, and the Xie-Beni +## validity index +## @item +## plots the input points together with the cluster centers +## @end itemize +## +## For a description of the data structures used in the script, see the +## documentation for gustafson_kessel. +## +## @seealso{gustafson_kessel, gustafson_kessel_demo_2, partition_coeff, partition_entropy, xie_beni_index, fcm, fcm_demo_1, fcm_demo_2} +## +## @end deftypefn + +## Author: L. Markowsky +## Keywords: fuzzy-logic-toolkit fuzzy partition clustering gustafson_kessel demo +## Directory: fuzzy-logic-toolkit/inst/ +## Filename: gustafson_kessel_demo_1.m +## Last-Modified: 8 July 2012 + +##------------------------------------------------------------------------------ +## Note: The input_data is taken from Chapter 13, Example 17 in +## Fuzzy Logic: Intelligence, Control and Information, by J. Yen and +## R. Langari, Prentice Hall, 1999, page 381 (International Edition). +##------------------------------------------------------------------------------ + +## Use gustafson_kessel to classify the data in matrix x. +input_data = [2 12; 4 9; 7 13; 11 5; 12 7; 14 4] +number_of_clusters = 2 +[cluster_centers, soft_partition, obj_fcn_history] = ... + gustafson_kessel (input_data, number_of_clusters) + +## Calculate and print the three validity measures. +printf ("Partition Coefficient: %f\n", partition_coeff (soft_partition)); +printf ("Partition Entropy (with a = 2): %f\n", partition_entropy (soft_partition, 2)); +printf ("Xie-Beni Index: %f\n\n", xie_beni_index (input_data, cluster_centers, soft_partition)); + +## Plot the data points as small blue x's. +figure ('NumberTitle', 'off', 'Name', 'Gustafson-Kessel Demo 1'); +for i = 1 : rows (input_data) + plot (input_data(i, 1), input_data(i, 2), 'LineWidth', 2, 'marker', 'x', ... + 'color', 'b'); + hold on; +endfor + +## Plot the cluster centers as larger red *'s. +for i = 1 : number_of_clusters + plot (cluster_centers(i, 1), cluster_centers(i, 2), 'LineWidth', 4, ... + 'marker', '*', 'color', 'r'); + hold on; +endfor + +## Make the figure look a little better: +## -- scale and label the axes +## -- show gridlines +xlim ([0 15]); +ylim ([0 15]); +xlabel ('Feature 1'); +ylabel ('Feature 2'); +grid +hold Added: trunk/octave-forge/main/fuzzy-logic-toolkit/inst/gustafson_kessel_demo_2.m =================================================================== --- trunk/octave-forge/main/fuzzy-logic-toolkit/inst/gustafson_kessel_demo_2.m (rev 0) +++ trunk/octave-forge/main/fuzzy-logic-toolkit/inst/gustafson_kessel_demo_2.m 2012-07-09 13:17:11 UTC (rev 10739) @@ -0,0 +1,120 @@ +## Copyright (C) 2012 L. Markowsky <lm...@us...> +## +## This file is part of the fuzzy-logic-toolkit. +## +## The fuzzy-logic-toolkit is free software; you can redistribute it +## and/or modify it under the terms of the GNU General Public License +## as published by the Free Software Foundation; either version 3 of +## the License, or (at your option) any later version. +## +## The fuzzy-logic-toolkit is distributed in the hope that it will be +## useful, but WITHOUT ANY WARRANTY; without even the implied warranty +## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with the fuzzy-logic-toolkit; see the file COPYING. If not, +## see <http://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {Script File} {} gustafson_kessel_demo_2 +## Use the Gustafson-Kessel algorithm to classify unlabeled data points and +## evaluate the quality of the resulting clusters. +## +## The demo: +## @itemize @minus +## @item +## classifies three-dimensional unlabeled data points using the +## Gustafson-Kessel algorithm into three fuzzy clusters +## @item +## calculates and prints (on standard output) three validity measures: +## the partition coefficient, the partition entropy, and the Xie-Beni +## validity index +## @item +## plots the input points together with the cluster centers +## @end itemize +## +## For a description of the data structures used in the script, see the +## documentation for gustafson_kessel. +## +## @seealso{gustafson_kessel, gustafson_kessel_demo_1, partition_coeff, partition_entropy, xie_beni_index, fcm, fcm_demo_1, fcm_demo_2} +## +## @end deftypefn + +## Author: L. Markowsky +## Keywords: fuzzy-logic-toolkit fuzzy partition clustering fcm demo +## Directory: fuzzy-logic-toolkit/inst/ +## Filename: gustafson_kessel_demo_2.m +## Last-Modified: 8 July 2012 + +##------------------------------------------------------------------------------ +## Note: The input_data was selected to form three areas of different shapes. +##------------------------------------------------------------------------------ + +## Use gustafson_kessel to classify the data in matrix x. +input_data = [1 11 5; 1 12 6; 1 13 5; 2 11 7; 2 12 6; 2 13 7; 3 11 6; 3 12 5; + 3 13 7; 1 1 10; 1 3 9; 2 2 11; 3 1 9; 3 3 10; 3 5 11; 4 4 9; + 4 6 8; 5 5 8; 5 7 9; 6 6 10; 9 10 12; 9 12 13; 9 13 14; 10 9 13; + 10 13 12; 11 10 14; 11 12 13; 12 6 12; 12 7 15; 12 9 15; 14 6 14; + 14 8 13] +number_of_clusters = 3 +[cluster_centers, soft_partition, obj_fcn_history] = ... + gustafson_kessel (input_data, number_of_clusters, [1 1 1], [NaN NaN NaN 0]) + +## Calculate and print the three validity measures. +printf ("Partition Coefficient: %f\n", partition_coeff (soft_partition)); +printf ("Partition Entropy (with a = 2): %f\n", partition_entropy (soft_partition, 2)); +printf ("Xie-Beni Index: %f\n\n", xie_beni_index (input_data, cluster_centers, soft_partition)); + +## Plot the data points in two dimensions (using features 1 and 2) +## as small blue x's. +figure ('NumberTitle', 'off', 'Name', 'Gustafson-Kessel Demo 2'); +for i = 1 : rows (input_data) + plot (input_data(i, 1), input_data(i, 2), 'LineWidth', 2, 'marker', 'x', ... + 'color', 'b'); + hold on; +endfor + +## Plot the cluster centers in two dimensions (using features 1 and 2) +## as larger red *'s. +for i = 1 : number_of_clusters + plot (cluster_centers(i, 1), cluster_centers(i, 2), 'LineWidth', 4, ... + 'marker', '*', 'color', 'r'); + hold on; +endfor + +## Make the figure look a little better: +## -- scale and label the axes +## -- show gridlines +xlim ([0 15]); +ylim ([0 15]); +xlabel ('Feature 1'); +ylabel ('Feature 2'); +grid + +## Plot the data points in two dimensions (using features 1 and 3) +## as small blue x's. +figure ('NumberTitle', 'off', 'Name', 'Gustafson-Kessel Demo 2'); +for i = 1 : rows (input_data) + plot (input_data(i, 1), input_data(i, 3), 'LineWidth', 2, 'marker', 'x', ... + 'color', 'b'); + hold on; +endfor + +## Plot the cluster centers in two dimensions (using features 1 and 3) +## as larger red *'s. +for i = 1 : number_of_clusters + plot (cluster_centers(i, 1), cluster_centers(i, 3), 'LineWidth', 4, ... + 'marker', '*', 'color', 'r'); + hold on; +endfor + +## Make the figure look a little better: +## -- scale and label the axes +## -- show gridlines +xlim ([0 15]); +ylim ([0 15]); +xlabel ('Feature 1'); +ylabel ('Feature 3'); +grid +hold Added: trunk/octave-forge/main/fuzzy-logic-toolkit/inst/partition_coeff.m =================================================================== --- trunk/octave-forge/main/fuzzy-logic-toolkit/inst/partition_coeff.m (rev 0) +++ trunk/octave-forge/main/fuzzy-logic-toolkit/inst/partition_coeff.m 2012-07-09 13:17:11 UTC (rev 10739) @@ -0,0 +1,76 @@ +## Copyright (C) 2012 L. Markowsky <lm...@us...> +## +## This file is part of the fuzzy-logic-toolkit. +## +## The fuzzy-logic-toolkit is free software; you can redistribute it +## and/or modify it under the terms of the GNU General Public License +## as published by the Free Software Foundation; either version 3 of +## the License, or (at your option) any later version. +## +## The fuzzy-logic-toolkit is distributed in the hope that it will be +## useful, but WITHOUT ANY WARRANTY; without even the implied warranty +## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with the fuzzy-logic-toolkit; see the file COPYING. If not, +## see <http://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{vpc} =} partition_coeff (@var{soft_partition}) +## +## Return the partition coefficient for a given soft partition. +## +## The argument to partition_coeff is: +## @itemize @w +## @item +## @var{soft_partition} - the membership degree of each input data point in each cluster +## @end itemize +## +## The return value is: +## @itemize @w +## @item +## @var{vpc} - the partition coefficient for the given soft partition +## @end itemize +## +## For more information about the @var{soft_partition} matrix, please see the +## documentation for function fcm. +## +## @seealso{fcm, fcm_demo_1, fcm_demo_2, gustafson_kessel, gustafson_kessel_demo_1, gustafson_kessel_demo_2, partition_entropy, xie_beni_index} +## +## @end deftypefn + +## Author: L. Markowsky +## Keywords: fuzzy-logic-toolkit fuzzy partition coefficient cluster validity +## Directory: fuzzy-logic-toolkit/inst/ +## Filename: partition_coeff.m +## Last-Modified: 8 July 2012 + +##------------------------------------------------------------------------------ +## Note: This function is an implementation of Equation 13.9 (corrected -- the +## equation in the book omits the exponent 2) in +## Fuzzy Logic: Intelligence, Control and Information, by J. Yen and +## R. Langari, Prentice Hall, 1999, page 384 (International Edition). +##------------------------------------------------------------------------------ + +function vpc = partition_coeff (soft_partition) + + ## If partition_coeff was called with an incorrect number of arguments, or the + ## argument does not have the correct type, print an error message and halt. + + if (nargin != 1) + puts ("Type 'help partition_coeff' for more information.\n"); + error ("partition_coeff requires 1 argument\n"); + elseif (!(is_real_matrix (soft_partition) && + (min (min (soft_partition)) >= 0) && + (max (max (soft_partition)) <= 1))) + puts ("Type 'help partition_coeff' for more information.\n"); + error ("partition_coeff's argument must be a matrix of real numbers mu, with 0 <= mu <= 1\n"); + endif + + ## Compute and return the partition coefficient. + + soft_part_sqr = soft_partition .* soft_partition; + vpc = (sum (sum (soft_part_sqr))) / columns (soft_partition); + +endfunction Added: trunk/octave-forge/main/fuzzy-logic-toolkit/inst/partition_entropy.m =================================================================== --- trunk/octave-forge/main/fuzzy-logic-toolkit/inst/partition_entropy.m (rev 0) +++ trunk/octave-forge/main/fuzzy-logic-toolkit/inst/partition_entropy.m 2012-07-09 13:17:11 UTC (rev 10739) @@ -0,0 +1,82 @@ +## Copyright (C) 2012 L. Markowsky <lm...@us...> +## +## This file is part of the fuzzy-logic-toolkit. +## +## The fuzzy-logic-toolkit is free software; you can redistribute it +## and/or modify it under the terms of the GNU General Public License +## as published by the Free Software Foundation; either version 3 of +## the License, or (at your option) any later version. +## +## The fuzzy-logic-toolkit is distributed in the hope that it will be +## useful, but WITHOUT ANY WARRANTY; without even the implied warranty +## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with the fuzzy-logic-toolkit; see the file COPYING. If not, +## see <http://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{vpe} =} partition_entropy (@var{soft_partition}, @var{a}) +## +## Return the partition entropy for a given soft partition. +## +## The arguments to partition_entropy are: +## @itemize @w +## @item +## @var{soft_partition} - the membership degree of each input data point in each cluster +## @item +## @var{a} - the log base to use in the calculation; must be a real number a > 1 +## @end itemize +## +## The return value is: +## @itemize @w +## @item +## @var{vpe} - the partition entropy for the given soft partition +## @end itemize +## +## For more information about the @var{soft_partition} matrix, please see the +## documentation for function fcm. +## +## @seealso{fcm, fcm_demo_1, fcm_demo_2, gustafson_kessel, gustafson_kessel_demo_1, gustafson_kessel_demo_2, partition_coeff, xie_beni_index} +## +## @end deftypefn + +## Author: L. Markowsky +## Keywords: fuzzy-logic-toolkit fuzzy partition entropy cluster validity +## Directory: fuzzy-logic-toolkit/inst/ +## Filename: partition_entropy.m +## Last-Modified: 8 July 2012 + +##------------------------------------------------------------------------------ +## Note: This function is an implementation of Equation 13.10 in +## Fuzzy Logic: Intelligence, Control and Information, by J. Yen and +## R. Langari, Prentice Hall, 1999, page 384 (International Edition). +##------------------------------------------------------------------------------ + +function vpe = partition_entropy (soft_partition, a) + + ## If partition_entropy was called with an incorrect number of arguments, or the + ## argument does not have the correct type, print an error message and halt. + + if (nargin != 2) + puts ("Type 'help partition_entropy' for more information.\n"); + error ("partition_entropy requires 2 arguments\n"); + elseif (!(is_real_matrix (soft_partition) && + (min (min (soft_partition)) >= 0) && + (max (max (soft_partition)) <= 1))) + puts ("Type 'help partition_entropy' for more information.\n"); + error ("partition_entropy's first argument must be a matrix of real numbers mu, with 0 <= mu <= 1\n"); + elseif (!(is_real (a) && a > 1)) + puts ("Type 'help partition_entropy' for more information.\n"); + error ("partition_entropy's second argument argument must be a real number a > 1\n"); + endif + + ## Compute and return the partition entropy. + + n = columns (soft_partition); + Mu = soft_partition; + log_a_Mu = log (Mu) / log (a); + vpe = -(sum (sum (Mu .* log_a_Mu))) / n; + +endfunction Added: trunk/octave-forge/main/fuzzy-logic-toolkit/inst/private/fcm_compute_convergence_criterion.m =================================================================== --- trunk/octave-forge/main/fuzzy-logic-toolkit/inst/private/fcm_compute_convergence_criterion.m (rev 0) +++ trunk/octave-forge/main/fuzzy-logic-toolkit/inst/private/fcm_compute_convergence_criterion.m 2012-07-09 13:17:11 UTC (rev 10739) @@ -0,0 +1,40 @@ +## Copyright (C) 2012 L. Markowsky <lm...@us...> +## +## This file is part of the fuzzy-logic-toolkit. +## +## The fuzzy-logic-toolkit is free software; you can redistribute it +## and/or modify it under the terms of the GNU General Public License +## as published by the Free Software Foundation; either version 3 of +## the License, or (at your option) any later version. +## +## The fuzzy-logic-toolkit is distributed in the hope that it will be +## useful, but WITHOUT ANY WARRANTY; without even the implied warranty +## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with the fuzzy-logic-toolkit; see the file COPYING. If not, +## see <http://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{convergence_criterion} =} fcm_compute_convergence_criterion (@var{V}, @var{V_previous}) +## +## Compute the sum of the changes in position (using the Euclidean +## distance) of the cluster centers. +## +## @seealso{fcm, fcm_init_prototype, fcm_update_membership_fcn, fcm_update_cluster_centers, fcm_compute_objective_fcn} +## +## @end deftypefn + +## Author: L. Markowsky +## Keywords: fuzzy-logic-toolkit fuzzy partition clustering fcm private +## Directory: fuzzy-logic-toolkit/inst/private/ +## Filename: fcm_compute_convergence_criterion.m +## Last-Modified: 7 July 2012 + +function convergence_criterion = fcm_compute_convergence_criterion (V, V_previous) + + V_delta = V - V_previous; + convergence_criterion = sum (sqrt (sum (V_delta .* V_delta)')); + +endfunction Added: trunk/octave-forge/main/fuzzy-logic-toolkit/inst/private/fcm_compute_objective_fcn.m =================================================================== --- trunk/octave-forge/main/fuzzy-logic-toolkit/inst/private/fcm_compute_objective_fcn.m (rev 0) +++ trunk/octave-forge/main/fuzzy-logic-toolkit/inst/private/fcm_compute_objective_fcn.m 2012-07-09 13:17:11 UTC (rev 10739) @@ -0,0 +1,44 @@ +## Copyright (C) 2012 L. Markowsky <lm...@us...> +## +## This file is part of the fuzzy-logic-toolkit. +## +## The fuzzy-logic-toolkit is free software; you can redistribute it +## and/or modify it under the terms of the GNU General Public License +## as published by the Free Software Foundation; either version 3 of +## the License, or (at your option) any later version. +## +## The fuzzy-logic-toolkit is distributed in the hope that it will be +## useful, but WITHOUT ANY WARRANTY; without even the implied warranty +## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with the fuzzy-logic-toolkit; see the file COPYING. If not, +## see <http://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{obj_fcn} =} fcm_compute_objective_fcn (@var{Mu_m}, @var{sqr_dist}) +## +## Compute the objective function for the current iteration. +## +## @seealso{fcm, fcm_init_prototype, fcm_update_membership_fcn, fcm_update_cluster_centers, fcm_compute_convergence_criterion} +## +## @end deftypefn + +## Author: L. Markowsky +## Keywords: fuzzy-logic-toolkit fuzzy partition clustering fcm private +## Directory: fuzzy-logic-toolkit/inst/private/ +## Filename: fcm_compute_objective_fcn.m +## Last-Modified: 7 July 2012 + +##------------------------------------------------------------------------------ +## Note: This function is an implementation of Equation 13.3 in +## Fuzzy Logic: Intelligence, Control and Information, by J. Yen and +## R. Langari, Prentice Hall, 1999, page 379 (International Edition). +##------------------------------------------------------------------------------ + +function obj_fcn = fcm_compute_objective_fcn (Mu_m, sqr_dist) + + obj_fcn = sum (sum (Mu_m .* sqr_dist)); + +endfunction Added: trunk/octave-forge/main/fuzzy-logic-toolkit/inst/private/fcm_init_prototype.m =================================================================== --- trunk/octave-forge/main/fuzzy-logic-toolkit/inst/private/fcm_init_prototype.m (rev 0) +++ trunk/octave-forge/main/fuzzy-logic-toolkit/inst/private/fcm_init_prototype.m 2012-07-09 13:17:11 UTC (rev 10739) @@ -0,0 +1,47 @@ +## Copyright (C) 2012 L. Markowsky <lm...@us...> +## +## This file is part of the fuzzy-logic-toolkit. +## +## The fuzzy-logic-toolkit is free software; you can redistribute it +## and/or modify it under the terms of the GNU General Public License +## as published by the Free Software Foundation; either version 3 of +## the License, or (at your option) any later version. +## +## The fuzzy-logic-toolkit is distributed in the hope that it will be +## useful, but WITHOUT ANY WARRANTY; without even the implied warranty +## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with the fuzzy-logic-toolkit; see the file COPYING. If not, +## see <http://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{V} =} fcm_init_prototype (@var{X}, @var{k}) +## +## Initialize k cluster centers to random locations in the ranges +## given by the min/max values of each feature of the dataset. +## +## @seealso{fcm, fcm_update_membership_fcn, fcm_update_cluster_centers, fcm_compute_objective_fcn, fcm_compute_convergence_criterion} +## +## @end deftypefn + +## Author: L. Markowsky +## Keywords: fuzzy-logic-toolkit fuzzy partition clustering fcm private +## Directory: fuzzy-logic-toolkit/inst/private/ +## Filename: fcm_init_prototype.m +## Last-Modified: 7 July 2012 + +function V = fcm_init_prototype (X, k) + + num_features = columns (X); + min_feature_value = min (X); + max_feature_value = max (X); + V = rand (k, num_features); + + for i = 1 : num_features + V(:, i) = (max_feature_value(i) - min_feature_value(i)) * V(:, i) + ... + min_feature_value(i); + endfor + +endfunction Added: trunk/octave-forge/main/fuzzy-logic-toolkit/inst/private/fcm_update_cluster_centers.m =================================================================== --- trunk/octave-forge/main/fuzzy-logic-toolkit/inst/private/fcm_update_cluster_centers.m (rev 0) +++ trunk/octave-forge/main/fuzzy-logic-toolkit/inst/private/fcm_update_cluster_centers.m 2012-07-09 13:17:11 UTC (rev 10739) @@ -0,0 +1,54 @@ +## Copyright (C) 2012 L. Markowsky <lm...@us...> +## +## This file is part of the fuzzy-logic-toolkit. +## +## The fuzzy-logic-toolkit is free software; you can redistribute it +## and/or modify it under the terms of the GNU General Public License +## as published by the Free Software Foundation; either version 3 of +## the License, or (at your option) any later version. +## +## The fuzzy-logic-toolkit is distributed in the hope that it will be +## useful, but WITHOUT ANY WARRANTY; without even the implied warranty +## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with the fuzzy-logic-toolkit; see the file COPYING. If not, +## see <http://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{V} =} fcm_update_cluster_centers (@var{Mu_m}, @var{X}, @var{k}) +## +## Update the cluster centers to correspond to the given membership +## function values. +## +## @seealso{fcm, fcm_init_prototype, fcm_update_membership_fcn, fcm_compute_objective_fcn, fcm_compute_convergence_criterion} +## +## @end deftypefn + +## Author: L. Markowsky +## Keywords: fuzzy-logic-toolkit fuzzy partition clustering fcm private +## Directory: fuzzy-logic-toolkit/inst/private/ +## Filename: fcm_update_cluster_centers.m +## Last-Modified: 7 July 2012 + +##------------------------------------------------------------------------------ +## Note: This function is an implementation of Equation 13.5 in +## Fuzzy Logic: Intelligence, Control and Information, by J. Yen and +## R. Langari, Prentice Hall, 1999, page 380 (International Edition). +##------------------------------------------------------------------------------ + +function V = fcm_update_cluster_centers (Mu_m, X, k) + + V = Mu_m * X; + sum_Mu_m = sum (Mu_m'); + + if (prod (sum_Mu_m) == 0) + error ("division by 0 in function fcm_update_cluster_centers\n"); + endif + + for i = 1 : k + V(i, :) /= sum_Mu_m(i); + endfor + +endfunction Added: trunk/octave-forge/main/fuzzy-logic-toolkit/inst/private/fcm_update_membership_fcn.m =================================================================== --- trunk/octave-forge/main/fuzzy-logic-toolkit/inst/private/fcm_update_membership_fcn.m (rev 0) +++ trunk/octave-forge/main/fuzzy-logic-toolkit/inst/private/fcm_update_membership_fcn.m 2012-07-09 13:17:11 UTC (rev 10739) @@ -0,0 +1,75 @@ +## Copyright (C) 2012 L. Markowsky <lm...@us...> +## +## This file is part of the fuzzy-logic-toolkit. +## +## The fuzzy-logic-toolkit is free software; you can redistribute it +## and/or modify it under the terms of the GNU General Public License +## as published by the Free Software Foundation; either version 3 of +## the License, or (at your option) any later version. +## +## The fuzzy-logic-toolkit is distributed in the hope that it will be +## useful, but WITHOUT ANY WARRANTY; without even the implied warranty +## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with the fuzzy-logic-toolkit; see the file COPYING. If not, +## see <http://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{Mu} =} fcm_update_membership_fcn (@var{V}, @var{X}, @var{m}, @var{k}, @var{n}, @var{sqr_dist}) +## +## Compute Mu for each (cluster center, input point) pair. +## +## @seealso{fcm, fcm_init_prototype, fcm_update_cluster_centers, fcm_compute_objective_fcn, fcm_compute_convergence_criterion} +## +## @end deftypefn + +## Author: L. Markowsky +## Keywords: fuzzy-logic-toolkit fuzzy partition clustering fcm private +## Directory: fuzzy-logic-toolkit/inst/private/ +## Filename: fcm_update_membership_fcn.m +## Last-Modified: 7 July 2012 + +##------------------------------------------------------------------------------ +## Note: This function is an implementation of Equation 13.4 in +## Fuzzy Logic: Intelligence, Control and Information, by J. Yen and +## R. Langari, Prentice Hall, 1999, page 380 (International Edition) +## and Step 3 of Algorithm 4.1 in Fuzzy and Neural Control, +## by Robert Babuska, November 2009, p. 63. +##------------------------------------------------------------------------------ + +function Mu = fcm_update_membership_fcn (V, X, m, k, n, sqr_dist) + + Mu = zeros (k, n); + + if (min (min (sqr_dist)) > 0) + exponent = 1.0 / (m - 1); + for i = 1 : k + for j = 1 : n + summation = 0.0; + for l = 1 : k + summation += (sqr_dist(i, j) / sqr_dist(l, j))^exponent; + endfor + if (summation != 0) + Mu(i, ... [truncated message content] |