machine-learning - 如何使用高斯过程进行二元分类?

标签 machine-learning classification scikit-learn gaussian

我知道高斯过程模型最适合回归而不是分类。但是,我仍然想将高斯过程应用于分类任务,但我不确定对模型生成的预测进行分类的最佳方法是什么。我已经查看了 scikit-learn 网站上提供的高斯过程分类示例:

http://scikit-learn.org/stable/auto_examples/gaussian_process/plot_gp_probabilistic_classification_after_regression.html

但是我发现这个例子令人困惑(我在问题末尾列出了我发现这个例子令人困惑的事情)。为了尝试更好地理解,我使用 scikit-learn 创建了一个非常基本的 Python 代码示例,该示例通过将决策边界应用于高斯过程所做的预测来生成分类:

#A minimum example illustrating how to use a
#Gaussian Processes for binary classification
import numpy as np
from sklearn import metrics
from sklearn.metrics import confusion_matrix
from sklearn.gaussian_process import GaussianProcess

if __name__ == "__main__":
    #defines some basic training and test data
    #If the descriptive features have large values
    #(i.e., 8s and 9s) the target is 1
    #If the descriptive features have small values
    #(i.e., 2s and 3s) the target is 0
    TRAININPUTS = np.array([[8, 9, 9, 9, 9],
                            [9, 8, 9, 9, 9],
                            [9, 9, 8, 9, 9],
                            [9, 9, 9, 8, 9],
                            [9, 9, 9, 9, 8],
                            [2, 3, 3, 3, 3],
                            [3, 2, 3, 3, 3],
                            [3, 3, 2, 3, 3],
                            [3, 3, 3, 2, 3],
                            [3, 3, 3, 3, 2]])
    TRAINTARGETS = np.array([1, 1, 1, 1, 1, 0, 0, 0, 0, 0])
    TESTINPUTS = np.array([[8, 8, 9, 9, 9],
                           [9, 9, 8, 8, 9],
                           [3, 3, 3, 3, 3],
                           [3, 2, 3, 2, 3],
                           [3, 2, 2, 3, 2],
                           [2, 2, 2, 2, 2]])
    TESTTARGETS = np.array([1, 1, 0, 0, 0, 0])
    DECISIONBOUNDARY = 0.5

    #Fit a gaussian process model to the data
    gp = GaussianProcess(theta0=10e-1, random_start=100)
    gp.fit(TRAININPUTS, TRAINTARGETS)
    #Generate a set of predictions for the test data
    y_pred = gp.predict(TESTINPUTS)
    print "Predicted Values:"
    print y_pred
    print "----------------"
    #Convert the continuous predictions into the classes
    #by splitting on a decision boundary of 0.5
    predictions = []
    for y in y_pred:
        if y > DECISIONBOUNDARY:
            predictions.append(1)
        else:
            predictions.append(0)
    print "Binned Predictions (decision boundary = 0.5):"
    print predictions
    print "----------------"
    #print out the confusion matrix specifiy 1 as the positive class
    cm = confusion_matrix(TESTTARGETS, predictions, [1, 0])
    print "Confusion Matrix (1 as positive class):"
    print cm
    print "----------------"
    print "Classification Report:"
    print metrics.classification_report(TESTTARGETS, predictions)

当我运行此代码时,我得到以下输出:

Predicted Values:
[ 0.96914832  0.96914832 -0.03172673  0.03085167  0.06066993  0.11677634]
----------------
Binned Predictions (decision boundary = 0.5):
[1, 1, 0, 0, 0, 0]
----------------
Confusion Matrix (1 as positive class):
[[2 0]
 [0 4]]
----------------
Classification Report:
         precision    recall  f1-score   support

          0       1.00      1.00      1.00         4
          1       1.00      1.00      1.00         2

avg / total       1.00      1.00      1.00         6

这个基本示例中使用的方法似乎适用于这个简单的数据集。但这种方法与我上面提到的 scikit-lean 网站上给出的分类示例有很大不同(此处重复 URL):

http://scikit-learn.org/stable/auto_examples/gaussian_process/plot_gp_probabilistic_classification_after_regression.html

所以我想知道我是否在这里遗漏了一些东西。因此,如果有人能够:

  1. 关于 scikit-learn 网站上给出的分类示例:

    1.1 解释一下这个例子中生成的概率是什么概率?它们是查询实例属于该类的概率 >0 吗?

    1.2 为什么示例使用累积密度函数而不是概率密度函数?

    1.3 为什么示例将模型做出的预测除以均方误差的平方根,然后再输入累积密度函数?

  2. 关于我在这里列出的基本代码示例,请澄清将简单的决策边界应用于高斯过程模型生成的预测是否是进行二元分类的适当方法?

很抱歉问了这么长的问题,感谢您的帮助。

最佳答案

在 GP 分类器中,函数上的标准 GP 分布被“压缩”,通常使用标准正态 CDF(也称为 probit function ),将其映射到二元类别上的分布。

此过程的另一种解释是通过分层模型(此 paper 有推导),并带有从高斯过程中提取的隐藏变量。

在 sklearn 的 gp 库中,y_pred, MSE=gp.predict(xx, eval_MSE=True) 的输出看起来是(近似)后验均值 (y_pred) >) 和后验方差 (MSE) 在 发生任何挤压之前中的 xx 点进行评估。

要获得测试集中的点属于正类的概率,您可以通过应用正态 CDF 将 y_pred 上的正态分布转换为二元分布(请再次参阅[本文]了解详情)。

概率压缩函数的分层模型由 0 决策边界定义(标准正态分布围绕 0 对称,即 PHI(0) =.5)。因此您应该设置 DECISIONBOUNDARY=0

关于machine-learning - 如何使用高斯过程进行二元分类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21031273/

相关文章:

python - Sklearn 分类器无法使用 Gensim Word2Vec 数据进行训练

调用 XGBoost .fit 后的 Python sklearn NotFittedError

encryption - 加密数据的机器学习

python - 在 2D 绘图 python 中可视化具有 100 个属性的 SVM 模型

azure - 如何在 Azure ML Studio 中使用预训练的 TF 模型

machine-learning - 没有朴素假设的朴素贝叶斯

python - 如何从管道内的 sklearn TFIDF Vectorizer 返回数据帧?

matlab - Softmax 回归的向量化实现

machine-learning - 如何使用歌词来解释 Word Embeddings/Word2Vec 生成的拼写变化(尤其是俚语)?

machine-learning - 反向传播和前馈神经网络有什么区别?