knn
最邻近分类
Class
= knnclassify(test_data,train_data,train_label, k
, distance
, rule
)
k:选择最邻近的数量
distance:距离度量
'euclidean' 欧几里得距离,默认的
'cityblock' 绝对差的和
'cosine' 余弦 (作为向量处理)
'correlation' 相关距离 样本相关性(作为值序列处理)
'Hamming' 海明距离 不同的比特百分比(仅适用于二进制数据)
rule:如何对样本进行分类
'nearest' 最近的K个的最多数
'random' 随机的最多数
'consensus' 共识规则
% Classify the sample using the nearest neighbor classification
training = [mvnrnd([ 1 1], 1.2*eye(2), 100); ...
mvnrnd([-1 -1], 1.5*eye(2), 100)];
group = [ones(100,1); repmat(2,100,1)];
gscatter(training(:,1),training(:,2),group,'rb','+x');
legend('Training group 1', 'Training group 2');
hold on;
sample = unifrnd(-5, 5, 100, 2); c = knnclassify(sample, training, group);
gscatter(sample(:,1),sample(:,2),c,'gc'); hold on;
legend('Training group 1','Training group 2', ...
'Data in group 1','Data in group 2');
hold off;
结果: