Changeset 834


Ignore:
Timestamp:
10/13/15 13:47:42 (7 months ago)
Author:
mmckerns
Message:

extended svm and svc KernelMatrix? to n-dimensions

Location:
mystic/mystic
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • mystic/mystic/svctools.py

    r830 r834  
    22# 
    33# Author: Patrick Hung (patrickh @caltech) 
     4# Author: Mike McKerns (mmckerns @caltech and @uqfoundation) 
    45# Copyright (c) 1997-2015 California Institute of Technology. 
    56# License: 3-clause BSD.  The full license text is available at: 
     
    910""" 
    1011 
    11 from numpy import zeros, multiply, ndarray, vectorize, array, dot, transpose, diag, sum 
     12from numpy import multiply, asarray, dot, transpose, sum 
    1213 
    1314def 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 
    2043 
    2144def WeightVector(alpha, X, y): 
  • mystic/mystic/svmtools.py

    r830 r834  
    22# 
    33# Author: Patrick Hung (patrickh @caltech) 
     4# Author: Mike McKerns (mmckerns @caltech and @uqfoundation) 
    45# Copyright (c) 1997-2015 California Institute of Technology. 
    56# License: 3-clause BSD.  The full license text is available at: 
     
    910""" 
    1011 
    11 from numpy import zeros, multiply, ndarray, vectorize, array 
     12from numpy import multiply, ndarray, vectorize, array, asarray 
    1213 
    13 def InnerProduct(i1, i2): 
     14def InnerProduct(i1, i2): # numpy.multiply(x,x) 
    1415    return i1 * i2 
    1516 
     
    1718    return 1. + i1 * i2 
    1819 
    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 
     20def 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 
    2825 
    2926def SupportVectors(alpha, eps=0): 
Note: See TracChangeset for help on using the changeset viewer.