Advertisement
Guest User

svmlin

a guest
Nov 19th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. import numpy as np
  2. # might need to add path to mingw-w64/bin for cvxopt to work
  3. # import os
  4. # os.environ["PATH"] += os.pathsep + ...
  5. import cvxopt
  6.  
  7.  
  8.  
  9.  
  10. def svmlin(X, t, C):
  11. # Linear SVM Classifier
  12. #
  13. # INPUT:
  14. # X : the dataset (num_samples x dim)
  15. # t : labeling (num_samples x 1)
  16. # C : penalty factor for slack variables (scalar)
  17. #
  18. # OUTPUT:
  19. # alpha : output of quadprog function (num_samples x 1)
  20. # sv : support vectors (boolean) (1 x num_samples)
  21. # w : parameters of the classifier (1 x dim)
  22. # b : bias of the classifier (scalar)
  23. # result : result of classification (1 x num_samples)
  24. # slack : points inside the margin (boolean) (1 x num_samples)
  25.  
  26.  
  27. #####Insert your code here for subtask 2a#####
  28.  
  29. dim = X.shape(1)
  30. print(dim)
  31. N = t.size
  32. print("X",X.shape)
  33. H = np.zeros((N,N))
  34. for i in range(0, N):
  35. for j in range(0, N):
  36. H[i,j] = t[i]*t[j]*np.dot(X[i]*X[j])
  37. n = H.shape[1]
  38. print("n:", H.shape)
  39.  
  40.  
  41. P = cvxopt.matrix(H)
  42. print("P", P )
  43. q = cvxopt.matrix((-1)*np.ones(N))
  44. G = np.vstack([-np.eye(n), np.eye(n)]).astype('double')
  45. #G = G[:2,:]
  46. G = cvxopt.matrix(G)
  47. print("G", G)
  48. A = t.astype('double')[np.newaxis]
  49. #A = np.transpose(A)
  50.  
  51. #A = np.matrix.transpose(A)
  52. print(np.size(A))
  53. print("wtf")
  54. A = cvxopt.matrix(A)
  55. print("A", A)
  56.  
  57. b = cvxopt.matrix(np.zeros(1))
  58. z = np.zeros(N)
  59. c = C *np.ones(N)
  60. h = (np.hstack([z,c])).astype('double')
  61. print("h", h.shape, h)
  62.  
  63. h = cvxopt.matrix(h)
  64.  
  65.  
  66. print(np.size(b))
  67. print(b)
  68. a = cvxopt.solvers.qp(P, q, G, h, A, b)
  69. print(a["z"])
  70.  
  71. print("d")
  72. alpha = a["x"]
  73. sv = [None]*N
  74.  
  75.  
  76. for i in range(0,N):
  77. if a["x"][i]==0:
  78. sv[i] = False
  79. else:
  80. sv[i] = True
  81.  
  82. w= np.zeros((1,))
  83. for i in range(0,N):
  84. w[0] = w[0] + alpha[i]*t[i]*X[i,0]
  85. w[1] = w[1] + alpha[i]*t[i]*X[i,1]
  86.  
  87. print("w",w)
  88.  
  89. b,nm, b1 = 0
  90. for i in range(0,N):
  91. if(alpha[i]<C and alpha[i]>0):
  92. nm = nm+1
  93.  
  94. for i in range(0,N):
  95. b1 = b1 + alpha[i]*t[i]*X[i,0]
  96. b1 = b1 + alpha[i]*t[i]*X[i,1]
  97.  
  98. b1 = b1
  99. result = 1
  100. slack = 1
  101.  
  102.  
  103.  
  104. return alpha, sv, w, b, result, slack
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement