我有3个不同大小的数据数组(类):
d1=[-3.34,0.11,1.07,0.82,-0.51,-1.24,4.0,1.15,1.29,-3.38,-1.12,1.35,-0.14,1.21,-2.11,0.48,2.16,0.91,-0.78,1.13,1.32];
d2=[3.27,4.57,4.12,4.99,4.40,4.08,5.96,3.37,4.0,3.56,4.81,3.02,3.01,2.62,3.77,7.01,2.84,2.79,4.41,2.08,6.66,6.65,4.65,5.78,5.81,5.65,3.73,4.31,4.84,3.70,4.73,2.98,3.95,3.58];
d3=[3.66,6.16,10.07,7.17,8.17,7.33,6.24,7.02,6.52,7.27,7.86,9.27,11.58,5.12,10.12,9.07,11.57,9.12,9.88,6.71,8.18,9.29,6.56,10.40,7.39,8.30,10.87,11.79,8.77,11.08,8.66,7.78,10.00,6.14,8.74];
我需要根据[-2,0,2,4,6,8,10]
和k=1
对testValues k=3
进行分类。
我正在尝试使用fitcknn
。但是对于fitcknn
,我不确定将参数构建为k1Model和k3Model的正确方法。
;;我检查并发现“knnclassify”在这里不合适,因为它需要矩阵。
答案 0 :(得分:0)
假设d1,d2和d3是三个类:
% data values
d1=[-3.34,0.11,1.07,0.82,-0.51,-1.24,4.0,1.15,1.29,-3.38,-1.12,1.35,-0.14,1.21,-2.11,0.48,2.16,0.91,-0.78,1.13,1.32];
d2=[3.27,4.57,4.12,4.99,4.40,4.08,5.96,3.37,4.0,3.56,4.81,3.02,3.01,2.62,3.77,7.01,2.84,2.79,4.41,2.08,6.66,6.65,4.65,5.78,5.81,5.65,3.73,4.31,4.84,3.70,4.73,2.98,3.95,3.58];
d3=[3.66,6.16,10.07,7.17,8.17,7.33,6.24,7.02,6.52,7.27,7.86,9.27,11.58,5.12,10.12,9.07,11.57,9.12,9.88,6.71,8.18,9.29,6.56,10.40,7.39,8.30,10.87,11.79,8.77,11.08,8.66,7.78,10.00,6.14,8.74];
% re-arrange data to a format as expected by fitcknn
% your predictors
X = vertcat(d1', d2', d3');
% your labels
Y = vertcat(repmat({'d1'}, length(d1), 1), repmat({'d2'}, length(d2), 1), repmat({'d3'}, length(d3), 1));
% fit a model with k (the number of neighbors) equal to 1:
k1Model = fitcknn(X,Y,'NumNeighbors',1,'Standardize',1);
% alternatively with k (the number of neighbors) equal to 3:
k3Model = fitcknn(X,Y,'NumNeighbors',3,'Standardize',1);
% get predictions on the test points by passing the trained model and your test values to
% the 'predict' function
testValues = [-2,0,2,4,6,8,10]';
k1Predictions = predict(k1Model, testValues);
k3Predictions = predict(k3Model, testValues);