Znn3bq.jpeg
±±¾©Ê¯ÓÍ»¯¹¤Ñ§Ôº2026ÄêÑо¿ÉúÕÐÉú½ÓÊÕµ÷¼Á¹«¸æ
²é¿´: 3605  |  »Ø¸´: 12

liangboliu

Òø³æ (СÓÐÃûÆø)

[½»Á÷] ¡¾Ô­´´¡¿·ÖÏíÒ»¸ömatlab±àдµÄÄ£ºýKNNµÄÀý×Ó£¬¾­¹ýÑé֤Ч¹û²»´í¡£ ÒÑÓÐ12È˲ÎÓë

·ÖÏíÒ»¸öÄ£ºýKNNµÄÀý×Ó£¬¾­¹ý²âÊÔЧ¹û²»´í¡£
½¨Òé´ó¼ÒÔÚʹÓõÄʱºò£¬ÒýÓÃÒ»ÏÂ×÷ÕßµÄÎÄÕ¡£
function [predicted,memberships, numhits] = fknn(data, labels, test, ...
            testlabels, k_values, info, fuzzy)
% FKNN Fuzzy k-nearest neighbor classification algorithm.
%         Y = FKNN(DATA, LABELS, TEST, TESTLABELS, K, INFO) Runs fuzzy
%         k-nearest neighbors on the given data. DATA is a N-by-D data matrix
%         consisting of N patterns each of which is D-dimensional. LABELS is a
%         N-vector containing the class labels (1,2,...,C) for each pattern.
%        TEST is a M-by-D matrix consisting of M test patterns. TESTLABELS
%        is an optional M-vector for the true class labels of the given test
%        data. If you don't have true labels, just give an empty matrix for this
%        TESTLABELS.
%        K is the number of nearest neighbors to look at.
%        The algorithm will print an information line at every INFO test
%        patterns, if INFO>0. If INFO is zero, nothing will be printed.
%        Y is a M-vector containing the predicted class labels for the given test
%        patterns.
%
%         [Y,MEMS,HITS] = FKNN(DATA, LABELS, TEST, TESTLABELS, K, INFO) returns
%         the fuzzy class-memberships values in MEMS, for each test pattern. It is
%         a M-by-C matrix, C being the number of classes.
%        HITS is the number of correctly predicted test patterns. Note that HITS
%        is meaningless if TESTLABELS is not provided.
%
%         [Y,MEMS,HITS] = FKNN(DATA, LABELS, TEST, TESTLABELS, K, INFO, FUZZY) If
%         you don't want to do "fuzzy" k-nn, then give FUZZY as 'false'.
%
%        This m-file is capable of testing several k-values simultaneously. If
%        you pass a vector of k-values, rather than a single scalar, in K, then
%        each output variable is populated accordingly. So, if you give K as
%        [5 10 15], then Y becomes M-by-3, MEMS M-by-C-by-3 and HITS  3-by-1.
%
%        References:
%        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.  
%
% TODO: Generalize this m-file to Lp norm
%
% Emre Akbas [eakbas2 @ uiuc.edu]  Dec 2006.
%

if nargin<7
    fuzzy = true;
end

num_train = size(data,1);
num_test  = size(test,1);

% scaling factor for fuzzy weights. see [1] for details
m = 2;

