def fit(self, X, Y): # INSERT_CODE # Here you have to set the matrices as in the general QP problem #P = #q = #G = #h = #A = # hint: this has to be a row vector #b = # hint: this has to be a scalar # this is already implemented so you don't have to # read throught the cvxopt manual alpha = np.array( qp(cvxmatrix(P, tc='d'), cvxmatrix(q, tc='d'), cvxmatrix(G, tc='d'), cvxmatrix(h, tc='d'), cvxmatrix(A, tc='d'), cvxmatrix(b, tc='d'))['x']).flatten()
def fit(self, X, Y): n_samples, n_features = X.shape # Compute the Gram matrix K = buildKernel(X.T, kernel=self.kernel, kernelparameter=self.kernelparameter) # construct P, q, A, b, G, h matrices for CVXOPT P = cvxmatrix(np.outer(Y, Y) * K) # diag instead? q = cvxmatrix(np.ones(n_samples) * -1) if self.C is None: G = cvxmatrix(np.diag(np.ones(n_samples) * -1)) h = cvxmatrix(np.zeros(n_samples)) else: diag1 = np.diag(np.ones(n_samples) * -1) diag2 = np.identity(n_samples) G = cvxmatrix(np.vstack((diag1, diag2))) zero = np.zeros(n_samples) C = np.ones(n_samples) * self.C h = cvxmatrix(np.hstack((zero, C))) A = cvxmatrix(Y, (1, n_samples)) b = cvxmatrix(0.0) # this is already implemented so you don't have to # read throught the cvxopt manual alpha = np.array( qp(cvxmatrix(P, tc='d'), cvxmatrix(q, tc='d'), cvxmatrix(G, tc='d'), cvxmatrix(h, tc='d'), cvxmatrix(A, tc='d'), cvxmatrix(b, tc='d'))['x']).flatten() # Support vectors have non zero lagrange multipliers mask = alpha > 1e-5 # some small threshold self.X_sv = X[mask] self.Y_sv = Y[mask] self.a = alpha[mask] indices = np.arange(len(alpha))[mask] b = .0 for n in range(len(self.a)): by = self.Y_sv[n] bypred = np.sum(self.a * self.Y_sv * K[indices[n], mask]) b = b + (by - bypred) self.b = b / len(self.a)
def fit(self, X, y=None): if self.display: solvers.options['show_progress'] = True else: solvers.options['show_progress'] = False n = X.shape[0] # Step 1: Optimizing the SVDD dual problem..... K = kernel(X, metric=self.kernel, n_jobs=1, filter_params=True, gamma=self.gamma, coef0=self.coef0, degree=self.degree) q = cvxmatrix(-K.diagonal(), tc='d') #TODO: make it a separate function call labeled_indices = np.where(y != 0)[0] anomaly_indices = np.where(y == 1)[0] normal_indices = np.where(y == -1)[0] if (len(labeled_indices)): c_weighted = self.influence(K, y) else: c_weighted = self.C * np.ones(n) # solver if self.method is 'qp': P = cvxmatrix(2 * K, tc='d') G = cvxmatrix(np.vstack((-np.eye(n), np.eye(n))), tc='d') # lhs box constraints c_weighted_eps = np.copy(c_weighted) c_weighted_eps[normal_indices] -= 2 * self.eps h = cvxmatrix(np.concatenate((np.zeros(n), c_weighted_eps)), tc='d') # zeros for >=0, c_weighted for <=c_i # optimize using cvx solver Aeq = np.zeros((len(anomaly_indices), n)) beq = np.zeros(len(anomaly_indices)) i = 0 for ind in anomaly_indices: Aeq[i, ind] = 1 beq[i] = c_weighted[ind] i += 1 A = cvxmatrix(Aeq, tc='d') b = cvxmatrix(beq, tc='d') # optimize using cvx solver sol = solvers.qp(P, q, G, h, A, b, initvals=cvxmatrix(np.zeros(n), tc='d')) if self.method is 'smo': #TODO: apply SMO algorithm alpha = None # setup SVC model alpha = np.asarray(sol['x']) inx = np.where(alpha > self.eps)[0] self.sv_inx = inx self.alpha = alpha[inx] self.nsv = inx.size self.sv_ind = np.where((np.ravel(alpha) > self.eps) & (np.ravel(alpha) < c_weighted - self.eps))[0] self.bsv_ind = np.where(np.ravel(alpha) >= c_weighted - self.eps)[0] self.inside_ind = np.where(np.ravel(alpha) < c_weighted - self.eps)[0] k_inx = K[inx[:, None], inx] # submatrix of K(sv+bsv, sv+bsv) k_sv = K[self.sv_ind[:, None], self.sv_ind] # submatrix of K(sv,sv) k_bsv = K[self.bsv_ind[:, None], self.bsv_ind] # submatrix of K(bsv,bsv) # 2-norm of center a^2 self.b = self.alpha.reshape(1, self.nsv).dot(k_inx).reshape( 1, self.nsv).dot(self.alpha.reshape(self.nsv, 1)) #including both of SV and BSV (bounded support vectors) self.sv = X[inx, :] d = k_sv.diagonal() - 2 * self.alpha.reshape(1, self.nsv).dot( K[inx[:, None], self.sv_ind]) + self.b * np.ones(self.sv_ind.size) self.r = d.max() self.rid = self.sv_ind[np.argmax(d.ravel())] d_bsv = k_bsv.diagonal() - 2 * self.alpha.reshape(1, self.nsv).dot(K[ inx[:, None], self.bsv_ind]) + self.b * np.ones(self.bsv_ind.size) self.fval = self.r + self.C * (d_bsv - self.r).sum() if self.cached: self.K = K self.y = -1 * np.ones(n) self.y[self.bsv_ind] = 1 if self.labeling: #Step 2: Labeling cluster index by using CG self.predict(X) self.c_weighted = c_weighted
def fit(self, X, y=None): if self.display: solvers.options['show_progress'] = True else: solvers.options['show_progress'] = False n = X.shape[0] # Step 1: Optimizing the SVDD dual problem..... K = kernel(X, metric=self.kernel, n_jobs=1, filter_params=True, gamma=self.gamma, coef0=self.coef0, degree=self.degree) q = cvxmatrix(-K.diagonal(), tc='d') #TODO: make it a separate function call labeled_indices = np.where(y!=0)[0] anomaly_indices = np.where(y==1)[0] normal_indices = np.where(y==-1)[0] if (len(labeled_indices)): c_weighted = self.influence(K,y) else: c_weighted = self.C*np.ones(n) # solver if self.method is 'qp': P = cvxmatrix(2*K, tc='d') G = cvxmatrix(np.vstack((-np.eye(n), np.eye(n))), tc='d') # lhs box constraints c_weighted_eps = np.copy(c_weighted) c_weighted_eps[normal_indices]-=2*self.eps h = cvxmatrix(np.concatenate((np.zeros(n), c_weighted_eps)), tc='d') # zeros for >=0, c_weighted for <=c_i # optimize using cvx solver Aeq = np.zeros((len(anomaly_indices),n)) beq = np.zeros(len(anomaly_indices)) i=0 for ind in anomaly_indices: Aeq[i,ind]=1 beq[i]=c_weighted[ind] i+=1 A = cvxmatrix(Aeq, tc='d') b = cvxmatrix(beq, tc='d') # optimize using cvx solver sol = solvers.qp(P,q,G,h,A,b,initvals=cvxmatrix(np.zeros(n), tc='d')) if self.method is 'smo': #TODO: apply SMO algorithm alpha = None # setup SVC model alpha = np.asarray(sol['x']) inx = np.where(alpha > self.eps)[0] self.sv_inx = inx self.alpha = alpha[inx] self.nsv = inx.size self.sv_ind = np.where((np.ravel(alpha) > self.eps) & (np.ravel(alpha) < c_weighted-self.eps))[0] self.bsv_ind= np.where(np.ravel(alpha) >= c_weighted-self.eps)[0] self.inside_ind = np.where(np.ravel(alpha) < c_weighted-self.eps)[0] k_inx = K[inx[:,None], inx] # submatrix of K(sv+bsv, sv+bsv) k_sv = K[self.sv_ind[:,None], self.sv_ind] # submatrix of K(sv,sv) k_bsv = K[self.bsv_ind[:,None], self.bsv_ind] # submatrix of K(bsv,bsv) # 2-norm of center a^2 self.b = self.alpha.reshape(1,self.nsv).dot(k_inx).reshape(1,self.nsv).dot(self.alpha.reshape(self.nsv,1)) #including both of SV and BSV (bounded support vectors) self.sv= X[inx, :] d = k_sv.diagonal() - 2*self.alpha.reshape(1,self.nsv).dot(K[inx[:,None], self.sv_ind]) + self.b * np.ones(self.sv_ind.size) self.r = d.max() self.rid = self.sv_ind[np.argmax(d.ravel())] d_bsv = k_bsv.diagonal() - 2*self.alpha.reshape(1,self.nsv).dot(K[inx[:,None], self.bsv_ind]) + self.b * np.ones(self.bsv_ind.size) self.fval = self.r+self.C*(d_bsv - self.r).sum() if self.cached: self.K = K self.y = -1*np.ones(n) self.y[self.bsv_ind] = 1 if self.labeling: #Step 2: Labeling cluster index by using CG self.predict(X) self.c_weighted = c_weighted
def L1(self): A = cvxmatrix(self.A) Y = cvxmatrix(self.Y) X = cvxmatrix(self.X) self.L1XX = l1regls(A, Y) self.L1err = norm(self.L1XX-X, 2)/norm(self.X,2)
def getPOIRobin(sensor_data, misc=None): x_, y_, phi_, Nc, T, V = sensor_data from cvxopt import matrix as cvxmatrix from cvxopt.solvers import lp Alpha = np.radians(10.) dm = MaxDistance DensityList = [] Density = np.zeros([ScoreSize, ScoreSize]) Weight = np.ones([Nc, ScoreSize, ScoreSize]) X = np.empty(T) Y = np.empty(T) Xo = np.empty(T) Yo = np.empty(T) S = V**.5 #Standard Deviation! XCoordinates = CamTemplY YCoordinates = -CamTemplX def LineDistance(x1, y1, x2, y2, x, y): return (y2 - y1) * (x - x1) - (y - y1) * (x2 - x1) IndSet = [] for t in range(T): print 't:' + str(t) Density *= 0.0 Weight[:, :, :] = 1.0 for i in range(Nc): phi = phi_[t, i] x = x_[t, i] y = y_[t, i] a = S[t, i] theta = np.pi / 2 - phi b = a + (dm + 2 * a) * np.tan(Alpha) xb = x + a * np.cos(np.pi + theta) yb = y + a * np.sin(np.pi + theta) xt = x + (a + dm) * np.cos(theta) yt = y + (a + dm) * np.sin(theta) x1 = xb + a * np.cos(np.pi / 2 + theta) y1 = yb + a * np.sin(np.pi / 2 + theta) x2 = xb + a * np.cos(3 * np.pi / 2 + theta) y2 = yb + a * np.sin(3 * np.pi / 2 + theta) x3 = xt + b * np.cos(3 * np.pi / 2 + theta) y3 = yt + b * np.sin(3 * np.pi / 2 + theta) x4 = xt + b * np.cos(np.pi / 2 + theta) y4 = yt + b * np.sin(np.pi / 2 + theta) #Line l1l4 Weight[i][LineDistance(x1, y1, x4, y4, XCoordinates, YCoordinates) < 0] = 0 #Line l3l2 Weight[i][LineDistance(x3, y3, x2, y2, XCoordinates, YCoordinates) < 0] = 0 #Line l2l1 Weight[i][LineDistance(x2, y2, x1, y1, XCoordinates, YCoordinates) < 0] = 0 #Line l4l3 Weight[i][LineDistance(x4, y4, x3, y3, XCoordinates, YCoordinates) < 0] = 0 Density += Weight[i] / np.sum(Weight[i]) Max = np.max(Density) def GetCentroid(Properties): Areas = [] for props in Properties: Areas.append(props.area) MaxIndx = np.argmax(np.array(Areas)) return Properties[MaxIndx].centroid BoolDensity = Density * 0.0 BoolDensity[Density == Max] = 1.0 LabelDensity = measure.label(BoolDensity, connectivity=1) Properties = measure.regionprops(LabelDensity) NoRegions = len(Properties) if NoRegions == 1: row, col = Properties[0].centroid else: row, col = GetCentroid(Properties) r = row - ScoreSizeBy2 c = col - ScoreSizeBy2 X[t] = c Y[t] = -r Xo[t] = c Yo[t] = -r #Creating index set of cameras which will contribute for event location! IndSetEntry = [] row = np.int(np.round(row)) col = np.int(np.round(col)) for i in range(Nc): if Weight[i][row, col] == 1.: IndSetEntry.append(i) IndSet.append(IndSetEntry) DensityList.append(deepcopy(Density)) DensityList = map(lambda Density: Density * 250 / np.max(Density), DensityList) Gamma = 1.0 tList = [] for t in range(T): print 't:', t Ind = IndSet[t] N = len(Ind) P = cvxmatrix(0.0, (3 * N, 3 * N + 2)) w = 1 / S[t, Ind] W = np.diag(w) P[:N, 2:2 + N] = W P[N:2 * N, 2 + N:2 + 2 * N] = W P[2 * N:, 2 + 2 * N:] = Gamma * np.eye(N, N) q = cvxmatrix(0.0, (3 * N, 1)) q[:N] = w * x_[t, Ind] q[N:2 * N] = w * y_[t, Ind] AA = cvxmatrix(0.0, (N, 3 * N + 2)) cos = np.cos(phi_[t, Ind]) sin = np.sin(phi_[t, Ind]) AA[:, 0] = -cos AA[:, 1] = sin AA[:, 2:2 + N] = np.diag(cos) AA[:, 2 + N:2 + 2 * N] = np.diag(-sin) AA[:, 2 + 2 * N:] = -np.eye(N, N) c = cvxmatrix(0.0, (6 * N + 2, 1)) c[3 * N + 2::] = np.ones(3 * N) G = cvxmatrix(0.0, (6 * N, 6 * N + 2)) G[:3 * N, :3 * N + 2] = P G[3 * N:, :3 * N + 2] = -P G[:3 * N, 3 * N + 2:] = -np.eye(3 * N, 3 * N) G[3 * N:, 3 * N + 2:] = -np.eye(3 * N, 3 * N) h = cvxmatrix(0.0, (6 * N, 1)) h[:3 * N] = q h[3 * N:] = -q A = cvxmatrix(0.0, (N, 6 * N + 2)) A[:, :3 * N + 2] = AA b = cvxmatrix(0.0, (N, 1)) if N > 1: Solution = lp(c, G, h, A, b) if not Solution['status'] == 'optimal': tList.append(t) Sol = Solution['x'] X[t] = Sol[0] Y[t] = Sol[1] print 'X,Y:', X[t], Y[t] for t in range(T): print 't:', t c = int(round(X[t] + ScoreSizeBy2)) r = int(round(-Y[t] + ScoreSizeBy2)) print 'r,c:', r, c EventIndexSet = GetEventIndexSet(int(round(r)), int(round(c))) print 'EventIndexSet:', EventIndexSet DensityList[t][EventIndexSet] = np.max( DensityList[t]) - DensityList[t][EventIndexSet] return X, Y
def fit(self, X): if self.display: solvers.options['show_progress'] = True else: solvers.options['show_progress'] = False n = X.shape[0] # Step 1: Optimizing the SVDD dual problem..... K = kernel(X, metric=self.kernel, n_jobs=1, \ filter_params=True, gamma=self.gamma, \ coef0=self.coef0, degree=self.degree) q = cvxmatrix(-K.diagonal(), tc='d') if self.method is 'qp': P = cvxmatrix(2 * K, tc='d') G = cvxmatrix(np.vstack((-np.eye(n), np.eye(n))), tc='d') # lhs box constraints h = cvxmatrix(np.concatenate((np.zeros(n), self.C * np.ones(n))), tc='d') # rhs box constraints # optimize using cvx solver sol = solvers.qp(P, q, G, h, initvals=cvxmatrix(np.zeros(n), tc='d')) if self.method is 'smo': #TODO: apply SMO algorithm alpha = None # setup SVC model alpha = np.asarray(sol['x']) inx = np.where(alpha > self.eps)[0] self.sv_inx = inx self.alpha = alpha[inx] self.nsv = inx.size self.sv_ind = np.where((alpha > self.eps) & (alpha < self.C - self.eps))[0] self.bsv_ind = np.where(alpha >= self.C - self.eps)[0] self.inside_ind = np.where(alpha < self.C - self.eps)[0] k_inx = K[inx[:, None], inx] # submatrix of K(sv+bsv, sv+bsv) k_sv = K[self.sv_ind[:, None], self.sv_ind] # submatrix of K(sv,sv) k_bsv = K[self.bsv_ind[:, None], self.bsv_ind] # submatrix of K(bsv,bsv) # 2-norm of center a^2 self.b = self.alpha.reshape(1, self.nsv).dot(k_inx).reshape( 1, self.nsv).dot(self.alpha.reshape(self.nsv, 1)) #including both of SV and BSV (bounded support vectors) self.sv = X[inx, :] d = k_sv.diagonal() - 2 * self.alpha.reshape(1, self.nsv).dot( K[inx[:, None], self.sv_ind]) + self.b * np.ones(self.sv_ind.size) self.r = d.max() self.rid = self.sv_ind[np.argmax(d.ravel())] d_bsv = k_bsv.diagonal() - 2 * self.alpha.reshape(1, self.nsv).dot(K[ inx[:, None], self.bsv_ind]) + self.b * np.ones(self.bsv_ind.size) self.fval = self.r + self.C * (d_bsv - self.r).sum() self.K = K self.y = -1 * np.ones(n) self.y[self.bsv_ind] = 1 if self.labeling: #Step 2: Labeling cluster index by using CG self.predict(X)