【问题标题】:How does NLTK's classifier transform the large amount of features to train the model?NLTK 的分类器是如何转换大量特征来训练模型的?
【发布时间】:2018-02-05 05:15:08
【问题描述】:

在文档示例(第 6 章,第 1.3 节)中,他们选择了训练集中的前 2000 个单词作为特征,数据集有 2000 行长。如果它用所有 2000 个单词作为列和 2000 行进行训练,由于维度的诅咒,它将无法找到任何模式。但它很有效,而且效果很好。所以我检查了源代码并找到了下面的代码,这对我来说没有任何意义。

在我看来,他们似乎将单词的频率映射到每个标签的概率。但这应该是朴素贝叶斯算法。这是如何训练的?

 def train(cls, labeled_featuresets, estimator=ELEProbDist):
    """
    :param labeled_featuresets: A list of classified featuresets,
        i.e., a list of tuples ``(featureset, label)``.
    """
    label_freqdist = FreqDist()
    feature_freqdist = defaultdict(FreqDist)
    feature_values = defaultdict(set)
    fnames = set()

    # Count up how many times each feature value occurred, given
    # the label and featurename.
    for featureset, label in labeled_featuresets:
        label_freqdist[label] += 1
        for fname, fval in featureset.items():
            # Increment freq(fval|label, fname)
            feature_freqdist[label, fname][fval] += 1
            # Record that fname can take the value fval.
            feature_values[fname].add(fval)
            # Keep a list of all feature names.
            fnames.add(fname)

    # If a feature didn't have a value given for an instance, then
    # we assume that it gets the implicit value 'None.'  This loop
    # counts up the number of 'missing' feature values for each
    # (label,fname) pair, and increments the count of the fval
    # 'None' by that amount.
    for label in label_freqdist:
        num_samples = label_freqdist[label]
        for fname in fnames:
            count = feature_freqdist[label, fname].N()
            # Only add a None key when necessary, i.e. if there are
            # any samples with feature 'fname' missing.
            if num_samples - count > 0:
                feature_freqdist[label, fname][None] += num_samples - count
                feature_values[fname].add(None)

    # Create the P(label) distribution
    label_probdist = estimator(label_freqdist)

    # Create the P(fval|label, fname) distribution
    feature_probdist = {}
    for ((label, fname), freqdist) in feature_freqdist.items():
        probdist = estimator(freqdist, bins=len(feature_values[fname]))
        feature_probdist[label, fname] = probdist

    return cls(label_probdist, feature_probdist)

【问题讨论】:

    标签: python classification nltk dimensionality-reduction


    【解决方案1】:

    朴素贝叶斯算法不做任何特征选择。我不知道当您写下“由于维度的诅咒而无法找到任何模式”时您在想象什么“模式”,但它确实使用了其模型中提供的所有功能。

    标签概率是通过组合每个单独特征的概率估计来估计的,就好像这些特征在统计上是相互独立的(这是模型的“幼稚”部分)。这使得构建朴素贝叶斯概率模型变得非常快速和容易。具有更强预测能力(与结果标签之一更密切相关)的特征对计算概率的影响更大。任何标签都可能以大致相等的概率出现的特征对估计的影响可以忽略不计。

    也许你的意思是,如果一个词只出现在一个训练文档中,它会虚假地与该文档的标签相关联?这是真的,但如果该词出现在被分类的文档中,它只会导致问题。大多数时候,更广泛的词将决定概率估计。

    【讨论】:

      猜你喜欢
      • 2011-06-21
      • 2017-03-19
      • 2017-08-13
      • 2018-10-25
      • 2020-09-22
      • 2016-05-11
      • 1970-01-01
      • 1970-01-01
      • 2018-10-31
      相关资源
      最近更新 更多