% this function finds the closes k neighbors to a row within a dataset % and returns the median of those k projects' effort values % and also the mre value function [knnMedian, mre, kEffort] = nnkForKernel(myRow,train,kValue) featureSize = size(train,2); % % firstly normalize the row and data trainActualCostValues = train(:,size(train,2)); actualRowCost = myRow(:,size(train,2)); myTempToNormalize = [myRow;train]; myTempToNormalize = (mapstd(myTempToNormalize'))'; % get normalized row and replace cost with actual cost myRow = myTempToNormalize(1,:); myRow(:,featureSize) = actualRowCost; % get normalized train data and replace cost with actual cost myTempToNormalize(1,:) = []; train = myTempToNormalize; train(:,featureSize) = trainActualCostValues; % below array keeps the kNN effort values kEffort = zeros(kValue,1); % below array keeps the distance values kDistances = zeros(kValue,1); for counter = 1:kValue % initially let the first instance have the best distance bestDist = sqrt(abs(myRow(1,1:(featureSize-1)).^2 - train(1,1:1:(featureSize-1)).^2)); bestIndex = 1; for i=2:size(train,1) tempDist = sqrt(abs(myRow(1,1:1:(featureSize-1)).^2 - train(i,1:1:(featureSize-1)).^2)); if tempDist < bestDist bestDist = tempDist; bestIndex = i; end end % save the effort value of the closest neighbor kEffort(counter,1) = train(bestIndex,featureSize); kDistances(counter,1) = bestDist; % delete the closest neighbor for the next run (if there is any) train(bestIndex,:) = []; end % return the median of the kNN knnMedian = median(kEffort); kDistances = sort(kDistances); [B, ] mre = abs(knnMedian - myRow(1,featureSize))/myRow(1,featureSize); end