MCPcopy Create free account
hub / github.com/lazyprogrammer/machine_learning_examples / rbf

Function rbf

svm_class/svm_gradient.py:23–34  ·  view source on GitHub ↗
(X1, X2, gamma=None)

Source from the content-addressed store, hash-verified

21 return X1.dot(X2.T) + c
22
23def rbf(X1, X2, gamma=None):
24 if gamma is None:
25 gamma = 1.0 / X1.shape[-1] # 1 / D
26 # gamma = 0.05
27 # gamma = 5. # for donut and spiral
28 if np.ndim(X1) == 1 and np.ndim(X2) == 1:
29 result = np.exp(-gamma * np.linalg.norm(X1 - X2)**2)
30 elif (np.ndim(X1) > 1 and np.ndim(X2) == 1) or (np.ndim(X1) == 1 and np.ndim(X2) > 1):
31 result = np.exp(-gamma * np.linalg.norm(X1 - X2, axis=1)**2)
32 elif np.ndim(X1) > 1 and np.ndim(X2) > 1:
33 result = np.exp(-gamma * np.linalg.norm(X1[:, np.newaxis] - X2[np.newaxis, :], axis=2)**2)
34 return result
35
36def sigmoid(X1, X2, gamma=0.05, c=1):
37 return np.tanh(gamma * X1.dot(X2.T) + c)

Callers 3

xorFunction · 0.70
donutFunction · 0.70
spiralFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected