neural network classification in matlab - neural-network

My input data is an 101*22 array(101 samples and 22 features).
These data(101) should be divided into 3 groups(L1, L2 and L3).
I want to use mat lab neural network as classifier.
What will be target array?
What other classifier you recommend?
Thanks

The target data should be the classes of the Input data. In your case you have 3 classes. You can use a binary coding.
More details about the input and target data can be found here at the end of the page see here
Other resources:
first
A simple example can be the following:
#this is the INPUT data that you have
X=randint(101,22,[0 10]);
#this is the TARGET data
y =randint(3,22,[0 1]);
#define hidden layer size
hiddenLayerSize = 10;
#create the neural net
my_net = patternnet(hiddenLayerSize);
#run it
[my_net,tr] = trainrp(my_net,X,y);
Then you should see something like the following:
Then explore this windows.
E.g. select confusion

Related

Machine Learning (MATLAB) - Neural network is saved as 'struct' instead of 'network' in checkpoint

I am training a neural network to learn a function. Everything is going great so far.
I have input matrix of 4x10000 and output matrix of 3x10000. I have much more data points than 10000. But not all of them can be fit at once so I have decided to feed pack of 10000-10000 data points and train same neural network on it.
There are three layers and 7 units in hidden layer.
So what I do is, I train the network with 10000 data points randomly and then again train on another random 10000 data points and so on.
So for this I store CheckPoints (in-built functionality of neural net toolkit). But what happens is that the network, which is being trained, is stored as struct in CheckPoints rather than network type itself. So when I load the checkpoint next time I run the program, it shows error something as below.
Undefined function 'train' for input arguments of type 'struct'
I am using fitnet network.
% Create a Fitting Network
hiddenLayerSize = 7;
net = fitnet(hiddenLayerSize,'trainlm');
% Setup Division of Data for Training, Validation, Testing
net.divideParam.trainRatio = 60/100;
net.divideParam.valRatio = 20/100;
net.divideParam.testRatio = 20/100;
load('Highlights_Checkpoint.mat');
existanceOfCheckpoint = exist('checkpoint', 'var');
if existanceOfCheckpoint==0
else
net = (checkpoint.net);
end
% Train the Network
[net,tr] = train(net,x,t,'useParallel', 'yes','showResources','yes', 'CheckpointFile','Highlights_Checkpoint.mat');
Well solution to this problem was quite easy.
All I had to do was the following:
net = network(checkpoint.net);
And all was set. :D

Data augmentation techniques for general datasets?

I am working in a machine learning problem and want to build neural network based classifiers on it in matlab. One problem is that the data is given in the form of features and number of samples is considerably lower. I know about data augmentation techniques for images, by rotating, translating, affine translation, etc.
I would like to know whether there are data augmentation techniques available for general datasets ? Like is it possible to use randomness to generate more data ? I read the answer here but I did not understand it.
Kindly please provide answers with the working details if possible.
Any help will be appreciated.
You need to look into autoencoders. Effectively you pass your data into a low level neural network, it applies a PCA-like analysis, and you can subsequently use it to generate more data.
Matlab has an autoencoder class as well as a function, that will do all of this for you. From the matlab help files
Generate the training data.
rng(0,'twister'); % For reproducibility
n = 1000;
r = linspace(-10,10,n)';
x = 1 + r*5e-2 + sin(r)./r + 0.2*randn(n,1);
Train autoencoder using the training data.
hiddenSize = 25;
autoenc = trainAutoencoder(x',hiddenSize,...
'EncoderTransferFunction','satlin',...
'DecoderTransferFunction','purelin',...
'L2WeightRegularization',0.01,...
'SparsityRegularization',4,...
'SparsityProportion',0.10);
Generate the test data.
n = 1000;
r = sort(-10 + 20*rand(n,1));
xtest = 1 + r*5e-2 + sin(r)./r + 0.4*randn(n,1);
Predict the test data using the trained autoencoder, autoenc .
xReconstructed = predict(autoenc,xtest');
Plot the actual test data and the predictions.
figure;
plot(xtest,'r.');
hold on
plot(xReconstructed,'go');
You can see the green cicrles which represent additional data generated with the auto-encoder.

Matlab neural network

I've seen the following instructions in one of my AI courses:
net = newp([-2 2;-2 2],2])
net.IW {1,1} = [-1 1; 3 4]
net.b{1} = [-2,3]
How does the neural network look? The perceptron has 2 neurons?
the easiest way to take a look at it is via:
view(net)
there you can see the number of inputs outputs and layers. Also you can check with
help netp
the documentation of the command and in there it says
NET = newp(P,T,TF,LF) takes these inputs,
P - RxQ matrix of Q1 representative input vectors.
T - SxQ matrix of Q2 representative target vectors.
TF - Transfer function, default = 'hardlim'.
LF - Learning function, default = 'learnp'.
net.iw{1,1} sets the input weigths to the chosen numbers
amd net.b{1} sets the biases of the network to the vector [-2,3].
Did this clearify things for you?

