?? fknn.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 fknn</title> <meta name="keywords" content="fknn"> <meta name="description" content="FKNN Fuzzy k-nearest neighbor classification rule"> <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> > fknn.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>fknn</h1><h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><div class="box"><strong>FKNN Fuzzy k-nearest neighbor classification rule</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 test_out = fknn(sample_in, sample_out, test_in, k, m) </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"> FKNN Fuzzy k-nearest neighbor classification rule
Usage:
TEST_OUT = FKNNR(SAMPLE_IN, SAMPLE_OUT, TEST_IN, K)
SAMPLE_IN: Input part of the sample data
SAMPLE_OUT: Output part of the sample data
TEST_IN: Input part of the test data
K: The "k" in "K-NNR"
TEST_OUT: Output of the test data according to fuzzy KNNR
The dimensions of the above matrices is
SAMPLE_IN: M1xN
SAMPLE_OUT: M1xF
TEST_IN: M2xN
TEST_OUT: M2xF
where
M1 = the no. of sample data
N = no. of features
F = no. of classes (or categories)
M2 = no. of test data
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 "fknn" with no arguments.
See also <a href="initfknn.html" class="code" title="function fuz_class = initfknn(sampledata, k)">INITFKNN</a> for obtaining a fuzzy version of SAMPLE_OUT.</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="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="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></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 test_out = fknn(sample_in, sample_out, test_in, k, m)</a>0002 <span class="comment">% FKNN Fuzzy k-nearest neighbor classification rule</span>0003 <span class="comment">%</span>0004 <span class="comment">% Usage:</span>0005 <span class="comment">% TEST_OUT = FKNNR(SAMPLE_IN, SAMPLE_OUT, TEST_IN, K)</span>0006 <span class="comment">%</span>0007 <span class="comment">% SAMPLE_IN: Input part of the sample data</span>0008 <span class="comment">% SAMPLE_OUT: Output part of the sample data</span>0009 <span class="comment">% TEST_IN: Input part of the test data</span>0010 <span class="comment">% K: The "k" in "K-NNR"</span>0011 <span class="comment">% TEST_OUT: Output of the test data according to fuzzy KNNR</span>0012 <span class="comment">%</span>0013 <span class="comment">% The dimensions of the above matrices is</span>0014 <span class="comment">%</span>0015 <span class="comment">% SAMPLE_IN: M1xN</span>0016 <span class="comment">% SAMPLE_OUT: M1xF</span>0017 <span class="comment">% TEST_IN: M2xN</span>0018 <span class="comment">% TEST_OUT: M2xF</span>0019 <span class="comment">%</span>0020 <span class="comment">% where</span>0021 <span class="comment">%</span>0022 <span class="comment">% M1 = 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">% M2 = no. of test data</span>0026 <span class="comment">%</span>0027 <span class="comment">% For more technical details, please refer to the paper:</span>0028 <span class="comment">%</span>0029 <span class="comment">% J. M. Keller, M. R. Gray, and J. A. Givens, Jr., "A Fuzzy K-Nearest</span>0030 <span class="comment">% Neighbor Algorithm", IEEE Transactions on Systems, Man, and Cybernetics,</span>0031 <span class="comment">% Vol. 15, No. 4, pp. 580-585.</span>0032 <span class="comment">%</span>0033 <span class="comment">% For selfdemo, type "fknn" with no arguments.</span>0034 <span class="comment">%</span>0035 <span class="comment">% See also INITFKNN for obtaining a fuzzy version of SAMPLE_OUT.</span>0036 0037 <span class="comment">% Roger Jang, 990805</span>0038 0039 <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>0040 0041 <span class="keyword">if</span> nargin < 5, m = 2; <span class="keyword">end</span>0042 <span class="keyword">if</span> nargin < 4, k = 3; <span class="keyword">end</span>0043 0044 sample_n = size(sample_in, 1);0045 test_n = size(test_in, 1);0046 feature_n = size(sample_in, 2);0047 class_n = size(sample_out, 2);0048 0049 <span class="comment">% Euclidean distance matrix</span>0050 distmat = <a href="vecdist.html" class="code" title="function distmat = vecdist(mat1, mat2)">vecdist</a>(sample_in, test_in);0051 0052 <span class="comment">% knnmat(i,j) = class of i-th nearest point of j-th input vector</span>0053 <span class="comment">% (The size of knnmat is k times test_n.)</span>0054 [junk, index] = sort(distmat);0055 <span class="comment">% knnmat = reshape(sample_out(index(1:k,:)), k, test_n);</span>0056 0057 test_out = zeros(test_n, class_n);0058 <span class="keyword">for</span> i = 1:test_n,0059 neighbor_index = index(1:k, i);0060 weight = distmat(neighbor_index, i)'.^(-2/(m-1));0061 weight(isinf(weight))=realmax; <span class="comment">% To avoid weight of inf</span>0062 test_out(i,:) = weight*sample_out(neighbor_index,:)/(sum(weight));0063 <span class="keyword">end</span>0064 0065 <span class="comment">% ========== Self demo ==========</span>0066 <a name="_sub1" href="#_subfunctions" class="code">function selfdemo</a>0067 0068 data_n = 50;0069 0070 data = rand(data_n, 2);0071 x = data(:, 1);0072 y = data(:, 2);0073 class = zeros(data_n, 1);0074 0075 index = find(y > x);0076 class(index) = 1;0077 index = find(y<=x & y>=-x+1);0078 class(index) = 2;0079 class(find(class==0)) = 3;0080 0081 sampledata = [x y class];0082 0083 <span class="comment">%colordef black;</span>0084 figure;0085 axis([0 1 0 1]);0086 box on;0087 axis equal square0088 0089 color = {<span class="string">'r'</span>, <span class="string">'g'</span>, <span class="string">'c'</span>};0090 0091 <span class="keyword">for</span> i = 1:3,0092 index = find(class==i);0093 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>0094 <span class="string">'color'</span>, color{i});0095 <span class="keyword">end</span>0096 0097 k = 3;0098 fuz_sample_out = <a href="initfknn.html" class="code" title="function fuz_class = initfknn(sampledata, k)">initfknn</a>(sampledata, k);0099 index = find(sum(fuz_sample_out.^0.5, 2)~=1); 0100 <span class="comment">%line(x(index), y(index), 'linestyle', 'none', 'marker', 'o', 'color', 'w');</span>0101 0102 test_in = rand(50, 2);0103 test_out = <a href="fknn.html" class="code" title="function test_out = fknn(sample_in, sample_out, test_in, k, m)">fknn</a>([x y], fuz_sample_out, test_in, k);0104 <span class="comment">% Plot test data</span>0105 line(test_in(:,1), test_in(:,2), <span class="string">'linestyle'</span>, <span class="string">'none'</span>, <span class="string">'marker'</span>, <span class="string">'.'</span>, <span class="string">'color'</span>, <span class="string">'w'</span>);0106 0107 <span class="comment">% Plot desired boundaries</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>, <span class="string">'Test data'</span>, -1);0113 0114 <span class="comment">% Plot classification result of the test data</span>0115 [junk, max_index] = max(test_out');0116 <span class="keyword">for</span> i = 1:3,0117 index = find(max_index==i);0118 line(test_in(index,1), test_in(index,2), <span class="string">'linestyle'</span>, <span class="string">'none'</span>, <span class="keyword">...</span>0119 <span class="string">'marker'</span>, <span class="string">'o'</span>, <span class="string">'color'</span>, color{i});0120 <span class="keyword">end</span>0121 0122 title(<span class="string">'The circle color of a sample point shows its predicted class via FKNN.'</span>);0123 0124 <span class="comment">%for i = index(:)',</span>0125 <span class="comment">% text(x(i), y(i), mat2str(fuz_sample_out(i, :), 2));</span>0126 <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 + -