rpart 摘要:图中缺少变量

rpart summary: missing variables in plot

我有一个包含 9 个特征的数据集,从 x1x9。目标变量是 Target (我有一个分类问题)。代码:

# Splitting the dataset into the Training set and Test set
# install.packages('caTools')
library(caTools)
set.seed(123)
split = sample.split(dataset$Target, SplitRatio = 0.75)
training_set = subset(dataset, split == TRUE)
test_set = subset(dataset, split == FALSE)

training_set[-c(2,5)] = scale(training_set[-c(2,5)])
test_set[-c(2,5)] = scale(test_set[-c(2,5)])


# Fitting Decision Tree Classification to the Training set
# install.packages('rpart')
library(rpart)
classifier = rpart(formula = Target ~ .,
                   data = training_set)

# Predicting the Test set results
y_pred = predict(classifier, newdata = test_set[-2], type = 'class')

# Making the Confusion Matrix
cm = table(test_set[, 2], y_pred)

plot(classifier, uniform=TRUE,margin=0.2)
text(classifier)

产生:

无论如何,我看到 7 个变量按重要性排序。第一个问题是:为什么只有7个(他们是9个)?

summary(classifier)


Variable importance
x7 x6 x4 x1 x3 x2 x5 
27 18 17 14 11  9  4

此外(这是第二个问题)情节中缺少x3。为什么?

数据集太大,我想我不能把它放在这里,但我想知道你是否遇到过类似的事情,你是否找到了任何可能的解释。

谢谢!

这是由于 rpart 算法中的树构建过程。有关一些真实案例研究示例的深入解释,请参阅 here。但是,树是通过以下过程构建的:首先,找到“最佳”的单个变量 将数据分成两组。数据被分开,然后这个过程分别应用于每个子组,递归地依此类推,直到子组达到最小大小或直到无法进行改进。所以这意味着可以从最终模型中排除一些变量。

此外,

The cp option of the summary function instructs it to prune the printout, but it does not prune the tree. For each node up to 5 surrogate splits (default) will be printed, but only those whose utility is greater than the baseline “go with the majority” surrogate.

我认为这可以解释缺失的 x3 预测因子