绘制两组二维数据点的非线性分离线

时间:2019-12-02 04:46:24

标签: r ggplot2

问题

说我有一个只有两个标签的数据集(n:约300个),其格式如下:

x    y    label
0.3  1.4  1
0.35 0.2  0
     ...

因此,可以用两种颜色绘制(x,y)来表示每个标签。但我希望绘制一条具有非线性形状的分隔线


代码

下面的代码在R中,它精确地说明了我面临的问题。

set.seed(20191201)
n = 300 # sample size
x_lb = y_lb = -pi 
x_ub = y_ub = pi 

# define the range of samples
xs = runif(n,x_lb,x_ub)
ys = runif(n,y_lb,y_ub)
data = data.frame(x=xs,y=ys)

# define the labels, add stochasticity
label = apply(data,1,function(xy) {xy[2] < sin(xy[1])})*1 # deterministic labeling
lev_stoch = 0.05
ind_stoch = sample(n,n*0.05) # indicators for unexpected labels
label[ind_stoch] = !(label[ind_stoch])

# plot
plot(xs,ys,col=label+1) # +1, because 0 shows up with white
lines(seq(x_lb,x_ub,len=1000), sin(seq(x_lb,x_ub,len=1000)), col="blue", lwd=3) # ground-truth reference line

尝试

我确实尝试使用SVM分隔线和discriminant analysis,它也给出了分隔线。

但是,它们都给我们线性线,其目的是简约地将两组分开。但是,我想要的只是一条非线性参考线,它显示了它们的边界(允许一定的随机性)的样子。 nonlinear是指我们在LOESSSPLINE中找到的一行。

我在寻找解决方案时遇到了麻烦。任何帮助将不胜感激。

0 个答案:

没有答案