在R中制定DFM之前可以将权重分配给不同的功能吗?

考虑R中的这个例子
str="apple is better than banana"mydfm=dfm(str, ignoredFeatures = stopwords("english"), verbose = FALSE)
DFM mydfm看起来像:

docs apple better banana
text1  1      1     1

但是,我想预先分配权重(苹果:5,香蕉:3),以便DFM mydfm看起来像:
docs apple better banana
text1  5      1     3

最佳答案

我不这么认为,但是之后您可以轻松地做到这一点:

library(quanteda)
str <- "apple is better than banana"
mydfm <- dfm(str, ignoredFeatures = stopwords("english"), verbose = FALSE)
idx <- which(names(weights) %in% colnames(mydfm))
mydfm[, names(weights)[idx]] <-  mydfm[, names(weights)[idx]] %*% diag(weights[idx])
mydfm
# 1 x 3 sparse Matrix of class "dgCMatrix"
#        features
# docs    apple better banana
#   text1     5      1      3

关于r - 为R中的不同特征分配权重,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36815926/

10-12 01:23