├── Hopfield.m ├── Kohonen.m ├── PCA.m ├── PCA_data.m ├── Perceptron_AND.m ├── Perceptron_XOR.m ├── XOR_bp.m ├── XOR_bp_alr.m ├── fuzzy_centre_1.m ├── genetic algorithms_1.m ├── genetic algorithms_2.m ├── genetic algorithms_TSP.m ├── sequential model初练手.py └── sklearn-SVC.py /Hopfield.m: -------------------------------------------------------------------------------- 1 | 2 | % Hit any key to define two target fundamental memories to be stored 3 | % in the network as the two columns of the matrix T. 4 | pause 5 | 6 | T=[1 1 1;-1 -1 -1]' 7 | 8 | % Hit any key to plot the Hopfield state space with the two fundamental memories 9 | % identified by red markers. 10 | pause 11 | 12 | plot3(T(1,:),T(2,:),T(3,:),'r.','markersize',20) 13 | axis([-1 1 -1 1 -1 1]); axis manual; hold on; 14 | title('Representation of the possible states for the three-neuron Hopfield network') 15 | set(gca,'box','on'); view([37.5 30]); 16 | 17 | % Hit any key to obtain weights and biases of the Hopfield network. 18 | pause 19 | 20 | net=newhop(T); 21 | 22 | % Hit any key to test the network with six unstable states represented as the 23 | % six-column matrix P. Unstable states are identified by blue markers. 24 | pause 25 | 26 | P=[-1 1 1;1 -1 1;1 1 -1;-1 -1 1;-1 1 -1;1 -1 -1]' 27 | 28 | for i=1:6 29 | a = {P(:,i)}; 30 | [y,Pf,Af]=sim(net,{1 10},{},a); 31 | record=[cell2mat(a) cell2mat(y)]; 32 | start=cell2mat(a); 33 | plot3(start(1,1),start(2,1),start(3,1),'b*',record(1,:),record(2,:),record(3,:)) 34 | drawnow; 35 | % Hit any key to continue. 36 | pause 37 | end 38 | 39 | % Each of these unstable states represents a single error, compared to the fundamental 40 | % memories. As you have just seen, the fundamental memories attract unstable states. 41 | 42 | % Hit any key to test the network with three random input vectors. Random states are 43 | % identified by green '*' markers. 44 | pause 45 | 46 | for i=1:3 47 | a={rands(3,1)}; 48 | [y,Pf,Af]=sim(net,{1 10},{},a); 49 | record=[cell2mat(a) cell2mat(y)]; 50 | start=cell2mat(a); 51 | plot3(start(1,1),start(2,1),start(3,1),'g*',record(1,:),record(2,:),record(3,:),'g-') 52 | % Hit any key to continue. 53 | pause 54 | end 55 | 56 | % The Hopfield network always converge to a stable state. However, this stable state 57 | % does not necessarily represents a fundamental memory. 58 | 59 | % Hit any key to see how four input vectors, represented as the four-column matrix P, 60 | % converge to a stable but undesired state in the center of the state space. 61 | % The undesired state is identified by the magenta circle. 62 | pause 63 | 64 | P=[1 0 -1; 0 1 -1; -1 0 1; 0 -1 1]' 65 | 66 | for i=1:4; 67 | a = {P(:,i)}; 68 | [y,Pf,Af] = sim(net,{1 10},{},a); 69 | record=[cell2mat(a) cell2mat(y)]; 70 | start=cell2mat(a); 71 | plot3(start(1,1),start(2,1),start(3,1),'m*',record(1,:),record(2,:),record(3,:),'m-') 72 | if i<2 73 | plot3(0,0,0,'mo') 74 | end 75 | % Hit any key to continue. 76 | pause 77 | end; 78 | 79 | echo off 80 | disp('end of Hopfield.m') -------------------------------------------------------------------------------- /Kohonen.m: -------------------------------------------------------------------------------- 1 | % Hit any key to create 200 two-dimensional input vectors randomly distributed 2 | % in a square region. 3 | pause 4 | 5 | rand('seed',1234); 6 | p=rands(2,200); 7 | plot(p(1,:),p(2,:),'r.') 8 | title('Input vectors'); 9 | xlabel('p(1)'); 10 | ylabel('p(2)'); 11 | 12 | % Hit any key to create and initialise the Kohonen network with 36 neurons 13 | % arranged in the form of a two-dimensional lattice with 6 rows and 6 columns. 14 | pause 15 | 16 | s1=6; s2=6; 17 | net=newsom([-1 1; -1 1],[s1 s2]); 18 | plotsom(net.iw{1,1},net.layers{1}.distances) 19 | 20 | % Each neuron is represented by a red dot at the location of its two weights. 21 | % The initiall weights are assigned to zero, so only a single dot appears. 22 | 23 | % Hit any key to train the Kohonen network on the 200 vectors for 5 epoch. 24 | pause 25 | 26 | echo off; 27 | 28 | net.trainParam.epochs=1; 29 | 30 | for i=1:5 31 | net=train(net,p); 32 | delete(findobj(gcf,'color',[1 0 0])); 33 | delete(findobj(gcf,'color',[0 0 1])); 34 | plotsom(net.IW{1,1},net.layers{1}.distances); 35 | title(['Weight vectors, Epoch # ',num2str(net.trainParam.epochs)]); 36 | pause(0.1) 37 | net.trainParam.epochs=net.trainParam.epochs + 1; 38 | end 39 | 40 | for i=1:(s1*s2); 41 | text(net.iw{1,1}(i,1)+0.02,net.iw{1,1}(i,2),sprintf('%g',i)); 42 | end 43 | 44 | echo on; 45 | 46 | % After training the Kohonen layer becomes self-organised so that each neuron 47 | % can now classify a different region of the input space. 48 | % 49 | % Hit any key to apply the Kohonen network for the classification of a randomly 50 | % generated 2-element input vector. The vector is identified by the green marker. 51 | % 52 | % The output denoted by "a" indicates which neuron is responding. 53 | pause 54 | 55 | rand('seed',1254) 56 | 57 | for i=1:3 58 | probe=rands(2,1); 59 | hold on; 60 | plot(probe(1,1),probe(2,1),'.g','markersize',25); 61 | a=sim(net,probe); 62 | a=find(a) 63 | text(probe(1,1)+0.03,probe(2,1),sprintf('%g',(a))); 64 | hold off 65 | % Hit any key to continue. 66 | if i<3 67 | pause 68 | end 69 | end 70 | 71 | echo off 72 | disp('end of Kohonen.m') -------------------------------------------------------------------------------- /PCA.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | % ============================================= 4 | % Problem: Apply PCA to a pilot-plant data set. 5 | % ============================================= 6 | % 7 | % Hit any key to load the pilot-plant data. 8 | pause 9 | 10 | [data] = PCA_data; 11 | 12 | xo=data(:,1); yo=data(:,2); 13 | 14 | % Hit any key to subtract the mean of the data dimension from each data value 15 | % in this dimension and plot the normalised pilot-plant data. 16 | pause 17 | 18 | x=xo-mean(xo); y=yo-mean(yo); 19 | plot(x,y,'bo','markersize',5); 20 | axis('equal'); axis([-100 80 -80 80]); 21 | title('A plot of the normalised pilot-plant data'); 22 | xlabel('Acid number (normalised)'); 23 | ylabel('Organic acid content (normalised)'); 24 | hold on 25 | 26 | % Hit any key to calculate the covariance matrix. 27 | pause 28 | 29 | C=cov(x,y) 30 | 31 | % Hit any key to calculate eigenvalues and eigenvectors of the covariance matrix. 32 | pause 33 | 34 | lambda=eig(C); % the eigenvalues 35 | lambda=sort(lambda,'descend') 36 | variance=lambda*100/sum(lambda) 37 | [V,D] = eig(C); % V is the matrix with the eigenvectors 38 | V=V(:,2:-1:1) 39 | e1=V(:,1) 40 | e2=V(:,2) 41 | 42 | % Hit any key to plot of the normalised pilot-plant data within the axes formed 43 | % by the eigenvectors e1 and e2. 44 | pause 45 | 46 | xx1=-100; 47 | yy1=xx1*V(2,1)/V(1,1); 48 | xx2=80; 49 | yy2=xx2*V(2,1)/V(1,1); 50 | line([xx1 xx2],[yy1 yy2]); 51 | hold on 52 | xx1=-100; 53 | yy1=xx1*V(2,2)/V(1,2); 54 | xx2=80; 55 | yy2=xx2*V(2,2)/V(1,2); 56 | line([xx1 xx2],[yy1 yy2]); 57 | line([-100 100],[0 0],'Color',[.8 .8 .8]); 58 | line([0 0],[-80 80],'Color',[.8 .8 .8]); 59 | text(-95,-20,'e1'); 60 | text(25,-70,'e2'); 61 | 62 | % Hit any key to plot of the normalised data projected onto the feature space 63 | % formed by both eigenvectors, e1 and e2 64 | pause 65 | 66 | xy=[x y]; 67 | fin=xy*V; 68 | figure 69 | plot(fin(:,1),fin(:,2),'bo','markersize',5); 70 | axis('equal'); axis([-80 100 -80 80]); 71 | line([-80 100],[0 0]); 72 | line([0 0],[-80 80]); 73 | title('Normalised data projected on the feature space formed by e1 and e2'); 74 | xlabel('Acid number (normalised)'); 75 | ylabel('Organic acid content (normalised)'); 76 | text(90,8,'e1'); 77 | text(3,70,'e2'); 78 | 79 | % Hit any key to plot of the normalised data projected onto the feature space 80 | % formed by a single eigenvector, e1 81 | pause 82 | 83 | V1=V(:,1) 84 | fin1=xy*V1; y=0; 85 | figure 86 | plot(fin1,y,'bo','markersize',5); 87 | axis('equal'); axis([-80 100 -80 80]); 88 | line([-80 100],[0 0]); 89 | title('Normalised data projected on the feature space formed by e1'); 90 | xlabel('Acid number (normalised)'); 91 | ylabel('Organic acid content (normalised)'); 92 | text(90,8,'e1'); 93 | 94 | % Hit any key to plot the reconstructed pilot-plant data derived using both 95 | % eigenvectors, e1 and e2 96 | pause 97 | 98 | figure 99 | data=fin*V'; 100 | data(:,1)=data(:,1)+mean(xo); data(:,2)=data(:,2)+mean(yo); 101 | plot(data(:,1),data(:,2),'bo','markersize',5); 102 | axis('equal'); axis([10 180 -40 120]); 103 | title('Reconstructed data derived using eigenvectors e1 and e2'); 104 | xlabel('Acid number (normalised)'); 105 | ylabel('Organic acid content (normalised)'); 106 | 107 | 108 | % Hit any key to plot the reconstructed pilot-plant data derived using a single 109 | % eigenvector, e1 110 | pause 111 | 112 | figure 113 | data1=fin1*V1'; 114 | data1(:,1)=data1(:,1)+mean(xo); data1(:,2)=data1(:,2)+mean(yo); 115 | plot(data1(:,1),data1(:,2),'bo','markersize',5); 116 | axis('equal'); axis([10 180 -40 120]); 117 | title('Reconstructed data derived using eigenvector e1'); 118 | xlabel('Acid number (normalised)'); 119 | ylabel('Organic acid content (normalised)'); 120 | 121 | echo off 122 | disp('end of PCA') -------------------------------------------------------------------------------- /PCA_data.m: -------------------------------------------------------------------------------- 1 | function [data] = PCA_data() 2 | 3 | % ================================== 4 | % Data set for the PCA demonstration 5 | % ================================== 6 | 7 | 8 | % Pilot-Plan Data 9 | data = ... 10 | [123 76 11 | 109 70 12 | 62 55 13 | 104 71 14 | 57 55 15 | 37 48 16 | 44 50 17 | 100 66 18 | 16 41 19 | 28 43 20 | 138 82 21 | 105 68 22 | 159 88 23 | 75 58 24 | 88 64 25 | 164 88 26 | 169 89 27 | 167 88 28 | 149 84 29 | 167 88]; -------------------------------------------------------------------------------- /Perceptron_AND.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LucyChen0228/ML-basic-artificial-neural-networks-algorithms/0273ec34986d1644dcc8561a1745e61b7330bd38/Perceptron_AND.m -------------------------------------------------------------------------------- /Perceptron_XOR.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LucyChen0228/ML-basic-artificial-neural-networks-algorithms/0273ec34986d1644dcc8561a1745e61b7330bd38/Perceptron_XOR.m -------------------------------------------------------------------------------- /XOR_bp.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LucyChen0228/ML-basic-artificial-neural-networks-algorithms/0273ec34986d1644dcc8561a1745e61b7330bd38/XOR_bp.m -------------------------------------------------------------------------------- /XOR_bp_alr.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LucyChen0228/ML-basic-artificial-neural-networks-algorithms/0273ec34986d1644dcc8561a1745e61b7330bd38/XOR_bp_alr.m -------------------------------------------------------------------------------- /fuzzy_centre_1.m: -------------------------------------------------------------------------------- 1 | 2 | % Hit any key to load the fuzzy system. 3 | pause 4 | 5 | a=readfis('centre_1.fis'); 6 | 7 | % Hit any key to display the whole system as a block diagram. 8 | pause 9 | 10 | figure('name','Block diagram of the fuzzy system'); 11 | plotfis(a); 12 | 13 | % Hit any key to display fuzzy sets for the linguistic variable "Mean delay". 14 | pause 15 | 16 | figure('name','Mean delay (normalised)'); 17 | plotmf(a,'input',1); 18 | 19 | % Hit any key to display fuzzy sets for the linguistic variable "Number of servers". 20 | pause 21 | 22 | figure('name','Number of servers (normalised)'); 23 | plotmf(a,'input',2); 24 | 25 | % Hit any key to display fuzzy sets for the linguistic variable "Repair utilisation 26 | % factor". 27 | pause 28 | 29 | figure('name','Repair utilisation factor'); 30 | plotmf(a,'input',3); 31 | 32 | % Hit any key to display fuzzy sets for the linguistic variable "Number of spares". 33 | pause 34 | 35 | figure('name','Number of spares (normalised)'); 36 | plotmf(a,'output',1); 37 | 38 | % Hit any key to bring up the Rule Editor. 39 | pause 40 | 41 | ruleedit(a); 42 | 43 | % Hit any key to generate three-dimensional plots for Rule Base. 44 | pause 45 | 46 | figure('name','Three-dimensional surface (a)'); 47 | gensurf(a,[2 1],1); view([140 37.5]); 48 | 49 | % Hit any key to continue. 50 | pause 51 | 52 | figure('name','Three-dimensional surface (b)'); 53 | gensurf(a,[1 3],1); 54 | 55 | % Hit any key to bring up the Rule Viewer. 56 | pause 57 | 58 | ruleview(a); 59 | 60 | % Hit any key to continue. 61 | pause 62 | 63 | % CASE STUDY 64 | % ===================================================================================== 65 | % Suppose, a service centre is required to supply its customers with spare parts within 66 | % 24 hours. The service centre employs 8 servers and the repair utilisation factor is 67 | % 60%. The inventory capacities of the centre are limited by 100 spares. The values for 68 | % the mean delay, number of servers and repair utilisation factor are 0.6, 0.8 and 0.6, 69 | % respectively. 70 | % ===================================================================================== 71 | 72 | % Hit any key to obtain the required number of spares. 73 | pause 74 | 75 | n=round((evalfis([0.7 0.8 0.6],a))*100) 76 | 77 | % Hit any key to continue. 78 | pause 79 | 80 | % =================================================================================== 81 | % Suppose, now a manager of the service centre wants to reduce the customer's average 82 | % waiting time to 12 hours. 83 | % =================================================================================== 84 | 85 | % Hit any key to see how this will effect the required number of spares. 86 | pause 87 | 88 | n=round((evalfis([0.35 0.8 0.6],a))*100) 89 | 90 | echo off 91 | disp('End of fuzzy_centre_1.m') 92 | -------------------------------------------------------------------------------- /genetic algorithms_1.m: -------------------------------------------------------------------------------- 1 | function genetic algorithms_1 2 | 3 | disp('=============================================================================') 4 | disp('Problem: It is desired to find the maximum value of the function (15*x - x*x)') 5 | disp(' where parameter "x" varies between 0 and 15. Assume that "x" takes ') 6 | disp(' only integer values. ') 7 | disp('=============================================================================') 8 | 9 | disp('Hit any key to define the objective function.') 10 | pause 11 | 12 | ObjFun='15*x -x.*x'; 13 | 14 | disp(' ') 15 | disp('ObjFun=15*x -x.*x') 16 | disp(' ') 17 | 18 | disp('Hit any key to define the size of a chromosome population, the number of genes') 19 | disp('in a chromosome, the crossover probability, the mutation probability, possible') 20 | disp('minimum and maximum values of parameter "x", and the number of generations.') 21 | pause 22 | 23 | nind=6; % Size of a chromosome population 24 | ngenes=4; % Number of genes in a chromosome 25 | Pc=0.9; % Crossover probability 26 | Pm=0.001; % Mutation probability 27 | xmin=0; % Possible minimum value of parameter "x" 28 | xmax=15; % Possible maximum value of parameter "x" 29 | ngener=20; % Number of generations 30 | 31 | disp(' ') 32 | fprintf(1,'nind=%.0f; Size of a chromosome population\n',nind); 33 | fprintf(1,'ngenes=%.0f; Number of genes in a chromosome\n',ngenes); 34 | fprintf(1,'Pc=%.1f; Crossover probability\n',Pc); 35 | fprintf(1,'Pm=%.3f; Mutation probability\n',Pm); 36 | fprintf(1,'xmin=%.0f; Possible minimum value of parameter "x"\n',xmin); 37 | fprintf(1,'xmax=%.0f; Possible maximum value of parameter "x"\n',xmax); 38 | fprintf(1,'ngener=%.0f; Number of generations\n',ngener); 39 | disp(' ') 40 | 41 | fprintf(1,'Hit any key to generate a population of %.0f chromosomes.\n',nind); 42 | pause 43 | 44 | chrom=round(rand(nind,ngenes)) 45 | 46 | disp('Hit any key to obtain decoded integers from the chromosome strings.') 47 | pause 48 | 49 | x=chrom*[2.^(ngenes-1:-1:0)]' 50 | 51 | % Calculation of the chromosome fitness 52 | ObjV=evalObjFun(ObjFun,x); 53 | best(1)=max(ObjV); 54 | ave(1)=mean(ObjV); 55 | 56 | disp('Hit any key to display initial locations of the chromosomes on the objective function.') 57 | pause 58 | 59 | figure('name','Chromosome locations on the objective function'); 60 | fplot(ObjFun,[xmin,xmax]) 61 | hold; 62 | plot(x,ObjV,'r.','markersize',15) 63 | legend(['The objective function: ',ObjFun],'Initial chromosome population',4); 64 | title('Hit any key to continue'); 65 | xlabel('Parameter "x"'); 66 | ylabel('Chromosome fitness'); 67 | hold; 68 | 69 | disp(' ') 70 | disp('Hit any key to run the genetic algorithm.') 71 | pause 72 | 73 | for i=1:ngener, 74 | 75 | % Fitness evaluation 76 | fitness=ObjV; 77 | if min(ObjV)<0 78 | fitness=fitness-min(ObjV); 79 | end 80 | 81 | % Roulette wheel selection 82 | numsel=round(nind*0.9); % The number of chromosomes to be selected for reproduction 83 | cumfit=repmat(cumsum(fitness),1,numsel); 84 | chance=repmat(rand(1,numsel),nind,1)*cumfit(nind,1); 85 | [selind,j]=find(chance < cumfit & chance >= [zeros(1,numsel);cumfit(1:nind-1,:)]); 86 | newchrom=chrom(selind,:); 87 | 88 | % Crossover 89 | points=round(rand(floor(numsel/2),1).*(ngenes-2))+1; 90 | points=points.*(rand(floor(numsel/2),1)= [zeros(1,numsel);cumfit(1:nind-1,:)]); 117 | newchrom=chrom(selind,:); 118 | 119 | % Crossover 120 | points=round(rand(floor(numsel/2),1).*(ngenes-2))+1; 121 | points=points.*(rand(floor(numsel/2),1)= [zeros(1,numsel);cumfit(1:nind-1,:)]); 124 | newchrom=chrom(selind,:); 125 | 126 | % Crossover 127 | points=round(rand(floor(numsel/2),1).*(ngenes-1))+1; 128 | points=[points round(rand(floor(numsel/2),1).*(ngenes-1))+1]; 129 | points=sort((points*(rand(1)