我知道高斯过程模型最适合用于回归而不是分类。然而,我仍然希望将高斯过程应用于分类任务,但我不知道将模型生成的预测进行分箱的最佳方法是什么。我已经查看了scikit-learn网站上提供的高斯过程分类示例,网址是:
但我发现这个示例令人困惑(我在问题末尾列出了我对这个示例感到困惑的地方)。为了更好地理解,我使用scikit-learn创建了一个非常基本的Python代码示例,通过对高斯过程的预测应用决策边界来生成分类:
#A minimum example illustrating how to use a#Gaussian Processes for binary classificationimport numpy as npfrom sklearn import metricsfrom sklearn.metrics import confusion_matrixfrom sklearn.gaussian_process import GaussianProcessif __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 2avg / total 1.00 1.00 1.00 6
这个基本示例中使用的做法似乎在这个简单的数据集上运行良好。但这种方法与我在上面提到的scikit-learn网站上给出的分类示例非常不同(网址再次重复):
所以我想知道我是否遗漏了什么。因此,我希望有人能:
-
关于scikit-learn网站上给出的分类示例:
1.1 解释这个示例中生成的概率是指什么?它们是查询实例属于大于0类别的概率吗?
1.2 为什么这个示例使用累积密度函数而不是概率密度函数?
1.3 为什么这个示例在将模型的预测输入累积密度函数之前,先将它们除以均方误差的平方根?
-
关于我在这里列出的基本代码示例,澄清是否将一个简单的决策边界应用于高斯过程模型生成的预测是进行二元分类的适当方法?
抱歉问题这么长,感谢任何帮助。
回答:
在GP分类器中,对函数的高斯分布通常会进行“压缩”,通常使用标准正态分布的累积分布函数(也称为probit函数),将其映射到二元类别的分布上。
对这个过程的另一种解释是通过层次模型(这篇论文有推导),其中有一个从高斯过程抽取的隐藏变量。
在sklearn的gp库中,看起来y_pred, MSE=gp.predict(xx, eval_MSE=True)
的输出是xx
中点的(近似)后验均值(y_pred
)和后验方差(MSE
),这些是在任何压缩发生之前计算的。
要获得测试集中某个点属于正类别的概率,可以通过应用正态分布的累积分布函数将y_pred
上的正态分布转换为二元分布(详见[这篇论文])。
probit压缩函数的层次模型由0
决策边界定义(标准正态分布在0
周围是对称的,意味着PHI(0)=.5
)。所以你应该设置DECISIONBOUNDARY=0
。