Matlab- Given matrix X with xi samples, y binary column vector, and a vector w plot all these into 3d graph

孤街醉人 提交于 2020-01-06 05:22:08

问题


I have started to learn Machine Learning, and programming in matlab. I want to plot a matrix sized m*d where d=3 and m are the number of points. with y binary vector I'd like to color each point with blue/red. and plot a plane which is described with the vertical vector to it w.

The problem I trying to solve is to give some kind of visual representation of the data and the linear predictor.

All I know is how to single points with plot3, but no any number of points.

Thanks.


回答1:


Plot the points using scatter3()

scatter3(X(y,1),X(y,2),X(y,3),'filled','fillcolor','red');
hold on;
scatter3(X(~y,1),X(~y,2),X(~y,3),'filled','fillcolor','blue');

or using plot3()

plot(X(y,1),X(y,2),X(y,3),' o','MarkerEdgeColor','red','MarkerFaceColor','red');
hold on;
plot(X(~y,1),X(~y,2),X(~y,3),' o','MarkerEdgeColor','blue','MarkerFaceColor','blue');

There are a few ways to plot a plane. As long as w(3) isn't very close to 0 then the following will work okay. I'm assuming your plane is defined by x'*w+b=0 where b is a scalar and w and x are column vectors.

x1min = min(X(:,1)); x2min = min(X(:,2));
x1max = max(X(:,1)); x2max = max(X(:,2));
[x1,x2] = meshgrid(linspace(x1min,x1max,20), linspace(x2min, x2max, 20));
x3 = -(w(1)*x1 + w(2)*x2 + b)/w(3);
surf(x1,x2,x3,'FaceColor',[0.6,0.6,0.6],'FaceAlpha',0.7,'EdgeColor',[0.4,0.4,0.4],'EdgeAlpha',0.4);
xlabel('x_1'); ylabel('x_2'); zlabel('x_3'); axis('vis3d');

Resulting plot



来源:https://stackoverflow.com/questions/47374920/matlab-given-matrix-x-with-xi-samples-y-binary-column-vector-and-a-vector-w-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!