Surface plot using fitrpg

14 views (last 30 days)
Tessa Kol
Tessa Kol on 2 Nov 2020
Commented: Mario Malic on 3 Nov 2020
Dear all,
I tried to make a surface plot of the gaussian process function with the following code:
gprMdl = fitrgp(X,Y1,'KernelFunction','squaredexponential','OptimizeHyperparameters','auto','HyperparameterOptimizationOptions',struct('AcquisitionFunctionName','expected-improvement-plus'));
ypred = predict(gprMdl,X);
[X1,X2] = meshgrid(0.1:0.1:0.9,0.1:0.1:0.9);
surf(X1,X2,reshape(ypred,9,9));
hold on
plot3(X(:,1),X(:,2),Y1,'.r','DisplayName','Observations')
Where the result is presented below.
However, I would expect something more like this. Because the surface plot I have now doesn't fit the data properly and the contour lines are widespread. What am I doing wrong here?
I also tried the following with the help of another post (https://nl.mathworks.com/matlabcentral/answers/407736-plot-3d-hyperplane-from-fitcsvm-results#answer_326570)
But this doens't seem sufficient either.
[x,y]=meshgrid(0.1:0.01:0.9,0.1:0.01:0.9);
xGrid = [x(:),y(:)];
gprMdl = fitrgp(X,Y1,'KernelFunction','squaredexponential','OptimizeHyperparameters','auto','HyperparameterOptimizationOptions',struct('AcquisitionFunctionName','expected-improvement-plus'));
[~,f]=predict(gprMdl2,xGrid);
f=reshape(f(:,1),size(x));
figure
surf(x,y,f)
hold on
plot3(X(:,1),X(:,2),Y1,'.r','DisplayName','Observations')

Accepted Answer

Mario Malic
Mario Malic on 3 Nov 2020
Hello,
Take a look at the code below, unfortunately I can't find the link where I found an example I took the code from, but look for scatteredInterpolant function. I am not sure whether this is applicable, so, please verify.
clc;
clear;
close all;
load data.mat
gprMdl = fitrgp(X,Y1,'KernelFunction','squaredexponential','OptimizeHyperparameters','auto','HyperparameterOptimizationOptions',struct('AcquisitionFunctionName','expected-improvement-plus'));
ypred = predict(gprMdl,X);
% so I don't write indexes below
x_data = X(:,1);
y_data = X(:,2);
% Getting the 2d grid
Num_Points = 50;
X_Lin = linspace(min(x_data),max(x_data),Num_Points);
Y_Lin = linspace(min(y_data),max(y_data),Num_Points);
[X,Y] = meshgrid(X_Lin,Y_Lin);
% Interpolating the surface from ypred
f = scatteredInterpolant(x_data, y_data, ypred);
Z = f(X,Y);
surf(X,Y,Z);
% plotting the observed points
hold on;
plot3(x_data,y_data, Y1,'.r','DisplayName','Observations');
  4 Comments
Tessa Kol
Tessa Kol on 3 Nov 2020
I tried that function, but it gives errors saying: "The sample points arrays must have the same size
as the sample values array."
Mario Malic
Mario Malic on 3 Nov 2020
I am not completely sure whether it's possible to do the griddedInterpolation as you need values for your objective function across the grid. Let's say your grid is 50x50, you would need values for your objective function at each grid point. Maybe you could use the scatteredInterpolation to get grid values and interpolated values for obj. fun., but I don't know if this is a silly thing to do.

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!