ℕʘʘḆḽḘ
ℕʘʘḆḽḘ

Reputation: 19375

how to extract elements from lists of two elements using purrr?

I am using quanteda's naive bayes model (textmodel_nb) to do some text-classification.

One of the output of the model is a list that contains the probabilities of each class. That is, if nb is my model, I see that

 > str(nb$PcGw)
 num [1:2, 1:462] 0.9446 0.0554 0.9259 0.0741 0.2932 ...
 - attr(*, "dimnames")=List of 2
  ..$ classes : chr [1:2] "FALSE" "TRUE"
  ..$ features: chr [1:462] "hello" "john" "jan" "index" ..

and priting the list gives something like

 nb$PcGw
       features
classes         ny        john
  FALSE 0.94457605 0.92594799
  TRUE  0.05542395 0.07405201

I wanted to use purrr to extract this information and come up with a data_frame like

variable    P_TRUE     P_FALSE
'ny'        0.05542395 0.94457605
'john'      0.07405201 0.92594799

However, I was unable to do so. Can someone give me some help here?

Here is a working example using quanteda's own example:

txt <- c(d1 = "Chinese Beijing Chinese",
         d2 = "Chinese Chinese Shanghai",
         d3 = "Chinese Macao",
         d4 = "Tokyo Japan Chinese",
         d5 = "Chinese Chinese Chinese Tokyo Japan")
trainingset <- dfm(txt, tolower = FALSE)
trainingclass <- factor(c("Y", "Y", "Y", "N", NA), ordered = TRUE)

## replicate IIR p261 prediction for test set (document 5)
nb_test <- textmodel_nb(trainingset, trainingclass)

str(nb_test$PcGw)

 num [1:2, 1:6] 0.659 0.341 0.562 0.438 0.562 ...
 - attr(*, "dimnames")=List of 2
  ..$ classes : chr [1:2] "Y" "N"
  ..$ features: chr [1:6] "Chinese" "Beijing" "Shanghai" "Macao"

Thanks!!

Upvotes: 1

Views: 295

Answers (1)

akrun
akrun

Reputation: 886968

If we need to get the transpose and format the columns, using %>%, transpose the matrix, convert to a data.frame , add a column of rownames (rownames_to_column) and rename if necessary

library(tidyverse)
nb_test$PwGc %>% 
     t %>% 
     as.data.frame %>% 
     rownames_to_column('variable') %>% 
     rename_at(2:3, ~ paste0("P_", c(TRUE, FALSE)))

Based on the communication with OP, if we need to nest some statements inside the %>% wrap it with {}

nb_test$PcGw %>% 
      t %>%
      as.data.frame() %>% 
      {as_tibble(rownames_to_column(., 'variable'))}

Or just use

nb_test$PcGw %>%
    t %>% 
    as.data.frame() %>% 
    rownames_to_column(., 'variable') %>% 
    as_tibble() 

Upvotes: 1

Related Questions