使用matlab进行k-NN回归?

时间:2017-06-12 14:31:36

标签: matlab machine-learning knn

我有两个表Training_tableTesting 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);

但似乎我错了。有人可以建议或建议我犯错的地方吗?

0 个答案:

没有答案