- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想使用 tf-idf 将文本文档转换为特征向量,然后训练朴素贝叶斯算法对其进行分类。
我可以轻松加载没有标签的文本文件,并使用 HashingTF() 将其转换为向量,然后使用 IDF() 根据单词的重要性对单词进行加权。但是,如果我这样做,我就摆脱了标签,而且似乎不可能将标签与矢量重新组合,即使顺序相同也是如此。
另一方面,我可以在每个单独的文档上调用 HashingTF() 并保留标签,但是我无法在其上调用 IDF(),因为它需要整个文档语料库(并且标签会进入方式)。
朴素贝叶斯的 spark 文档只有一个示例,其中的点已经被标记和矢量化,因此没有太大帮助。
我也看过这个指南:http://help.mortardata.com/technologies/spark/train_a_machine_learning_model但在这里他只在没有 idf 的情况下对每个文档应用哈希函数。
所以我的问题是,是否有一种方法不仅可以对朴素贝叶斯分类器使用 idf 的词进行矢量化,还可以对词进行加权?主要问题似乎是 sparks 坚持只接受标记点的 rdds 作为 NaiveBayes 的输入。
def parseLine(line):
label = row[1] # the label is the 2nd element of each row
features = row[3] # the text is the 4th element of each row
features = tokenize(features)
features = hashingTF.transform(features)
return LabeledPoint(label, features)
labeledData = data1.map(parseLine)
最佳答案
标准 PySpark 方法(拆分 -> 转换 -> 压缩)似乎工作得很好:
from pyspark.mllib.feature import HashingTF, IDF
from pyspark.mllib.regression import LabeledPoint
from pyspark.mllib.classification import NaiveBayes
training_raw = sc.parallelize([
{"text": "foo foo foo bar bar protein", "label": 1.0},
{"text": "foo bar dna for bar", "label": 0.0},
{"text": "foo bar foo dna foo", "label": 0.0},
{"text": "bar foo protein foo ", "label": 1.0}])
# Split data into labels and features, transform
# preservesPartitioning is not really required
# since map without partitioner shouldn't trigger repartitiong
labels = training_raw.map(
lambda doc: doc["label"], # Standard Python dict access
preservesPartitioning=True # This is obsolete.
)
tf = HashingTF(numFeatures=100).transform( ## Use much larger number in practice
training_raw.map(lambda doc: doc["text"].split(),
preservesPartitioning=True))
idf = IDF().fit(tf)
tfidf = idf.transform(tf)
# Combine using zip
training = labels.zip(tfidf).map(lambda x: LabeledPoint(x[0], x[1]))
# Train and check
model = NaiveBayes.train(training)
labels_and_preds = labels.zip(model.predict(tfidf)).map(
lambda x: {"actual": x[0], "predicted": float(x[1])})
要获得一些统计数据,您可以使用 MulticlassMetrics
:
from pyspark.mllib.evaluation import MulticlassMetrics
from operator import itemgetter
metrics = MulticlassMetrics(
labels_and_preds.map(itemgetter("actual", "predicted")))
metrics.confusionMatrix().toArray()
## array([[ 2., 0.],
## [ 0., 2.]])
相关
关于python - 如何使用 spark 朴素贝叶斯分类器对 IDF 进行文本分类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32231049/
我正在阅读此 article它说 Note that IDF is dependent on the query term (T) and the database as a whole. In pa
据我了解,IDF 用于计算有多少文档包含该术语(有点像想法)。您可以计算训练集中的 IDF(以及 TF),因为您事先拥有所有文档。但是,如果我事先没有测试集,并且以顺序方式获取测试文档(例如从网络爬虫
我运行具有 TF-IDF 和非 TF-IDF 特征的随机森林算法。 总共特征数量约为 130k(在对 TF-IDF 特征进行特征选择之后),训练集的观察数量约为 120k。 其中大约 500 个是非
在本书《TensorFlow Machine Learning Cookbook》的第七章中,作者在预处理数据时使用了scikit-learn的fit_transform函数来获取tfidf特征用于训
这是一个关于 ngram 线性回归的问题,使用 Tf-IDF(术语频率 - 逆文档频率)。为此,我使用 numpy 稀疏矩阵和 sklearn 进行线性回归。 使用一元语法时,我有 53 个案例和 6
我有一个大小为 208 的列表(208 个句子数组),它看起来像: all_words = [["this is a sentence ... "] , [" another one hello bo
我有一个包含几千行文本的数据集,我的目标是计算 tfidf 分数,然后计算文档之间的余弦相似度,这是我在 Python 中使用 gensim 按照教程所做的: dictionary = corpora
统计十篇新闻TF-IDF 统计TF-IDF词频,每篇文章的 top10 的高频词存储为 json 文件 TF-IDF TF-IDF(term frequency–inverse documen
我的数据库中有一个表,其中包含自由文本字段列。 我想知道每个单词在所有行中出现的频率,或者甚至计算所有单词的 TF-IDF,其中我的文档是该字段每行的值。 是否可以使用 Sql 查询来计算此值?如果没
我正在使用 ES 使用模糊搜索技术搜索大量人名。 TF适用于打分,IDF对我来说真的不需要。这真的是在冲淡分数。我仍然希望将 TF 和 Field Norm 应用于分数。 如何为我的查询禁用/抑制 I
我正在尝试使用Textacy计算整个标准语料库中单个单词的TF-IDF分数,但是对于我收到的结果有点不清楚。 我期待一个单一的浮点数,它代表了语料库中单词的出现频率。那么,为什么我会收到包含7个结果的
我想计算存储在 HBase 中的文档的 TF(词频)和 IDF(逆文档频率)。 我还想把计算出来的TF保存在一个HBase表中,也想把计算出来的IDF保存在另一个HBase表中。 你能指导我完成吗?
我现在在做文本分类。 TF-IDF 有没有比使用词频向量更糟糕的情况?怎么解释呢?谢谢 最佳答案 Both metrics ...discriminate along two dimensions –
我正在对文本文档进行聚类。我正在使用 tf-idf 和余弦相似度。然而,即使我正在使用这些措施,有些事情我还是不太明白。 tf-idf 权重会影响两个文档之间的相似度计算吗? 假设我有这两个文件: 1
我试图更好地了解 lucene 如何对我的搜索进行评分,以便我可以对我的搜索配置或文档内容进行必要的调整。 以下是分数明细的一部分。 产品: 0.34472802 = queryWeight,
我如何计算 tf-idf查询?我了解如何为具有以下定义的一组文档计算 tf-idf: tf = occurances in document/ total words in document idf
我使用 duinotech XC-3800 在 ESP32 芯片上使用 ESP IDF 测试运行裸机代码,并在图像大小方面获得以下结果。 ESP32 的分析二进制大小 文件夹结构 温度/ 主要的/ C
我有一个基于职位描述关键字的简历筛选候选人的用例。由于每次将新的候选人资料添加到内容列表时我都无法负担分数的变化(我认为IDF会发生变化),因此我想省略TF_IDF。 索引文件是 {
在嵌套字段上运行匹配查询时,是基于所有根文档中的所有嵌套文档还是仅基于单个根文档下的嵌套文档来计算每个嵌套文档的相关性得分?基本上,在计算TF / IDF时,用于IDF的集合的范围是什么? 这是一个嵌
我想使用tf-idf计算词频。我起草了一个方程式,您应该在左侧获取tf-idf值。这样对吗? DOCUMENT的TF-IDF: tf-idf(WORD) = occurrences(WORD,DOCU
我是一名优秀的程序员,十分优秀!