как использовать квантиду на агрегированных данных? - PullRequest
0 голосов
/ 15 февраля 2019

Рассмотрим этот пример

tibble(text = c('a grande latte with soy milk',
                'black coffee no room'),
       repetition = c(100, 2)) 
# A tibble: 2 x 2
  text                         repetition
  <chr>                             <dbl>
1 a grande latte with soy milk        100
2 black coffee no room                  2

Данные означают, что предложение a grande latte with soy milk встречается в моем наборе данных 100 раз.Конечно, хранить эту избыточность - пустая трата памяти, и именно поэтому у меня есть переменная repetition.

Тем не менее, я хотел бы получить dtm от quanteda, чтобы отразить это из-за редкостиДФМ дает мне немного места, чтобы сохранить эту информацию.То есть как я могу иметь 100 строк для первого текста в dfm?Просто использование следующего кода не учитывает repetition

tibble(text = c('a grande latte with soy milk',
                'black coffee no room'),
       repetition = c(100, 2)) %>% 
  corpus() %>% 
  tokens() %>% 
  dfm()
Document-feature matrix of: 2 documents, 10 features (50.0% sparse).
2 x 10 sparse Matrix of class "dfm"
       features
docs    a grande latte with soy milk black coffee no room
  text1 1      1     1    1   1    1     0      0  0    0
  text2 0      0     0    0   0    0     1      1  1    1

Ответы [ 2 ]

0 голосов
/ 16 февраля 2019

Вы можете использовать индексирование, чтобы получить желаемое повторение, сохраняя при этом эффективность, состоящую только из отдельных текстов.

library("tibble")
library("quanteda", warn.conflicts = FALSE)
## Package version: 1.4.1
## Parallel computing: 2 of 12 threads used.
## See https://quanteda.io for tutorials and examples.

tib <- tibble(
  text = c(
    "a grande latte with soy milk",
    "black coffee no room"
  ),
  repetition = c(100, 2)
)
dfmat <- corpus(tib) %>%
  dfm()

Определите функцию для повторения вашей переменной «повторение»:

repindex <- function(x) rep(seq_along(x), times = x)

Затем просто повторите индексацию двух документов dfm:

dfmat2 <- dfmat[repindex(tib$repetition), ]
dfmat2
## Document-feature matrix of: 102 documents, 10 features (40.4% sparse).

head(dfmat2, 2)
## Document-feature matrix of: 2 documents, 10 features (40.0% sparse).
## 2 x 10 sparse Matrix of class "dfm"
##        features
## docs    a grande latte with soy milk black coffee no room
##   text1 1      1     1    1   1    1     0      0  0    0
##   text1 1      1     1    1   1    1     0      0  0    0
tail(dfmat2, 4)
## Document-feature matrix of: 4 documents, 10 features (50.0% sparse).
## 4 x 10 sparse Matrix of class "dfm"
##        features
## docs    a grande latte with soy milk black coffee no room
##   text1 1      1     1    1   1    1     0      0  0    0
##   text1 1      1     1    1   1    1     0      0  0    0
##   text2 0      0     0    0   0    0     1      1  1    1
##   text2 0      0     0    0   0    0     1      1  1    1
0 голосов
/ 15 февраля 2019

Предположим, что ваш data.frame называется df1, вы можете использовать cbind, чтобы добавить столбец в dfm.Но это может не дать вам требуемого результата.Другие два варианта ниже, вероятно, лучше.

cbind

df1 <- tibble(text = c('a grande latte with soy milk',
                'black coffee no room'),
       repetition = c(100, 2))

my_dfm <- df1 %>%  
  corpus() %>% 
  tokens() %>% 
  dfm() %>% 
  cbind(repetition = df1$repetition) # add column to dfm with name repetition

Document-feature matrix of: 2 documents, 11 features (45.5% sparse).
2 x 11 sparse Matrix of class "dfm"
       features
docs    a grande latte with soy milk black coffee no room repetition
  text1 1      1     1    1   1    1     0      0  0    0        100
  text2 0      0     0    0   0    0     1      1  1    1          2

docvars

Вы также можете добавить данные черезфункция docvars, затем данные добавляются в dfm, но немного более скрыты в слотах класса dfm (доступно с помощью @).

docvars(my_dfm, "repetition") <- df1$repetition
docvars(my_dfm)

      repetition
text1        100
text2          2

умножение

Использование умножения:

my_dfm * df1$repetition

Document-feature matrix of: 2 documents, 10 features (50.0% sparse).
2 x 10 sparse Matrix of class "dfm"
       features
docs      a grande latte with soy milk black coffee no room
  text1 100    100   100  100 100  100     0      0  0    0
  text2   0      0     0    0   0    0     2      2  2    2
...