tag*_*aga 8 python nlp machine-learning nltk
我有一个包含 3 列的大型数据集,列是文本、短语和主题。我想找到一种基于主题提取关键短语(短语列)的方法。Key-Phrase 可以是文本值的一部分,也可以是整个文本值。
import pandas as pd
text = ["great game with a lot of amazing goals from both teams",
"goalkeepers from both teams made misteke",
"he won all four grand slam championchips",
"the best player from three-point line",
"Novak Djokovic is the best player of all time",
"amazing slam dunks from the best players",
"he deserved yellow-card for this foul",
"free throw points"]
phrase = ["goals", "goalkeepers", "grand slam championchips", "three-point line", "Novak Djokovic", "slam dunks", "yellow-card", "free throw points"]
topic = ["football", "football", "tennis", "basketball", "tennis", "basketball", "football", "basketball"]
df = pd.DataFrame({"text":text,
"phrase":phrase,
"topic":topic})
print(df.text)
print(df.phrase)
Run Code Online (Sandbox Code Playgroud)
我在寻找执行此类操作的路径时遇到了大麻烦,因为我的数据集中有超过 50000 行,大约有 48000 个独特的短语值和 3 个不同的主题。
我想建立一个包含所有足球、篮球和网球主题的数据集并不是最好的解决方案。所以我正在考虑为此制作某种 ML 模型,但这又意味着我将有 2 个特征(文本和主题)和一个结果(短语),但我的结果中将有超过 48000 个不同的类,这不是一个好方法。
我正在考虑使用文本列作为特征并应用分类模型以找到情绪。之后我可以使用预测的情绪来提取关键特征,但我不知道如何提取它们。
还有一个问题是,当我尝试使用CountVectorizer或TfidfTransformer使用随机森林、决策树或任何其他分类算法对情绪进行分类时,我只能获得66% 的准确率,如果我TextBlob用于情绪分析,准确率也只有 66% 。
有什么帮助吗?
yat*_*atu 10
看起来这里的一个好方法是使用潜在狄利克雷分配模型,这是所谓的主题模型的一个例子。
ALDA是一种无监督模型,可在一组观察中找到相似的组,然后您可以使用这些组为每个组分配一个主题。在这里,我将通过使用text列中的句子训练模型来介绍解决此问题的方法。尽管在这种情况下phrases它们具有足够的代表性并包含模型捕获的必要信息,但它们也可能是训练模型的好(可能更好)候选者,尽管您最好自己判断。
在训练模型之前,您需要应用一些预处理步骤,包括对句子进行标记、删除停用词、词形还原和词干提取。为此,您可以使用nltk:
from nltk.stem import WordNetLemmatizer
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import lda
from sklearn.feature_extraction.text import CountVectorizer
ignore = set(stopwords.words('english'))
stemmer = WordNetLemmatizer()
text = []
for sentence in df.text:
words = word_tokenize(sentence)
stemmed = []
for word in words:
if word not in ignore:
stemmed.append(stemmer.lemmatize(word))
text.append(' '.join(stemmed))
Run Code Online (Sandbox Code Playgroud)
现在我们有了更合适的语料来训练模型:
print(text)
['great game lot amazing goal team',
'goalkeeper team made misteke',
'four grand slam championchips',
'best player three-point line',
'Novak Djokovic best player time',
'amazing slam dunk best player',
'deserved yellow-card foul',
'free throw point']
Run Code Online (Sandbox Code Playgroud)
然后我们可以通过 将文本转换为标记计数矩阵CountVectorizer,这是输入LDA将期望的:
vec = CountVectorizer(analyzer='word', ngram_range=(1,1))
X = vec.fit_transform(text)
Run Code Online (Sandbox Code Playgroud)
请注意,您可以使用该ngram参数来缩小训练模型时要考虑的 n-gram 范围。通过设置ngram_range=(1,2),例如,你想最终包含所有单个单词的功能,以及2-grams在每个句子,这里是被训练的例子CountVectorizer有ngram_range=(1,2):
vec.get_feature_names()
['amazing',
'amazing goal',
'amazing slam',
'best',
'best player',
....
Run Code Online (Sandbox Code Playgroud)
使用的好处n-grams是您还可以找到Key-Phrases除单个单词之外的其他单词。
然后我们可以LDA用你想要的任何数量的主题来训练,在这种情况下,我只会选择3主题(请注意,这与topics列无关),你可以认为这是Key-Phrases- 或者words在这种情况下 -你提到。这里我将使用lda,尽管有几个选项,例如gensim。每个主题都将与训练过的词汇表中的一组单词相关联,每个单词都有一个分数,用于衡量单词在主题中的相关性。
model = lda.LDA(n_topics=3, random_state=1)
model.fit(X)
Run Code Online (Sandbox Code Playgroud)
通过topic_word_我们现在可以获得与每个主题相关的这些分数。我们可以使用argsort对分数向量进行排序,并使用它来索引特征名称向量,我们可以通过以下方式获得vec.get_feature_names:
topic_word = model.topic_word_
vocab = vec.get_feature_names()
n_top_words = 3
for i, topic_dist in enumerate(topic_word):
topic_words = np.array(vocab)[np.argsort(topic_dist)][:-(n_top_words+1):-1]
print('Topic {}: {}'.format(i, ' '.join(topic_words)))
Topic 0: best player point
Topic 1: amazing team slam
Topic 2: yellow novak card
Run Code Online (Sandbox Code Playgroud)
在这种情况下,打印的结果并没有真正代表多少,因为模型已经用问题中的样本进行了训练,但是通过使用整个语料库进行训练,您应该会看到更清晰、更有意义的主题。
另请注意,对于此示例,我使用了整个词汇表来训练模型。但是,在您的情况下,似乎更有意义的是根据topics您已有的不同将文本列分成组,并在每个组上训练一个单独的模型。但希望这能让你对如何进行有一个好主意。
| 归档时间: |
|
| 查看次数: |
5110 次 |
| 最近记录: |