决策树总是预测类别标签为Yes

我在R语言中尝试在一个小数据集上拟合决策树模型,但无论输入什么数据集,它总是预测类别标签为Yes。

数据

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')

预测函数总是给出结果为yes,无论输入是什么。

问题出在哪里?


回答:

对于如此小的训练集,你需要更新模型控制,并且对结果持保留态度!

model <- rpart(class ~ ., data = data, control = rpart.control(minsplit = 1))predict(model, newdata = data, type = 'class')#   1   2   3   4   5   6   7   8   9  10  11  12  13  14 #  no  no yes yes yes  no yes  no yes yes yes yes yes  no # Levels: no yes

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注