How to apply weights in rpart?

别说谁变了你拦得住时间么 提交于 2019-12-10 10:57:11

问题


I have this data on houses from the Kaggle practice competition and I'm using rpart to train a simple first model to predict the sale price.

The model is not correctly identifying sales where the sale condition was abnormal or a down payment. Therefore, I'd like to increase the importance of this variable which is obviously overlooked in the model.

I'm assuming this is done by using the "weights" parameter but how is this parameter used? How can I pinpoint which variables I want to have a higher weight?


回答1:


From the documentation:

weights

optional case weights.

cost

a vector of non-negative costs, one for each variable in the model. Defaults to one for all variables. These are scalings to be applied when considering splits, so the improvement on splitting on a variable is divided by its cost in deciding which split to choose.

The weights is for rows (e.g. give higher weight to smaller class), the cost is for columns.

Example usage for applying the weights parameter (not necessarily the best way to define the weights):

positiveWeight = 1.0 / (nrow(subset(training, Y == TRUE)) / nrow(training))
negativeWeight = 1.0 / (nrow(subset(training, Y != TRUE)) / nrow(training))

modelWeights <- ifelse(training$Y== TRUE, positiveWeight, negativeWeight)

dtreeModel <- rpart(predFormula, training, weights = modelWeights)


来源:https://stackoverflow.com/questions/43452106/how-to-apply-weights-in-rpart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!