本文介绍了为 R 中的不同特征分配权重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在 R 中制定 DFM 之前为不同的特征分配权重?
Is it possible to assign weights to different features before formulating a DFM in R?
在 R 中考虑这个例子
Consider this example in R
str="苹果比香蕉好"mydfm=dfm(str, ignoreFeatures = stopwords("english"), verbose = FALSE)
DFM mydfm 看起来像:
DFM mydfm looks like:
docs apple better banana
text1 1 1 1
但是,我想事先分配权重(苹果:5,香蕉:3),以便 DFM mydfm 看起来像:
But, I want to assign weights(apple:5, banana:3) beforehand, so that DFM mydfm looks like:
docs apple better banana
text1 5 1 3
推荐答案
我不这么认为,但是你可以很容易地做到:
I don't think so, however you can easily do it afterwards:
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 中的不同特征分配权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!