def readlibsvm(fname, X, Y, direction="rows", min_d=0, max_n=-1): cdirection = None if direction == 0 or direction == "columns": cdirection = 1 if direction == 1 or direction == "rows": cdirection = 2 if cdirection is None: raise ValueError("Direction must be either columns/rows or 0/1") X = lib.adapt(X) Y = lib.adapt(Y) Xobj = X.ptr() Yobj = Y.ptr() if (Xobj == -1 or Yobj == -1): raise errors.InvalidObjectError("Invalid/unsupported object passed as X, Y") lib.callsl("sl_readlibsvm", fname, \ X.ctype(), Xobj, \ Y.ctype(), Yobj, \ cdirection, min_d, max_n) X.ptrcleaner() Y.ptrcleaner() return (X.getobj(), Y.getobj())
def gram(self, X, K, dirX="rows", dirY="rows", Y=None): """ Returns the dense Gram matrix evaluated over the datapoints. :param X: n-by-d data matrix :param Y: another data matrix. If Y is None, then X is used. :param K: placeholder for output Gram matrix. """ if Y is None: Y = X dirY = dirX cdirX = self.__get_direction(dirX) cdirY = self.__get_direction(dirY) X = lib.adapt(X) Y = lib.adapt(Y) K = lib.adapt(K) lib.callsl("sl_kernel_gram", cdirX, cdirY, self._kernel_obj, \ X.ctype(), X.ptr(), \ Y.ctype(), Y.ptr(), \ K.ctype(), K.ptr()) X.ptrcleaner() Y.ptrcleaner() K.ptrcleaner() return K.getobj()
def readlibsvm(fname, X, Y, direction="rows", min_d=0, max_n=-1): cdirection = None if direction == 0 or direction == "columns": cdirection = 1 if direction == 1 or direction == "rows": cdirection = 2 if cdirection is None: raise ValueError("Direction must be either columns/rows or 0/1") X = lib.adapt(X) Y = lib.adapt(Y) Xobj = X.ptr() Yobj = Y.ptr() if (Xobj == -1 or Yobj == -1): raise errors.InvalidObjectError( "Invalid/unsupported object passed as X, Y") lib.callsl("sl_readlibsvm", fname, \ X.ctype(), Xobj, \ Y.ctype(), Yobj, \ cdirection, min_d, max_n) X.ptrcleaner() Y.ptrcleaner() return (X.getobj(), Y.getobj())
def __init__(self, d, nu, l): self._d = d self._nu = nu self._l = l self._kernel_obj = c_void_p() lib.callsl("sl_create_kernel", "matern", d, byref(self._kernel_obj), \ c_double(self_nu), c_double(self._l))
def __init__(self, d, q=3, c=0, gamma=1): if c < 0: raise ValueError("kernel paramter must be >= 0") if type(q) is not int: raise errors.InvalidParamterError("exponent must be integer") self._d = d self._q = q self._c = c self._gamma = gamma self._kernel_obj = c_void_p() lib.callsl("sl_create_kernel", "polynomial", self._d, byref(self._kernel_obj), \ c_int(self._q), c_double(self._gamma), c_double(self._c))
def approximate_svd(A, U, S, V, k=10, params=None): """ Compute the SVD of **A** such that **SVD(A) = U S V^T**. :param A: Input matrix. :param U: Output U (left singular vectors). :param S: Output S (singular values). :param V: Output V (right singular vectors). :param k: Dimension to apply along. :param params: Parmaters for the SVD. :returns: (U, S, V) """ A = lib.adapt(A) U = lib.adapt(U) S = lib.adapt(S) V = lib.adapt(V) Aobj = A.ptr() Uobj = U.ptr() Sobj = S.ptr() Vobj = V.ptr() if (Aobj == -1 or Uobj == -1 or Sobj == -1 or Vobj == -1): raise errors.InvalidObjectError( "Invalid/unsupported object passed as A, U, S or V ") # use default params in case none are provided if params == None: params = SVDParams() params_json = params.str() + '\0' lib.callsl("sl_approximate_svd", \ A.ctype(), Aobj, \ U.ctype(), Uobj, \ S.ctype(), Sobj, \ V.ctype(), Vobj, \ k, params_json, lib.ctxt_obj) A.ptrcleaner() U.ptrcleaner() S.ptrcleaner() V.ptrcleaner() return (U.getobj(), S.getobj(), V.getobj())
def faster_least_squares(A, B, X, orientation=El.NORMAL, params=None): """ Compute a solution to the least squares problem: If orientation == El.NORMAL: argmin_X ||A * X - B||_F If orientation == El.ADJOINT: argmin_X ||A^H * X - B||_F * ADJOINT not yet supported! * :param A: Input matrix. :param B: Right hand side. :param X: Solution :param params: Parmaters. :returns: X """ A = lib.adapt(A) B = lib.adapt(B) X = lib.adapt(X) Aobj = A.ptr() Bobj = B.ptr() Xobj = X.ptr() if (Aobj == -1 or Bobj == -1 or Xobj == -1): raise errors.InvalidObjectError( "Invalid/unsupported object passed as A, B, X ") # use default params in case none are provided if params == None: params = FasterLeastSquaresParams() params_json = params.str() + '\0' lib.callsl("sl_faster_least_squares", \ orientation, \ A.ctype(), Aobj, \ B.ctype(), Bobj, \ X.ctype(), Xobj, \ params_json, lib.ctxt_obj) A.ptrcleaner() B.ptrcleaner() X.ptrcleaner() return X.getobj()
def approximate_svd(A, U, S, V, k=10, params=None): """ Compute the SVD of **A** such that **SVD(A) = U S V^T**. :param A: Input matrix. :param U: Output U (left singular vectors). :param S: Output S (singular values). :param V: Output V (right singular vectors). :param k: Dimension to apply along. :param params: Parmaters for the SVD. :returns: (U, S, V) """ A = lib.adapt(A) U = lib.adapt(U) S = lib.adapt(S) V = lib.adapt(V) Aobj = A.ptr() Uobj = U.ptr() Sobj = S.ptr() Vobj = V.ptr() if (Aobj == -1 or Uobj == -1 or Sobj == -1 or Vobj == -1): raise errors.InvalidObjectError("Invalid/unsupported object passed as A, U, S or V ") # use default params in case none are provided if params == None: params = SVDParams() params_json = params.str() + '\0' lib.callsl("sl_approximate_svd", \ A.ctype(), Aobj, \ U.ctype(), Uobj, \ S.ctype(), Sobj, \ V.ctype(), Vobj, \ k, params_json, lib.ctxt_obj) A.ptrcleaner() U.ptrcleaner() S.ptrcleaner() V.ptrcleaner() return (U.getobj(), S.getobj(), V.getobj())
def faster_least_squares(A, B, X, orientation=El.NORMAL, params=None): """ Compute a solution to the least squares problem: If orientation == El.NORMAL: argmin_X ||A * X - B||_F If orientation == El.ADJOINT: argmin_X ||A^H * X - B||_F * ADJOINT not yet supported! * :param A: Input matrix. :param B: Right hand side. :param X: Solution :param params: Parmaters. :returns: X """ A = lib.adapt(A) B = lib.adapt(B) X = lib.adapt(X) Aobj = A.ptr() Bobj = B.ptr() Xobj = X.ptr() if (Aobj == -1 or Bobj == -1 or Xobj == -1): raise errors.InvalidObjectError("Invalid/unsupported object passed as A, B, X ") # use default params in case none are provided if params == None: params = FasterLeastSquaresParams() params_json = params.str() + '\0' lib.callsl("sl_faster_least_squares", \ orientation, \ A.ctype(), Aobj, \ B.ctype(), Bobj, \ X.ctype(), Xobj, \ params_json, lib.ctxt_obj) A.ptrcleaner() B.ptrcleaner() X.ptrcleaner() return X.getobj()
def __init__(self, d): self._d = d self._kernel_obj = c_void_p() lib.callsl("sl_create_kernel", "linear", d, byref(self._kernel_obj))
def __init__(self, d, beta): self._d = d self._beta = beta self._kernel_obj = c_void_p() lib.callsl("sl_create_kernel", "expsemigroup", d, byref(self._kernel_obj))
def __init__(self, d, sigma): self._d = d self._sigma = sigma self._kernel_obj = c_void_p() lib.callsl("sl_create_kernel", "laplacian", d, byref(self._kernel_obj), c_double(sigma))