📄 initfknn.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"><html><head> <title>Description of initfknn</title> <meta name="keywords" content="initfknn"> <meta name="description" content="INITfknn Initialize fuzzy membership grades of sample output for fuzzy KNN."> <meta http-equiv="Content-Type" content="text/html; charset=big5"> <meta name="generator" content="m2html © 2003 Guillaume Flandin"> <meta name="robots" content="index, follow"> <link type="text/css" rel="stylesheet" href="../m2html.css"></head><body><a name="_top"></a><div><a href="../index.html">Home</a> > <a href="index.html">dcpr</a> > initfknn.m</div><!--<table width="100%"><tr><td align="left"><a href="../index.html"><img alt="<" border="0" src="../left.png"> Master index</a></td><td align="right"><a href="index.html">Index for dcpr <img alt=">" border="0" src="../right.png"></a></td></tr></table>--><h1>initfknn</h1><h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><div class="box"><strong>INITfknn Initialize fuzzy membership grades of sample output for fuzzy KNN.</strong></div><h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><div class="box"><strong>function fuz_class = initfknn(sampledata, k) </strong></div><h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><div class="fragment"><pre class="comment">INITfknn Initialize fuzzy membership grades of sample output for fuzzy KNN.
Usage:
FUZ_CLASS = INITFKNN(SAMPLE_DATA, K)
SAMPLE_DATA: Sample data matrix, where each row is a sample data, with
the last column being the class information. Note that the
elements in the last column should take values from 1 to F,
where F is the no. of classes (or categories).
K: The no. of neighbors used in estimating the fuzzy membership grades.
FUZ_CLASS: The modified fuzzy membership grades of each sample data.
The dimensions of the above matrices is
SAMPLE_DATA: Mx(N+1)
K: 1x1
FUZ_CLASS: MxF
where
M = the no. of sample data
N = no. of features
F = no. of classes (or categories)
For more technical details, please refer to the paper:
J. M. Keller, M. R. Gray, and J. A. Givens, Jr., "A Fuzzy K-Nearest
Neighbor Algorithm", IEEE Transactions on Systems, Man, and Cybernetics,
Vol. 15, No. 4, pp. 580-585.
For selfdemo, type "initfknn" with no arguments.
See also <a href="fknn.html" class="code" title="function test_out = fknn(sample_in, sample_out, test_in, k, m)">FKNN</a>.</pre></div><!-- crossreference --><h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>This function calls:<ul style="list-style-image:url(../matlabicon.gif)"><li><a href="initfknn.html" class="code" title="function fuz_class = initfknn(sampledata, k)">initfknn</a> INITfknn Initialize fuzzy membership grades of sample output for fuzzy KNN.</li><li><a href="vecdist.html" class="code" title="function distmat = vecdist(mat1, mat2)">vecdist</a> VECDIST Distance between two set of vectors</li></ul>This function is called by:<ul style="list-style-image:url(../matlabicon.gif)"><li><a href="fknn.html" class="code" title="function test_out = fknn(sample_in, sample_out, test_in, k, m)">fknn</a> FKNN Fuzzy k-nearest neighbor classification rule</li><li><a href="flda.html" class="code" title="function [newSampleIn, discrimVec] = lda(sampleIn, sampleOut, discrimVecNum)">flda</a> LDA Linear discriminant analysis</li><li><a href="fldainsel.html" class="code" title="function recog = fldainsel(feature, class, dim, k1, k2);">fldainsel</a> LDAINSEL LDA for input selection</li><li><a href="initfknn.html" class="code" title="function fuz_class = initfknn(sampledata, k)">initfknn</a> INITfknn Initialize fuzzy membership grades of sample output for fuzzy KNN.</li></ul><!-- crossreference --><h2><a name="_subfunctions"></a>SUBFUNCTIONS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><ul style="list-style-image:url(../matlabicon.gif)"><li><a href="#_sub1" class="code">function selfdemo</a></li></ul><h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function fuz_class = initfknn(sampledata, k)</a>0002 <span class="comment">%INITfknn Initialize fuzzy membership grades of sample output for fuzzy KNN.</span>0003 <span class="comment">%</span>0004 <span class="comment">% Usage:</span>0005 <span class="comment">% FUZ_CLASS = INITFKNN(SAMPLE_DATA, K)</span>0006 <span class="comment">%</span>0007 <span class="comment">% SAMPLE_DATA: Sample data matrix, where each row is a sample data, with</span>0008 <span class="comment">% the last column being the class information. Note that the</span>0009 <span class="comment">% elements in the last column should take values from 1 to F,</span>0010 <span class="comment">% where F is the no. of classes (or categories).</span>0011 <span class="comment">% K: The no. of neighbors used in estimating the fuzzy membership grades.</span>0012 <span class="comment">% FUZ_CLASS: The modified fuzzy membership grades of each sample data.</span>0013 <span class="comment">%</span>0014 <span class="comment">% The dimensions of the above matrices is</span>0015 <span class="comment">%</span>0016 <span class="comment">% SAMPLE_DATA: Mx(N+1)</span>0017 <span class="comment">% K: 1x1</span>0018 <span class="comment">% FUZ_CLASS: MxF</span>0019 <span class="comment">%</span>0020 <span class="comment">% where</span>0021 <span class="comment">%</span>0022 <span class="comment">% M = the no. of sample data</span>0023 <span class="comment">% N = no. of features</span>0024 <span class="comment">% F = no. of classes (or categories)</span>0025 <span class="comment">%</span>0026 <span class="comment">% For more technical details, please refer to the paper:</span>0027 <span class="comment">%</span>0028 <span class="comment">% J. M. Keller, M. R. Gray, and J. A. Givens, Jr., "A Fuzzy K-Nearest</span>0029 <span class="comment">% Neighbor Algorithm", IEEE Transactions on Systems, Man, and Cybernetics,</span>0030 <span class="comment">% Vol. 15, No. 4, pp. 580-585.</span>0031 <span class="comment">%</span>0032 <span class="comment">% For selfdemo, type "initfknn" with no arguments.</span>0033 <span class="comment">%</span>0034 <span class="comment">% See also FKNN.</span>0035 0036 <span class="comment">% Roger Jang, 990805</span>0037 0038 <span class="keyword">if</span> nargin==0, <a href="#_sub1" class="code" title="subfunction selfdemo">selfdemo</a>; <span class="keyword">return</span>; <span class="keyword">end</span>0039 0040 feature_n = size(sampledata,2)-1;0041 data_n = size(sampledata,1);0042 sample_input = sampledata(:, 1:feature_n);0043 sample_output = sampledata(:, feature_n+1);0044 class_n = max(sample_output);0045 0046 <span class="comment">% Euclidean distance matrix</span>0047 distmat = <a href="vecdist.html" class="code" title="function distmat = vecdist(mat1, mat2)">vecdist</a>(sample_input);0048 distmat(1:(data_n+1):data_n^2) = inf; <span class="comment">% Set diagonal elements to infs</span>0049 0050 <span class="comment">% knnmat(i,j) = class of i-th nearest point of j-th input vector</span>0051 <span class="comment">% (The size of knnmat is k times data_n.)</span>0052 [junk, index] = sort(distmat);0053 knnmat = reshape(sample_output(index(1:k,:)), k, data_n);0054 0055 <span class="comment">% class_count(i,j) = count of class-i points within j-th test input vector's</span>0056 <span class="comment">% neighborhood</span>0057 <span class="comment">%class_count = zeros(class_n, test_n);</span>0058 <span class="comment">%for i = 1:test_n,</span>0059 <span class="comment">% [sorted_element, element_count] = countele(knnmat(:,i));</span>0060 <span class="comment">% class_count(sorted_element, i) = element_count;</span>0061 <span class="comment">%end</span>0062 0063 <span class="comment">% Compute the membership grades for each sample point</span>0064 fuz_class = zeros(data_n, class_n);0065 <span class="keyword">for</span> i = 1:data_n,0066 <span class="keyword">for</span> j = 1:class_n0067 desired_class = sampledata(i, end);0068 n = length(find(knnmat(:,i)==j));0069 <span class="keyword">if</span> j == desired_class,0070 fuz_class(i,j) = n/k*0.49+0.51;0071 <span class="keyword">else</span>0072 fuz_class(i,j) = n/k*0.49;0073 <span class="keyword">end</span>0074 <span class="keyword">end</span>0075 <span class="keyword">end</span>0076 0077 <span class="comment">% ========== Self Demo ==========</span>0078 <a name="_sub1" href="#_subfunctions" class="code">function selfdemo</a>0079 0080 data_n = 50;0081 0082 data = rand(data_n, 2);0083 x = data(:, 1);0084 y = data(:, 2);0085 class = zeros(data_n, 1);0086 0087 index = find(y > x);0088 class(index) = 1;0089 index = find(y<=x & y>=-x+1);0090 class(index) = 2;0091 class(find(class==0)) = 3;0092 0093 sampledata = [x y class];0094 0095 <span class="comment">%colordef black;</span>0096 figure;0097 axis([0 1 0 1]);0098 box on;0099 axis equal square0100 0101 color = {<span class="string">'r'</span>, <span class="string">'g'</span>, <span class="string">'c'</span>};0102 0103 <span class="keyword">for</span> i = 1:3,0104 index = find(class==i);0105 line(x(index), y(index), <span class="string">'linestyle'</span>, <span class="string">'none'</span>, <span class="string">'marker'</span>, <span class="string">'.'</span>, <span class="keyword">...</span>0106 <span class="string">'color'</span>, color{i});0107 <span class="keyword">end</span>0108 line([0 1], [0 1], <span class="string">'linestyle'</span>, <span class="string">':'</span>);0109 line([0.5 1], [0.5 0], <span class="string">'linestyle'</span>, <span class="string">':'</span>);0110 0111 legend(<span class="string">'Sample data: Class 1'</span>, <span class="string">'Sample data: Class 2'</span>,<span class="keyword">...</span>0112 <span class="string">'Sample data: Class 3'</span>, -1);0113 0114 k = 3;0115 fuz_out = <a href="initfknn.html" class="code" title="function fuz_class = initfknn(sampledata, k)">initfknn</a>(sampledata, k);0116 index = find(sum(fuz_out.^0.5, 2)~=1); 0117 line(x(index), y(index), <span class="string">'linestyle'</span>, <span class="string">'none'</span>, <span class="string">'marker'</span>, <span class="string">'o'</span>, <span class="string">'color'</span>, <span class="string">'w'</span>);0118 0119 title(<span class="string">'Circled data points have fuzzy membership grades.'</span>);0120 0121 <span class="comment">%for i = index(:)',</span>0122 <span class="comment">% text(x(i), y(i), mat2str(fuz_out(i, :), 2));</span>0123 <span class="comment">%end</span></pre></div><hr><address>Generated on Thu 30-Oct-2008 12:53:56 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> © 2003</address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -