<s> from cvxopt import matrix , spmatrix , sparse <EOL> from cvxopt . blas import dot , dotu <EOL> from cvxopt . solvers import qp <EOL> import numpy as np <EOL> from kernel import Kernel <EOL> class OCSVM : <EOL> """<STR_LIT>""" <EOL> MSG_ERROR = - <NUM_LIT:1> <EOL> MSG_OK = <NUM_LIT:0> <EOL> PRECISION = <NUM_LIT:10> ** - <NUM_LIT:3> <EOL> kernel = [ ] <EOL> samples = - <NUM_LIT:1> <EOL> C = <NUM_LIT:1.0> <EOL> isDualTrained = False <EOL> alphas = [ ] <EOL> svs = [ ] <EOL> threshold = <NUM_LIT:0.0> <EOL> def __init__ ( self , kernel , C = <NUM_LIT:1.0> ) : <EOL> self . kernel = kernel <EOL> self . C = C <EOL> ( self . samples , foo ) = kernel . size <EOL> print ( '<STR_LIT>' . format ( self . samples , C ) ) <EOL> def train_dual ( self ) : <EOL> """<STR_LIT>""" <EOL> if ( self . samples < <NUM_LIT:1> ) : <EOL> print ( '<STR_LIT>' ) <EOL> return OCSVM . MSG_ERROR <EOL> N = self . samples <EOL> P = self . kernel <EOL> q = matrix ( <NUM_LIT:0.0> , ( N , <NUM_LIT:1> ) ) <EOL> A = matrix ( <NUM_LIT:1.0> , ( <NUM_LIT:1> , N ) ) <EOL> b = matrix ( <NUM_LIT:1.0> , ( <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> G1 = spmatrix ( <NUM_LIT:1.0> , range ( N ) , range ( N ) ) <EOL> G = sparse ( [ G1 , - G1 ] ) <EOL> h1 = matrix ( self . C , ( N , <NUM_LIT:1> ) ) <EOL> h2 = matrix ( <NUM_LIT:0.0> , ( N , <NUM_LIT:1> ) ) <EOL> h = matrix ( [ h1 , h2 ] ) <EOL> sol = qp ( P , - q , G , h , A , b ) <EOL> self . isDualTrained = True <EOL> self . alphas = sol [ '<STR_LIT:x>' ] <EOL> self . svs = [ ] <EOL> for i in range ( N ) : <EOL> if self . alphas [ i ] > OCSVM . PRECISION : <EOL> self . svs . append ( i ) <EOL> ( thres , MSG ) = self . apply_dual ( self . kernel [ self . svs , self . svs ] ) <EOL> self . threshold = matrix ( max ( thres ) ) <EOL> T = np . single ( self . threshold ) <EOL> cnt = <NUM_LIT:0> <EOL> for i in range ( len ( self . svs ) ) : <EOL> if thres [ i , <NUM_LIT:0> ] < ( T - OCSVM . PRECISION ) : <EOL> cnt += <NUM_LIT:1> <EOL> print ( '<STR_LIT>' . format ( len ( self . svs ) , cnt ) ) <EOL> print ( '<STR_LIT>' . format ( self . threshold ) ) <EOL> return OCSVM . MSG_OK <EOL> def get_threshold ( self ) : <EOL> return self . threshold <EOL> def get_support_dual ( self ) : <EOL> return self . svs <EOL> def get_alphas ( self ) : <EOL> return self . alphas <EOL> def get_support_dual_values ( self ) : <EOL> return self . alphas [ self . svs ] <EOL> def set_train_kernel ( self , kernel ) : <EOL> ( dim1 , dim2 ) = kernel . size <EOL> if ( dim1 != dim2 and dim1 != self . samples ) : <EOL> print ( '<STR_LIT>' ) <EOL> return OCSVM . MSG_ERROR <EOL> self . kernel = kernel ; <EOL> return OCSVM . MSG_OK <EOL> def apply_dual ( self , kernel ) : <EOL> """<STR_LIT>""" <EOL> N = self . samples <EOL> ( tN , foo ) = kernel . size <EOL> if ( tN < <NUM_LIT:1> ) : <EOL> print ( '<STR_LIT>' ) <EOL> return <NUM_LIT:0> , OCSVM . MSG_ERROR <EOL> if ( self . isDualTrained != True ) : <EOL> print ( '<STR_LIT>' ) <EOL> return <NUM_LIT:0> , OCSVM . MSG_ERROR <EOL> res = matrix ( [ dotu ( kernel [ i , : ] , self . alphas [ self . svs ] ) for i in range ( tN ) ] ) <EOL> return res , OCSVM . MSG_OK </s>