% convert class labels to unary membership vectors (of 1s and 0s)
max_class = max(labels);
temp = zeros(length(labels),max_class);
for i=1:num_train
    temp(i, = [zeros(1, labels(i)-1) 1 zeros(1,max_class - labels(i))];
end
labels = temp;
clear temp;

% allocate space for storing predicted labels
predicted = zeros(num_test, length(k_values));

% allocate space for 'numhits'. This will only be used if 'testlabels' is
% provided
numhits = zeros(length(k_values),1);

% will the memberships be stored? if yes, allocate space
store_memberships = false;
if nargout > 1,
    store_memberships=true;
    memberships = zeros(num_test, max_class, length(k_values));
end

%% BEGIN kNN
% for each test point, do:
t0=clock;
tstart = t0;
for i=1:num_test
    distances = (repmat(test(i,, num_train,1) - data).^2;
    % for efficiency, no need to take sqrt since it is a non-decreasing function
    distances = sum(distances,2)';

    % sort the distances
    [junk, indeces] = sort(distances);

    for k=1:length(k_values)
        neighbor_index = indeces(1:k_values(k));
        weight = ones(1,length(neighbor_index));
        if fuzzy,
            % originally, this weight calculation should be:
            % weight = distances(neighbor_index).^(-2/(m-1));
            % but since we didn't take sqrt above and the inverse 2th power
            % the weights are:
            % weight = sqrt(distances(neighbor_index)).^(-2/(m-1));
            % which is equaliavent to:
            weight = distances(neighbor_index).^(-1/(m-1));

            % set the Inf (infite) weights, if there are any, to  1.
            if max(isinf(weight))
                warning(['Some of the weights are Inf for sample: ' ...
                        num2str(i) '. These weights are set to 1.']);
                weight(isinf(weight))=1;
            end
        end
        test_out = weight*labels(neighbor_index,/(sum(weight));

        if store_memberships, memberships(i,:,k) = test_out; end;

        % find predicted class (the one with the max. fuzzy vote)
        [junk, index_of_max] = max(test_out');

        predicted(i,k) = index_of_max;

        % compute current hit rate, if test labels are given
        if ~isempty(testlabels) && predicted(i,k)==testlabels(i)
            numhits(k) = numhits(k)+1;
        end
    end

    % print info
    if mod(i,info)==0
        elapsed = etime(clock, t0);
        fprintf(1,['%dth sample done.  Elapsed (from previous info): %.2f' ...
            ' sn.  Estimated left: %.2f sn.\n\tHit rate(s) so far:   '], ...
            i, elapsed, etime(clock, tstart)*((num_test-i)/i) );
        for k=1:length(k_values)
            fprintf(1,'%3d: %.3f\t',k_values(k), 100*numhits(k)/i);
        end
        fprintf(1,'\n');

        t0=clock; % start timer again
    end
end
»Ø¸´´ËÂ¥

» ²ÂÄãϲ»¶

» ±¾Ö÷ÌâÏà¹Ø¼ÛÖµÌùÍÆ¼ö£¬¶ÔÄúͬÑùÓаïÖú:

ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû

stephenliu89

Òø³æ (СÓÐÃûÆø)

¡ï
Сľ³æ(½ð±Ò+0.5):¸ø¸öºì°ü£¬Ð»Ð»»ØÌû½»Á÷
¶¥Ï£¡±ÈÎÒÓÃC++дµÄÄǸö¼òÒ׵ĺã¡
Email:stephenliu1989@163.com
2Â¥2010-09-14 10:53:39
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû
лл·ÖÏí
3Â¥2010-10-29 11:04:41
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû

qiaoyinhu

ľ³æ (ÕýʽдÊÖ)

²»´í£¬Ð»Ð»·ÖÏí£¡
4Â¥2010-11-29 22:28:35
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû
лл·ÖÏí£¡
5Â¥2011-04-14 10:07:40
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû

tywww

Ìú¸Ëľ³æ (СÓÐÃûÆø)

¡ï
Сľ³æ(½ð±Ò+0.5):¸ø¸öºì°ü£¬Ð»Ð»»ØÌû
²»´í£¬ÏÂÀ´¿´¿´£¡ £¬Ð»Ð»·ÖÏí£¡
6Â¥2012-03-06 21:20:55
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû

ËÕËØ2006

ľ³æ (СÓÐÃûÆø)

¡ï
Сľ³æ: ½ð±Ò+0.5, ¸ø¸öºì°ü£¬Ð»Ð»»ØÌû
лл·ÖÏí
[zeros(1, labels(i)-1) 1 zeros(1,max_class - labels(i))];
ÕâÒ»¾äÊDz»ÊÇÓÐÎÊÌ⣿

[ Last edited by ËÕËØ2006 on 2013-1-8 at 21:44 ]
7Â¥2013-01-08 21:22:36
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû

sandyuu

гæ (³õÈëÎÄ̳)

8Â¥2013-01-21 15:19:36
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû

zzpnlz

ľ³æ (ÕýʽдÊÖ)

Â¥Ö÷¹±Ï×Á¼¶à
ÌìÏÂÓ¢ÐÛ³öÎÒ±²£¬Ò»Èë½­ºþËêÔ´ß
9Â¥2013-01-21 20:11:39
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû

june_522

гæ (³õÈëÎÄ̳)

лл·ÖÏí£¬Ñ§Ï°ÁË
10Â¥2014-03-06 16:52:43
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû
Ïà¹Ø°æ¿éÌø×ª ÎÒÒª¶©ÔÄÂ¥Ö÷ liangboliu µÄÖ÷Ìâ¸üÐÂ
×î¾ßÈËÆøÈÈÌûÍÆ¼ö [²é¿´È«²¿] ×÷Õß »Ø/¿´ ×îºó·¢±í
[˶²©¼ÒÔ°] ÓÐûÓÐѧУ²ÄÁÏרҵÊÕ¿çµ÷(Ò»Ö¾Ô¸085410) +3 momo(Éϰ¶°æ) 2026-04-06 6/300 2026-04-08 14:53 by »¯Ñ§521
[¿¼ÑÐ] ¼ÆËã»ú11408 287 Çóµ÷¼Á +3 LiLe5 2026-04-07 3/150 2026-04-07 23:15 by shanqishi
[¿¼ÑÐ] ũѧ£¬Çóµ÷¼Á£¬314·Ö +4 ·Ã¿Í¼Ç¼¿É°® 2026-04-04 4/200 2026-04-07 21:07 by µÈ°¶
[¿¼ÑÐ] 306Çóµ÷¼Á +3 15287505595 2026-04-03 3/150 2026-04-07 18:08 by À¶ÔÆË¼Óê
[¿¼ÑÐ] 22408 µ÷¼Á²ÄÁÏ +7 ÎÒ½Ðez 2026-04-06 8/400 2026-04-07 17:12 by À¶ÔÆË¼Óê
[¿¼ÑÐ] 308Çóµ÷¼Á +3 ÖÕ²»ËÆ´Óǰ 2026-04-05 3/150 2026-04-05 20:07 by à£à£à£0119
[¿¼ÑÐ] ¹¤¿Æ08ר˶»úе275Çóµ÷¼Á +3 AaAa7420 2026-04-02 3/150 2026-04-05 13:26 by jp9609
[¿¼ÑÐ] 298Çóµ÷¼Á +7 manman511 2026-04-05 7/350 2026-04-05 10:29 by ÌÆãå¶ù
[¿¼ÑÐ] 295Çóµ÷¼Á +4 AÄãºÃÑо¿Éú 2026-04-04 5/250 2026-04-04 22:46 by yu221
[¿¼ÑÐ] ²ÄÁϵ÷¼Á +10 ÀÁÑòÑòÇáÖÃÓñÍÎ 2026-04-02 11/550 2026-04-04 21:56 by laoshidan
[¿¼ÑÐ] »·¾³285·Ö£¬¹ýÁù¼¶£¬Çóµ÷¼Á +10 xhr12 2026-04-02 10/500 2026-04-04 21:53 by bn53987
[¿¼ÑÐ] 085601£¬Ò»Ö¾Ô¸Ïôó334¸´ÊÔ±»Ë¢Çóµ÷¼Á +13 ÔøÑöÖ® 2026-04-03 15/750 2026-04-04 20:13 by dongzh2009
[¿¼ÑÐ] ÔõôɾÌû×Ó°¡ +3 ·ìêØ1000 2026-04-04 3/150 2026-04-04 14:20 by ÍÁľ˶ʿÕÐÉú
[¿¼ÑÐ] һ־Ը˫·Ç085502£¬267·Ö£¬¹ýËļ¶Çóµ÷¼Á +3 ÔÙæҲҪ³Ô·¹°¡ 2026-04-03 3/150 2026-04-04 05:03 by gswylq
[¿¼ÑÐ] »¯¹¤Çóµ÷¼Á +11 ÀóÏã֥ʿҬÄÌ 2026-04-03 11/550 2026-04-03 22:06 by à£à£à£0119
[¿¼ÑÐ] 320µ÷¼Á +4 ũҵ¹¤³ÌÓëÐÅÏ¢¼ 2026-04-03 4/200 2026-04-03 21:40 by lbsjt
[¿¼ÑÐ] 285Çóµ÷¼Á +5 AZMK 2026-04-03 8/400 2026-04-03 18:17 by AZMK
[¿¼ÑÐ] Ò»Ö¾Ô¸Î人Àí¹¤0856£¬³õÊÔ334 +3 26¿¼ÑвÄÁÏ 2026-04-02 3/150 2026-04-02 21:22 by dongzh2009
[¿¼ÑÐ] Ò»Ö¾Ô¸±±¾©¿Æ¼¼´óѧ085601²ÄÁϹ¤³ÌÓ¢Ò»Êý¶þ³õÊÔ×Ü·Ö335Çóµ÷¼Á +9 Ë«ÂíβƦÀϰå2 2026-04-01 9/450 2026-04-02 12:14 by oooqiao
[¿¼ÑÐ] Ò»Ö¾Ô¸°²»Õ´óѧ¼ÆËã»ú¿ÆÑ§Óë¼¼Êõѧ˶£¬331·ÖÇóµ÷¼Á +5 ½¯²ýÅôqtj 2026-04-01 5/250 2026-04-02 08:10 by fxue1114
ÐÅÏ¢Ìáʾ
ÇëÌî´¦ÀíÒâ¼û