Example #1
0
def nns(X_train, y_train, X_valid, y_valid0, X_test, y_test0, norm_y, nets_num, hypers, seed):

    y_valid = y_valid0
    y_test = y_test0

    seed = (seed - 1) * nets_num + 1
    # seed is public
    print('random seed range (inclusive)', seed, nets_num - 1 + seed)
    for inet in range(nets_num):
        # inet+seed - random seed
        iy_pre, iy_val = nn(X_train, y_train, X_valid, y_valid, X_test, y_test, inet + seed, hypers)
        if inet == 0:
            y_test_pre = iy_pre
            y_valid_pre = iy_val
        else:
            y_test_pre = pnp.hstack([y_test_pre, iy_pre])
            y_valid_pre = pnp.hstack([y_valid_pre, iy_val])
        print(y_test_pre.shape, y_valid_pre.shape)
    
    y_test_pre = pnp.mean(y_test_pre, axis = 1)
    # keepdim
    y_test_pre = pnp.reshape(y_test_pre, (y_test_pre.shape[0], 1))
    y_test = inverse_scale(y_test, norm_y)
    y_test_pre = inverse_scale(y_test_pre, norm_y)
    
    y_valid_pre = pnp.mean(y_valid_pre, axis = 1)
    # keepdim
    y_valid_pre = pnp.reshape(y_valid_pre, (y_valid_pre.shape[0], 1))
    y_valid = inverse_scale(y_valid, norm_y)
    y_valid_pre = inverse_scale(y_valid_pre, norm_y)
    return R2(y_test, y_test_pre), y_test_pre
Example #2
0
def dice_sim_matrix_po(X):
    sumX = pnp.sum(X, axis = 1)
    sumX = pnp.reshape(pnp.tile(sumX, len(X)), (len(X), len(X)))
    sumX = sumX + pnp.transpose(sumX)
    cmpX = pnp.zeros((len(X), len(X)))
    for i in range(len(X)):
        cmpX[i] = pnp.sum(X * pnp.reshape(pnp.tile(X[i], len(X)), (len(X), len(X[0]))), axis = 1)
    result = 2 * cmpX * myreciprocal(sumX * sumX, 0.0)
    result = sumX * result
    return result
Example #3
0
def scale(x, max_abs):
    #x = x / np.dot(np.ones((x.shape[0], 1)), max_abs)
    x_ones = pnp.ones((x.shape[0], 1))    
    sca = pnp.dot(x_ones, max_abs)
    tar_shape = x.shape
    #x = x * pnp.reciprocal(sca)
    batch_size = 20
    tmp_x = [(x[i:i+batch_size, :] * pp.reciprocal(sca[i:i+batch_size, :])) for i in range(0, x.shape[0], batch_size)]
    
    if(len(tmp_x) == 1):
        x = pp.farr(tmp_x)
        x = pnp.reshape(x, tar_shape)
    else:
        x = pnp.vstack(tmp_x)
        x = pnp.reshape(x, tar_shape)

    print("!!!!!!!!!1", x.shape)
    return x
Example #4
0
def jaccard_sim_po(mat):
    intersect = pnp.dot(mat, pnp.transpose(mat))
    union = pnp.sum(mat, axis = 1)
    union = pnp.reshape(pnp.tile(union, len(mat)), (len(mat), len(mat)))
    union = union + pnp.transpose(union) - intersect + 0.0
    sim = intersect * myreciprocal(union,0.0)
    for i in range(sim.shape[0]):
        sim[i, i] = pp.sfixed(1.0)
    return sim
Example #5
0
def maxabsscaler(x):

    batch = 20
    max_abs = pp.farr([pnp.max(pnp.abs(x[i:i+batch,:]), axis=0) for i in range(0, x.shape[0], batch)])
    if (len(max_abs) == 1):
        max_abs = pp.farr(max_abs)
    else:
        max_abs = pnp.vstack(pp.farr(max_abs))

    max_abs = pnp.max(max_abs, axis = 0)
    m_tmp = pnp.ravel(max_abs)
    
    #max_abs[max_abs == 0.0] = 1.0
    for i in range(len(m_tmp)):
        flag_zero = (m_tmp[i] < 1e-8)
        m_tmp[i] = m_tmp[i]*(1-flag_zero) + 1.0*flag_zero

    max_abs = pnp.reshape(m_tmp, (1, -1))

    return max_abs