24小时热门版块排行榜    

查看: 4090  |  回复: 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的回帖
谢谢分享
3楼2010-10-29 11:04:41
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 13 个回答

stephenliu89

银虫 (小有名气)


小木虫(金币+0.5):给个红包,谢谢回帖交流
顶下!比我用C++写的那个简易的好!
Email:stephenliu1989@163.com
2楼2010-09-14 10:53:39
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

qiaoyinhu

木虫 (正式写手)

不错,谢谢分享!
4楼2010-11-29 22:28:35
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

tywww

铁杆木虫 (小有名气)


小木虫(金币+0.5):给个红包,谢谢回帖
不错,下来看看! ,谢谢分享!
6楼2012-03-06 21:20:55
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[基金申请] 大佬帮着分析今年的评审意见?该课题连续申请了三年了,明年要不要换个方向。 100+3 cai-11138 2026-08-31 10/500 2026-09-06 19:40 by babero
[考博] 售SCI一区T0P文章,我:8O.55.1.O.5.4,科目齐全,可+急 +3 j5mmowWXNWxv 2026-09-05 4/200 2026-09-06 19:21 by eObJPa4gWmp6
[硕博家园] 售SCI文章,我:8O.5.5.1O.54,科目全,可十急 +4 jPDp0sc2B7zQ 2026-09-05 6/300 2026-09-06 18:21 by eObJPa4gWmp6
[博后之家] 售SCI一区T0P文章,我:8.O55.1.O.54,科目全,可十急 +3 jPDp0sc2B7zQ 2026-09-05 6/300 2026-09-06 18:09 by eObJPa4gWmp6
[论文投稿] 售SCI文章,我:8O5.5.1.O.54,科目齐全,可+急 +4 jPDp0sc2B7zQ 2026-09-05 6/300 2026-09-06 18:01 by eObJPa4gWmp6
[找工作] 售一区SCI文章T0P,我:8O.551.O54,科目全,可十急 +3 jPDp0sc2B7zQ 2026-09-05 8/400 2026-09-06 17:49 by eObJPa4gWmp6
[文学芳草园] 两块石头 +7 阿美_Lml888 2026-09-03 9/450 2026-09-06 17:48 by myrtle
[考研] 售SCI文章,我:8O.5.5.1O.54,科目全,可十急 +3 jPDp0sc2B7zQ 2026-09-04 8/400 2026-09-06 17:09 by eObJPa4gWmp6
[博后之家] 广西大学-广州大学招聘博士后 欢迎广大优秀人才!!! +5 SCSIOyxguo 2026-09-03 8/400 2026-09-06 15:06 by 可爱小花朵
[教师之家] 售SCI一区T0P文章,我:8.O55.1.O.54,科目全,可十急 +3 j5mmowWXNWxv 2026-09-05 3/150 2026-09-06 13:09 by eObJPa4gWmp6
[考博] 售SCI一区文章,我:8O5.5.1.O5.4,科目全,可伽急 +3 j5mmowWXNWxv 2026-09-05 4/200 2026-09-06 12:49 by eObJPa4gWmp6
[基金申请] 科研人应该花精力去思考如何解决问题,而不是去凝练问题 +10 瞬息宇宙 2026-09-01 19/950 2026-09-06 11:15 by jiangxuan2004
[考研] 售SCI文章,我:8O5.5.1.O.54,科目齐全,可+急 +3 jPDp0sc2B7zQ 2026-09-05 4/200 2026-09-06 07:20 by E1iiBLMU2XnD
[论文投稿] 售SCI一区T0P文章,我:8.O.55.1.O54,科目全,可伽急 +6 7ZYIEW2YWn72 2026-09-04 9/450 2026-09-06 06:00 by E1iiBLMU2XnD
[教师之家] 售SCI一区T0P文章,我:8O.55.1.O.54,科目全,可伽急 +8 CRJ0Aq2FtVH0 2026-09-03 15/750 2026-09-06 05:48 by E1iiBLMU2XnD
[基金申请] 面上没中,邀请各位路过的虫友分析一下分数 +12 jackleilei 2026-09-03 13/650 2026-09-05 18:10 by fireguard
[基金申请] 学科评审组评审是指会评吗? +6 瞬息宇宙 2026-08-31 9/450 2026-09-05 13:11 by haizai99
[硕博家园] Ei源刊怎么投 +4 Fengshun9711 2026-09-04 4/200 2026-09-04 19:30 by 研途知予
[硕博家园] 哈尔滨工业大学韩晓军教授课题组招收2027年硕士推免生及博士研究生 +3 灯灯灯灯DDDD 2026-09-03 3/150 2026-09-03 21:22 by 科研皮皮猪
[基金申请] 要骂人了,新模版改版就是要淡化问题凝练这种虚的东西,结果有个评委还在说凝练得不够 +9 瞬息宇宙 2026-08-31 17/850 2026-09-02 14:04 by anjeeshine
信息提示
请填处理意见