⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 classifierann.m

📁 用Cross validation的方法建立人工神经网络的模型!
💻 M
字号:
% ANN neural network classifier

function [mse,R2,accuracy] = classifierANN(data)

[nr, nc] = size(data)
nf = nc - 1;           % number of features/attributes

% Transform the target representation: class label '2/1' to '0/1'
i = find(data(:, nc) == 2);
data(i, nc) = 0;


% 2. NORMALIZE THE DATA - we want all the features/attributes and the target variable to have the same means (0) and variances (1)
[meanv, stdv, data(:,1:nf)] = normalize(data(:,1:nf), [], []);

%% 5-cross validation
% divide the dataset into 5 groups
[left, a1] = divideset(data, 20);
[left, a2] = divideset(left, 25);
[left, a3] = divideset(left, 33);
[a5, a4] = divideset(left, 50);

for i_cross = 1: 5

    % 3. SPLIT THE DATASET
    % We should randomly split the whole dataset into 3 disjoint subsets: training, validation and test.
    % Reserve 30% of data as test set, for estimating the accuracy
    %[trval, test] = divideset(data, 30);
    % Assign 30% of trval to validation and 70% to training set
    %[tdata, vdata] = divideset(trval, 30);

    [trval, test] = combine5(a1,a2,a3,a4,a5,i_cross);
    % Assign 20% of tr_val to validation and 80% to training set
    [tdata, vdata]   = divideset(trval, 20);    

    % 4. INITIALIZE NEURAL NETWORK
    % we need choose the architecture of the neural network. For this example, we will use a feed-forward 
    % neural network with one hidden layer, sigmoid activation functions in the hidden layer, and linear 
    % activation function in the output layer. 

    num_hidd = 6;   % 10 neurons in the hidden layer

    % Select a neural network training algorithm (for adjusting the weights)
    method = 1;
    switch method
      case 1,    train_alg = 'traingd'    % Standard Back Propagation
      case 2,	 train_alg = 'trainrp'    % Resilient - Back propagation
      case 3,	 train_alg = 'traingdx'   % Variable Learning Rate
      case 4,	 train_alg = 'traincgb'   % Powell-Beale Conjugate Gradient
      case 5,	 train_alg = 'trainlm'    % Levenberg-Marquardt                 <-- selected
      case 6,	 train_alg = 'trainbfg'   % Quasi-Newton Algorithm
      case 7, 	 train_alg = 'trainscg'   % Scaled Conjugate Gradient
      otherwise, train_alg = 'traingd'
    end

    % Create the neural network object
    net = newff([min(tdata(:, 1 : nf)); max(tdata(:, 1 : nf))]', ...  % the range of each attribute
                [num_hidd 1],  ...                                                        % # of neurons in the hidden and output layers, respectively
                {'logsig', 'purelin'}, ...                                                % activation functions for neurons in the hidden and output layers, respectively
                train_alg);                                                                % training algorithm

    % Define other important parameters for neural network training. If ommit learning rate, Matlab automatically determines 
    % the learnig rate, which might be better in many cases
    net.trainParam.lr = 0.2;      % Choose learning rate
    net.trainParam.epochs = 250;  % maximum number of epochs
    net.trainParam.show = 50;     % show results each 10 epochs
    net.trainParam.max_fail = 10;  % stop if the error on validation set does not improve over 5 iterations

    % 5. PERFORM TRAINING
    % Reshape training, validation and test sets to fit the neural network training function
    P = tdata(:, 1 : nf)';     
    T = tdata(:, nf + 1)';     
    VV.P = vdata(:, 1 : nf)';
    VV.T = vdata(:, nf + 1)';
    TV.P = test(:, 1 : nf)';
    TV.T = test(:, nf + 1)';
    % The actual training
    [net, tc] = train(net, P, T, [], [], VV, TV);

    % 6. MAKE PREDICTION ON THE TEST SET AND ESTIAMTE ACCURACY
    prediction = sim(net, test(:, 1 : nf)')';

    pred_error = prediction - test(:, nf + 1);
    % plot the histogram of errors
    %hist(pred_error);

    % Calculate MEAN Squared Error (MSE)
    mse(i_cross) = (pred_error' * pred_error) / length(pred_error);
    % Calculate R-square (PREDICTION ACCURACY)
    R2(i_cross) = 1 - mse(i_cross) / var(test(:, nc));
    %title(sprintf('R2 = %.3f', R2));

    pred_decided = [];
    
    q = find(prediction >= 0.5);
    pred_decided(q) = 1;
    q = find(prediction < 0.5);
    pred_decided(q) = 0;
    
    test_c = [];
    test_c = test(:, nf + 1);
    size_pred_decided = size(pred_decided);
    size_test_c = size(test_c);

    % evaluate accuracy of prediction
    q11 = find(test_c' == 1 & pred_decided == 1); % true class 1, predicted class 1
    q10 = find(test_c' == 1 & pred_decided == 0); % true class 1, predicted class 0
    q01 = find(test_c' == 0 & pred_decided == 1); % true class 0, predicted class 1
    q00 = find(test_c' == 0 & pred_decided == 0); % true class 0, predicted class 0
    % maybe, a better way to calculate accuracy is for both classes
    sensitivity = length(q11) / (length(q11) + length(q10)); % accuracy on class 1
    specificity = length(q00) / (length(q01) + length(q00)); % accuracy on class 0

    accuracy(i_cross) = (sensitivity + specificity) / 2;    
end
mse;
accuracy;

mse = mean(mse);
R2 = mean(R2);
accuracy = mean(accuracy);

disp 'ANN done'

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -