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?
John Williams answered .
2025-11-20
[ 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
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