我已经成功实现了我自己的自定义线性内核,使用clf.predict
. 但是,当我想使用clf.decision_function
它时,它会为所有点提供恒定值。
这是自定义内核的代码:
```
def linear_basis(x, y):
return np.dot(x.T, y)
def linear_kernel(X, Y, K=linear_basis):
gram_matrix = np.zeros((X.shape[0], Y.shape[0]))
for i, x in enumerate(X):
for j, y in enumerate(Y):
gram_matrix[i,j] = K(x,y)
return gram_matrix
```
现在将此内核用于小型线性训练集。
```
#creating random 2D points
sample_size = 100
dat = {
'x': [random.uniform(-2,2) for i in range(sample_size)],
'y': [random.uniform(-2,2) for i in range(sample_size)]
}
data = pd.DataFrame(dat)
# giving the random points a linear structure
f_lin = np.vectorize(lambda x, y: 1 if x > y else 0)
data['z_lin'] = f_lin(data['x'].values, data['y'].values)
data_pos = data[data.z_lin == 1.]
data_neg = data[data.z_lin == 0.]
X_train = data[['x', 'y']]
y_train = data[['z_lin']]
clf_custom_lin = svm.SVC(kernel=linear_kernel) # using my custom kernel here
clf_custom_lin.fit(X_train.values,y_train.values)
# creating a 100x100 grid to manually predict each point in 2D
gridpoints = np.array([[i,j] for i in np.linspace(-2,2,100) for j in np.linspace(-2,2,100)])
gridresults = np.array([clf.predict([gridpoints[k]]) for k in range(len(gridpoints))])
# now plotting each point and the training samples
plt.scatter(gridpoints[:,0], gridpoints[:,1], c=gridresults, cmap='RdYlGn')
plt.scatter(data_pos['x'], data_pos['y'], color='green', marker='o', edgecolors='black')
plt.scatter(data_neg['x'], data_neg['y'], color='red', marker='o', edgecolors='black')
plt.show()
```
这给出了以下结果:
现在我想使用以下方法重现情节clf.decision_function
:
(!请注意,我在这里不小心切换了颜色!)
```
h = .02
xx, yy = np.meshgrid(np.arange(-2 - .5, 2 + .5, h),
np.arange(-2 - .5, 2 + .5, h))
# using the .decision_function here
Z = clf_custom_lin.decision_function(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap=plt.cm.RdBu, alpha=.8)
plt.scatter(data_pos['x'], data_pos['y'], color='blue', marker='o', edgecolors='black')
plt.scatter(data_neg['x'], data_neg['y'], color='red', marker='o', edgecolors='black')
plt.show()
```
这给出了以下情节:
这是使用集成线性内核 (kernel="linear") 绘制相同数据的示例:
由于自定义内核的预测函数刚刚起作用,它应该与这里的决策函数给出相同的工作图,对吧?我不知道为什么这适用于集成线性函数,但不适用于自定义线性函数,它也适用于仅预测没有决策函数的点。希望有人可以在这里提供帮助。