How do I assign membership functions to the input and output of this ANFIS code
16 ビュー (過去 30 日間)
古いコメントを表示
I want to add membership function to this ANFIS code to be able to train it. the membership function type is Gaussian and the number of mfs is numMembershipFunctions = [4 3 2 3 3 4 2] .Attached is the dataset file. The code is:
data = loadpublicHealthHeartDataset;
X = data(:,1:6);
Y = data(:,7);
trnData=[X,Y];
trnX = X(1:2:end,:); % Training input data set
trnY = Y(1:2:end,:); % Training output data set
vldX = X(2:2:end,:); % Validation input data set
vldY = Y(2:2:end,:); % Validation output data set
% Tune the data using Anfis
genAnfis = genfis(trnX,trnY, genfisOptions('GridPartition'));
opt = anfisOptions;
opt.InitialFIS = genAnfis;
opt.ValidationData = [vldX,vldY];
opt.EpochNumber = 30;
anfis1 = anfis(trnData,opt);
% Function to upload the dataset
function data= loadpublicHealthHeartDataset
% Get data from the original data file.
tableData = readtable("heartss.CSV");
% Convert the table to a numerical array
data = table2array(tableData(:, 1:7));
end
採用された回答
Sam Chak
2024 年 8 月 1 日
Hi @Ahmad
The following code can be used to train the system using ANFIS based on the desired number of Gaussian membership functions for the inputs. Since there are six inputs, the NumMembershipFunctions parameter should be specified as a vector with a length equal to the number of inputs, which is 6 in this case. The results and the membership functions are shown below. Note that 'anfis()' only generates a single-output Sugeno FIS. Feel free to modify the code to suit your specific needs.
Additionally, there are two open questions. If you find the proposed solution helpful, please consider accepting the answers ✔ and providing upvotes 👍. Your support is greatly appreciated. Alternatively, you can post comments so that the solutions can be evaluated and updated with satisfactory resolutions.
- https://www.mathworks.com/matlabcentral/answers/2058539-how-can-i-fix-the-error-message-each-rule-must-have-a-consequent-pointing-to-a-different-output-me?s_tid=srchtitle
- https://www.mathworks.com/matlabcentral/answers/2053757-how-to-evaluate-the-performance-metrics-accuracy-precision-recall-f1score-on-the-tuned-fis?s_tid=srchtitle
MATLAB Code:
% Observational Data
data = readtable('heartss.csv');
X = table2array(data(:, 1:6)); % 6 Inputs of patient data
Y = table2array(data(:, 7)); % 1 Output (target)
data_Train = [X Y];
% Setting up the initial FIS using Subtractive Clustering method
% genOpt = genfisOptions('SubtractiveClustering');
genOpt = genfisOptions('GridPartition'); % disable this if 'SubtractiveClustering' is selected
genOpt.NumMembershipFunctions = [4 3 2 3 3 4]; % disable this if 'SubtractiveClustering' is selected
genOpt.InputMembershipFunctionType = "gaussmf"; % disable this if 'SubtractiveClustering' is selected
inFIS = genfis(X, Y, genOpt);
anOpt = anfisOptions('InitialFIS', inFIS, 'EpochNumber', 10); % to decide on the EpochNumber
% Training data with ANFIS
outFIS = anfis(data_Train, anOpt)
showfis(outFIS)
ruleview(outFIS)
% Predict using trained FIS & display RMSE
Ypred1 = round(evalfis(outFIS, X));
rmse1 = sqrt(mean((Y - Ypred1).^2));
txt1 = sprintf('RMSE of the prediction is %.4f.', rmse1);
disp(txt1)
% Identify outliers by finding indices
Outliers = abs(Y - Ypred1); % Forecasts that differ significantly from observations
idx = find(Outliers == 1);
numOutliers = length(idx);
numSamples = length(Y);
Percentage = 100*numOutliers/numSamples;
txt2 = sprintf('%2.2f percent of the %d samples are outliers, which is equal to %d.', Percentage, numSamples, numOutliers);
disp(txt2)
% Get Prediction 2
Ypred2 = Ypred1;
Ypred2(idx) = ~Ypred2(idx);
rmse2 = sqrt(mean((Y - Ypred2).^2));
txt3 = sprintf('RMSE of the prediction with outlier factor is %.4f.', rmse2);
disp(txt3)
RESULTS:
Desired number of Gaussian Membership functions for Input 1 to Input 3:
Desired number of Gaussian Membership functions for Input 4 to Input 6:
View the fuzzy inference process for 864 rules
1 件のコメント
Siddharth Bhutiya
2024 年 8 月 5 日
Some suggestions to improve the code. The following two lines do some extra work that could be avoided by using alternate indexing expression.
X = table2array(data(:, 1:6)); % 6 Inputs of patient data
Y = table2array(data(:, 7)); % 1 Output (target)
Here you are getting the sub-table and then converting into a homogeneous array. You could directly do that in one step by using curly braces {}. Also in the second expression you only want one variable, so it even simpler (and faster) to just use dot indexing as shown below.
X = data{:, 1:6}; % 6 Inputs of patient data
Y = data.(7); % 1 Output (target)
These are just some suggestions on table indexing, however, you dont really need tables here, you could simplify this code further by using readmatrix to directly read your csv file as a double matrix
data = readmatrix(filename);
X = data(:,1:6);
Y = data(:,7);
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Fuzzy Logic Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!