# Iterative training of potential function algorithm

1, Experimental purpose

Through the study of this experiment, students can understand or master the method of designing nonlinear discriminant function by using the idea of potential function in pattern recognition, and can realize pattern classification. Learn to use the learned pilot courses, such as data structure and algorithm design knowledge, and select the appropriate data structure to complete the algorithm design and program implementation. The nonlinear discriminant function is established through the training data, the classification prediction is carried out by replacing the samples to be classified, and the correctness of the classifier is tested by checking the prediction results and the geometric distribution characteristics of the data. By selecting this classification method for classifier design experiment, students can strengthen their understanding and application of nonlinear classifier, so as to firmly grasp the content knowledge of pattern recognition course.

2, Experimental content

It is assumed that the normal (w1) and abnormal (w2) data obtained from the examination of the three main indexes of the patient are as follows:

Class w1:   (1，2， 5)， (1，1， 2)，(3，3，6);

Category w2:   (5，6，10)，(7，6，11)，(8，7，12).

3, Basic thought

Iterative method, also known as rolling method, is a process of constantly pushing new values from the old values of variables. It is a basic method to solve problems. By allowing the computer to repeatedly execute a group of instructions (or certain steps), each time the group of instructions (or these steps) are executed, a new value is pushed from the original value of variables.

The basic idea of iterative algorithm is: in order to find the solution X of a problem, a new value X1 can be obtained from a given initial value x0 according to an iterative formula, which is closer to the required value x than the initial value x0; then take the new value as the initial value, that is, x1 → x0, find X1 again according to the original method, and repeat this process until | x1-x0|< ε (a given precision). In this case, X1 can be taken as the solution X of the problem.

Using iterative algorithm to solve problems requires the following three aspects:

(1) Determine iterative variables. In the problems that can be solved by iterative algorithms, there is at least one variable that directly or indirectly continuously deduces new values from old values. This variable is iterative variable.

(2) Establish iterative relationship. The so-called iterative relationship refers to the formula (or relationship) of how to deduce the next value of a variable from the previous value. The establishment of iterative relationship is the key to solving iterative problems.

(3) Control the iterative process. When to end the iterative process? This is a problem that must be considered in writing the iterative program. The iterative process cannot be repeated endlessly. The control of the iterative process can usually be divided into two cases: one is that the required number of iterations is a certain value and can be calculated; the other is that the required number of iterations cannot be determined. For the former In one case, a fixed number of cycles can be constructed to control the iterative process; in the latter case, the conditions used to end the iterative process need to be further analyzed.

Iteration is also implemented in a loop structure, but the operation to be repeated is to continuously calculate the new value of a variable from its old value. Its basic format is described as follows:

Initial value of iteration variable;

while (iteration termination condition)

{
Calculate the new value from the old value according to the iterative expression;

The new value replaces the old value to prepare for the next iteration;

}

4, Experimental steps

1. Select the set potential function (choose one of the three bivariate symmetric basis functions; or make multiple choices to realize manual and automatic selection);

2. Determine the appropriate data structure to complete the correct representation of potential function and discriminant function respectively;

3. The training samples are trained and studied, and the discriminant function is established to meet the classification requirements

4. Record and output training rounds;

5. Use your classifier to judge the categories of all samples (classification decision), and compare the differences with the actual categories;

6. Judge the classified samples to obtain their category (prediction), and explain it with geometric distribution if possible;

7. Output the expression of your discriminant function (Note: the expression should be easy to read and understand).

5, Testing

1. First test the correctness of existing samples.

2. Classify with the data to be classified. Here, the samples: (2, 3, 5), (6, 7, 10)

Test them respectively to check whether their geometric distribution is consistent with the results of w1 and w2 respectively, so as to confirm that the designed classifier is correct.

6, Implementation tips

1) The samples are stored in the matrix s, and each row of S is a sample. In order to facilitate programming, the category number can be added to each sample as the last dimension;

2) In order to save and calculate the discriminant function, an auxiliary structure array ftbl can be used. Each component of the array contains two components: index and symbol. Index records the label under the corresponding sample, and symbol records the symbol of the item.

7, Reference code

