| 查看: 3392 | 回复: 12 | |||
[交流]
【原创】分享一个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 |
» 猜你喜欢
孩子确诊有中度注意力缺陷
已经有12人回复
2025冷门绝学什么时候出结果
已经有3人回复
天津工业大学郑柳春团队欢迎化学化工、高分子化学或有机合成方向的博士生和硕士生加入
已经有4人回复
康复大学泰山学者周祺惠团队招收博士研究生
已经有6人回复
AI论文写作工具:是科研加速器还是学术作弊器?
已经有3人回复
2026博士申请-功能高分子,水凝胶方向
已经有6人回复
论文投稿,期刊推荐
已经有4人回复
硕士和导师闹得不愉快
已经有13人回复
请问2026国家基金面上项目会启动申2停1吗
已经有5人回复
同一篇文章,用不同账号投稿对编辑决定是否送审有没有影响?
已经有3人回复
» 本主题相关价值贴推荐,对您同样有帮助:
有关物质方法验证之稳定性
已经有15人回复
求助啊......matlab
已经有10人回复
MATLAB用于三元回归参数
已经有3人回复
matlab 积分计算 算不出结果,求帮助!!!
已经有10人回复
关于气相色谱有关物质分析方法学验证~~~
已经有10人回复
请教一个matlab问题
已经有7人回复
matlab处于busy
已经有7人回复
matlab反求未知输入参数
已经有4人回复
急求一个matlab编码,各位如果知道一定要帮我一把啊
已经有10人回复
关于matlab 2010b 的一个小问题
已经有7人回复
【求助】一个matlab窗口占25%CPU,如何提高?
已经有11人回复
【求助】请教一个matlab程序,谢谢帮忙!!
已经有15人回复
【求助】如何使得matlab拟合的曲线强制经过一个点
已经有8人回复
【求助】问一个关于matlab中legend的问题
已经有11人回复
stephenliu89
银虫 (小有名气)
- 应助: 3 (幼儿园)
- 金币: 448.7
- 散金: 13
- 红花: 3
- 帖子: 183
- 在线: 25.7小时
- 虫号: 953342
- 注册: 2010-02-06
- 性别: GG
- 专业: 理论和计算化学

2楼2010-09-14 10:53:39
谢谢分享![]() |
3楼2010-10-29 11:04:41
qiaoyinhu
木虫 (正式写手)
- 应助: 1 (幼儿园)
- 金币: 2018.6
- 帖子: 630
- 在线: 130小时
- 虫号: 713122
- 注册: 2009-03-02
- 性别: GG
- 专业: 机械测试理论与技术
4楼2010-11-29 22:28:35
| 谢谢分享! |
5楼2011-04-14 10:07:40
tywww
铁杆木虫 (小有名气)
- 应助: 0 (幼儿园)
- 金币: 10517.2
- 红花: 1
- 帖子: 247
- 在线: 35.1小时
- 虫号: 203142
- 注册: 2006-03-01
- 专业: 机械制造及其自动化
6楼2012-03-06 21:20:55
苏素2006
木虫 (小有名气)
- 应助: 0 (幼儿园)
- 金币: 3320.9
- 散金: 20
- 红花: 3
- 帖子: 206
- 在线: 142.8小时
- 虫号: 274845
- 注册: 2006-08-26
- 专业: 计算机应用技术
7楼2013-01-08 21:22:36
8楼2013-01-21 15:19:36
zzpnlz
木虫 (正式写手)
- 应助: 6 (幼儿园)
- 金币: 1847.4
- 散金: 108
- 红花: 2
- 帖子: 337
- 在线: 81小时
- 虫号: 1188249
- 注册: 2011-01-12
- 性别: GG
- 专业: 化学计量学与化学信息学

9楼2013-01-21 20:11:39
10楼2014-03-06 16:52:43













= [zeros(1, labels(i)-1) 1 zeros(1,max_class - labels(i))];
回复此楼

