Changeset 834
- Timestamp:
- 10/13/15 13:47:42 (7 months ago)
- Location:
- mystic/mystic
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
mystic/mystic/svctools.py
r830 r834 2 2 # 3 3 # Author: Patrick Hung (patrickh @caltech) 4 # Author: Mike McKerns (mmckerns @caltech and @uqfoundation) 4 5 # Copyright (c) 1997-2015 California Institute of Technology. 5 6 # License: 3-clause BSD. The full license text is available at: … … 9 10 """ 10 11 11 from numpy import zeros, multiply, ndarray, vectorize, array, dot, transpose, diag, sum12 from numpy import multiply, asarray, dot, transpose, sum 12 13 13 14 def KernelMatrix(X, k=dot): 14 n = X.shape[0] 15 Q = zeros((n,n)) 16 for i in range(n): 17 for j in range(i, n): 18 Q[i,j] = k(X[i,:],X[j,:]) 19 return Q + transpose(Q) - diag(Q.diagonal()) 15 "inner product of X with self, using k as elementwise product function" 16 # the following is tensordot(X,X,axes=(-1,-1)), with dot --> k 17 # 3-clause BSD (see: v1.7.2 http://docs.scipy.org/doc/numpy/license.html) 18 X = asarray(X) 19 Xs = X.shape 20 ndX = len(X.shape) 21 nX = 1 22 axes = [ndX - 1] 23 24 # Move the axes to sum over to the end of "a" 25 # and to the front of "b" in inner(a,b) 26 notin = [_ for _ in range(ndX) if _ not in axes] 27 newaxes_a = notin + axes 28 N2 = 1 29 for axis in axes: N2 *= Xs[axis] 30 newshape_a = (-1, N2) 31 olda = [Xs[axis] for axis in notin] 32 33 newaxes_b = axes + notin 34 N2 = 1 35 for axis in axes: N2 *= Xs[axis] 36 newshape_b = (N2, -1) 37 oldb = [Xs[axis] for axis in notin] 38 39 at = X.transpose(newaxes_a).reshape(newshape_a) 40 bt = X.transpose(newaxes_b).reshape(newshape_b) 41 return k(at,bt).reshape(olda + oldb) 42 20 43 21 44 def WeightVector(alpha, X, y): -
mystic/mystic/svmtools.py
r830 r834 2 2 # 3 3 # Author: Patrick Hung (patrickh @caltech) 4 # Author: Mike McKerns (mmckerns @caltech and @uqfoundation) 4 5 # Copyright (c) 1997-2015 California Institute of Technology. 5 6 # License: 3-clause BSD. The full license text is available at: … … 9 10 """ 10 11 11 from numpy import zeros, multiply, ndarray, vectorize,array12 from numpy import multiply, ndarray, vectorize, array, asarray 12 13 13 def InnerProduct(i1, i2): 14 def InnerProduct(i1, i2): # numpy.multiply(x,x) 14 15 return i1 * i2 15 16 … … 17 18 return 1. + i1 * i2 18 19 19 def KernelMatrix(X, k=InnerProduct): 20 n = X.size 21 Q = zeros((n,n)) 22 for i in range(n): 23 for j in range(n): 24 # dumb, but how else to do outer products of arbitrary functions 25 # without going through ufunc C-api ? 26 Q[i,j] = k(X[i],X[j]) 27 return Q 20 def KernelMatrix(X, k=multiply): 21 "outer product of X with self, using k as elementwise product function" 22 X = asarray(X).ravel() 23 return k(X[:,None], X[None,:]) 24 28 25 29 26 def SupportVectors(alpha, eps=0):
Note: See TracChangeset
for help on using the changeset viewer.