Exemple #1
0
def hartigans(data, k, ordered_select=True, init=None):

    n = data.shape[0]

    if init is None:
        partitions = np.random.choice(k, size=(n))
    else:
        partitions = init

    means = np.array(
        [np.mean(data[np.where(partitions == i)], axis=0) for i in range(k)])

    sizes = np.bincount(partitions)
    assert len(sizes) == k

    i = 0

    while True:
        n_updates = 0

        if not ordered_select:
            index_order = np.permutation(range(n))
        else:
            index_order = range(n)

        for j in index_order:
            b = partitions[j]
            if (sizes[b] - 1 == 0):
                continue

            phi_old = sizes[b] / (sizes[b] - 1) * np.sum(
                np.square((means[b] - data[j])))
            costs = []
            for l in range(k):
                if l != b:
                    phi_new = sizes[l] / (sizes[l] + 1) * np.sum(
                        np.square((means[l] - data[j])))
                    costs.append((phi_old - phi_new, l))

            (cost_new, l) = max(costs)

            if cost_new >= 0:
                partitions[j] = l
                means[l] = (sizes[l] * means[l] + data[j]) / (sizes[l] + 1)
                means[b] = (sizes[b] * means[b] - data[j]) / (sizes[b] - 1)
                sizes[l] += 1
                sizes[b] -= 1
                n_updates += 1

        i += 1
        #print ('{}: {} changes'.format(i, n_updates))

        if n_updates == 0:
            break

    return means, partitions
Exemple #2
0
 def reader(data_dir):
     texts = []
     labels = []
     for _file in glob('{}/pos/*'.format(data_dir)):
         texts.append(open(_file).read().strip())
         labels.append(1)
     for _file in glob('{}/neg/*'.format(data_dir)):
         texts.append(open(_file).read().strip())
         labels.append(0)
     # mix the positives and negatives
     perm = np.arange(len(texts))
     perm = np.permutation(perm)
     return np.array(texts)[perm], np.array(labels)[perm]
    def __call__(self, img, hflip=True, rotate=False):

        tsfms = []
        if hflip:
            tsfms.append(transforms.RandomHorizontalFlip(p=0.5))

        if rotate:
            degrees = np.array([d * 30 for d in range(12)])
            degree = Random.choice(degrees)
            tsfms.append(
                transforms.RandomRotation(degree,
                                          resample=False,
                                          expand=False,
                                          center=True))

        tsfms = Compose(np.permutation(tsfms))  # 随机打乱后组合
        img = tsfms(img)
        return img
Exemple #4
0
def sign(x):
    if x > 0:
        return 1
    else:
        return -1


train_sample = train_Y.shape[0]

error_num = train_sample
Error = error_num

Error_sum = 0

for n in range(2000):
    permutation = np.permutation(train_sample)
    train_X = train_X[permutation, :]
    train_Y = np.array(train_Y[permutation])

    #開始進行迭代
    Error = train_sample
    for i in range(50):
        error_num = 0
        w_save = w
        for j in range(train_sample):
            if sign(np.dot(w, train_X[j].T)) != train_Y[j]:
                w = w + train_Y[j] * train_X[j]
                for k in range(train_sample):
                    if sign(np.dot(w, train_X[k].T)) != train_Y[k]:
                        error_num += 1
                break
Exemple #5
0
空间复杂度: 1
稳定性: 不稳定

思路: 
for i in [0,len-1):
    记录当前 i 所在位置的值 List[i]
    搜索序列空间[i,len),逐个与List[i]进行对比,找出最小值,并与List[i]交换,并重复过程直到结束.
