【发布时间】:2020-11-12 16:23:58
【问题描述】:
我有一系列要矢量化的标记。但是,我不断收到错误消息“TypeError:预期的字符串或类似字节的对象”。
我的文本标记:
tokens_raw
0 [kitchen, getting, children, ready, school, ru...
1 [shanghai, appointed, manager, taco, bell, chi...
2 [april, uber, announced, acquisition, otto, sa... etc.....
我的代码:
# list of tokens from text documents
tokens_raw = articles_df_60k['processed_content']
# create the transform
vectorizer = TfidfVectorizer(sublinear_tf=True, min_df=5, lowercase=False,
encoding='latin-1', ngram_range=(1, 2))
# tokenize and build vocab
vectorizer.fit(tokens_raw)
# summarize
print("vocabulary count:", vectorizer.vocabulary_, sep='\n')
print('\n')
print("inverse document frequency:", vectorizer.idf_, sep='\n')
print('\n')
# encode document
vector = vectorizer.transform(tokens_raw)
# summarize encoded vector
print("vector shape:", vector_raw.shape, sep='\n')
print('\n')
print("vector array:", vector_raw.toarray(), sep='\n')
同样,错误消息是“TypeError:预期的字符串或类似字节的对象”。如果我只输入它就可以了
tokens_raw[0]
但是,尝试将其应用于所有行会返回错误消息。任何指导、解释或解决方案将不胜感激。
【问题讨论】:
标签: python python-3.x machine-learning nlp vectorization