# Iterative training of three potential function algorithm in pattern recognition experiment

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 (class) and abnormal (class) data obtained from the examination of the three main indexes of the patient are as follows:
Class: (1, 2, 5), (1, 1, 2), (3, 3, 6);
Class: (5, 6, 10), (7, 6, 11), (8, 7, 12)
3, 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).
4, 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 separately to check whether their geometric distribution is consistent with the results belonging to class and class respectively, so as to confirm that the designed classifier is correct.
5, 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.
6, 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
```

Keywords: Algorithm

Added by mgason on Wed, 27 Oct 2021 17:41:25 +0300