计算一个文档的矢量化为bigrams,然后取平均值

时间:2016-08-18 01:35:06

标签: python nlp scikit-learn

我正在尝试编写一个接收一个文档的函数,count会对该文档的bigrams进行矢量化。这不应该有任何零,因为我一次只对一个文档执行此操作。然后我想取这些数字的平均值来获得重复次数的重复感。

此代码有任何问题吗?

def avg_bigram(x):
    bigram_vectorizer =  CountVectorizer(stop_words='english', ngram_range=(2,2))
    model = bigram_vectorizer.fit_transform(x)
    vector = model.toarray()
    return vector.mean()

我用我知道的文字进行了测试,其中包含的不仅仅是停止词语,而是我回来了

"空词汇;也许这些文件只包含停用词"

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

facet_wrap()期待语料库,而您提供单个文档。只需将您的文档包装在library(ggplot2) library(egg) library(grid) f1 <- ggplot(diamonds[diamonds$cut=="Fair",], aes(carat, price))+ geom_point()+ facet_wrap(~cut, ncol =2)+ scale_x_continuous(limits = c(0,4), breaks=c(0, 1, 2, 3, 4))+ scale_y_continuous(limits = c(0,10000), breaks=c(0, 2500, 5000, 7500, 10000))+ labs(x=expression(" "), y=expression(" ")) f2 <- ggplot(diamonds[diamonds$cut=="Good",], aes(carat, price))+ geom_point()+ facet_wrap(~cut, ncol =2)+ scale_y_continuous(limits = c(0,5000), breaks=c(0, 1000, 2000, 3000, 4000, 5000))+ labs(x=expression(" "), y=expression(" ")) f3 <- ggplot(diamonds[diamonds$cut=="Very Good",], aes(carat, price))+ geom_point()+ facet_wrap(~cut, ncol =2)+ scale_x_continuous(limits = c(0,1), breaks=c(0, 0.2, 0.4, 0.6, 0.8, 1))+ scale_y_continuous(limits = c(0,1000), breaks=c(0, 200, 400, 600, 800, 1000))+ labs(x=expression(" "), y=expression(" ")) f4 <- ggplot(diamonds[diamonds$cut=="Premium",], aes(carat, price))+ geom_point()+ facet_wrap(~cut, ncol =2)+ scale_x_continuous(limits = c(0,1.5), breaks=c(0, 0.2, 0.4, 0.6, 0.8, 1, 1.2, 1.4))+ scale_y_continuous(limits = c(0, 3000), breaks=c(0, 500, 1000, 1500, 2000, 2500, 3000))+ labs(x=expression(" "), y=expression(" ")) fin_fig <- ggarrange(f1, f2, f3, f4, ncol =2) fin_fig 中即可。 E.g:

CountVectorizer
相关问题