如何在熊猫数据框上使用 sklearn TFIdfVectorizer
Posted
技术标签:
【中文标题】如何在熊猫数据框上使用 sklearn TFIdfVectorizer【英文标题】:How to use sklearn TFIdfVectorizer on pandas dataframe 【发布时间】:2020-02-17 00:18:09 【问题描述】:我正在使用如下所示的制表符分隔文件:
0 abch7619 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 42Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat…..........
1 uewl0928 Duis aute irure d21olor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excep3teur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
0 ahwb3612 Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur
1 llll2019 adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur???? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?
0 jdne2319 At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
1 asbq0918 Et harum quidem rerum facilis est et expedita distinctio................................ Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut
我的目标是生成一个如下所示的数据框:
classification ID word1 word2 word3 word4
foo foo foo foo foo foo
TSV 长文本字段中的 ech 单词作为特征(列)出现,其值为单词 TFIDF。
我可以尝试手动执行此操作,但我希望使用sklearn's TFIDFVECTORIZER
来生成此操作。但是,我需要对字段中的文本进行预处理,以遵循某些准则。
到目前为止,我可以读取.tsv
文件、创建数据框并预处理文本。我遇到的麻烦是将我的文本格式化功能组合起来,然后将其传递给TFIDFVECTORIZER
以下是我所拥有的:
import nltk, string, csv, operator, re, collections, sys, struct, zlib, ast, io, math, time
from nltk.tokenize import word_tokenize, RegexpTokenizer
from nltk.corpus import stopwords
from collections import defaultdict, Counter
from bs4 import BeautifulSoup as soup
from math import sqrt
from itertools import islice
import pandas as pd
# This function removes numbers from an array
def remove_nums(arr):
# Declare a regular expression
pattern = '[0-9]'
# Remove the pattern, which is a number
arr = [re.sub(pattern, '', i) for i in arr]
# Return the array with numbers removed
return arr
# This function cleans the passed in paragraph and parses it
def get_words(para):
# Create a set of stop words
stop_words = set(stopwords.words('english'))
# Split it into lower case
lower = para.lower().split()
# Remove punctuation
no_punctuation = (nopunc.translate(str.maketrans('', '', string.punctuation)) for nopunc in lower)
# Remove integers
no_integers = remove_nums(no_punctuation)
# Remove stop words
dirty_tokens = (data for data in no_integers if data not in stop_words)
# Ensure it is not empty
tokens = [data for data in dirty_tokens if data.strip()]
# Ensure there is more than 1 character to make up the word
tokens = [data for data in tokens if len(data) > 1]
# Return the tokens
return tokens
def main():
tsv_file = "filepath"
print(tsv_file)
csv_table=pd.read_csv(tsv_file, sep='\t')
csv_table.columns = ['rating', 'ID', 'text']
s = pd.Series(csv_table['text'])
new = s.str.cat(sep=' ')
vocab = get_words(new)
print(vocab)
main()
产生:
['decent', 'terribly', 'inconsistent', 'food', 'ive', 'great', 'dishes', 'terrible', 'ones', 'love', 'chaat', 'times', 'great', 'fried', 'greasy', 'mess', 'bad', 'way', 'good', 'way', 'usually', 'matar', 'paneer', 'great', 'oversalted', 'peas', 'plain', 'bad', 'dont', 'know', 'coinflip', 'good', 'food', 'oversalted', 'overcooked', 'bowl', 'either', 'way', 'portions', 'generous', 'looks', 'arent', 'everything', 'little', 'divito', 'looks', 'little', 'scary', 'looking', 'like', 'ive', 'said', 'cant', 'judge', 'book', 'cover', 'necessarily', 'kind', 'place', 'take', 'date', 'unless', 'shes', 'blind', 'hungry', 'man', 'oh', 'man', 'food', 'ever', 'good', 'ordered', 'breakfast', 'lunch', 'dinner', 'fantastico', 'make', 'homemade', 'corn', 'tortillas', 'several', 'salsas', 'breakfast', 'burritos', 'world', 'cost', 'mcdonalds', 'meal', 'family', 'eats', 'frequently', 'frankly', 'tired',
但是,我不确定这是否是允许TFIDFVECTORIZER
正常工作的正确格式。当我尝试使用它时,我使用了以下运行正常的代码:
from sklearn.feature_extraction.text import TfidfVectorizer
tfidf = TfidfVectorizer()
feature_matrix = tfidf.fit_transform(csv_table['text'])
df = pd.DataFrame(data=feature_matrix.todense(), columns=tfidf.get_feature_names())
print(df)
但只是给了我这样的结果:
(0, 4147) 0.09801030349526582
(0, 4482) 0.11236176486916101
(0, 6304) 0.13511683683910816
: :
(1998, 11298) 0.08469000607646575
(1998, 500) 0.10185473904595721
(1998, 3196) 0.07801251063240894
我不知道我在看什么。如何使用 TFIDFVECTORIZER 来实现我的目标,即使用 TFIDF 值创建每个单词的特征矩阵(在应用我的清理逻辑之后)?
【问题讨论】:
我相信你需要将feature_matrix转换为dense 这是什么意思? @DanielMesejo 输出是一个稀疏矩阵,稀疏矩阵通过不表示为零的值来节省内存空间,因此需要将其转换为稠密 我改成密集的,相同的输出@DanielMesejo 在您的示例中,您正在打印特征矩阵,打印 df,todense 不会更改 feature_matrix 它返回一个新矩阵 【参考方案1】:fit_transform 的输出是一个稀疏矩阵,因此您需要将其转换为密集形式,并包含您可以尝试的清理步骤:
s = pd.Series(csv_table['text'])
corpus = s.apply(lambda s: ' '.join(get_words(s)))
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(corpus)
df = pd.DataFrame(data=X.todense(), columns=vectorizer.get_feature_names())
print(df)
基本上,您需要做的是在将其传递给fit_transform
之前,对csv_table['text']
(s
中的元素)中的每个文档应用您的清理程序(get_words
)。 p>
【讨论】:
我读过同样的例子。这如何处理 my 数据,其中我的corpus
是一个数据框字段?此外,我该如何构建我的清洁步骤?
这很有意义。我很欣赏解释,而不仅仅是代码。
很高兴我能帮助@JerryM。以上是关于如何在熊猫数据框上使用 sklearn TFIdfVectorizer的主要内容,如果未能解决你的问题,请参考以下文章