簡體   English   中英

randomForest分類預測限制

[英]randomForest Categorical Predictor Limits

我理解並贊賞R的randomForest函數只能處理少於54個類別的類別預測變量。 但是,當我將類別預測變量減少到少於54個類別時,我仍然會收到錯誤。 我所看到的關於stackoverflow的分類預測變量限制的唯一問題是如何繞開該類別限制,但是我正嘗試減少類別數以遵循函數的限制,但仍然會出現錯誤。

下面的腳本創建一個數據框,以便我們可以預測“專業”。 可以理解,由於變量“ college_id”,在“ df”上嘗試運行randomForest()時,出現“無法處理超過53個類別的類別預測變量”錯誤。

但是,當我將數據集修整為僅包括前40個大學ID時,會遇到相同的錯誤。 我現在是否缺少一些保留所有類別的基本數據框概念,即使現在在“ df2”數據框中僅填充了40個類別? 我可以使用什么解決方法?

library(dplyr)
library(randomForest)

# create data frame
df <- data.frame(profession = sample(c("accountant", "lawyer", "dentist"), 10000, replace = TRUE),
             zip = sample(c("32801", "32807", "32827", "32828"), 10000, replace = TRUE),
             salary = sample(c(50000:150000), 10000, replace = TRUE),
             college_id = as.factor(c(sample(c(1001:1040), 9200, replace = TRUE),
                                      sample(c(1050:9999), 800, replace = TRUE))))


# results in error, as expected
rfm <- randomForest(profession ~ ., data = df)


# arrange college_ids by count and retain the top 40 in the 'df' data frame
sdf <- df %>% 
  dplyr::group_by(college_id) %>% 
  dplyr::summarise(n = n()) %>% 
  dplyr::arrange(desc(n))
sdf <- sdf[1:40, ]
df2 <- dplyr::inner_join(df, sdf, by = "college_id")
df2$n <- NULL


# confirm that df2 only contains 40 categories of 'college_id'
nrow(df2[which(!duplicated(df2$college_id)), ])


# THIS IS WHAT I WANT TO RUN, BUT STILL RESULTS IN ERROR
rfm2 <- randomForest(profession ~ ., data = df2)

我認為您在變量中仍然具有所有因子水平。 在再次適合林之前,請嘗試添加以下行:

df2$college_id <- factor(df2$college_id)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM