在单个测试数据点上运行 xgboost 模型 [英] Run xgboost model on single test data point

查看:24
本文介绍了在单个测试数据点上运行 xgboost 模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在单个测试数据点上使用 Xg boost 模型.

a <- data.frame(satisfaction_level=0.14,last_evaluation=0.92,number_project=2,average_montly_hours=350,time_spend_company=5,工伤事故=0,Promotion_last_5years=1,sales=factor("sales",levels=levels(Bdata$sales)),工资=因素(中等",水平=水平(Bdata$salary)))#转换成矩阵格式字符串(一)<- as.data.frame.model.matrix(a)

使用模型进行预测时出现以下错误

xgb.preds = predict(xgb.model, a)

<块引用>

xgb.DMatrix 中的错误(新数据,缺失 = 缺失):xgb.DMatrix:不支持从列表构造

使用以下方法创建模型:

xgb.model <- xgboost(param =param, data = xgb.train.data,nrounds = 1500,eta = 0.05,subsample = 1)

和 Bdata 包含:

head(Bdata)满意度_级别上次_评价数_项目平均_每月_小时时间_花费_公司工作_事故遗留晋升_上_5年销售工资1 0.38 0.53 2 157 3 0 1 0 销量低2 0.80 0.86 5 262 6 0 1 0 销售媒介3 0.11 0.88 7 272 4 0 1 0 销售媒介4 0.72 0.87 5 223 5 0 1 0 销量低5 0.37 0.52 2 159 3 0 1 0 销量低6 0.41 0.50 2 153 3 0 1 0 销量低>

解决方案

你不应该使用 as.data.frame.model.matrix.你的 a 对象仍然是一个 data.frame.您需要使用 a <- as.matrix(a).

有关使用 iris 数据集的可行示例,请参见下文.

库(xgboost)x = as.matrix(iris[, 1:4])y = as.numeric(factor(iris[, 5]))-1模型 <- xgboost(数据 = x,标签 = y,nrounds = 10)新 <- data.frame(Sepal.Length = 5.1,萼片宽度 = 3.5,花瓣.长度 = 1.4,花瓣.宽度 = 0.2)#error 因为它是一个 data.frame预测 <- 预测(模型,新数据 = 新)# xgb.DMatrix(newdata, missing = missing) 中的错误:# xgb.DMatrix: 不支持从列表构造# 这是有效的,因为 data.frame 变成了一个矩阵预测 <- 预测(模型,新数据 = as.matrix(新))

I am trying to role Xg boost model on single test data point.

a <- data.frame(satisfaction_level=0.14,
                     last_evaluation=0.92,
                     number_project=2,
                     average_montly_hours=350,
                      time_spend_company=5,
                   Work_accident=0,
                   promotion_last_5years=1,
                   sales=factor("sales",levels=levels(Bdata$sales)),

                 salary=factor("medium",levels=levels(Bdata$salary)))
#Converting it into matrix format
str(a) 
a <- as.data.frame.model.matrix(a)

I get below error when I predict using the model

xgb.preds = predict(xgb.model, a) 

Error in xgb.DMatrix(newdata, missing = missing) : xgb.DMatrix: does not support to construct from list

Created the model using:

xgb.model <- xgboost(param =param,  data = xgb.train.data,nrounds = 1500 ,eta = 0.05,subsample = 1 )

and Bdata contains:

head(Bdata)
  satisfaction_level last_evaluation number_project average_montly_hours time_spend_company Work_accident left promotion_last_5years sales salary
1               0.38            0.53              2                  157                  3             0    1                     0 sales    low
2               0.80            0.86              5                  262                  6             0    1                     0 sales medium
3               0.11            0.88              7                  272                  4             0    1                     0 sales medium
4               0.72            0.87              5                  223                  5             0    1                     0 sales    low
5               0.37            0.52              2                  159                  3             0    1                     0 sales    low
6               0.41            0.50              2                  153                  3             0    1                     0 sales    low
> 

解决方案

You should not use as.data.frame.model.matrix. Your a object is still a data.frame. You need to use a <- as.matrix(a).

See below for a workable example using the iris dataset.

library(xgboost)

x = as.matrix(iris[, 1:4])
y = as.numeric(factor(iris[, 5]))-1

model <- xgboost(data = x, label = y, nrounds = 10)

new <- data.frame(Sepal.Length = 5.1,
                  Sepal.Width = 3.5,
                  Petal.Length = 1.4,
                  Petal.Width = 0.2)

#error because it is a data.frame
preds <- predict(model, newdata = new)

# Error in xgb.DMatrix(newdata, missing = missing) : 
#   xgb.DMatrix: does not support to construct from  list

# This works because data.frame is turned into a matrix
preds <- predict(model, newdata = as.matrix(new))

这篇关于在单个测试数据点上运行 xgboost 模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