Predict function in KNN
조회 수: 3 (최근 30일)
이전 댓글 표시
Alberto Azzari
2020년 4월 19일
답변: Thiago Henrique Gomes Lobato
2020년 4월 19일
Hi, I'm trying to implement a Leave One Out using knn. This is the code. I'm reporting the error. Thanks
for i=1:loo_cycles
full_index = 1:rows_X;
index_train = full_index(full_index~=i);
x_set = X(i, :);
x_train = X(index_train, :);
y_train = y(index_train, 1);
fprintf("Running loo step %d of %d\n",i, loo_cycles);
cvp = cvpartition(y_train, 'KFold');
KNN = fitcknn(x_train, y_train,'CVPartition', cvp);
y_pred_test(i, 1) = predict(KNN, x_set);
end
Error:
Check for missing argument or incorrect argument data type in call to function 'predict'.
Error in test_knn (line 40)
y_pred_test(i, 1) = predict(KNN, x_set);
댓글 수: 0
채택된 답변
Thiago Henrique Gomes Lobato
2020년 4월 19일
Your KNN is not a single model to make predictions but rather a RegressionPartitionedModel. Which means it has all the statistics of the cross validation and all individual models for each fold. If you want to make predictions you will have to select one of the trained models, as example:
y_pred_test(i, 1) = predict(KNN.Trained{1}, x_set);
I don't really understand why you would need this internal cross-validation since you're already looping to each data point. For me it would make more sense to simply train the model in all training data for each iteration:
KNN = fitcknn(x_train, y_train);
y_pred_test(i, 1) = predict(KNN, x_set);
Then you have only one model and can use the predict function.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Discriminant Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!