% An example of RBF regression using built-in k-means. % We will need the edm from the folder. %% Step 1: Create the data numpts=150; X = rand([numpts,1]); X = sort(X); noise = 0.1*randn(numpts,1); y = sin(3*pi*X)+noise; clear noise % Split into training and test sets. We'll do it manually here: idx=randperm(numpts); N1=round(0.3*numpts); N2=numpts-N1; Xtrain=X(idx(1:N2),:); Xtest=X(idx(N2+1:end),:); ytrain=y(idx(1:N2),:); ytest=y(idx(N2+1:end),:); clear idx N1 N2 %% Step 2: k-means on the training input data to get the centers. k=4; [~,C]=kmeans(X,k); %% Step 3: Build the RBF matrices and solve for weights and biases spr=sqrt(-log(0.5)); % This is Matlab's default D=edm(C,Xtrain); Phi=exp(-spr*(D.^2)); [nc,nr]=size(Phi); % Phi should be numcenters x num data point % Add a row of ones Phi_hat=[Phi;ones(1,nr)]; W_hat=ytrain'*pinv(Phi_hat); %% Step 4: Construct the RBF output on the testing data: spr=sqrt(-log(0.5)); % This is Matlab's default D=edm(C,Xtest); Phi=exp(-spr*(D.^2)); [nc,nr]=size(Phi); % Phi should be numcenters x num data point % Add a row of ones Phi_hat=[Phi;ones(1,nr)]; y_out=W_hat*Phi_hat; plot(X,y,'b.-',Xtest,y_out','ro',C,zeros(size(C)),'k*'); legend('Orig Data','RBF Output on Test set','Center Location')