使用文档矢量建立词汇

问题描述 投票:0回答:1

我不能够建立词汇和得到一个错误:

类型错误:“诠释”对象不是可迭代

这里是我的代码是基于媒体的文章:

https://towardsdatascience.com/implementing-multi-class-text-classification-with-doc2vec-df7c3812824d

我试图提供熊猫系列,列出来build_vocab功能。

import pandas as pd

from gensim.test.utils import common_texts
from gensim.models.doc2vec import Doc2Vec, TaggedDocument
from sklearn.model_selection import train_test_split
import multiprocessing
import nltk
from nltk.corpus import stopwords

def tokenize_text(text):
    tokens = []
    for sent in nltk.sent_tokenize(text):
        for word in nltk.word_tokenize(sent):
            if len(word) < 2:
                continue
            tokens.append(word.lower())
    return tokens

df = pd.read_csv("https://raw.githubusercontent.com/RaRe-Technologies/movie-plots-by-genre/master/data/tagged_plots_movielens.csv")

tags_index = {
    "sci-fi": 1,
    "action": 2,
    "comedy": 3,
    "fantasy": 4,
    "animation": 5,
    "romance": 6,
}

df["tindex"] = df.tag.replace(tags_index)
df = df[["plot", "tindex"]]

mylist = list()
for i, q in df.iterrows():
    mylist.append(
        TaggedDocument(tokenize_text(str(q["plot"])), tags=q["tindex"])
    )

df["tdoc"] = mylist

X = df[["tdoc"]]
y = df["tindex"]

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

cores = multiprocessing.cpu_count()
model_doc2vec = Doc2Vec(
    dm=1,
    vector_size=300,
    negative=5,
    hs=0,
    min_count=2,
    sample=0,
    workers=cores,
)
model_doc2vec.build_vocab([x for x in X_train["tdoc"]])

该文档是这种方法非常混乱。

doc2vec
1个回答
1
投票

Doc2Vec需要其语料库TaggedDocument状物体的可迭代序列(如被馈送到build_vocab()train())。

如果出现了错误,你也应该显示完整的堆栈伴随着它,所以它是清楚什么行的代码,和周围的呼叫帧,都有涉及。

但是,目前还不清楚,如果你已经输入到数据帧,然后通过出数据帧支架的访问,然后通过train_test_split(),实际上是。

所以,我建议分配的事情描述临时变量,并验证它们包含正确的各种各样的事情,在每一个步骤。

X_train["tdoc"][0]适当TaggedDocument,具有words属性,它是一个列表的弦,并tags属性列表的标签? (而且,其中每个标签可能是一个字符串,但或许是一个纯INT,从0递增计数)

mylist[0]适当TaggedDocument

另外:Doc2Vec使用的许多网上的例子有令人震惊的错误,你的链接中的文章也不例外。它在一个循环中多次调用train()的做法通常是不必要的,而且很容易出错,而事实上,在严重的学习率alpha管理不善那篇文章的结果。 (例如,来自于一个负有效0.002alpha 30次结果的起始默认0.025,这是从来没有对齐,意味着模型扣除alpha正在本身恶化与每一个例子,这可能是导致可怕的报告分类器的一个因素准确性。)

我会完全无视文章和其他地方寻求更好的例子。

© www.soinside.com 2019 - 2024. All rights reserved.