def rbfClassifier(v, v0, k, gamma):
  X = v.X[:,1:]
  Y = v.Y
  U, S = k_neighbour(X, k, True)
  Fi = rbfTransform(X, U, gamma)
  w = ln.lstsq(Fi, Y)[0]
  yy = Fi.dot(w)
  yy[yy>=0]=1
  yy[yy<0]=-1
  e_in = hw8.classificationError(Y, yy)
  ''' Generate E_out '''
  X = v0.X[:,1:]
  Y = v0.Y
  yy = rbfTransform(X, U, gamma).dot(w)
  yy[yy>=0]=1
  yy[yy<0]=-1
  e_out = hw8.classificationError(Y, yy)
  return e_in, e_out
def rbfClassifier(v, v0, k, gamma):
    X = v.X[:, 1:]
    Y = v.Y
    U, S = k_neighbour(X, k, True)
    Fi = rbfTransform(X, U, gamma)
    w = ln.lstsq(Fi, Y)[0]
    yy = Fi.dot(w)
    yy[yy >= 0] = 1
    yy[yy < 0] = -1
    e_in = hw8.classificationError(Y, yy)
    ''' Generate E_out '''
    X = v0.X[:, 1:]
    Y = v0.Y
    yy = rbfTransform(X, U, gamma).dot(w)
    yy[yy >= 0] = 1
    yy[yy < 0] = -1
    e_out = hw8.classificationError(Y, yy)
    return e_in, e_out
def problem14():
  runs = 100
  notSep = 0
  bit = 0
  error = 0
  func = SimFunction()
  k = 9
  gamma = 1.5
  e_out = 0
  e_out_k = 0
  w0 = 0.0
  for run in range(runs):
    points = 100
    v = VSet(Dataset(points), func)
    X = v.X[:,1:]
    Y = v.Y
    try:
      U, S = k_neighbour(X, k, True)
#     clf = KMeans(k)
#     clf.fit(X)
#     U = clf.cluster_centers_
      Fi = rbfTransform(X, U, gamma)
      w = ln.lstsq(Fi, Y)[0]
      w0 = max(w0, np.max(np.abs(w)))
#       print w
      clf = SVC(C=1e6, kernel='rbf', gamma=gamma, coef0=1.)
      clf.fit(X, Y)
      ''' Generate E_out '''
      v = VSet(Dataset(1000), func)
      X = v.X[:,1:]
      Y = v.Y
      yy = rbfTransform(X, U, gamma).dot(w)
      yy[yy>=0]=1
      yy[yy<0]=-1
      rbf = hw8.classificationError(Y, yy)
      e_out += rbf
      kernel = hw8.classificationError(Y, clf.predict(X))
      e_out_k += kernel
      if kernel < rbf: bit += 1
    except:
      error += 1
  print bit, error, e_out, e_out_k, w0
def problem14():
    runs = 100
    notSep = 0
    bit = 0
    error = 0
    func = SimFunction()
    k = 9
    gamma = 1.5
    e_out = 0
    e_out_k = 0
    w0 = 0.0
    for run in range(runs):
        points = 100
        v = VSet(Dataset(points), func)
        X = v.X[:, 1:]
        Y = v.Y
        try:
            U, S = k_neighbour(X, k, True)
            #     clf = KMeans(k)
            #     clf.fit(X)
            #     U = clf.cluster_centers_
            Fi = rbfTransform(X, U, gamma)
            w = ln.lstsq(Fi, Y)[0]
            w0 = max(w0, np.max(np.abs(w)))
            #       print w
            clf = SVC(C=1e6, kernel='rbf', gamma=gamma, coef0=1.)
            clf.fit(X, Y)
            ''' Generate E_out '''
            v = VSet(Dataset(1000), func)
            X = v.X[:, 1:]
            Y = v.Y
            yy = rbfTransform(X, U, gamma).dot(w)
            yy[yy >= 0] = 1
            yy[yy < 0] = -1
            rbf = hw8.classificationError(Y, yy)
            e_out += rbf
            kernel = hw8.classificationError(Y, clf.predict(X))
            e_out_k += kernel
            if kernel < rbf: bit += 1
        except:
            error += 1
    print bit, error, e_out, e_out_k, w0
def problem13():
  runs = 1000
  notSep = 0
  func = SimFunction()
  for run in range(runs):
    points = 100
    v = VSet(Dataset(points), func)
  #   writeV(points)
  #   v = readV()
    X = v.X[:,1:]
    Y = v.Y
    clf = SVC(kernel='rbf', C=1e6, gamma=1.5, coef0=1., verbose=False)
    clf.fit(X, Y)
#     hw8.plotGraph(X, Y, clf, func)
    e_in = hw8.classificationError(Y, clf.predict(X))
    if e_in > 0.0:
      print e_in
      notSep += 1
  print notSep, notSep/float(runs)
def problem13():
    runs = 1000
    notSep = 0
    func = SimFunction()
    for run in range(runs):
        points = 100
        v = VSet(Dataset(points), func)
        #   writeV(points)
        #   v = readV()
        X = v.X[:, 1:]
        Y = v.Y
        clf = SVC(kernel='rbf', C=1e6, gamma=1.5, coef0=1., verbose=False)
        clf.fit(X, Y)
        #     hw8.plotGraph(X, Y, clf, func)
        e_in = hw8.classificationError(Y, clf.predict(X))
        if e_in > 0.0:
            print e_in
            notSep += 1
    print notSep, notSep / float(runs)