pnn分类 matlab,使用PNN进行数据分类 | 学步园

Classification of an XOR problem with a PNN

% ========================================================================

% PROBLEM DESCRIPTION:

% 4 clusters of data (A,B,C,D) are defined in a 2-dimensional input space.

% (A,C) and (B,D) clusters represent XOR classification problem.

% The task is to define a neural network for solving the XOR problem.

% ========================================================================

% NEURAL NETWORK course example by Primoz Potocnik, 2010.

% Optimized for MATLAB 2010b with Neural Network Toolbox 7.0

% ========================================================================

% 清除变量及清理环境

close all; clear; clc

Define 4 clusters of input data

% number of samples of each class

K = 100;

% define 4 clusters of input data

q = .6; % offset of classes

A = [rand(1,K)-q; rand(1,K)+q];

B = [rand(1,K)+q; rand(1,K)+q];

C = [rand(1,K)+q; rand(1,K)-q];

D = [rand(1,K)-q; rand(1,K)-q];

% plot clusters

plot(A(1,:),A(2,:),'k+')

hold on

grid on

plot(B(1,:),B(2,:),'b*')

plot(C(1,:),C(2,:),'k+')

plot(D(1,:),D(2,:),'b*')

nn06_rbfn_xor_3_newpnn_01.png

Define output coding

% coding (+1/-1) for 2-class XOR problem

a = 1; % PNN class 1

c = 1;

b = 2; % PNN class 2

d = 2;

Prepare inputs & outputs for network training

% define inputs (combine samples from all four classes)

% size(P) = 2行 X 400列

P = [A B C D];

% define targets

% size(T) = 1行 X 400列

T = [repmat(a,1,length(A)) repmat(b,1,length(B)) ...

repmat(c,1,length(C)) repmat(d,1,length(D)) ];

Create a PNN

% choose a spread constant

% 传播参数

spread = .5;

% create a neural network

% 创建网络

net = newpnn(P,ind2vec(T),spread);

% view network

view(net)

view3.jpg

Evaluate network performance

% simulate RBFN on training data

% 模拟输出

Y = net(P);

% convert PNN outputs

% 结果转换为类标签

Y = vec2ind(Y);

% calculate [%] of correct classifications

correct = 100 * length(find(T==Y)) / length(T);

fprintf('\nSpread = %.2f\n',spread)

fprintf('Num of neurons = %d\n',net.layers{1}.size)

fprintf('Correct class = %.2f %%\n',correct)

% plot targets and network response

figure;

plot(T')

ylim([0 3])

set(gca,'ytick',[0 1 2 3])

hold on

grid on

plot(Y','r')

legend('Targets','Network response')

xlabel('Sample No.')

Spread = 0.50

Num of neurons = 400

Correct class = 100.00 %

nn06_rbfn_xor_3_newpnn_02.png

Plot classification result for the complete input space

% generate a grid

span = -1:.02:2;

[P1,P2] = meshgrid(span,span);

pp = [P1(:) P2(:)]';

% simualte PNN on a grid

aa = sim(net,pp);

aa = full(vec2ind(aa))-1.5; % convert to

% plot classification regions based on MAX activation

figure(1)

m = mesh(P1,P2,reshape(-aa,length(span),length(span))-5);

set(m,'facecolor',[1 0.2 .7],'linestyle','none');

hold on

view(2)

m = mesh(P1,P2,reshape(aa,length(span),length(span))-5);

set(m,'facecolor',[1 1.0 0.5],'linestyle','none');

nn06_rbfn_xor_3_newpnn_03.png

Plot PNN centers on the classification result

% plot PNN centers

plot(net.iw{1}(:,1),net.iw{1}(:,2),'gs','linewidth',2)

nn06_rbfn_xor_3_newpnn_04.png

Published with MATLAB® 7.11

来源:http://lab.fs.uni-lj.si/lasin/www/teaching/neural/nn06_rbfn_xor/html/nn06_rbfn_xor_3_newpnn.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的PNN神经网络分类数据Matlab代码示例: ```matlab % 读入数据 data = load('data.txt'); X = data(:,1:2)'; Y = data(:,3)'; % 数据归一化 [X_norm, mu, sigma] = zscore(X); % 将数据拆分为训练集和测试集 [trainInd,valInd,testInd] = dividerand(size(X,2),0.6,0.2,0.2); x_train = X_norm(:,trainInd); y_train = Y(trainInd); x_val = X_norm(:,valInd); y_val = Y(valInd); x_test = X_norm(:,testInd); y_test = Y(testInd); % 训练PNN神经网络模型 net = newpnn(x_train,y_train); % 预测测试集数据 y_pred = sim(net,x_test); % 计算准确率 accuracy = sum(y_pred==y_test)/length(y_test); % 绘制决策边界 plotDecisionBoundary(x_train, y_train, net); % 绘制训练集和测试集数据散点图 figure; hold on; scatter(x_train(1,:),x_train(2,:),10,y_train,'filled'); scatter(x_test(1,:),x_test(2,:),10,y_pred,'filled','d'); hold off; % 定义绘制决策边界函数 function plotDecisionBoundary(X,Y,net) xMin = min(X(1,:)); xMax = max(X(1,:)); yMin = min(X(2,:)); yMax = max(X(2,:)); [xx,yy] = meshgrid(xMin:0.01:xMax,yMin:0.01:yMax); XGrid = [xx(:) yy(:)]'; YGrid = sim(net,XGrid); contour(xx,yy,reshape(YGrid,size(xx)),[0.5 0.5],'LineWidth',2); end ``` 其中,`data.txt`是包含输入数据和标签的文件,每行包含两个特征和一个标签。`zscore`函数用于归一化数据,`dividerand`函数用于将数据划分为训练集、验证集和测试集。`newpnn`函数用于创建PNN神经网络模型,`sim`函数用于进行预测。`plotDecisionBoundary`函数用于绘制决策边界。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值