about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/netlab3.3/knnfwd.m
diff options
context:
space:
mode:
authorziejd22017-09-28 15:04:40 -0500
committerziejd22017-09-28 15:04:40 -0500
commit8070dc963753142bb86c4ed698d91fd623ed28e7 (patch)
treed0f6dd8fc46a49b819aa55c1a90faa14d8448883 /sourcecodes/bnt-master/netlab3.3/knnfwd.m
parent7cc31810d53176e805532b2789955f4eedbce6bb (diff)
downloadBNW-8070dc963753142bb86c4ed698d91fd623ed28e7.tar.gz
BNW using Octave instead of Matlab.
This version of BNW should perform the same as the original version. The only difference is that it uses Octave instead of Matlab when running BayesNet Toolbox during parameter learning.

I am calling this BNW_1.02. It can be accessed at:
compbio.uthsc.edu/BNW_1.02
Diffstat (limited to 'sourcecodes/bnt-master/netlab3.3/knnfwd.m')
-rw-r--r--sourcecodes/bnt-master/netlab3.3/knnfwd.m53
1 files changed, 53 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/netlab3.3/knnfwd.m b/sourcecodes/bnt-master/netlab3.3/knnfwd.m
new file mode 100644
index 00000000..db450bae
--- /dev/null
+++ b/sourcecodes/bnt-master/netlab3.3/knnfwd.m
@@ -0,0 +1,53 @@
+function [y, l] = knnfwd(net, x)
+%KNNFWD	Forward propagation through a K-nearest-neighbour classifier.
+%
+%	Description
+%	[Y, L] = KNNFWD(NET, X) takes a matrix X of input vectors (one vector
+%	per row)   and uses the K-nearest-neighbour rule on the training data
+%	contained in NET to  produce  a matrix Y of outputs and a matrix L of
+%	classification labels. The nearest neighbours are determined using
+%	Euclidean distance. The IJth entry of Y counts the number of
+%	occurrences that an example from class J is among the K closest
+%	training examples to example I from X. The matrix L contains the
+%	predicted class labels as an index 1..N, not as 1-of-N coding.
+%
+%	See also
+%	KMEANS, KNN
+%
+
+%	Copyright (c) Ian T Nabney (1996-2001)
+
+
+errstring = consist(net, 'knn', x);
+if ~isempty(errstring)
+  error(errstring);
+end
+
+ntest = size(x, 1);		              % Number of input vectors.
+nclass = size(net.tr_targets, 2);		% Number of classes.
+
+% Compute matrix of squared distances between input vectors from the training 
+% and test sets.  The matrix distsq has dimensions (ntrain, ntest).
+
+distsq = dist2(net.tr_in, x);
+
+% Now sort the distances. This generates a matrix kind of the same 
+% dimensions as distsq, in which each column gives the indices of the
+% elements in the corresponding column of distsq in ascending order.
+
+[vals, kind] = sort(distsq);
+y = zeros(ntest, nclass);
+
+for k=1:net.k
+  % We now look at the predictions made by the Kth nearest neighbours alone,
+  % and represent this as a 1-of-N coded matrix, and then accumulate the 
+  % predictions so far.
+
+  y = y + net.tr_targets(kind(k,:),:);
+
+end
+
+if nargout == 2
+  % Convert this set of outputs to labels, randomly breaking ties
+  [temp, l] = max((y + 0.1*rand(size(y))), [], 2);
+end
\ No newline at end of file