Rules formation method using fuzzy c means clustering method.
8 views (last 30 days)
Show older comments
I want to know about tool that automatically genreate the rules on the basis of dataset with fuzzy c means clustering method .Please explain with any dataset to generate rules automatically .It is very urgent for my study .
0 Comments
Accepted Answer
Sam Chak
on 27 Dec 2023
Hi @Mamta
Here is a simple example of automatically generating fuzzy rules from data using the FCM clustering method. The data is randomly generated. For more information, you can look up the 'genfis()' command. Please note that this method requires the Fuzzy Logic Toolbox.
Let me know if the proposed approach in the Answer is helpful.
%% Data with 2 inputs and 1 output
inputData = [2*rand(100,1) 3*rand(100,1)-1.5]; % input data
outputData = 5*rand(100,1); % output data
%% Generate fuzzy rules from data using FCM clustering
opt = genfisOptions('FCMClustering', 'FISType', 'mamdani'); % set method & FIS type
opt.NumClusters = 'auto'; % set auto or specify a fixed number of FCM clusters
opt.Verbose = 0;
fis = genfis(inputData, outputData, opt); % generate FIS from the data
showrule(fis)
%% Plot data
figure
scatter3(inputData(:,1), inputData(:,2), outputData), grid on
xlabel("Input 1"), ylabel("Input 2"), zlabel("Output")
%% Plot membership functions
figure
[in1, mf1] = plotmf(fis, 'input', 1);
subplot(3,1,1), plot(in1, mf1), grid on
xlabel('Membership Functions for Input 1'), ylabel('\mu')
[in2, mf2] = plotmf(fis, 'input', 2);
subplot(3,1,2), plot(in2, mf2), grid on
xlabel('Membership Functions for Input 2'), ylabel('\mu')
[out, mf3] = plotmf(fis, 'output', 1);
subplot(3,1,3), plot(out, mf3), grid on
xlabel('Membership Functions for Output'), ylabel('\mu')
6 Comments
More Answers (3)
Sam Chak
on 29 Dec 2023
Hi @Mamta
Your observation is correct. When employing the data clustering method, the fuzzy system (FIS) will have one fuzzy rule for each cluster, and each input and output variable will have one membership function per cluster. In the following example, I have set a fixed number of clusters.
%% Data with 2 inputs and 1 output
inputData = [2*rand(100,1) 3*rand(100,1)-1.5]; % input data
outputData = 5*rand(100,1); % output data
%% Generate fuzzy rules from data using FCM clustering
opt = genfisOptions('FCMClustering', 'FISType', 'mamdani'); % set method & FIS type
opt.NumClusters = 3; % set a fixed number of FCM clusters
opt.Verbose = 0;
myFIS = genfis(inputData, outputData, opt); % generate FIS from the data
showrule(myFIS)
%% Plot membership functions
figure
[in1, mf1] = plotmf(myFIS, 'input', 1);
subplot(3,1,1), plot(in1, mf1), grid on
xlabel('Membership Functions for Input 1'), ylabel('\mu')
[in2, mf2] = plotmf(myFIS, 'input', 2);
subplot(3,1,2), plot(in2, mf2), grid on
xlabel('Membership Functions for Input 2'), ylabel('\mu')
[out, mf3] = plotmf(myFIS, 'output', 1);
subplot(3,1,3), plot(out, mf3), grid on
xlabel('Membership Functions for Output'), ylabel('\mu')
Sam Chak
on 29 Dec 2023
Here is the 3rd answer at your request. If you want to generate rules with antecedents that contain all possible combinations of the input membership function, then you must use the Grid Partition method. However, this "non-clustering" method will produce only the Sugeno FIS, which, in my opinion, is more powerful than the classic Mamdani FIS.
Don't forget to vote for other answers as tokens of appreciation for providing explanations and guidance.
%% Data with 2 inputs and 1 output
inputData = [2*rand(100,1) 3*rand(100,1)-1.5]; % input data
outputData = 5*rand(100,1); % output data
%% Generate fuzzy rules from data using FCM clustering
opt = genfisOptions('GridPartition'); % will create Sugeno FIS
opt.NumMembershipFunctions = [3 3]; % specify number of MFs for each input
opt.InputMembershipFunctionType = "gaussmf";
myFIS = genfis(inputData, outputData, opt); % generate Sugeno FIS from the data
showrule(myFIS)
%% Plot membership functions
figure
[in1, mf1] = plotmf(myFIS, 'input', 1);
subplot(2,1,1), plot(in1, mf1), grid on
xlabel('Membership Functions for Input 1'), ylabel('\mu')
[in2, mf2] = plotmf(myFIS, 'input', 2);
subplot(2,1,2), plot(in2, mf2), grid on
xlabel('Membership Functions for Input 2'), ylabel('\mu')
0 Comments
Sam Chak
on 30 Dec 2023
Hi @Mamta
Here is an example to demonstrate how to change the names of the membership functions to 'low,' 'med,' and 'high' for Input 1. Don't forget to vote on the Answer.
%% Data with 2 inputs and 1 output
inputData = [2*rand(50,1) 3*rand(50,1)-1.5]; % input data
outputData = 5*rand(50,1); % output data
%% Generate fuzzy rules from data using FCM clustering
opt = genfisOptions('FCMClustering', 'FISType', 'mamdani'); % set method & FIS type
opt.NumClusters = 3; % set auto or specify a fixed number of FCM clusters
opt.Verbose = 0;
myFIS = genfis(inputData, outputData, opt) % generate FIS from the data
%% genfis auto-assigned the names of the fuzzy sets as in1cluster1, in1cluster2, in1cluster3
plotmf(myFIS, 'input', 1)
%% Check the names of the fuzzy sets of Input 2
myFIS.Inputs(2).MembershipFunctions
%% The names of the fuzzy sets can be renamed using this syntax
myFIS.Inputs(1).MembershipFunctions(1).Name = "low";
myFIS.Inputs(1).MembershipFunctions(2).Name = "med";
myFIS.Inputs(1).MembershipFunctions(3).Name = "high";
plotmf(myFIS, 'input', 1)
3 Comments
Sam Chak
on 15 Jan 2024
Hi @Mamta
The benefit is that FCM allows for flexibility in the number of clusters to be formed, and therefore, rules can be generated for an arbitrary number of clusters. Regarding why it generated 4 rules, it is because the membership values obtained from FCM can be interpreted as the strength of association of a data point with different clusters. Something like "If the input is similar to cluster A, then the output is associated with class A."
The real question is, "Did the FCM produce a fairly accurate fuzzy system with only 4 rules to predict the input-output pair?" If it didn't, the data may exhibit a complex pattern that can be better captured by a varying number of clusters. Therefore, multiple runs with different initializations may be necessary.
If you want to manually enter all possible rules for 9 inputs of three membership functions for each input, then you will have to decide on the outputs of number of rules. How accurate it will be? That's another story.
See Also
Categories
Find more on Fuzzy Inference System Modeling in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!