```% Design of nonlinear discriminator by potential function method

n=6;  % n Represents the total number of samples. Here n=6,The first three samples belong to the first category, and the last three samples belong to the second category

m=30; % Maximum number of terms of discriminant function

d=3;  % d Represents dimension length

r=0;  % r Represents the number of items in the discriminant function (each item is a basis function, including 3 coordinate components (dimensions=3))

tag=1; %Flag quantity for judging whether to continue the cycle

g=0;

% sample

s=[ 1,2, 5,1

1,1, 2,1

3,3, 6,1

5,6,11,2

7,6,11,2

8,7,12,2];   % Column 4 indicates the category: 1 Indicates belonging to category 1 % 2 Indicates belonging to category 2

run=0; % run Is the round, and the initial value is set to 0

while tag==1

run=run+1;

tag=0;

for k=1:n  % n Represents the total number of samples.

if r==0   % r==0 Indicates that the discriminant function does not contain any term

r=r+1;            %r Point to the last term of the potential function obtained so far, and prepare to include the first term

% ftbl Is an array of structures. Each component of the array contains index and symbol For two components, record the sample number and symbol respectively

ftbl(r).symbol=1;  % The symbol for this item. 1--Positive;-1--negative

ftbl(r).index=1;   % The corresponding sample label of this item

continue;

else

g=0;

% Change the current page k Samples are first substituted into the established partial discriminant function for calculation, and then judge whether the classification is correct

for i=1:r % i An integer variable that scans each item

temp=0;

for j=1:d    % d Indicates dimension length. Here, d Actually 3, i.e d=3

temp=temp+(s(k,j)-s(ftbl(i).index,j))*(s(k,j)-s(ftbl(i).index,j));

end

g= g+ftbl(i).symbol*exp(-temp);  %Each term is in the form of an index,Find common r Sum of items

end

if ((g>0 &s(k,4)==1)||(g<0&s(k,4)==2))

continue;  %When the classification is correct, the discriminant function is not modified

else  % The current sample should form an item and be saved in the discrimination expression

tag=1;

r=r+1;

ftbl(r).index=k;

if(g>0& s(k,4)==2)

ftbl(r).symbol=-1;

else if(g<0&s(k,4)==1)

ftbl(r).symbol=1;

end

end

end

end

end

end

fprintf('Number of cycles= %d',run);

fprintf('\n Expression of output discriminant function:\n');

% Output discriminant function,That is, output each item of the discriminant function. Through the output structure array ftbl Each component of

for i=1:r

% Output No i term

if(ftbl(i).symbol==1)

if i==1

fprintf('exp{-[(x1')

else

fprintf('+exp{-[(x1')

end

else

fprintf('-exp{-[(x1');

end

% Whether the first component of the sample is a positive sign or a negative sign determines the sign before the output component value

if (s(ftbl(i).index,1)>0)  % The first component of the sample is a positive sign

fprintf('-')

fprintf('%d',s(ftbl(i).index,1))

fprintf(')^2+(x2')

else if(s(ftbl(i).index,1)<0)  % The first component of the sample is a minus sign

fprintf('+')

fprintf('%d',-s(ftbl(i).index,1)) % Negative is positive

fprintf(')^2+(x2');

else  %s(ftbl(i).index,1)==0

fprintf(')^2+(x2');

end

end

if (s(ftbl(i).index,2)>0)

fprintf('-')

fprintf('%d',s(ftbl(i).index,2))

fprintf(')^2+(x3')

else if(s(ftbl(i).index,2)<0)

fprintf('+')

fprintf('%d',-s(ftbl(i).index,2))

fprintf(')^2+(x3');

else

fprintf(')^2+(x3')

end

end

if  (s(ftbl(i).index,3)>0)

fprintf('-')

fprintf('%d',s(ftbl(i).index,3))

fprintf(')^2]}');

else

if(s(ftbl(i).index,3)<0)

fprintf('+')

fprintf('%d',-s(ftbl(i).index,3))

fprintf(')^2]}');

else

fprintf(')^2]}')

end

end

end

fprintf('\n')

% Identify the category of each sample:

fprintf('Identify the category of each sample:\n');

for k=1:n;

g=0;

for i=1:r

temp=0;

for j=1:d  %d Represents dimension length

temp=temp+(s(k,j)-s(ftbl(i).index,j))*(s(k,j)-s(ftbl(i).index,j));

end

g=g+ftbl(i).symbol*exp(-temp);  %common r Each term is in the form of an index

end

if (g>0)

fprintf('The first')

fprintf('%d',k)

fprintf('The category of samples is: ')

fprintf('%d\n',1)

else if (g<0)

fprintf('The first')

fprintf('%d',k)

fprintf('The category of samples is: ')

fprintf('%d\n',2)

else  %g==1

fprintf('The first')

fprintf('%d',k)

fprintf('The category of samples cannot be distinguished!  ')

fprintf('But the first')

fprintf('%d',k)

fprintf('The actual category of samples is: ')

fprintf('%d\n',s(k,4));%Output actual category

end

end

end

% cout<<endl;

%Judge the categories of (2, 3, 5) and (6, 7, 11) respectively:

a=[2,3,5];

g=0;

for i=1:r

temp=0;

for j=1:d  %d Represents dimension length

temp=temp+(a(j)-s(ftbl(i).index,j))*(a(j)-s(ftbl(i).index,j));

end

g=g+ftbl(i).symbol*exp(-temp);  %common r Each term is in the form of an index

end

if g>0

fprintf('sample a=(2，3，5)The category of is: ')

fprintf('%d\n',1)

else

if (g<0)

fprintf('sample a=(2，3，5)The category of is: ')

fprintf('%d\n',2)

else

fprintf('sample a=(2，3，5)The category of cannot be distinguished!\n')

end

end

%Now for the second sample,I.e. (6, 7, 11)

b=[6,7,11];

g=0;

for i=1:r

temp=0;

for j=1:d  % d Represents dimension length

temp=temp+(b(j)-s(ftbl(i).index,j))*(b(j)-s(ftbl(i).index,j));

end

g=g+ftbl(i).symbol*exp(-temp);  %common r Each term is in the form of an index

end

if g>0

fprintf('sample b=(6，7，11)The category of is: ')

fprintf('%d\n',1)

else

if (g<0)

fprintf('sample b=(6，7，11)The category of is: ')

fprintf('%d\n',2)

else

fprintf('sample b=(6，7，11)The category of cannot be distinguished!\n')

end

end

fprintf('\n')

%%%%

%%%

function g=calfun(s,ftbl,r)

% s Store samples; ftbl Storage sample number and symbol; r Is the number of items

g=1;

for i=1:r

temp=1;

for j=1:d   % d Represents dimension length

temp= temp+(s(k,j)-s(ftbl(i).index,j))*(s(k,j)-s(ftbl(i).index,j));

g= g+ftbl(i).symbol*exp(-temp);  %common r Each term is in the form of an index

end

end

end```

Tags: Algorithm

Posted on Wed, 27 Oct 2021 20:32:49 -0400 by Lillefix