2017-02-15 73 views
3

這實際上是一個完全有用的問題的副本,其答案是(部分)由提問者提供的。原標題:「R中用於文本分析的神經網絡模型具有超過512個字符的公式」。他最終解決了這個問題,雖然他給出的推理是錯誤的,他通過刪除問題並使評論和解決方案不可見來加重了這個錯誤。配方到神經網絡的錯誤


我試圖擬合一個神經網絡模型來分類2個桶中的一個網站。培訓數據特徵是網站上所有鏈接中的詞,例如,一個網站可能具有「家」,「約」,「聯繫人」,「產品」等特徵。數據的結構爲帶有類列的數據框,然後是培訓中每個單詞的列。每行都有該類別(合格或不合格)以及該網站上顯示的每個單詞的0和1。

顯示合理次數的單詞總數約爲1000,我希望將它們全部用作特徵。但是,公式似乎有225個字符限制,所以我無法這樣做。

我沒有一個好的數據集來給出可重複的輸出,但這裏是我的代碼和我得到的錯誤。

如果我嘗試做一個公式,它就會被切斷:

f <- as.formula(paste("class ~ ", paste(clean.features, collapse = "+", sep = ""))) 
Error in parse(text = x, keep.source = FALSE) : :2:0: unexpected end of input 1: ranty+recipes+contract+just+inventory+types+working+wine+hampshire+suppliers+rise+body+selection+laurel+trek+arlington+cabinet+citrus+advertisers+rhode+highway+intl+province+jewelers+cycles+wy 

如果我嘗試使用所有的功能:如果我使用as.formula

nn.model <- neuralnet(paste("class ~ ", paste(clean.features, collapse = "+", sep = "")), data = training.data, 
       hidden = num.nodes) 
       ) 
Error in parse(text = x, keep.source = FALSE) : :2:0: unexpected end of input 1: ranty+recipes+contract+just+inventory+types+working+wine+hampshire+suppliers+rise+body+selection+laurel+trek+arlington+cabinet+citrus+advertisers+rhode+highway+intl+province+jewelers+cycles+wy 

同樣的事情發生在數據集中,它表示沒有「數據」參數(即使存在):

nn.model <- neuralnet(class ~ . , data = training.data, 
       hidden = num.nodes, 0)) 
       ) 
Error in terms.formula(formula) : '.' in formula and no 'data' argument 

> sessionInfo() 
R version 3.3.2 (2016-10-31) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows >= 8 x64 (build 9200) 

任何變通辦法的想法?

+0

鏈接到原始:http://stackoverflow.com/questions/42235474/formulas-with-more-than-512-characters-for-neuralnet-model-in-r-for-text-analysi –

回答

1

總結:

  • 問題:reformulate(c(paste(sep="","X",1:5), "for", paste(sep="","X",1:5)), quote(Y))
  • 解決方案: deparse(引號(`for`),反引號= TRUE)

詳細說明:

調試完以下行後,我也同意你的錯誤是與R語言中保留關鍵字的評估有關。

reformulate(c(paste(sep="","X",1:5), "for", paste(sep="","X",1:5)), quote(Y)) 

實際的錯誤時termtext文本進行解析,並用R

Browse[3]> termtext 
# [1] "response ~ X1+X2+X3+X4+X5+for+X1+X2+X3+X4+X5" 

Browse[3]> n 
debug: rval <- eval(parse(text = termtext, keep.source = FALSE)[[1L]]) 

Browse[3]> n 
# Error in parse(text = termtext, keep.source = FALSE) : 
# <text>:1:30: unexpected '+' 
# 1: response ~ X1+X2+X3+X4+X5+for+ 
#        ^

評估,以重現錯誤發生時,我評估單獨與+關鍵詞後綴的for,我得到了程序終止。

eval(parse(text = "for+", keep.source = FALSE)) 
# Error in parse(text = "for+", keep.source = FALSE) : 
# <text>:1:4: unexpected '+' 
# 1: for+ 
#  ^

完整的調試跟蹤:

Browse[3]> n 
debug: if (!is.character(termlabels) || !length(termlabels)) stop("'termlabels' must be a character vector of length at least one") 
Browse[3]> n 
debug: has.resp <- !is.null(response) 
Browse[3]> n 
debug: termtext <- paste(if (has.resp) "response", "~", paste(termlabels, 
                   collapse = "+"), collapse = "") 
Browse[3]> ls() 
# [1] "has.resp" "intercept" "response" "termlabels" 
Browse[3]> has.resp 
# [1] TRUE 
Browse[3]> intercept 
# [1] TRUE 
Browse[3]> response 
# Y 
Browse[3]> termlabels 
# [1] "X1" "X2" "X3" "X4" "X5" "for" "X1" "X2" "X3" "X4" "X5" 

Browse[3]> n 
debug: if (!intercept) termtext <- paste(termtext, "- 1") 
Browse[3]> termtext 
# [1] "response ~ X1+X2+X3+X4+X5+for+X1+X2+X3+X4+X5" 

Browse[3]> n 
debug: rval <- eval(parse(text = termtext, keep.source = FALSE)[[1L]]) 

