Professional Documents
Culture Documents
Crab Classification
This demo illustrates using a neural network as a classifier to identify the sex of crabs from physical dimensions
of the crab.
Contents
The Problem: Classification of Crabs
Why Neural Networks?
Preparing the Data
Building the Neural Network Classifier
Testing the Classifier
The six physical characterstics will act as inputs to a neural network and the sex of the crab will be target. Given
an input, which constitutes the six observed values for the physical characterstics of a crab, the neural network is
expected to identify if the crab is male or female.
This is achieved by presenting previously recorded inputs to a neural network and then tuning it to produce the
desired target outputs. This process is called neural network training.
There are several ways to translate textual or symbolic data into numeric data. Some of the common symbol
translation techniques used are unary encoding, binary encoding and numbering classes. We are going to use
unary encoding in this demo to perform symbol translation.
fid = fopen('private/crabdata.csv');
C = textscan(fid,'%f%f%f%f%f%f%s','delimiter',','); % Import data
fclose(fid);
http://www.mbfys.ru.nl/~robvdw/CNP04/LAB_ASSIGMENTS/LAB05_CN05/MATLAB2007b/nnet/nndemos/html/crabclassify.html 1/3
3/2/2019 Crab Classification
The first 6 columns of data represent the crab's physical characterstics. The 7th column represents the sex of the
crab.
physchars = [C{1} C{2} C{3} C{4} C{5} C{6}]; % inputs to neural network
'Female' is now represented by the vector [1 0] and 'Male' is represented by the vector [0 1]. This process is
called unary encoding. The neural network will now be trained to produce a [1 0] output for 'Female' crabs and
[0 1] output for 'Male' crabs.
We could also have used numbers to represent the two sexes (Male=1, Female=2) or performed binary encoding
(Male=[0 0], Female=[0 1]).
The next step is to preprocess the data into a form that can be used with a neural network.
The neural network object in the toolbox expects the samples along columns and its features along rows. Our
dataset has its samples along rows and its features along columns. Hence the matrices have to be transposed.
physchars = physchars';
sex = sex';
Since the neural network starts with random initial weights, the results of this demo will differ slightly every
time it is run. The random seed is set to avoid this randomness. However this is not necessary for your own
applications.
rand('seed', 491218382)
A 1-hidden layer feed forward network is created with 20 neurons in the hidden layer.
Now the network is ready to be trained. The samples are automatically divided into training, validation and test
sets. The training set is used to teach the network. Training continues as long as the network continues
improving on the validation set. The test set provides a completely independent measure of network accuracy.
[net,tr] = train(net,physchars,sex);
The network response can now be compared against the desired target response to build the classification matrix
which will provides a comprehensive picture of a classifiers performance.
http://www.mbfys.ru.nl/~robvdw/CNP04/LAB_ASSIGMENTS/LAB05_CN05/MATLAB2007b/nnet/nndemos/html/crabclassify.html 2/3
3/2/2019 Crab Classification
[y_out,I_out] = max(out);
[y_t,I_t] = max(testTargets);
cm =
18 0
1 21
The classification matrix provides a comprehensive picture of the classification performance of the classifier.
The ideal classification matrix is the one in which the sum of the diagonal is equal to the number of samples.
It can also be understood in terms of percentages. The following matrix provides the same information as above
but in terms of percentages.
cm_p =
45.0000 0
2.5000 52.5000
Explore other demos and the documentation for more insight into neural networks and its applications.
MATLAB and Simulink are registered trademarks of The MathWorks, Inc. Please see
www.mathworks.com/trademarks for a list of other trademarks owned by The MathWorks, Inc. Other product or
brand names are trademarks or registered trademarks of their respective owners.
http://www.mbfys.ru.nl/~robvdw/CNP04/LAB_ASSIGMENTS/LAB05_CN05/MATLAB2007b/nnet/nndemos/html/crabclassify.html 3/3