【发布时间】:2021-07-03 23:34:34
【问题描述】:
我正在尝试在 R 中的一个小数据集上拟合决策树模型,但它总是将类标签预测为“是”,而不管输入的数据集是什么。
数据
outlook <- c("sunny", "sunny", "overcast", "rain", "rain", "rain", "overcast", "sunny", "sunny", "rain", "sunny", "overcast", "overcast", "rain")
temperature <- c("hot", "hot", "hot", "mild", "cool", "cool", "cool", "mild", "cool", "mild", "mild", "mild", "hot", "mild")
humidity <- c("high", "high", "high", "high", "normal", "normal", "normal", "high", "normal", "normal", "normal", "high", "normal", "high")
wind <- c("weak", "strong", "weak", "weak", "weak", "strong", "strong", "weak", "weak", "weak", "strong", "strong", "weak", "strong")
class <- c("no", "no", "yes", "yes", "yes", "no", "yes", "no", "yes", "yes", "yes", "yes", "yes", "no")
data <- data.frame(outlook, temperature, humidity, wind, class)
data
编码数据
outlook_new <- as.numeric(as.factor(outlook))
temperature_new <- as.numeric(as.factor(temperature))
humidity_new <- as.numeric(as.factor(humidity))
wind_new <- as.numeric(as.factor(wind))
class_new <- as.numeric(as.factor(class))
data_new <- data.frame(outlook_new, temperature_new, humidity_new, wind_new, class_new)
data_new
构建模型
model <- rpart(class_new ~ ., data=data_new)
创建测试数据点
test_data <- data.frame(outlook_new = 2, temperature_new = 2, humidity_new = 1, wind_new = 1)
test_data
预测
predict(model, test_data, type='response')
无论输入如何,预测函数总是给出结果。
有什么问题?
【问题讨论】:
-
您的模型没有发现任何分裂。可能重复:stackoverflow.com/questions/40313198/…
标签: r machine-learning decision-tree