'''


class Solution:
    def selectSort(self, ls: List[int], replace=False) -> List[int]:
        if replace == False:
            nums = ls.copy()
        else:
            nums = ls
        Len = len(nums)
        if Len <= 1:
            return Len
        for idx in range(Len - 1):
            minIdx = idx
            for j in range(idx, Len):
                minIdx = j if nums[minIdx] > nums[j] else minIdx
            minIdx, idx = idx, minIdx
        return nums


ls = permutation([i for i in range(20)])
print(f'original list:{ls}')
print(f'sorted list:{Solution().selectSort(ls)}')
Exemple #6
0
def PNU_SL(x,
           y,
           prior,
           eta_list,
           n_fold=5,
           model='gauss',
           sigma_list=None,
           lambda_list=np.logspace(-3, 1, 10),
           n_basis=200,
           nargout=2):

    x_p, x_n, x_u = x[y == +1, :], x[y == -1, :], x[y == 0, :]
    n_p, n_n, n_u = x_p.shape[0], x_n.shape[0], x_u.shape[0]

    if model == 'gauss':
        b = np.minimum(n_basis, n_u)
        center_index = np.permutation(n_u)[:b]
        x_c = x_u[center_index, :]
        d_p, d_n, d_u = sqdist(x_p, x_c), sqdist(x_n, x_c), sqdist(x_u, x_c)
        if sigma_list is None:
            med = np.median(d_u.ravel())
            sigma_list = np.sqrt(med) * np.logspace(-1, 1, 11)
    elif model == 'lm':
        b = x.shape[1] + 1
        x_c = None
        d_p, d_n, d_u = np.c_[x_p, np.ones(n_p)], np.c_[x_n, np.ones(n_n)], \
                        np.c_[x_u, np.ones(n_u)]
        sigma_list = [1]
    else:
        raise Exception('Invalid model')

    cv_index_p = (np.arange(n_p, dtype=np.int_) * n_fold) // n_p
    cv_index_p = cv_index_p[np.random.permutation(n_p)]
    cv_index_n = (np.arange(n_n, dtype=np.int_) * n_fold) // n_n
    cv_index_n = cv_index_n[np.random.permutation(n_n)]
    cv_index_u = (np.arange(n_u, dtype=np.int_) * n_fold) // n_u
    cv_index_u = cv_index_u[np.random.permutation(n_u)]

    etab = calc_etab(n_p, n_n, prior)

    score_cv_fold = np.empty(
        (len(sigma_list), len(lambda_list), len(eta_list), n_fold))
    if len(eta_list) == 1 and len(sigma_list) == 1 and \
       len(lambda_list) == 1:
        score_cv = np.emepty((1, 1, 1))
        score_cv = -np.inf
    else:
        for ite_sigma, sigma in enumerate(sigma_list):
            K_p, K_n, K_u = ker(d_p, d_n, d_u, sigma, model)

            for ite_fold in range(n_fold):
                H_ptr, H_ntr, H_utr, h_ptr, h_ntr, h_utr = pre_solve(
                    K_p[cv_index_p != ite_fold, :],
                    K_n[cv_index_n != ite_fold, :],
                    K_u[cv_index_u != ite_fold, :], prior)

                K_pte = K_p[cv_index_p == ite_fold, :]
                K_nte = K_n[cv_index_n == ite_fold, :]
                K_ute = K_u[cv_index_u == ite_fold, :]

                for ite_eta, eta in enumerate(eta_list):
                    for ite_lambda, lam in enumerate(lambda_list):
                        theta_cv = solve(H_ptr, H_ntr, H_utr, h_ptr, h_ntr,
                                         h_utr, lam, eta, model, b)
                        gp, gn, gu = K_pte.dot(theta_cv), K_nte.dot(theta_cv), \
                                     K_ute.dot(theta_cv)
                        score_cv_fold[ite_sigma, ite_lambda, ite_eta, ite_fold] \
                            = calc_risk(gp, gn, gu, prior, etab)

        score_cv = np.mean(score_cv_fold, axis=3)

    score_list = np.empty(len(eta_list))
    score_best = np.inf
    funcs = []
    for ite_eta, eta in enumerate(eta_list):
        sub_score_cv = score_cv[:, :, ite_eta]
        tmp = np.argmin(sub_score_cv.ravel())
        tmp = np.unravel_index(tmp, sub_score_cv.shape)
        sigma_index, lambda_index = tmp[0], tmp[1]
        score_list[ite_eta] = sub_score_cv[sigma_index, lambda_index]
        if score_list[ite_eta] < score_best:
            score_best = score_list[ite_eta]
            best_sigma_index = sigma_index
            best_lambda_index = lambda_index
            best_eta_index = ite_eta
        if nargout == 3:
            sigma, lam = sigma_list[sigma_index], lambda_list[lambda_index]
            K_p, K_n, K_u = ker(d_p, d_n, d_u, sigma, model)
            H_p, H_n, H_u, h_p, h_n, h_u = pre_solve(K_p, K_n, K_u, prior)
            theta = solve(H_p, H_n, H_u, h_p, h_n, h_u, lam, eta, model, b)
            funcs.append(partial(make_func, theta, x_c, sigma, model))

    sigma, lam = sigma_list[best_sigma_index], lambda_list[best_lambda_index]
    eta = eta_list[best_eta_index]

    if nargout > 1:
        outs = {
            'sigma_index': best_sigma_index,
            'lambda_index': best_lambda_index,
            'eta_index': best_eta_index,
            'score_table': score_cv,
            'score_list': score_list,
            'w': theta
        }
    if nargout < 3:
        K_p, K_n, K_u = ker(d_p, d_n, d_u, sigma, model)
        H_p, H_n, H_u, h_p, h_n, h_u = pre_solve(K_p, K_n, K_u, prior)
        theta = solve(H_p, H_n, H_u, h_p, h_n, h_u, lam, eta, model, b)
        f_dec = partial(make_func, theta, x_c, sigma, model)
        if nargout == 1:
            return f_dec
        else:
            return f_dec, outs
    if nargout > 2:
        f_dec = funcs[best_eta_index]
        return f_dec, outs, funcs
Exemple #7
0
import numpy as np
import matplotlib.pyplot as plt


class Sort:
    def insertSort(self, ls):
        arr = ls.copy()
        Len = len(arr)
        if Len <= 1:
            return arr
        for i in range(1, Len - 1):
            curVal = arr[i]
            j = i - 1
            while j >= 0 and arr[j] > curVal:
                arr[j + 1] = arr[j]
            arr[j + 1] = curVal
        return arr


arr = np.permutation(np.arange(10))
print(Solution().insertSort(arr))
# def check():
#     for i in range(10):