how to implement a Multilayer Neural Network in Matlab?

m1= xlsread('NSL_KDD_TRAIN.xlsx','A2:AO67344'); % m1=input
m= xlsread('NSL_KDD_TRAIN.xlsx','AP2:AP67344'); % m=output
net=newff(m1',m',8);
net=train(net,m1',m');
y=sim(net,m1'); %training data output
y3=round(y); % making nonfraction number
n1= xlsread('NSL_KDD_TRAIN.xlsx','A67345:AO79000'); % n1=input
n= xlsread('NSL_KDD_TRAIN.xlsx','AP67345:AP79000'); % n=output
net=newff(n1',n',8);
net=train(net,n1',n');
y1=sim(net,n1'); %training data output
y31=round(y1);
i want to use the outputs of feed forward neural networks as input for training another same kind of neural network. how can i do that using built-in function?,from the above code i need to train a new newff where my input arguments will be y3 and y31. in m1 and n1 a have 41 columns and i need all 41 columns presence in the new network. how can i do that using built-in function?
Why not to use two layers and train just one network if you assume architecture is the same?
net = newff(input,target,[Layer_1 Layer_2],{'tansig' 'tansig' 'tansig'});
You can specify the size of Layer_1 and Layer_2 as much as you want as well as you can change activation function between each of the layers.
Tutorial to create custom network from Mathworks
Hope this will help

How to use SVM in Matlab?

I am new to Matlab. Is there any sample code for classifying some data (with 41 features) with a SVM and then visualize the result? I want to classify a data set (which has five classes) using the SVM method.
I read the "A Practical Guide to Support Vector Classication" article and I saw some examples. My dataset is kdd99. I wrote the following code:
%% Load Data
[data,colNames] = xlsread('TarainingDataset.xls');
groups = ismember(colNames(:,42),'normal.');
TrainInputs = data;
TrainTargets = groups;
%% Design SVM
C = 100;
svmstruct = svmtrain(TrainInputs,TrainTargets,...
'boxconstraint',C,...
'kernel_function','rbf',...
'rbf_sigma',0.5,...
'showplot','false');
%% Test SVM
[dataTset,colNamesTest] = xlsread('TestDataset.xls');
TestInputs = dataTset;
groups = ismember(colNamesTest(:,42),'normal.');
TestOutputs = svmclassify(svmstruct,TestInputs,'showplot','false');
but I don't know that how to get accuracy or mse of my classification, and I use showplot in my svmclassify but when is true, I get this warning:
The display option can only plot 2D training data
Could anyone please help me?
I recommend you to use another SVM toolbox,libsvm. The link is as follow:
http://www.csie.ntu.edu.tw/~cjlin/libsvm/
After adding it to the path of matlab, you can train and use you model like this:
model=svmtrain(train_label,train_feature,'-c 1 -g 0.07 -h 0');
% the parameters can be modified
[label, accuracy, probablity]=svmpredict(test_label,test_feaure,model);
train_label must be a vector,if there are more than two kinds of input(0/1),it will be an nSVM automatically.
train_feature is n*L matrix for n samples. You'd better preprocess the feature before using it. In the test part, they should be preprocess in the same way.
The accuracy you want will be showed when test is finished, but it's only for the whole dataset.
If you need the accuracy for positive and negative samples separately, you still should calculate by yourself using the label predicted.
Hope this will help you!
Your feature space has 41 dimensions, plotting more that 3 dimensions is impossible.
In order to better understand your data and the way SVM works is to begin with a linear SVM. This tybe of SVM is interpretable, which means that each of your 41 features has a weight (or 'importance') associated with it after training. You can then use plot3() with your data on 3 of the 'best' features from the linear svm. Note how well your data is separated with those features and choose a basis function and other parameters accordingly.