Beispiel #1
0
def gaussian_vector_rbf(a, b, x, eps):
    '''VECTOR Gaussian radial basis function. Equals `1/exp(b - a)` in point `(a+b)/2` and 0 far from it.'''
    center = (a + b) / 2
    edge = b - a
    dx = x - center
    edge_length = vector_norm(edge)
    norm = np.exp(edge_length)
    r = vector_norm(dx)
    return (edge / edge_length) * np.exp(-(eps * r) ** 2) / edge_length / norm
Beispiel #2
0
def edge_cos(a, b, x, eps):
    '''REAL cosine similarity fuction with fade out. For `c=(a+b)/2` equals `cos_sim(x-c, b-c)/exp(norm(x-c))`. Positive where `ab` and `x` lay in the same semispace.'''
    c = (a + b) / 2
    dx = x - c
    cs = cos(dx.reshape(1, -1), (b - c).reshape(1, -1))[0][0]
    val = cs / np.exp(vector_norm(dx))
    return val
Beispiel #3
0
    def f(x, eps=0.05):
        # step 1. Get closest edge points
    
#         # todo: build an index
#         cl = SortedList()
#         for e, length in cut:
#             a, b = G.nodes[e[0]].value, G.nodes[e[1]].value
#             center = (a + b) / 2
#             if G.nodes[e[0]]._class > G.nodes[e[1]]._class: a, b = b, a
            
#             vect = (b - a) / vector_norm(b - a)
#             d = np.dot(center - x, center - x)
#             cl.add((d, vect))


        vs, cs, rs = set(), SortedList(), SortedList()
        tops, hops = edge_index.search_nsw_basic(x, vs, cs, rs, top=100)
        summ = 0.
        vectors = np.zeros(edge_index.nodes[0].value.shape)
        for d, idx in tops:
            if d > eps: break    
            center = edge_index.nodes[idx].value
            edge = edge_index.nodes[idx]._class
            a, b = G.nodes[edge[0]].value, G.nodes[edge[1]].value
            if G.nodes[edge[0]]._class > G.nodes[edge[1]]._class: a, b = b, a
            vect = vect = (b - a) / vector_norm(b - a)
            # c = 1
            c = 2 / (1 + d / eps) - 1
            vectors += c * vect
            summ += 1
        if summ > 0:
            vectors /= summ
        return vectors
Beispiel #4
0
def gen(N=400, border=0.8):
    values = []
    for i in range(N):
        p = np.array([1 - 2 * random.random(), 1 - 2 * random.random()])
        cls = 1 if vector_norm(p) > border else 0
        values.append((p, cls))
    return values
Beispiel #5
0
def gen2(N=400, border1=0.3, border2=0.9):
    values = []
    for i in range(N):
        p = np.array([1 - 2 * random.random(), 1 - 2 * random.random()])
        cls = 1 if border2 > vector_norm(p) > border1 else 0
        values.append((p, cls))
    return values
Beispiel #6
0
def gen_kd(N=400, k=3, border=0.3, noise=0.0):
    values = []
    for i in range(N):
        p = np.array([1 - 2 * random.random() for _ in range(k)])
        cls = 1 if vector_norm(p) > border else 0
        if random.random() < noise:
            cls = 1 - cls
        values.append((p, cls))
    return values
Beispiel #7
0
def gaussian_rbf(p, x, eps):
    '''REAL Gaussian radial basis function. Equals 1 in `p` and 0 far from `p` '''
    r = vector_norm(p - x)
    return np.exp(-(eps * r) ** 2)
      frac = (x-Greens_x[index_x_old])/(Greens_x[index_x_old+1]-Greens_x[index_x_old])

      # Cubic interpolation for all values of z
      lowx_vals = Greens_G_th_Spline[index_x_old](lnz_arr)
      highx_vals = Greens_G_th_Spline[index_x_old+1](lnz_arr)

      G_th[index_x_new,:] = (lowx_vals*(1.-frac)+highx_vals*frac)
      G_th[index_x_new,:] *= bb_vis*1.e-8
      G_th[index_x_new,:] += DI_T_shift[index_x_new,:]*1.e-8
      #G_th[index_x_new,:] += Gdist[index_x_new]*df_g
    except:
      raise ValueError("{} is not in the file range [{},{}] for file '{}'".format(x,Greens_x[0],Greens_x[-1],readfile))

  # Begin orthonormlization
  # Y distortion
  e_Y = Ydist/vector_norm(Ydist)
  M_Y = np.dot(e_Y,Mdist)
  G_Y = np.dot(e_Y,Gdist)
  # Mu distortion
  Mperp = Mdist-M_Y*e_Y
  e_M = Mperp/vector_norm(Mperp)
  G_M = np.dot(e_M,Gdist)
  # G distortion
  Gperp = Gdist-G_Y*e_Y-G_M*e_M
  e_G = Gperp/vector_norm(Gperp)

  f_g = np.zeros(Nz_arr)
  f_mu = np.zeros(Nz_arr)
  f_y = np.zeros(Nz_arr)
  # Now, factorize G into orthonormal subspace
  for index_z in range(Nz_arr):