首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R:在R中打印3D对象(光圈数据)

R:在R中打印3D对象(光圈数据)
EN

Stack Overflow用户
提问于 2020-12-24 09:22:16
回答 1查看 243关注 0票数 1

我使用的是R编程语言。我在这里尝试遵循这个教程:http://www.semspirit.com/artificial-intelligence/machine-learning/regression/support-vector-regression/support-vector-regression-in-r/

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

代码语言:javascript
复制
  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/):

代码语言:javascript
复制
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")

但是我得到了以下错误:

代码语言:javascript
复制
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.Width和$物种的值

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-24 10:42:56

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

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

代码语言:javascript
复制
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")
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65432879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档