I am having a little difficulty in understanding the implementation of kNN
1 view (last 30 days)
Show older comments
% In the attached data file, I am predicting two classes (purchased or not purchased) using KNN based on two predictors that are Age and %Estimated Slary. I have three questions in the following code that are mentioned. Guidance will be appreciated
classification_model = fitcknn(data,'Purchased~Age+EstimatedSalary');% Can we modify all the properties of knn such as number of neighbors, metric space and break ties in one oline of code
%% -------------- Test and Train sets ---------------------------
% In the following lines of codes I have divided the data, 80% for training and 20% for test.
cv = cvpartition(classification_model.NumObservations, 'HoldOut', 0.2);
% The model is trained on 80% of the data
cross_validated_model = crossval(classification_model,'cvpartition',cv); %
%% -------------- Making Predictions for Test sets ---------------
Predictions = predict(cross_validated_model.Trained{1},data(test(cv),1:end-1)); % I don't understand the logic here? Can someone kindly explain, especially the "data(test(cv),1:end-1)".
%% -------------- Analyzing the predictions ---------------------
Results = confusionmat(cross_validated_model.Y(test(cv)),Predictions); % Is there another way to access the known predictions instead of the bold code in this line?
1 Comment
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!