|On this page…|
Clustering of numerical data forms the basis of many classification and system modeling algorithms. The purpose of clustering is to identify natural groupings of data from a large data set to produce a concise representation of a system's behavior.
Fuzzy Logic Toolbox™ tools allow you to find clusters in input-output training data. You can use the cluster information to generate a Sugeno-type fuzzy inference system that best models the data behavior using a minimum number of rules. The rules partition themselves according to the fuzzy qualities associated with each of the data clusters. Use the command-line function, genfis2 to automatically accomplish this type of FIS generation.
 Bezdec, J.C., Pattern Recognition with Fuzzy Objective Function Algorithms, Plenum Press, New York, 1981.
 Chiu, S., "Fuzzy Model Identification Based on Cluster Estimation," Journal of Intelligent & Fuzzy Systems, Vol. 2, No. 3, Spet. 1994.
Fuzzy c-means (FCM) is a data clustering technique wherein each data point belongs to a cluster to some degree that is specified by a membership grade. This technique was originally introduced by Jim Bezdek in 1981 as an improvement on earlier clustering methods. It provides a method that shows how to group data points that populate some multidimensional space into a specific number of different clusters.
Fuzzy Logic Toolbox command line function fcm starts with an initial guess for the cluster centers, which are intended to mark the mean location of each cluster. The initial guess for these cluster centers is most likely incorrect. Additionally, fcm assigns every data point a membership grade for each cluster. By iteratively updating the cluster centers and the membership grades for each data point, fcm iteratively moves the cluster centers to the right location within a data set. This iteration is based on minimizing an objective function that represents the distance from any given data point to a cluster center weighted by that data point's membership grade.
The command line function fcm outputs a list of cluster centers and several membership grades for each data point. You can use the information returned by fcm to help you build a fuzzy inference system by creating membership functions to represent the fuzzy qualities of each cluster.
This example shows how FCM clustering works using quasi-random two-dimensional data.
Load the data set and plot it.
load fcmdata.dat plot(fcmdata(:,1),fcmdata(:,2),'o')
Next, invoke the command-line function, fcm, to find two clusters in this data set until the objective function is no longer decreasing much at all.
[center,U,objFcn] = fcm(fcmdata,2);
Iteration count = 1, obj. fcn = 8.970479 Iteration count = 2, obj. fcn = 7.197402 Iteration count = 3, obj. fcn = 6.325579 Iteration count = 4, obj. fcn = 4.586142 Iteration count = 5, obj. fcn = 3.893114 Iteration count = 6, obj. fcn = 3.810804 Iteration count = 7, obj. fcn = 3.799801 Iteration count = 8, obj. fcn = 3.797862 Iteration count = 9, obj. fcn = 3.797508 Iteration count = 10, obj. fcn = 3.797444 Iteration count = 11, obj. fcn = 3.797432 Iteration count = 12, obj. fcn = 3.797430
center contains the coordinates of the two cluster centers, U contains the membership grades for each of the data points, and objFcn contains a history of the objective function across the iterations.
The fcm function is an iteration loop built on top of the following routines:
initfcm � initializes the problem
distfcm � performs Euclidean distance calculation
stepfcm � performs one iteration of clustering
To view the progress of the clustering, plot the objective function.
figure plot(objFcn) title('Objective Function Values') xlabel('Iteration Count') ylabel('Objective Function Value')
Finally, plot the two cluster centers found by the fcm function. The large characters in the plot indicate cluster centers.
maxU = max(U); index1 = find(U(1,:) == maxU); index2 = find(U(2,:) == maxU); figure line(fcmdata(index1,1), fcmdata(index1,2), 'linestyle',... 'none','marker', 'o','color','g'); line(fcmdata(index2,1),fcmdata(index2,2),'linestyle',... 'none','marker', 'x','color','r'); hold on plot(center(1,1),center(1,2),'ko','markersize',15,'LineWidth',2) plot(center(2,1),center(2,2),'kx','markersize',15,'LineWidth',2)
Note: Every time you run this example, the fcm function initializes with different initial conditions. This behavior swaps the order in which the cluster centers are computed and plotted.
If you do not have a clear idea how many clusters there should be for a given set of data, Subtractive clustering, , is a fast, one-pass algorithm for estimating the number of clusters and the cluster centers in a set of data. The cluster estimates, which are obtained from the subclust function, can be used to initialize iterative optimization-based clustering methods (fcm) and model identification methods (like anfis). The subclust function finds the clusters by using the subtractive clustering method.
The genfis2 function builds upon the subclust function to provide a fast, one-pass method to take input-output training data and generate a Sugeno-type fuzzy inference system that models the data behavior.
This example shows how to apply the genfis2 function to model the relationship between the number of automobile trips generated from an area and the area's demographics. Demographic and trip data are from 100 traffic analysis zones in New Castle County, Delaware. Five demographic factors are considered: population, number of dwelling units, vehicle ownership, median household income, and total employment. Hence, the model has five input variables and one output variable.
Load and plot the data.
mytripdata subplot(2,1,1); plot(datin); ylabel('input'); subplot(2,1,2); plot(datout);ylabel('output');
The mytripdata function creates several variables in the workspace. Of the original 100 data points, use 75 data points as training data (datin and datout)and 25 data points as checking data, (as well as for test data to validate the model). The checking data input/output pairs are denoted by chkdatin and chkdatout.
Use the genfis2 function to generate a model from data using clustering. genfis2 requires you to specify a cluster radius. The cluster radius indicates the range of influence of a cluster when you consider the data space as a unit hypercube. Specifying a small cluster radius usually yields many small clusters in the data, and results in many rules. Specifying a large cluster radius usually yields a few large clusters in the data, and results in fewer rules. The cluster radius is specified as the third argument of genfis2. The following code calls the genfis2 function using a cluster radius of 0.5.
The genfis2 function is a fast, one-pass method that does not perform any iterative optimization. A FIS structure is returned; the model type for the FIS structure is a first order Sugeno model with three rules.
Use the following commands to verify the model. Here, trnRMSE is the root mean square error of the system generated by the training data.
trnRMSE = 0.5276
Next, apply the test data to the FIS to validate the model. In this example, the checking data is used for both checking and testing the FIS parameters. Here, chkRMSE is the root mean square error of the system generated by the checking data.
chkRMSE = 0.6179
Plot the output of the model, chkfuzout, against the checking data, chkdatout.
figure plot(chkdatout) hold on plot(chkfuzout,'o') hold off
The model output and checking data are shown as circles and solid blue line, respectively. The plot shows that the model does not perform well on the checking data.
At this point, you can use the optimization capability of anfis to improve the model. First, try using a relatively short anfis training (20 epochs) without implementing the checking data option, and then test the resulting FIS model against the testing data. To perform the optimization, type the following command:
fismat2=anfis([datin datout],fismat,[20 0 0.1])
ANFIS info: Number of nodes: 44 Number of linear parameters: 18 Number of nonlinear parameters: 30 Total number of parameters: 48 Number of training data pairs: 75 Number of checking data pairs: 0 Number of fuzzy rules: 3 Start training ANFIS ... 1 0.527607 2 0.513727 3 0.492996 4 0.499985 5 0.490585 6 0.492924 7 0.48733 Step size decreases to 0.090000 after epoch 7. 8 0.485036 9 0.480813 10 0.475097 Step size increases to 0.099000 after epoch 10. 11 0.469759 12 0.462516 13 0.451177 14 0.447856 Step size increases to 0.108900 after epoch 14. 15 0.444356 16 0.433904 17 0.433739 18 0.420408 Step size increases to 0.119790 after epoch 18. 19 0.420512 20 0.420275 Designated epoch number reached --> ANFIS training completed at epoch 20. fismat2 = name: 'sug51' type: 'sugeno' andMethod: 'prod' orMethod: 'probor' defuzzMethod: 'wtaver' impMethod: 'prod' aggMethod: 'max' input: [1x5 struct] output: [1x1 struct] rule: [1x3 struct]
Here, 20 is the number of epochs, 0 is the training error goal, and 0.1 is the initial step size.
After the training is done, validate the model.
fuzout2=evalfis(datin,fismat2); trnRMSE2=norm(fuzout2-datout)/sqrt(length(fuzout2)) chkfuzout2=evalfis(chkdatin,fismat2); chkRMSE2=norm(chkfuzout2-chkdatout)/sqrt(length(chkfuzout2))
trnRMSE2 = 0.4203 chkRMSE2 = 0.5894
The model has improved a lot with respect to the training data, but only a little with respect to the checking data. Plot the improved model output obtained using anfis against the testing data.
figure plot(chkdatout) hold on plot(chkfuzout2,'o') hold off
The model output and checking data are shown as circles and solid blue line, respectively. This plot shows that genfis2 can be used as a stand-alone, fast method for generating a fuzzy model from data, or as a preprocessor to anfis for determining the initial rules. An important advantage of using a clustering method to find rules is that the resultant rules are more tailored to the input data than they are in a FIS generated without clustering. This reduces the problem of an excessive propagation of rules when the input data has a high dimension.
Overfitting Overfitting can be detected when the checking error starts to increase while the training error continues to decrease.
To check the model for overfitting, use anfis with the checking data option to train the model for 200 epochs. Here, fismat3 is the FIS structure when the training error reaches a minimum. fismat4 is the snapshot FIS structure taken when the checking data error reaches a minimum.
[fismat3,trnErr,stepSize,fismat4,chkErr]= ... anfis([datin datout],fismat,[200 0 0.1],[0 0 0 0], ... [chkdatin chkdatout]);
This command returns a list of output arguments. The output arguments show a history of the step sizes, the RMSE using the training data, and the RMSE using the checking data for each training epoch.
After the training completes, validate the model.
fuzout4=evalfis(datin,fismat4); trnRMSE4=norm(fuzout4-datout)/sqrt(length(fuzout4)) chkfuzout4=evalfis(chkdatin,fismat4); chkRMSE4=norm(chkfuzout4-chkdatout)/sqrt(length(chkfuzout4))
trnRMSE4 = 0.3393 chkRMSE4 = 0.5834
The error with the training data is the lowest thus far, and the error with the checking data is also slightly lower than before. This result suggests perhaps there is an overfit of the system to the training data. Overfitting occurs when you fit the fuzzy system to the training data so well that it no longer does a very good job of fitting the checking data. The result is a loss of generality.
View the improved model output. Plot the model output against the checking data.
figure plot(chkdatout) hold on plot(chkfuzout4,'o') hold off
The model output and checking data are shown as circles and solid blue line, respectively.
Next, plot the training error, trnErr.
figure plot(trnErr) title('Training Error') xlabel('Number of Epochs') ylabel('Training Error')
This plot shows that the training error settles at about the 60th epoch point.
Plot the checking error, chkErr.
figure plot(chkErr) title('Checking Error') xlabel('Number of Epochs') ylabel('Checking Error')
The plot shows that the smallest value of the checking data error occurs at the 52nd epoch, after which it increases slightly even as anfis continues to minimize the error against the training data all the way to the 200th epoch. Depending on the specified error tolerance, the plot also indicates the model's ability to generalize the test data.
You can also compare the output of fismat2 and fistmat4 against the checking data, chkdatout.
figure plot(chkdatout) hold on plot(chkfuzout4,'ob') plot(chkfuzout2,'+r')
The Clustering GUI Tool implements the fuzzy data clustering functions fcm and subclust and lets you perform clustering on the data. For more information on the clustering functions, see Fuzzy C-Means Clustering and Subtractive Clustering.
This GUI lets you perform the following tasks:
Load and plot the data.
Start the clustering.
Save the cluster center.
Access the online help topics by clicking Info or using the Help menu in the Clustering GUI.
To load a data set in the GUI, perform either of the following actions:
Click Load Data, and select the file containing the data.
Open the GUI with a data set directly by invoking findcluster with the data set as the argument, in the MATLAB Command Window.
The data set must have the extension.dat. For example, to load the data set, clusterdemo.dat, type findcluster('clusterdemo.dat').
The Clustering GUI Tool works on multidimensional data sets, but displays only two of those dimensions on the plot. To select other dimensions in the data set for plotting, you can use the drop-down lists under X-axis and Y-axis.
To start clustering the data:
Choose the clustering function fcm (fuzzy C-Means clustering) or subtractiv (subtractive clustering) from the drop-down menu under Methods.
Set options for the selected method using the Influence Range, Squash, Aspect Ratio, and Reject Ratio fields.
Begin clustering by clicking Start.
After clustering gets completed, the cluster centers appear in black as shown in the next figure.
To save the cluster centers, click Save Center.