gpt4 book ai didi

python - 可以在 scikit-learn 中使用预先计算的内核从 SVM 绘制 ROC 图吗?

转载 作者:太空宇宙 更新时间:2023-11-03 11:54:17 25 4
gpt4 key购买 nike

我正在使用此示例根据 SVM 分类结果创建 ROC 图:http://scikit-learn.org/0.13/auto_examples/plot_roc.html

但是,每个数据点实际上由 4 个长度为 d 的特征向量组成,并使用不符合特定 K(X, X) 范例的自定义内核函数进行组合。因此,我必须向 scikit-learn 提供一个预先计算的内核才能进行分类。它看起来像这样:

K = numpy.zeros(shape = (n, n))

# w1 + w2 + w3 + w4 = 1.0

# v1: array, shape (n, d)
# w1: float in [0, 1)
chi = sklearn.metrics.pairwise.chi2_kernel(v1, v1)
mu = 1.0 / numpy.mean(chi)
K += w1 * numpy.exp(-mu * chi)

# v2: array, shape (n, d)
# w2: float in [0, 1)
chi = sklearn.metrics.pairwise.chi2_kernel(v2, v2)
mu = 1.0 / numpy.mean(chi)
K += w2 * numpy.exp(-mu * chi)

# v3: array, shape (n, d)
# w3: float in [0, 1)
chi = sklearn.metrics.pairwise.chi2_kernel(v3, v3)
mu = 1.0 / numpy.mean(chi)
K += w3 * numpy.exp(-mu * chi)

# v4: array, shape (n, d)
# w4: float in [0, 1)
chi = sklearn.metrics.pairwise.chi2_kernel(v4, v4)
mu = 1.0 / numpy.mean(chi)
K += w4 * numpy.exp(-mu * chi)

return K

生成 ROC 图(来自上面的链接)的主要障碍似乎是将数据分成两组,然后在测试集上调用 predict_proba() 的过程。 是否可以使用预先计算的内核在 scikit-learn 中执行此操作?

最佳答案

简短的回答是“也许不是”。您是否尝试过以下内容?

基于 http://scikit-learn.org/stable/modules/svm.html 的示例,你需要这样的东西:

    import numpy as np

from sklearn import svm
X = np.array([[0, 0], [1, 1]])
y = [0, 1]
clf = svm.SVC(kernel='precomputed')

# kernel computation
K = numpy.zeros(shape = (n, n))

# "At the moment, the kernel values between all training vectors
# and the test vectors must be provided."
# according to scikit learn web page.
# -- This is the problem!
# v1: array, shape (n, d)
# w1: float in [0, 1)
chi = sklearn.metrics.pairwise.chi2_kernel(v1, v1)
mu = 1.0 / numpy.mean(chi)
K += w1 * numpy.exp(-mu * chi)

# v2: array, shape (n, d)
# w2: float in [0, 1)
chi = sklearn.metrics.pairwise.chi2_kernel(v2, v2)
mu = 1.0 / numpy.mean(chi)
K += w2 * numpy.exp(-mu * chi)

# v3: array, shape (n, d)
# w3: float in [0, 1)
chi = sklearn.metrics.pairwise.chi2_kernel(v3, v3)
mu = 1.0 / numpy.mean(chi)
K += w3 * numpy.exp(-mu * chi)

# v4: array, shape (n, d)
# w4: float in [0, 1)
chi = sklearn.metrics.pairwise.chi2_kernel(v4, v4)
mu = 1.0 / numpy.mean(chi)
K += w4 * numpy.exp(-mu * chi)

# scikit-learn is a wrapper LIBSVM and looking at the LIBSVM Readme file
# it seems you need kernel values for test data something like this:

Kt = numpy.zeros(shape = (nt, n))
# t1: array, shape (nt, d)
# w1: float in [0, 1)
chi = sklearn.metrics.pairwise.chi2_kernel(t1, v1)
mu = 1.0 / numpy.mean(chi)
Kt += w1 * numpy.exp(-mu * chi)

# v2: array, shape (n, d)
# w2: float in [0, 1)
chi = sklearn.metrics.pairwise.chi2_kernel(t2, v2)
mu = 1.0 / numpy.mean(chi)
Kt += w2 * numpy.exp(-mu * chi)

# v3: array, shape (n, d)
# w3: float in [0, 1)
chi = sklearn.metrics.pairwise.chi2_kernel(t3, v3)
mu = 1.0 / numpy.mean(chi)
Kt += w3 * numpy.exp(-mu * chi)

# v4: array, shape (n, d)
# w4: float in [0, 1)
chi = sklearn.metrics.pairwise.chi2_kernel(t4, v4)
mu = 1.0 / numpy.mean(chi)
Kt += w4 * numpy.exp(-mu * chi)

clf.fit(K, y)

# predict on testing examples
probas_ = clf.predict_proba(Kt)

从这里开始,只需复制 http://scikit-learn.org/0.13/auto_examples/plot_roc.html 的底部

关于python - 可以在 scikit-learn 中使用预先计算的内核从 SVM 绘制 ROC 图吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16719263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com