我是靠谱客的博主 暴躁眼神,最近开发中收集的这篇文章主要介绍pythonsvc_python-Scikit学习SVC Decision_function和Predi,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

对于那些感兴趣的人,我将发布一个从C ++(在这里)转换为python的predict函数的快速示例:

# I've only implemented the linear and rbf kernels

def kernel(params, sv, X):

if params.kernel == 'linear':

return [np.dot(vi, X) for vi in sv]

elif params.kernel == 'rbf':

return [math.exp(-params.gamma * np.dot(vi - X, vi - X)) for vi in sv]

# This replicates clf.decision_function(X)

def decision_function(params, sv, nv, a, b, X):

# calculate the kernels

k = kernel(params, sv, X)

# define the start and end index for support vectors for each class

start = [sum(nv[:i]) for i in range(len(nv))]

end = [start[i] + nv[i] for i in range(len(nv))]

# calculate: sum(a_p * k(x_p, x)) between every 2 classes

c = [ sum(a[ i ][p] * k[p] for p in range(start[j], end[j])) +

sum(a[j-1][p] * k[p] for p in range(start[i], end[i]))

for i in range(len(nv)) for j in range(i+1,len(nv))]

# add the intercept

return [sum(x) for x in zip(c, b)]

# This replicates clf.predict(X)

def predict(params, sv, nv, a, b, cs, X):

''' params = model parameters

sv = support vectors

nv = # of support vectors per class

a = dual coefficients

b = intercepts

cs = list of class names

X = feature to predict

'''

decision = decision_function(params, sv, nv, a, b, X)

votes = [(i if decision[p] > 0 else j) for p,(i,j) in enumerate((i,j)

for i in range(len(cs))

for j in range(i+1,len(cs)))]

return cs[max(set(votes), key=votes.count)]

predict和decision_function有很多输入参数,但是请注意,在调用predict(X)时,这些参数都在模型内部使用。实际上,拟合后,您可以在模型内部访问所有这些参数:

# Create model

clf = svm.SVC(gamma=0.001, C=100.)

# Fit model using features, X, and labels, Y.

clf.fit(X, y)

# Get parameters from model

params = clf.get_params()

sv = clf.support_vectors

nv = clf.n_support_

a = clf.dual_coef_

b = clf._intercept_

cs = clf.classes_

# Use the functions to predict

print(predict(params, sv, nv, a, b, cs, X))

# Compare with the builtin predict

print(clf.predict(X))

最后

以上就是暴躁眼神为你收集整理的pythonsvc_python-Scikit学习SVC Decision_function和Predi的全部内容,希望文章能够帮你解决pythonsvc_python-Scikit学习SVC Decision_function和Predi所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部