【问题标题】:R: Plotting 3D Objects in R (iris data)R:在 R 中绘制 3D 对象(虹膜数据)
【发布时间】:2021-04-02 14:01:31
【问题描述】:

我正在使用 R 编程语言。我正在尝试在这里学习本教程:http://www.semspirit.com/artificial-intelligence/machine-learning/regression/support-vector-regression/support-vector-regression-in-r/

对于著名的鸢尾花数据集,我正在尝试绘制随机森林算法的 3D 决策面(使用 tsne 维度):

  library(Rtsne)
    library(dplyr)
    library(ggplot2)
    library(plotly)
    library(caret)
    library(randomForest)
    
  #data
a = iris
a <- unique(a)

#create two species just to make things easier
s <- c("a","b")
species<- sample(s , 149, replace=TRUE, prob=c(0.3, 0.7))
a$species = species
a$species = as.factor(a$species)

#split data into train/test, and then random forest 

index = createDataPartition(a$species, p=0.7, list = FALSE)
train = a[index,]
test = a[-index,]

rf = randomForest(species ~ ., data=train, ntree=50, mtry=2)

#have the model predict the test set
pred = predict(rf, test, type = "prob")
labels = as.factor(ifelse(pred[,2]>0.5, "a", "b"))
confusionMatrix(labels, test$species)


#tsne algorithm
tsne_obj_3 <- Rtsne(test[,-5], perplexity=1, dims=3)
df_m2 <- as.data.frame(tsne_obj_3$Y)

df_m2$labels = test$species

从这里开始,我正在尝试绘制 3d 决策面 (http://www.semspirit.com/artificial-intelligence/machine-learning/regression/support-vector-regression/support-vector-regression-in-r/):

axis_1 = df_m2$V1
axis_2 = df_m2$V2
axis_3 = df_m2$V3

plot_ly(x=as.vector(axis_1),y=as.vector(axis_2),z=axis_3, type="scatter3d", mode="markers", name = "Obs", marker = list(size = 3)) %>%
add_trace(x=as.vector(axis_1),y=as.vector(axis_2),z=df_m2$labels, type = "mesh3d", name = "Preds")

但我收到以下错误:

2: In RColorBrewer::brewer.pal(N, "Set2") :
  minimal value for n is 3, returning requested palette with 3 different levels

3: 'mesh3d' objects don't have these attributes: 'mode', 'marker'
Valid attributes include:
'type', 'visible', 'legendgroup', 'name', 'uid', 'ids', 'customdata', 'meta', 'hoverlabel', 'stream', 'uirevision', 'x', 'y', 'z', 'i', 'j', 'k', 'text', 'hovertext', 'hovertemplate', 'delaunayaxis', 'alphahull', 'intensity', 'intensitymode', 'color', 'vertexcolor', 'facecolor', 'cauto', 'cmin', 'cmax', 'cmid', 'colorscale', 'autocolorscale', 'reversescale', 'showscale', 'colorbar', 'coloraxis', 'opacity', 'flatshading', 'contour', 'lightposition', 'lighting', 'hoverinfo', 'showlegend', 'xcalendar', 'ycalendar', 'zcalendar', 'scene', 'idssrc', 'customdatasrc', 'metasrc', 'xsrc', 'ysrc', 'zsrc', 'isrc', 'jsrc', 'ksrc', 'textsrc', 'hovertextsrc', 'hovertemplatesrc', 'intensitysrc', 'vertexcolorsrc', 'facecolorsrc', 'hoverinfosrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
 

生成了 3D 绘图,但 3D 平面完全消失了。

谁能告诉我我做错了什么?

我试图让当您将鼠标移到每个点上时,对于该点,它将显示 a$Sepal.Length、a$Sepal.Width、a$Petal.Length、a$Petal 的值。宽度,一个$物种

谢谢

【问题讨论】:

    标签: r plotly data-visualization data-manipulation


    【解决方案1】:

    当您调用add_trace() 时,z 分配不正确。标签不会绘制;您需要绘制您确定的概率,z=df_m2$pred

    有多种方法可以解决网格图问题,但最简单的方法是使用add_mesh 而不是add_trace

    plot_ly(x=as.vector(axis_1),
            y=as.vector(axis_2), 
            z=axis_3, 
            type="scatter3d",
            mode="markers", 
            name = "Obs", 
            marker = list(size = 3)) %>% 
       add_mesh(x=as.vector(axis_1), 
                y=as.vector(axis_2), 
                z=df_m2$pred, 
                type = "mesh3d", 
                name = "Preds")
    

    【讨论】:

    • 非常感谢!我花了几个小时试图弄清楚这一点!这是完美的!只有两个问题:当您将鼠标移到每个点上时,如何制作标签(花瓣宽度、花瓣长度等)?是否可以将标签“a”标记为蓝色,将“b”标记为红色?非常感谢!
    • 有时间可以看看这个吗? stackoverflow.com/questions/65434105/… 谢谢
    猜你喜欢
    • 2021-09-26
    • 2018-04-21
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    相关资源
    最近更新 更多