Question
Hi, I am fairly new to MATLAB and I would like help in understanding about datasets. For classification in neural network, the example for wine classification show: [x,t] = wine_dataset; size(x) size(t) net = patternnet(10); view(net) I have a dataset of input [8x4]matrix and target [4x4]matrix. How do I input this into neural network such that I can use the function patternnet?
Related Questions
Expert Answer
John Williams
PhD Expert
Answered Nov 20, 2025
Exactly as indicated in
help patternnet
and
doc patternnet
where
x = yourinput;
t = yourtarget; % target columns should be unit vectors with a single 1
DOCUMENTATION BUG: the default input is (10,'trainscg') NOT (20,'trainlm')!
close all, clear all, clc
[ x, t ] = iris_dataset; [ I N ] = size(x) % [ 4 150 ] [ O N ] = size(t) % [ 3 150 ] trueclass = vec2ind(t); MSE00 = mean(var(t',1)) % 0.222 biased MSE for naive constant output model MSE00a = mean(var(t',0)) % 0.224 unbiased MSE for naive constant output model Ntrn = N - 2*(0.15*N) % 105 default size of training set Ntrneq = Ntrn*O % 315 Number of training equations
% Nw = (I+1)*H+(H+1)*O % No. of unknown weights for H hidden nodes
Hub = -1 + ceil( (Ntrneq-O)/(I+O+1))% 38 Upperbound for H if Ntrneq > Nw H = floor(Hub/10)% 3 Desire Ntrneq >> Nw to mitigate noise and errors in data Nw = (I+1)*H+(H+1)*O % 27 Number of unknown weights Ndof = Ntrneq - Nw % 288 No. of estimation degrees of freedom (See Wikipedia reference below) MSEgoal = 0.01*Ndof*MSE00a/Ntrneq % 0.002 Desire adjusted R^2 >= 0.99
MinGrad = MSEgoal/10 % 2.05e-4 rng(0) % Initialize RNG net = patternnet(H); net.trainParam.goal = MSEgoal; net.trainParam.min_grad = MinGrad; [net tr Y E ] = train(net,x,t); % E = t-Y stopcrit = tr.stop % Validation stop. numepochs = tr.num_epochs % 60 bestepoch = tr.best_epoch % 54 MSE = tr.perf(bestepoch+1) % 0.0115 MSEa = Ntrneq*MSE/Ndof % 0.0126 R2 = 1-MSE/MSE00 % 0.948 R2a = 1-MSEa/MSE00a % 0.944 assignedclass = vec2ind(Y); err = assignedclass~=trueclass; Nerr = sum(err) % 2 PctErr = 100*Nerr/N % 1.33
result = [ H numepochs bestepoch R2 R2a Nerr PctErr]
result = [ 3 60 54 0.948 0.944 2 1.33 ]
% NOTE1: Can use tr to obtain trn/val/tst breakdown
%NOTE2: To imprcve performance try other (e.g.,9 more) random weight initializations. If that doesn't work try 10 random weight initialization trials with H = 4.
Have a different question? Ask here