我有两个表Training_table
和Testing table
每个包含两个大小为100的参数。我想使用k-NN
进行训练training_table
并使用' Testing_table'值。
让我们考虑一下,Training table
x = [4 5.5 6.5 8 9 10] ; y = [100 200 400 600 900 10000]
在Testing_table
,
x1 = [7 8 9 ]; y1 = ?
因此,对于给定的x1
值,y1
的估算值是多少?
到目前为止,我已编写代码,
testing_data = size(test_data,1);
training_data = size(training_data,1);
% absolute distance between all test and training data
dist = abs(repmat(testing_data,1,training_data) - repmat(training_data(:,1)',testing_data,1));
% indicies of nearest neighbors
[~,nearest] = sort(dist,2);
% k nearest
nearest = nearest(:,1:k);
% mode of k nearest
val = reshape(training_data(nearest,2),[],k);
out_data = mode(val,2);
% if mode is 1, output nearest instead
output(output==1) = val(output==1,1);
但似乎我错了。有人可以建议或建议我犯错的地方吗?