如何使用朴素贝叶斯和主成分分析对文档进行分类(C#,Accord.NET)

How to classify documents using Naive Bayes and Principal Component Analysis (C#, Accord.NET)

我正在从事一个电子邮件分类项目,该项目将电子邮件分类为特定类别。到目前为止,我们将有趣的数据(例如:主题和正文)连同其他信息一起保存到我们的数据库中。我们已成功将词频 - 逆文档频率应用到项目中,以检索在我们电子邮件的主题和正文中找到的所有 terms/features 的矩阵。该矩阵的一个非常小的样本输出将是:

      dog    cat    fish
doc1  0,024  0,011  0,008
doc2  0,011  0,014  0,007
doc3  0,005  0,024  0,003
doc4  0,008  0,028  0,008
doc5  0,002  0,03   0,006

实际上这个矩阵要大得多,因为对于一组 165 封电子邮件,我们有大约 23000 个术语。因为我们需要使用这个矩阵中的术语对电子邮件进行分类,所以 23000 个特征实在是太多了。这就是我们实施降维算法 (PCA) 的原因。这是通过使用此代码(Accord 框架)完成的:

// Creates the Principal Component Analysis of the given source
pca = new PrincipalComponentAnalysis(matrix, AnalysisMethod.Center);

// Compute the Principal Component Analysis
pca.Compute();         

// Creates a projection of the information
double[,] components = pca.Transform(matrix, 20);

// Creates form to show components
frmRPCA frmPCA = new frmRPCA(components);
frmPCA.ShowDialog();

现在我们已经对维度的数量进行了硬编码,但这暂时不应该成为问题。

我一直在看关于如何使用朴素贝叶斯进行分类的 Accord 框架示例,但我真的不知道如何将其付诸实践。主要是因为这个例子在我们处理数字时使用了文本,我不太了解分类是如何工作的。 See the example on how to implement Naive Bayes.

基本上,我的原始矩阵包含我的特征及其 TF-IDF 值(参见上面的示例),我想使用包含我的 PCA 的矩阵(pca.Transform 方法的输出)对它们进行分类。目前,我只有 2 类 个我想对我的电子邮件进行分类(注册和提交)。我将如何实现这一目标?另外,如果我想在将来添加多个 类,我将如何扩展它?

示例输出应该是这样的:

doc1 Registration
doc2 Registration
doc3 Registration
doc4 Submission
doc5 Submission

如果您有兴趣进行 class化,那么 LDA(及其变体)可能更适合您的情况。事实上,PCA 试图通过仅查看您的数据来最小化方差。但是,如果您有关于数据的额外信息,例如 class 标签,则有更好的方法可以满足您的需求。

  • 如果您有 class 标签形式的额外信息(即, 数据集中的每个样本都有一个关联的整数值,表示它属于哪个 class),那么您可以使用 LDA(线性判别分析)来减少 维度对class化很有用。

  • 如果您有 真实输出形式的额外信息(即,每个 数据集中的样本实际上有一个与关联的双精度值 它),那么你可以使用 PLS(偏最小二乘)以一种对以下有用的方式降低维度 回归。

假设您有一个class化问题,下面是一个关于如何使用 LDA 减少特征数据​​数量的例子:

// Create some sample input data instances. This is the same
// data used in the Gutierrez-Osuna's example available at:
// http://research.cs.tamu.edu/prism/lectures/pr/pr_l10.pdf

double[][] inputs = 
{
    // Class 0
    new double[] {  4,  1 }, 
    new double[] {  2,  4 },
    new double[] {  2,  3 },
    new double[] {  3,  6 },
    new double[] {  4,  4 },

    // Class 1
    new double[] {  9, 10 },
    new double[] {  6,  8 },
    new double[] {  9,  5 },
    new double[] {  8,  7 },
    new double[] { 10,  8 }
};

int[] output = 
{
    0, 0, 0, 0, 0, // The first five are from class 0
    1, 1, 1, 1, 1  // The last five are from class 1
};

// Then, we will create a LDA for the given instances.
var lda = new LinearDiscriminantAnalysis(inputs, output);

lda.Compute(); // Compute the analysis


// Now we can project the data into LDA space:
double[][] projection = lda.Transform(inputs);

如果您想将问题从 2 维减少到 1 维,您可以使用:

double[][] reduced_data = lda.Transform(inputs, 1);

结果将是一个 10x1 矩阵。它将包含对执行 classification 仍然有用的数据的低维表示。因此,您可以使用 reduced_data 而不是使用原始数据来学习 class 运算符。

此外,LDA 对象带有一个简单的最小距离 class 器,您可以使用它来 class 化您的实例。例如,您可以 class 使用

验证您的数据集
int[] results = lda.Classify(inputs);

但是,没有什么能阻止您使用您可能喜欢的任何其他 classifier(例如朴素贝叶斯)。例如,为了使用朴素贝叶斯,您可以使用

// Create a new normal distribution Naive Bayes classifier for 
// a classification problem with 1 feature and the two classes
var nb = new NaiveBayes.Normal(classes: 2, inputs: 1);

// Compute the Naive Bayes model
nb.Estimate(reduced_data, output);

// Now, if we would like to classify the first instance 
// in our dataset, we would use
int result = nb.Compute(lda.Transform(input[0]));

还有框架附带的示例应用程序应该 demonstrate how LDA works and how naive bayes works