Browse[3]> n 
# Error in parse(text = termtext, keep.source = FALSE) : 
# <text>:1:30: unexpected '+' 
# 1: response ~ X1+X2+X3+X4+X5+for+ 
#        ^

編輯:

解決方案: 如果這樣計算是不可避免的,那麼人們可能會找出R語言的關鍵詞第一然後圍繞它使用deparse()函數,這將消除t的評估下襬作爲該級別的關鍵詞,相反,它將被評估爲文字。

reformulate(c(paste(sep="","X",1:5), deparse("for"), paste(sep="","X",1:5)), quote(Y)) 
# Y ~ X1 + X2 + X3 + X4 + X5 + "for" + X1 + X2 + X3 + X4 + X5 

但是這會在模型創建過程中產生錯誤invalid model。更好的方法是引用deparse()函數中的關鍵詞,然後創建公式並將數據應用於模型。

reformulate(c(paste(sep="","X",1:5), deparse(quote(`for`), backtick = TRUE), paste(sep="","X",1:5)), quote(Y)) 

# Y ~ X1 + X2 + X3 + X4 + X5 + `for` + X1 + X2 + X3 + X4 + X5 

下面是一個例子:

df1 <- data.frame(`for` = 6:10, y = 1:5, stringsAsFactors = FALSE) 
    colnames(df1) <- c('for', 'y') 
    df1 
    # for y 
    # 1 6 1 
    # 2 7 2 
    # 3 8 3 
    # 4 9 4 
    # 5 10 5 

    my_formula <- reformulate(deparse(quote(`for`), backtick = TRUE), 'y') 
    my_formula 
    # y ~ `for` 

    lm(my_formula, data = df1) 
    # Call: 
    # lm(formula = my_formula, data = df1) 
    # 
    # Coefficients: 
    # (Intercept)  `for` 
    #   -5   1 

browser()設置

要更改瀏覽上的錯誤,類型的全局設置中options(error = browser),然後調試你的代碼,然後改回通過設置options(error = NULL)將其設置爲出錯時爲NULL的出廠默認值。

在我上面的調試過程中,我創建了一個函數myfun並插入了browser()命令,然後找到它。最後,當我調用該函數時,我進入了瀏覽器模式。完成調試過程後,將刪除插入功能代碼的browser()命令。 注意:我沒有更改出錯的出廠默認選項錯誤在瀏覽器上使用options()

myfun <- function() 
{ 
    browser() 
    reformulate(c(paste(sep="","X",1:5), "for", paste(sep="","X",1:5)), quote(Y)) 
} 

source('myfun.R') 
myfun() 

對於內部的瀏覽器所使用的命令的詳細信息(csnQ等),見?browser

+0

有見地的示範。建議您添加所需的代碼以更改允許使用瀏覽器的選項的出廠默認設置。 –

2

我打算建議一個可能的起點來隔離問題。如果公式本身的長度存在問題,那麼只需創建公式本身就可以重現問題。試試這個:

form <- reformulate(clean.features, quote(class)) 

Ew,只是鍵入,這使得我的內部R分析器畏縮。請將您的LHS變量重命名爲除此類中心R函數之外的其他內容。也許這:

names(training.data)[ names(training.data) %in% "class"] <- "myclass" 
form <- reformulate(clean.features, quote(myclass)) 

的提問回答說我不在這裏重複其他意見。我建議他說,他的字符限制爲512個字符的理論是不正確的,但他然後發佈:

因此,有很多手動審查,它看起來像「for」恰好在在其他帖子中提到的字符限制(512)。但實際的問題是「for」被公認爲公式中的一個函數。抱歉,所有的困惑。


這只是不正確的。這個問題與公式中的字符限制無關,而與列的名稱爲「for」無關。這是R中的一個保留控制函數,可能發生在公式中的任何位置。看到這個演示(呈現出一些保留字做,但不是全部)

f <- reformulate(c(paste(sep="","X",1:5), "for", paste(sep="","X",1:5)), quote(Y)) 
Error in parse(text = termtext, keep.source = FALSE) : 
    <text>:1:30: unexpected '+' 
1: response ~ X1+X2+X3+X4+X5+for+ 
           ^
> f <- reformulate(c(paste(sep="","X",1:5), "class", paste(sep="","X",1:5)), quote(Y)) 
# no error ... OK perhaps not a reserved word 
> f <- reformulate(c(paste(sep="","X",1:5), "in", paste(sep="","X",1:5)), quote(Y)) 
Error in parse(text = termtext, keep.source = FALSE) : 
    <text>:1:27: unexpected 'in' 
1: response ~ X1+X2+X3+X4+X5+in 
          ^
> f <- reformulate(c(paste(sep="","X",1:5), "TRUE", paste(sep="","X",1:5)), quote(Y)) 
# no error, so maybe "TRUE" is not reserved and quote(TRUE) is? 

所以提高的期限是否可以共享一個函數的名稱問題是正確的。答案並不完全如我所料。如果有人想提供更謹慎的CS解釋,我會很樂意勾選他們的努力。

出現此問題的其他上下文是調用幫助頁的前綴?運算符。嘗試獲得?for的幫助。你只會得到一行續行+提示。解析器正在等待左括號。