Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Group info
Members: 34
Language: English
Group categories: Not categorized
More group info »
Recent pages and files
5). Some Customized Functions    

1.1).      Normalization

A Sample M-File: normalize.m

function data = normalize( original )

%Suppose the last column is remark, we only normalize the columns from 1 to end-1

muo = mean( original( :, 1:end-1) );

stdo = std( original( :, 1:end-1) );

stdo2 = 1 ./ stdo;

data = [ (original(:,1:end-1) - ones(row,1)*muo ) * diag(stdo2)     original(:,end) ];

 

1.2).      PCA

A Sample M-File: pcaDR.m

function data = pcaDR(original)

%Suppose the last column is remark

t = original(:,1:end-1);

[u, d, v] = svd(t,0);

data = [t*v original(:,end)];

 

1.3).      Cross-Validation

A Sample M-File: crossValidation.m (This file may be used for Prototype Methods)

function [v errRate testErrRate] = crossValidation2(classifyfunh, data, foldNum, prototypeNum)

% Input:

% classifyfunh is the function handle, e.g.  @kmeans for function kmeans().

% Output:

% v is the vector of prototypes

% Notice: if you design all prototypes methods with the same input and

% output parameters, the cross validation function may be reused.

[row, column] = size(data);

errRate = ones(prototypeNum,foldNum);

testErrRate = ones(prototypeNum,foldNum);

perFold = floor(row / foldNum);

fn = 0;

while fn < foldNum

    i1 = fn*perFold;

    i2 = (fn+1)*perFold;

    testData = data(i1+1:i2, :);

    trainingData = [ data(1:i1, :); data(i2+1:row, :) ];

    fn = fn + 1;

    for i = 1:prototypeNum

        [v errRate(i,fn) testErrRate(i,fn)] = classifyfunh(trainingData, testData, i);

        % Function of classifyfunh.

        % Input:

        % i is the number of prototypes

        % Output

        % v is a vector of prototypes   

    end

end

 

1.4).      Forward Selection

A Sample M-File: forwardSelection.m (This file may be used for Prototype Methods)

function [v trainingErrRate testErrRate] = forwardSelection(classifyfunh, trainingData, testData, prototypeNum)

% Input:

% classifyfunh is the function handle, e.g. kmeans(), kcenter()

% Output:

% v is a vector of prototypes

% Notice: if you design all prototypes methods with the same input and

% output parameters, the cross validation function may be reused.

[row column] = size(trainingData);

[trow tcolumn] = size(testData);

trainingErrRate = ones(column-1,1);

testErrRate = ones(column-1,1);

ori = 1:column-1;

forward = 0;

forwardIndex = zeros(1,column-1);

remainIndex = ones(1,column - 1);

while forward < column-1

    remains = ori(remainIndex(1,:)==1);

    jj = 1;

    tIndex = 0;

    while jj <= column - 1 - forward

        pc = [forwardIndex(1:forward) remains(jj)];

        [vt errRatet testErrRatet] = classifyfunh(trainingData(:,[pc,end]),testData(:,[pc,end]),prototypeNum);

        % Function :  classifyfunh.

        % Output

        % vt is the vector of prototypes   

        if( trainingErrRate(forward+1) > errRatet)

            testErrRate(forward+1) = testErrRatet;

            trainingErrRate(forward+1) = errRatet;

            tIndex = remains(jj);

            v = vt;

        end

        jj = jj+1;

    end

    remainIndex(tIndex) = 0;

    forwardIndex(forward+1) = tIndex;

    forward = forward + 1;

end;

 

 

1.5).      Scatter Plot for Prototype Methods (with Classification Boundary )

A Sample M-File: plot4pm.m

function plot4pm(data, v)

%data is 2-D samples and the last column is class mark

%v is prototype set with class mark

%Plot the boundary line according to v

minx = min(data(:,1));

maxx = max(data(:,1));

miny = min(data(:,2));

maxy = max(data(:,2));

stepx = (maxx -minx)/200;

stepy = (maxy -miny)/200;

 

x = minx:stepx:maxx;

y = miny:stepy:maxy;

 

v1 = v(v(:,end)==0,:);

[v1r,tt] = size(v1);

v2 = v(v(:,end) == 1,:);

[v2r,tt] = size(v2);

repmat([1, 2],v1r,1);

 

%axis tight fill square normal;

axis([minx maxx miny maxy]);

%plot data

data0 = data(data(:,end)==0,1:2);

data1 = data(data(:,end)==1,1:2);

plot(data0(:,1),data0(:,2),'b*');

hold on;

plot(data1(:,1),data1(:,2),'ro');

 

%plot the boundary

fh = @(x1,x2) min( sum( ( repmat([x1 x2],v1r,1) - v1(:,1:2) ).^2, 2 ) ) - min( sum( ( repmat([x1 x2],v2r,1) - v2(:,1:2) ).^2, 2 ) );

h = ezplot(fh,[minx maxx miny maxy]);

set(h,'Color','black');

%set(h,'LineStyle',':');                                     

%set(h,'LineWidth',2);                                        

%set(h,'Marker','s');  

 

%plot prototypes

plot(v(:,1),v(:,2),'ks');

 

%plot the backgound points

x = repmat(x',100,1);

y = reshape(repmat(y,100,1),[],1);

meshdata = [x,y];

flag = classifyByDist(meshdata, v(:,1:2));

scatter(x,y,1,flag);

 

title('Classfication Result - Scatter & Class Boundary');

%xlabel({'Column',forwardIndex(1)}), ylabel({'Column',forwardIndex(2)})

legend('With Diabete(1)','Withought Diabete(0)','Class Boundary','Prototypes');

hold off;

Notice: this function deals with a simple situation with only two classes. But you can still apply it to draw the boundary when there are more than two classes. For example, if there are n classes: C1, C2,…,Cn;  you can look on C1,…,Ci-1, Ci+1…,Cn as a super class Cei, and then draw the boundary between Ci and Cei, this is the situation of only two classes (i = 1,2,…,n). The deficiency of this method is that the boundary has been draw twice.

Version: 
Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google