├── fcm ├── iris.mat ├── euclidean.m └── fcm.m ├── README.md ├── example.m └── LICENSE.txt /fcm/iris.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HosseinAbedi/FCM/HEAD/fcm/iris.mat -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #### Implementaion of "fuzzy clustering method (FCM)" in Matlab/GNU Octave 2 | 3 | ##### How to use : 4 | Please have look at example.m file and the other files in the fcm folder. 5 | 6 | 7 | -------------------------------------------------------------------------------- /fcm/euclidean.m: -------------------------------------------------------------------------------- 1 | % A function for calculation of euclidean distance of a point x(e.g. [3, 2, 1, 1]) from... 2 | % ...a set of points in matrix format Y(e.g. [3, 2, 1, 2; 4, 3, 2, 1]) 3 | function [d] = euclidean(x, Y) 4 | S = size(Y); 5 | d = sum((repmat(x, [S(1),1])-Y).^2, 2); 6 | d = sqrt(d); 7 | end -------------------------------------------------------------------------------- /example.m: -------------------------------------------------------------------------------- 1 | clear all 2 | clc 3 | addpath('fcm') 4 | load('fcm/iris.mat') 5 | 6 | c = 3; 7 | metric = @euclidean; 8 | X = iris(1:150, 1:4); 9 | m = 1.5; 10 | Max = 1000; 11 | tol = 1e-3; 12 | 13 | 14 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 15 | %Data Normalization 16 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 17 | [nr nc] = size(X); 18 | for i = 1:nc 19 | for j = 1:nr 20 | data(j, i) = (X(j, i)-std(X(:, i)))/mean(X(:, i)); 21 | end 22 | end 23 | 24 | 25 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 26 | %Running the Algorithms 27 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 28 | %***************FCM************************ 29 | [prediction,v] = fcm(c, data, m, metric, Max, tol); 30 | %v 31 | %prediction 32 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 2016 Hossein Abedi 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /fcm/fcm.m: -------------------------------------------------------------------------------- 1 | %Fuzzy C-means Algorithm in GnuOctave (V.3.6.4) 2 | %Fuzzy type 1 C-means algorithm 3 | %Inputs: 4 | %******c: Number of clusters 5 | %******X: Data Matrix N_samples*N_features 6 | %******U_up: U updating function 7 | %******m: Fuzzifier as a real number 8 | %******metric: Distance metric as a function (by default Euclidean) 9 | %******Max: Maximum number of iterations 10 | %******tol: Tolerance 11 | %*********************************************** 12 | %Outputs: 13 | %******prediction: Predicted labels of data 14 | %******v: Center of clusters as a matrix c*N_features 15 | %*********************************************** 16 | function [prediction v] = fcm(c, X, m, metric, Max, tol) 17 | [n, no] = size(X); 18 | U = zeros([c, n]); 19 | v = repmat(max(X), c, 1).*rand([c, no]); 20 | U = rand([c, n]); 21 | 22 | for j = 1:n 23 | U(:, j) = U(:, j)./sum(U(:, j)); 24 | end 25 | 26 | for i = 1:c 27 | v(i, :) = sum((X(:, :).*repmat(U(i, :)'.^m, 1, no)),1)./sum(U(i, :).^m); 28 | end 29 | 30 | v_old = v; 31 | delta = 1e4; 32 | k = 0; 33 | while (ktol) 34 | for i = 1:c 35 | for j = 1:n 36 | U(i, j) = 1/sum((metric(X(j, :), v(i, :))./metric(X(j, :), v)).^(2/(m-1))); 37 | end 38 | end 39 | for i = 1:c 40 | v(i, :) = sum((X(:, :).*repmat(U(i, :)'.^m, 1, no)), 1)./sum(U(i, :).^m); 41 | end 42 | v_new = v; 43 | delta = max(max(abs(v_new-v_old))); 44 | v_old = v; 45 | 46 | k = k+1; 47 | end 48 | prediction = zeros([1, n]); 49 | for i = 1:n 50 | [M, prediction(i)]=max(U(:, i)); 51 | end 52 | 53 | end --------------------------------------------------------------------------------