You are on page 1of 1

clc;

clear all;
close all;
filename = 'traindataassignment6 - Copy.csv';
[num,txt,raw] = xlsread(filename);
X = num;
Y = txt;
% rng(10); % For reproducibility
Mdl = fitcknn(X,Y,'NumNeighbors',5,'Standardize',1);
% Examine the resubstitution loss, which, by default,
% is the fraction of misclassifications from the predictions
% of Mdl. (For nondefault cost, weights, or priors, see loss.).
rloss = resubLoss(Mdl)
% The classifier predicts incorrectly for % of the training
% data.

% Construct a cross-validated classifier from the model.

CVMdl = crossval(Mdl);
% Examine the cross-validation loss, which is the average
% loss of each cross-validation model when predicting on data
% that is not used for training.

kloss = kfoldLoss(CVMdl)

% Predict the classification


filename2 = 'testdataassignment6.csv';
[num2] = xlsread(filename2);
predicted_dataClass = predict(Mdl,num2)
%%
% Predict the labels of the training data.

predictedY = resubPredict(Mdl);
% Create a confusion matrix chart from the true labels
% Y and the predicted labels predictedY.

% accuracy = accuracy_score(Y(1:999,:),predictedY)

cm = confusionchart(Y(1:999,:),predictedY);
% Modify the appearance and behavior of the confusion
% matrix chart by changing property values. Add a title.

cm.Title = 'Classification Using KNN';


% Add column and row summaries.

% A row-normalized row summary displays the percentages


% of correctly and incorrectly classified observations for
% each true class.
cm.RowSummary = 'row-normalized';

% A column-normalized column summary displays


% the percentages of correctly and incorrectly classified
% observations for each predicted class.
cm.ColumnSummary = 'column-normalized';

You might also like