Knn code to compare two excel sheet

we are working on a power system on which we have to determine whether the failure of the power system is a fault or not a fault. We have created a dataset with all possibilities of faults and not faults of the system called training set. we have created another excel sheet called the training set in which we have taken some values of faults and not faults from the dataset. We want to use knn algorithm and euclidean distance to compare/classify whether the readings in the training set are the values of faults or not faults when compared with the original dataset. As we are putting four five values in the training set, how do we make matlab read and classify all the values one after the other without manually entering the values
P.S- 1 represents 'fault' and 2 represents 'not a fault' (in the AE column of the excel sheet)in the dataset excel sheet. After running knn algorithm we want 1 or 2 displayed on command line and written on training set.

1 comentario

Rik
Rik el 23 de Mzo. de 2017
You mean you need to read the Excel files into Matlab? That can be done with xlsread. It sounds like after that it is only a few matrix multiplications. (potentially useful function: repmat, also keep in mind that Matlab trims the result from xlsread, removing empty rows and columns)

Iniciar sesión para comentar.

Respuestas (1)

sam  CP
sam CP el 26 de Mzo. de 2017
Editada: per isakson el 2 de Abr. de 2017
%The following code will helps you.
clc
clear all
training0 = xlsread('training set.xls');
training1 = xlsread('dataset.xlsx');
zero = zeros(10,1);
one = ones(10,1);
group = [zero;one];
test = TestFeatinputMRI;
training = [training0;training1];
KNN = fitcknn(training,group)
Class = knnclassify(test,training,group)

8 comentarios

Nana Fernandes
Nana Fernandes el 28 de Mzo. de 2017
Thanks for the code when i run it, i get the following error Undefined function or variable 'training'.
also i place of ('training set.xls'); and ('dataset.xlsx'); i have put the path along with the file name. is that correct?
Rik
Rik el 28 de Mzo. de 2017
What is your exact code for xlsread?
Nana Fernandes
Nana Fernandes el 29 de Mzo. de 2017
xlsread('D:\ieee14\faultset.xlsx','sheet1', 'A1:A16801');
Rik
Rik el 29 de Mzo. de 2017
The strange thing is that the line training=[training0;training1]; is called before KNN=fitcknn(training,group). This should mean that either that first line should error, or the variable training should exist.
Nana Fernandes
Nana Fernandes el 29 de Mzo. de 2017
i put training=[training0;training1]; before KNN=fitcknn(training,group). and i get the following error - Error using vertcat Dimensions of matrices being concatenated are not consistent.
can you explain the flow of the program above? i cant understand what training is assigned to .
Rik
Rik el 29 de Mzo. de 2017
Then training0 and training1 don't have the correct sizes to be put together. The training matrix should contain first the data for group zero (fault) and then for group one (not fault). This is then encoded in the variable group. I can't give you more details than this, because I am not familiar with this topic specifically.
Nana Fernandes
Nana Fernandes el 30 de Mzo. de 2017
the excel sheet contains the values for faults first(one after the other) and then all not faults
Nana Fernandes
Nana Fernandes el 2 de Abr. de 2017
Undefined function or variable 'TestFeatinputMRI'. Now i am getting this error @per isakson

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 23 de Mzo. de 2017

Comentada:

el 2 de Abr. de 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by