首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >gbm()分类-错误

gbm()分类-错误
EN

Stack Overflow用户
提问于 2014-06-02 10:00:39
回答 2查看 6.6K关注 0票数 3
代码语言:javascript
复制
cancer <- read.csv('breast-cancer-wisconsin.data', header = FALSE, na.strings="?")
cancer <- cancer[complete.cases(cancer),]
names(cancer)[11] <- "class"
cancer[, 11] <- factor(cancer[, 11], labels = c("benign", "malignant"))
library(gbm)
  • 数据
  • 数据描述

首先,我使用complete.cases删除'NA‘值,并将第11列"class“作为因子。我想使用"class“作为响应变量和其他列(除了第一个列)作为预测变量。

在我第一次尝试时,我输入了:

代码语言:javascript
复制
boost.cancer <- gbm(class ~ .-V1, data = cancer, distribution = "bernoulli") 

Error in gbm.fit(x, y, offset = offset, distribution = distribution, w = w,  : 
Bernoulli requires the response to be in {0,1}

然后,我用班级的对比而不是课堂。

代码语言:javascript
复制
boost.cancer <- gbm(contrasts(class) ~ .-V1, distribution = "bernoulli", data = cancer)

Error in model.frame.default(formula = contrasts(class) ~ . - V1, data = cancer,  : 
variable lengths differ (found for 'V1')

如何纠正这些错误?我肯定我的方法有问题。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-06-02 13:31:00

正如错误所述,您的响应不在0,1中。您可以这样做,而不是创建以下因素:

代码语言:javascript
复制
> cancer$class <- (cancer$class -2)/2

> boost.cancer <- gbm(class ~ .-V1, data = cancer, distribution = "bernoulli")
> boost.cancer
gbm(formula = class ~ . - V1, distribution = "bernoulli", data = cancer)
A gradient boosted model with bernoulli loss function.
100 iterations were performed.
There were 9 predictors of which 4 had non-zero influence.
票数 3
EN

Stack Overflow用户

发布于 2017-04-09 10:40:18

您还可以使用:

boost.cancer <- gbm((unclass(class(Class)-1)-1)~-V1,数据=癌症,distribution = "bernoulli")摘要(boost.cancer)

在“预测”函数和精确确定混淆矩阵的同时,做类似的事情。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23991903

复制
相关文章

相似问题

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