Пример #1
0
def Kmean_MEDA(Xsource, Ysource, Xtarget, Ytarget):
    '''
    :param Xs: a numpy array size ns*m
    :param Ys: a numpy array size 1*ns
    :param Xt: a numpy array size nt*m
    :param Yt: a numpy array size 1*nt
    :return: accuracy
    '''
    global ns, nt, C, Xs, Ys, Xt, Yt, YY, K, A, e, M0, L, archive, sim
    Xs = Xsource
    Ys = Ysource
    Xt = Xtarget
    Yt = Ytarget
    ns, nt, m = Xs.shape[0], Xt.shape[0], Xs.shape[1]
    C = len(np.unique(Ys))

    # Transform data using gfk
    gfk = GFK.GFK(dim=dim)
    _, Xs_new, Xt_new = gfk.fit(Xs, Xt)
    Xs_new, Xt_new = Xs_new.T, Xt_new.T
    X = np.hstack((Xs_new, Xt_new))
    X /= np.linalg.norm(X, axis=0)
    Xs = X[:, :ns].T
    Xt = X[:, ns:].T
    X = X.T

    # perform clustering
    kmeans = cluster.KMeans(n_clusters=C, random_state=0, max_iter=200).fit(X)
    source_cluster_kmeans = kmeans.labels_[:ns]
    # dbscan = cluster.DBSCAN(eps=3, min_samples=3).fit(X)
    # source_cluster_dbscan = dbscan.labels_[:ns]
    aggc = cluster.AgglomerativeClustering(n_clusters=C,
                                           linkage='complete').fit(X)
    source_cluster_aggc = aggc.labels_[:ns]
    print 'test'
Пример #2
0
    def fit_predict(self, Xs, Ys, Xt, Yt):
        '''
        Transform and Predict
        :param Xs: ns * n_feature, source feature
        :param Ys: ns * 1, source label
        :param Xt: nt * n_feature, target feature
        :param Yt: nt * 1, target label
        :return: acc, y_pred, list_acc
        '''
        gfk = GFK.GFK(dim=self.dim)
        _, Xs_new, Xt_new = gfk.fit(Xs, Xt)
        Xs_new, Xt_new = Xs_new.T, Xt_new.T
        X = np.hstack((Xs_new, Xt_new))
        n, m = Xs_new.shape[1], Xt_new.shape[1]
        C = len(np.unique(Ys))
        list_acc = []
        YY = np.zeros((n, C))
        for c in range(1, C + 1):
            ind = np.where(Ys == c)
            YY[ind, c - 1] = 1
        YY = np.vstack((YY, np.zeros((m, C))))
        YY[0, 1:] = 0

        X /= np.linalg.norm(X, axis=0)
        L = 0  # Graph Laplacian is on the way...
        knn_clf = KNeighborsClassifier(n_neighbors=1)
        knn_clf.fit(X[:, :n].T, Ys.ravel())
        Cls = knn_clf.predict(X[:, n:].T)
        K = kernel(self.kernel_type, X, X2=None, gamma=self.gamma)
        E = np.diagflat(np.vstack((np.ones((n, 1)), np.zeros((m, 1)))))
        for t in range(1, self.T + 1):
            mu = self.estimate_mu(Xs_new.T, Ys, Xt_new.T, Cls)
            e = np.vstack((1 / n * np.ones((n, 1)), -1 / m * np.ones((m, 1))))
            M = e * e.T * C
            N = 0
            for c in range(1, C + 1):
                e = np.zeros((n + m, 1))
                tt = Ys == c
                e[np.where(tt == True)] = 1 / len(Ys[np.where(Ys == c)])
                yy = Cls == c
                ind = np.where(yy == True)
                inds = [item + n for item in ind]
                e[tuple(inds)] = -1 / len(Cls[np.where(Cls == c)])
                e[np.isinf(e)] = 0
                N += np.dot(e, e.T)
            M = (1 - mu) * M + mu * N
            M /= np.linalg.norm(M, 'fro')
            left = np.dot(E + self.lamb * M + self.rho * L,
                          K) + self.eta * np.eye(n + m, n + m)
            Beta = np.dot(np.linalg.inv(left), np.dot(E, YY))
            F = np.dot(K, Beta)
            Cls = np.argmax(F, axis=1) + 1
            Cls = Cls[n:]
            acc = np.mean(Cls == Yt.ravel())
            list_acc.append(acc)
            print('MEDA iteration [{}/{}]: mu={:.2f}, Acc={:.4f}'.format(
                t, self.T, mu, acc))
        return acc, Cls, list_acc
Пример #3
0
def evolve(Xsource, Ysource, Xtarget, Ytarget):
    """
    Running GA algorithms, where each individual is a set of target pseudo labels.
    :return: the best solution of GAs.
    """
    global ns, nt, C, Xs, Ys, Xt, Yt, YY, K, A, e, M0, L, archive
    archive_label = []
    archive_beta = []
    Xs = Xsource
    Ys = Ysource
    Xt = Xtarget
    Yt = Ytarget
    ns, nt = Xs.shape[0], Xt.shape[0]
    C = len(np.unique(Ys))

    # Transform data using gfk
    gfk = GFK.GFK(dim=dim)
    _, Xs_new, Xt_new = gfk.fit(Xs, Xt)
    Xs_new, Xt_new = Xs_new.T, Xt_new.T
    X = np.hstack((Xs_new, Xt_new))
    X /= np.linalg.norm(X, axis=0)
    Xs = X[:, :ns].T
    Xt = X[:, ns:].T

    # now run the meda
    meda = MEDA.meda()

    # build some matrices that are not changed
    K = kernel(kernel_type, X, X2=None, gamma=gamma)
    A = np.diagflat(np.vstack((np.ones((ns, 1)), np.zeros((nt, 1)))))
    e = np.vstack((1.0 / ns * np.ones((ns, 1)), -1.0 / nt * np.ones((nt, 1))))
    M0 = e * e.T * C
    L = laplacian_matrix(X.T, p)

    YY = np.zeros((ns, C))
    for c in range(1, C + 1):
        ind = np.where(Ys == c)
        YY[ind, c - 1] = 1
    YY = np.vstack((YY, np.zeros((nt, C))))

    # parameters for GA
    N_BIT = nt
    N_GEN = 10
    N_IND = 10
    MUTATION_RATE = 1.0 / N_BIT
    CXPB = 0.2
    MPB = 0.8

    pos_min = 1
    pos_max = C

    creator.create("FitnessMin", base.Fitness, weights=(-1.0, ))
    creator.create("Ind", list, fitness=creator.FitnessMin, beta=list)
    creator.create("Pop", list)

    # for creating the population
    toolbox.register("bit", random.randint, a=pos_min, b=pos_max)
    toolbox.register("ind",
                     tools.initRepeat,
                     creator.Ind,
                     toolbox.bit,
                     n=N_BIT)
    toolbox.register("pop", tools.initRepeat, creator.Pop, toolbox.ind)
    # for genetic operators
    toolbox.register("select", tools.selTournament, tournsize=3)
    toolbox.register("crossover", tools.cxUniform, indpb=0.5)
    toolbox.register("mutate",
                     tools.mutUniformInt,
                     low=pos_min,
                     up=pos_max,
                     indpb=MUTATION_RATE)
    # pool = multiprocessing.Pool(4)
    # toolbox.register("map", pool.map)

    # initialize some individuals by predefined classifiers
    pop = toolbox.pop(n=N_IND)

    classifiers = list([])
    classifiers.append(KNeighborsClassifier(1))
    classifiers.append(
        SVC(kernel="linear", C=0.025, random_state=np.random.randint(2**10)))
    classifiers.append(
        GaussianProcessClassifier(1.0 * RBF(1.0),
                                  random_state=np.random.randint(2**10)))
    classifiers.append(KNeighborsClassifier(3))
    classifiers.append(
        SVC(kernel="rbf", C=1, gamma=2, random_state=np.random.randint(2**10)))
    classifiers.append(
        DecisionTreeClassifier(max_depth=5,
                               random_state=np.random.randint(2**10)))
    classifiers.append(KNeighborsClassifier(5))
    classifiers.append(GaussianNB())
    classifiers.append(
        RandomForestClassifier(max_depth=5,
                               n_estimators=10,
                               random_state=np.random.randint(2**10)))
    classifiers.append(
        AdaBoostClassifier(random_state=np.random.randint(2**10)))

    step = N_IND / len(classifiers)
    for ind_index, classifier in enumerate(classifiers):
        classifier.fit(Xs, Ys)
        Yt_pseu = classifier.predict(Xt)
        for bit_idex, value in enumerate(Yt_pseu):
            pop[ind_index * step][bit_idex] = value

    # for index, value in enumerate(Yt):
    #     pop[len(pop)-1][index] = Yt[index]

    # now initialize the beta
    # Beta, fitness, MMD, SRM, acc_s, acc_t, Yt_pseu
    for ind in pop:
        beta, fitness, _, _, _, _, Yt_pseu = fit_predict_label(ind)
        ind.beta = beta
        ind.fitness.values = fitness,
        for idx in range(len(ind)):
            ind[idx] = Yt_pseu[idx]

    hof = tools.HallOfFame(maxsize=1)
    hof.update(pop)

    for g in range(N_GEN):
        # print("=========== Iteration %d ===========" % g)
        # selection
        offspring_label = toolbox.select(pop, len(pop))
        offspring_label = map(toolbox.clone, offspring_label)

        # applying crossover
        for c1, c2 in zip(offspring_label[::2], offspring_label[1::2]):
            if np.random.rand() < CXPB:
                toolbox.crossover(c1, c2)
                del c1.fitness.values
                del c2.fitness.values

        # applying mutation
        for mutant in offspring_label:
            if np.random.rand() < MPB:
                # retain the fitness value of parent to children
                toolbox.mutate(mutant)
                del mutant.fitness.values

        inv_inds = [ind for ind in offspring_label if not ind.fitness.valid]
        for inv_ind in inv_inds:
            beta, fitness, _, _, _, _, Yt_pseu = fit_predict_label(inv_ind)
            inv_ind.beta = beta
            inv_ind.fitness.values = fitness,
            for idx in range(len(inv_ind)):
                inv_ind[idx] = Yt_pseu[idx]

        # Now using beta phase to evolve the current pop
        offspring_beta = map(toolbox.clone, pop)
        for ind in offspring_beta:
            beta, fitness, _, _, _, _, Yt_pseu = fit_predict_beta(ind.beta)
            ind.beta = beta
            ind.fitness.values = fitness,
            for idx in range(len(inv_ind)):
                inv_ind[idx] = Yt_pseu[idx]

        for idx in range(len(pop)):
            ind = pop[idx]
            ind_label = offspring_label[idx]
            ind_beta = offspring_beta[idx]
            if ind.fitness.values <= ind_label.fitness.values and ind.fitness.values <= ind_beta.fitness.values:
                # if both approaches do not improve the fitness, indicate a local optima
                # create a new one
                new_label = re_initialize()
                new_beta, new_fitness, _, _, _, _, new_label = fit_predict_label(
                    new_label)
                # store the current one to archive
                archive_beta.append(ind.beta)
                pseudo_labels = [ind[ins_idx] for ins_idx in range(len(ind))]
                archive_label.append(pseudo_labels)
                # assign the new one to the current individual
                for bit_index in range(len(new_label)):
                    pop[idx][bit_index] = new_label[bit_index]
                pop[idx].beta = new_beta
                pop[idx].fitness.values = new_fitness,
            elif ind.fitness.values > ind_label.fitness.values and ind_beta.fitness.values > ind_label.fitness.values:
                # the beta step improve the fitness
                pop[idx] = toolbox.clone(ind_label)
            elif ind.fitness.values > ind_beta.fitness.values and ind_label.fitness.values > ind_beta.fitness.values:
                pop[idx] = toolbox.clone(ind_beta)

        hof.update(pop)
        best_ind = tools.selBest(pop, 1)[0]
        # print("Best fitness %f " % best_ind.fitness.values)

    # print("=========== Final result============")
    best_ind = tools.selBest(pop, 1)[0]
    _, _, _, _, acc_s, acc_t, Yt_pseu = fit_predict_label(best_ind)
    print("Accuracy of the best individual: source - %f target - %f " %
          (acc_s, acc_t))

    labels = []
    for ind in pop:
        _, _, _, _, _, _, Yt_pseu = fit_predict_label(ind)
        labels.append(Yt_pseu)
    labels = np.array(labels)
    vote_label = voting(labels)
    acc = np.mean(vote_label == Yt)
    print("Accuracy of the population: %f" % acc)

    # Use the archive of beta
    labels = []
    for beta in archive_beta:
        _, _, _, _, _, _, Yt_pseu = fit_predict_beta(beta)
        labels.append(Yt_pseu)
    labels = np.array(labels)
    vote_label = voting(labels)
    acc = np.mean(vote_label == Yt)
    print("Accuracy of the archive beta: %f" % acc)

    # Use the archive of label
    labels = []
    for label in archive_label:
        _, _, _, _, _, _, Yt_pseu = fit_predict_label(label)
        labels.append(Yt_pseu)
    labels = np.array(labels)
    vote_label = voting(labels)
    acc = np.mean(vote_label == Yt)
    print("Accuracy of the archive label: %f" % acc)
Пример #4
0
def evolve(Xsource, Ysource, Xtarget, Ytarget, file, mutation_rate, full_init, dim_p=20, eta_p=0.1):
    """
    Running GA algorithms, where each individual is a set of target pseudo labels.
    :return: the best solution of GAs.
    """
    exe_time = 0
    start = time.time()
    global ns, nt, C, Xs, Ys, Xt, Yt, YY, K, A, e, M0, L, dim, eta
    dim = dim_p
    eta = eta_p
    archive = []
    Xs = Xsource
    Ys = Ysource
    Xt = Xtarget
    Yt = Ytarget
    ns, nt = Xs.shape[0], Xt.shape[0]
    C = len(np.unique(Ys))

    # Transform data using gfk
    gfk = GFK.GFK(dim=dim)
    _, Xs_new, Xt_new = gfk.fit(Xs, Xt)
    Xs_new, Xt_new = Xs_new.T, Xt_new.T
    X = np.hstack((Xs_new, Xt_new))
    X /= np.linalg.norm(X, axis=0)
    Xs = X[:, :ns].T
    Xt = X[:, ns:].T

    # perform instance selection
    # kmm = KMM.KMM(kernel_type='rbf', gamma=0.5)
    # ins_weight = kmm.fit(Xs, Xt)
    # ins_weight = np.array(ins_weight)
    # w_mean = np.mean(ins_weight)
    # mask = np.ravel(ins_weight > w_mean)
    # Xs = Xs[mask]
    # Ys = Ys[mask]
    # print('Before selection: '+str(ns))
    # ns = len(Ys)
    # print('After selection: '+str(ns))
    # X = np.hstack((Xs.T, Xt.T))
    # X /= np.linalg.norm(X, axis=0)
    # Xs = X[:, :ns].T
    # Xt = X[:, ns:].T

    # do not using any feature transformation
    # X = np.hstack((Xs.T, Xt.T))
    # X /= np.linalg.norm(X, axis=0)
    # Xs = X[:, :ns].T
    # Xt = X[:, ns:].T

    # coral = CORAL.CORAL()
    # Xs = coral.fit(Xs, Xt)
    # X = np.hstack((Xs.T, Xt.T))
    # X /= np.linalg.norm(X, axis=0)
    # Xs = X[:, :ns].T
    # Xt = X[:, ns:].T

    # tca = TCA.TCA(kernel_type='linear', dim=dim, lamb=1, gamma=1.0)
    # Xs_new, Xt_new = tca.fit(Xs, Xt)
    # X = np.hstack((Xs_new.T, Xt_new.T))
    # X /= np.linalg.norm(X, axis=0)
    # Xs = X[:, :ns].T
    # Xt = X[:, ns:].T

    # build some matrices that are not changed
    K = kernel(kernel_type, X, X2=None, gamma=gamma)
    A = np.diagflat(np.vstack((np.ones((ns, 1)), np.zeros((nt, 1)))))
    e = np.vstack((1.0 / ns * np.ones((ns, 1)), -1.0 / nt * np.ones((nt, 1))))
    M0 = e * e.T * C
    exe_time += time.time()-start

    L = Helpers.laplacian_matrix(X.T, p)

    start = time.time()
    YY = np.zeros((ns, C))
    for c in range(1, C + 1):
        ind = np.where(Ys == c)
        YY[ind, c - 1] = 1
    YY = np.vstack((YY, np.zeros((nt, C))))

    pos_min = 1
    pos_max = C

    # parameters for GA
    N_BIT = nt
    MUTATION_RATE = 1.0/N_BIT*C
    MPB = mutation_rate
    CXPB = 1-mutation_rate

    creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
    creator.create("Ind", list, fitness=creator.FitnessMin)
    creator.create("Pop", list)

    # for creating the population
    toolbox.register("bit", random.randint, a=pos_min, b=pos_max)
    toolbox.register("ind", tools.initRepeat, creator.Ind, toolbox.bit, n=N_BIT)
    toolbox.register("pop", tools.initRepeat, creator.Pop, toolbox.ind)
    # for evaluation
    toolbox.register("evaluate", fitness_evaluation)
    # for genetic operators
    toolbox.register("select", tools.selTournament, tournsize=2)
    toolbox.register("crossover", tools.cxUniform, indpb=0.5)
    toolbox.register("mutate", tools.mutUniformInt, low=pos_min, up=pos_max,
                     indpb=MUTATION_RATE)
    # pool = multiprocessing.Pool(4)
    # toolbox.register("map", pool.map)
    # initialize some individuals by predefined classifiers
    pop = toolbox.pop(n=N_IND)

    classifiers = list([])
    classifiers.append(KNeighborsClassifier(1))
    # classifiers.append(SVC(kernel="linear", C=0.025, random_state=np.random.randint(2 ** 10)))
    # classifiers.append(GaussianProcessClassifier(1.0 * RBF(1.0), random_state=np.random.randint(2 ** 10)))
    # classifiers.append(KNeighborsClassifier(3))
    # classifiers.append(SVC(kernel="rbf", C=1, gamma=2, random_state=np.random.randint(2 ** 10)))
    # classifiers.append(DecisionTreeClassifier(max_depth=5, random_state=np.random.randint(2 ** 10)))
    # classifiers.append(KNeighborsClassifier(5))
    # classifiers.append(GaussianNB())
    # classifiers.append(RandomForestClassifier(max_depth=5, n_estimators=10, random_state=np.random.randint(2 ** 10)))
    # classifiers.append(AdaBoostClassifier(random_state=np.random.randint(2 ** 10)))

    step = N_IND/len(classifiers)
    for ind_index, classifier in enumerate(classifiers):
        classifier.fit(Xs, Ys)
        Yt_pseu = classifier.predict(Xt)
        for bit_idex, value in enumerate(Yt_pseu):
            pop[ind_index*step][bit_idex] = value

    if full_init:
        Helpers.opposite_init(pop, pos_min, pos_max)

    # evaluate the initialized populations
    fitnesses = toolbox.map(toolbox.evaluate, pop)
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit,

    hof = tools.HallOfFame(maxsize=1)
    hof.update(pop)
    exe_time = exe_time + time.time()-start

    to_write = ''
    for g in range(N_GEN):
        to_write += "*****Iteration %d*****\n" % (g+1)
        start = time.time()
        # selection
        offspring = toolbox.select(pop, len(pop))
        offspring = map(toolbox.clone, offspring)

        # applying crossover
        for c1, c2 in zip(offspring[::2], offspring[1::2]):
            if np.random.rand() < CXPB:
                toolbox.crossover(c1, c2)
                del c1.fitness.values
                del c2.fitness.values

        # applying mutation
        for mutant in offspring:
            if np.random.rand() < MPB:
                toolbox.mutate(mutant)
                del mutant.fitness.values

        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
        for ind, fitness in zip(invalid_ind, fitnesses):
                ind.fitness.values = fitness,

        # now select the best individual from offspring
        # pass it to the single step meda to refine the label
        best_inds = tools.selBest(offspring, N_IND/10)

        for best_ind in best_inds:
            Yt_pseu = label_evolve(best_ind)
            new_ind = toolbox.ind()
            for index, label in enumerate(Yt_pseu):
                new_ind[index] = label
            new_ind.fitness.values = fitness_evaluation(new_ind),
            offspring.append(new_ind)
            archive.append(new_ind)

        # The population is entirely replaced by the offspring
        exe_time = exe_time + time.time() - start
        pop[:] = tools.selBest(offspring + list(hof), len(pop))
        hof.update(pop)
        to_write += ('Average distance: %f\n' %(Helpers.pop_distance(pop)))
        to_write += ('Best fitness: %f\n' %(hof[0].fitness.values[0]))

        best_ind = tools.selBest(pop, 1)[0]
        acc = np.mean(best_ind == Yt)
        to_write += ("Accuracy of the best individual: %f\n" % acc)

        no_10p = int(0.1*N_IND)
        top10 = tools.selBest(pop, no_10p)
        vote_label = Helpers.voting(top10)
        acc = np.mean(vote_label == Yt)
        to_write += ("Accuracy of the 10%% population: %f\n" % acc)

        # Use the whole population
        vote_label = Helpers.voting(pop)
        acc = np.mean(vote_label == Yt)
        to_write += ("Accuracy of the population: %f\n" % acc)

    to_write += ("*****Final result*****\n")
    best_ind = tools.selBest(pop, 1)[0]
    acc = np.mean(best_ind == Yt)
    to_write += ("Accuracy of the best individual: %f\n" % acc)

    best_evolve = label_evolve(best_ind)
    acc = np.mean(best_evolve == Yt)
    return_acc = acc
    to_write += ("Accuracy of the evovled best individual: %f\n" % acc)

    top10 = tools.selBest(pop, 10)
    vote_label = Helpers.voting(top10)
    acc = np.mean(vote_label == Yt)
    to_write += ("Accuracy of the 10%% population: %f\n" % acc)

    # Use the whole population
    vote_label = Helpers.voting(pop)
    acc = np.mean(vote_label == Yt)
    to_write += ("Accuracy of the population: %f\n" % acc)

    # Use the archive
    vote_label = Helpers.voting(archive)
    acc = np.mean(vote_label == Yt)
    to_write += ("Accuracy of the archive: %f\n" % acc)

    to_write += ('GA-MEDA time: %f' % (exe_time)+'\n')

    if file is not None:
        file.write(to_write)
    return return_acc, best_evolve
Пример #5
0
def evolve(Xsource, Ysource, Xtarget, Ytarget):
    """
    Running GA algorithms, where each individual is a set of target pseudo labels.
    :return: the best solution of GAs.
    """
    global ns, nt, C, Xs, Ys, Xt, Yt, YY, K, A, e, M0, L, archive, sim
    Xs = Xsource
    Ys = Ysource
    Xt = Xtarget
    Yt = Ytarget
    ns, nt = Xs.shape[0], Xt.shape[0]
    C = len(np.unique(Ys))

    # Transform data using gfk
    gfk = GFK.GFK(dim=dim)
    _, Xs_new, Xt_new = gfk.fit(Xs, Xt)
    Xs_new, Xt_new = Xs_new.T, Xt_new.T
    X = np.hstack((Xs_new, Xt_new))
    X /= np.linalg.norm(X, axis=0)
    Xs = X[:, :ns].T
    Xt = X[:, ns:].T

    # build some matrices that are not changed
    K = kernel(kernel_type, X, X2=None, gamma=gamma)
    A = np.diagflat(np.vstack((np.ones((ns, 1)), np.zeros((nt, 1)))))
    e = np.vstack((1.0 / ns * np.ones((ns, 1)), -1.0 / nt * np.ones((nt, 1))))
    M0 = e * e.T * C
    L = laplacian_matrix(X.T, p)
    sim = simliarity_matrix(X.T, p)

    YY = np.zeros((ns, C))
    for c in range(1, C + 1):
        ind = np.where(Ys == c)
        YY[ind, c - 1] = 1
    YY = np.vstack((YY, np.zeros((nt, C))))

    # for testing
    knn = KNeighborsClassifier(1)
    knn.fit(Xs, Ys)
    cls = knn.predict(Xt)
    # knn.fit(Xt, cls)
    # Ys_re = knn.predict(Xs)
    # print("Reverse accuracy %f" %(np.mean(Ys_re == Ys)))
    Yt_pseu = meda(cls, T=10)
    acc = np.mean(Yt_pseu == Yt)
    print("MEDA accuracy: %f" % acc)

    # parameters for GA
    N_BIT = nt
    N_GEN = 10
    N_IND = 10
    MUTATION_RATE = 1.0 / N_BIT
    CXPB = 0.9
    MPB = 0.1

    pos_min = 1
    pos_max = C

    creator.create("FitnessMin", base.Fitness, weights=(-1.0, ))
    creator.create("Ind", list, fitness=creator.FitnessMin)
    creator.create("Pop", list)

    # for creating the population
    toolbox.register("bit", random.randint, a=pos_min, b=pos_max)
    toolbox.register("ind",
                     tools.initRepeat,
                     creator.Ind,
                     toolbox.bit,
                     n=N_BIT)
    toolbox.register("pop", tools.initRepeat, creator.Pop, toolbox.ind)
    # for evaluation
    toolbox.register("evaluate", reverse_clsasification)
    # for genetic operators
    toolbox.register("select", tools.selTournament, tournsize=3)
    toolbox.register("crossover", tools.cxUniform, indpb=0.5)
    toolbox.register("mutate",
                     tools.mutUniformInt,
                     low=pos_min,
                     up=pos_max,
                     indpb=MUTATION_RATE)
    # pool = multiprocessing.Pool(4)
    # toolbox.register("map", pool.map)

    # initialize some individuals by predefined classifiers
    pop = toolbox.pop(n=N_IND)

    classifiers = list([])
    classifiers.append(KNeighborsClassifier(1))
    classifiers.append(
        SVC(kernel="linear", C=0.025, random_state=np.random.randint(2**10)))
    classifiers.append(
        GaussianProcessClassifier(1.0 * RBF(1.0),
                                  random_state=np.random.randint(2**10)))
    classifiers.append(KNeighborsClassifier(3))
    classifiers.append(
        SVC(kernel="rbf", C=1, gamma=2, random_state=np.random.randint(2**10)))
    classifiers.append(
        DecisionTreeClassifier(max_depth=5,
                               random_state=np.random.randint(2**10)))
    classifiers.append(KNeighborsClassifier(5))
    classifiers.append(GaussianNB())
    classifiers.append(
        RandomForestClassifier(max_depth=5,
                               n_estimators=10,
                               random_state=np.random.randint(2**10)))
    classifiers.append(
        AdaBoostClassifier(random_state=np.random.randint(2**10)))

    class_labels = []
    for classifier in classifiers:
        classifier.fit(Xs, Ys)
        Yt_pseu = classifier.predict(Xt)
        class_labels.append(Yt_pseu)
    class_labels = np.asarray(class_labels)

    # calculate the accuracy of ensemble meda
    en_meda = []
    for Yt_init in class_labels:
        Yt_pseu = meda(Yt_init, T=10)
        en_meda.append(Yt_pseu)
    Yt_pseu = voting(en_meda)
    acc = np.mean(Yt_pseu == Yt)
    print("En-MEDA accuracy: %f" % acc)

    step = N_IND / len(classifiers)
    for ind_index in range(len(classifiers)):
        if ind_index * step < len(pop):
            Yt_pseu = class_labels[ind_index]
            for bit_idex, value in enumerate(Yt_pseu):
                pop[ind_index * step][bit_idex] = value
        else:
            break

    # for index, value in enumerate(Yt):
    #     pop[len(pop)-1][index] = Yt[index]

    # evaluate the initialized populations
    start_fitness = toolbox.map(toolbox.evaluate, pop)
    for ind, fit in zip(pop, start_fitness):
        ind.fitness.values = fit,

    hof = tools.HallOfFame(maxsize=1)
    hof.update(pop)

    for g in range(N_GEN):
        # print("=========== Iteration %d ===========" % g)

        # selection
        offspring = toolbox.select(pop, len(pop))
        offspring = map(toolbox.clone, offspring)

        # applying crossover
        for c1, c2 in zip(offspring[::2], offspring[1::2]):
            if np.random.rand() < CXPB:
                toolbox.crossover(c1, c2)
                del c1.fitness.values
                del c2.fitness.values
        # applying mutation
        for mutant in offspring:
            if np.random.rand() < MPB:
                toolbox.mutate(mutant)
                del mutant.fitness.values

        # evaluate all the offspring, since the evaluation creates new positions
        fits = toolbox.map(toolbox.evaluate, offspring)
        for ind_index, fit in enumerate(fits):
            ind = offspring[ind_index]
            ind.fitness.values = fit,

        # The population is entirely replaced by the offspring
        pop[:] = tools.selBest(offspring + list(hof), len(pop))
        hof.update(pop)
        best_ind = tools.selBest(pop, 1)[0]
        # print("Best fitness %f " % best_ind.fitness.values)

        # Find the distance between ind
        dist = 0
        for i1 in range(len(pop)):
            for i2 in range(len(pop)):
                dist += np.linalg.norm(np.array(pop[i1]) - np.array(pop[i2]))
        # print("Distance %f" %(dist/len(pop)**2))

    # print("=========== Final result============")

    Yt_pseu = [label for label in hof[0]]
    Yt_pseu = meda(Yt_init=Yt_pseu, T=10)
    acc = np.mean(Yt_pseu == Yt)
    print("GA-MEDA accuracy: %f" % acc)

    Yt_en = []
    for ind in pop:
        Yt_pseu = [label for label in ind]
        Yt_pseu = meda(Yt_init=Yt_pseu, T=1)
        Yt_en.append(Yt_pseu)
    Yt_en = np.array(Yt_en)
    vote_label = voting(Yt_en)
    acc = np.mean(vote_label == Yt)
    print("Ensemb(1)-GA-MEDA accuracy: %f" % acc)

    Yt_en = []
    for ind in pop:
        Yt_pseu = [label for label in ind]
        Yt_pseu = meda(Yt_init=Yt_pseu, T=10)
        Yt_en.append(Yt_pseu)
    Yt_en = np.array(Yt_en)
    vote_label = voting(Yt_en)
    acc = np.mean(vote_label == Yt)
    print("Ensemb(10)-GA-MEDA accuracy: %f" % acc)
Пример #6
0
    def __init__(self, normalize, gfk_dim):
        """
        :param normalize: whether to normalize the data or not
        :param gfk_dim: what is the dimension of gfk
        """
        # eta can be tuned later through arguments
        self.eta = 0.1

        # first load the data
        source = np.genfromtxt("Source", delimiter=",")
        self.no_features, self.no_src_instances = source.shape[
            1] - 1, source.shape[0]
        self.Xs = source[:, 0:self.no_features]
        self.Ys = np.ravel(source[:, self.no_features:self.no_features + 1])
        self.Ys = np.array([int(label) for label in self.Ys])

        target = np.genfromtxt("Target", delimiter=",")
        self.no_tar_instances = target.shape[0]
        self.Xt = target[:, 0:self.no_features]
        self.Yt = np.ravel(target[:, self.no_features:self.no_features + 1])
        self.Yt = np.array([int(label) for label in self.Yt])

        if normalize:
            self.Xs, self.Xt = Pre.normalize_data(self.Xs, self.Xt)

        # make sure the class indices start from 1
        self.no_class = len(np.unique(self.Ys))
        if self.no_class > np.max(self.Ys):
            self.Ys = self.Ys + 1
            self.Yt = self.Yt + 1

        # transform data using gfk
        gfk = GFK.GFK(dim=gfk_dim)
        _, Xs_new, Xt_new = gfk.fit(self.Xs, self.Xt)
        Xs_new, Xt_new = Xs_new.T, Xt_new.T
        X = np.hstack((Xs_new, Xt_new))
        X /= np.linalg.norm(X, axis=0)
        self.X = X.T
        self.Xs = X[:, :self.no_src_instances].T
        self.Xt = X[:, self.no_src_instances:].T

        self.YY = np.zeros((self.no_src_instances, self.no_class))
        for c in range(1, self.no_class + 1):
            ind = np.where(self.Ys == c)
            self.YY[ind, c - 1] = 1
        self.YY = np.vstack(
            (self.YY, np.zeros((self.no_tar_instances, self.no_class))))

        # build some matrices that are not changed in the evaluation
        self.K = self.kernel('rbf', self.X.T, X2=None, gamma=0.5)
        self.A = np.diagflat(
            np.vstack((np.ones((self.no_src_instances, 1)),
                       np.zeros((self.no_tar_instances, 1)))))
        e = np.vstack((1.0 / self.no_src_instances * np.ones(
            (self.no_src_instances, 1)),
                       -1.0 / self.no_tar_instances * np.ones(
                           (self.no_tar_instances, 1))))
        self.M0 = e * e.T * self.no_class

        self.L = Pre.laplacian_matrix(self.X, k=10)

        super(MultiTransferProblem, self).__init__()

        # initialize the objectives for multi-objective optimisation
        self.number_of_variables = self.no_tar_instances
        self.number_of_constraints = 0

        self.obj_labels = ['Discrepancy', 'Manifold']
        self.number_of_objectives = len(self.obj_labels)
        self.obj_directions = [
            self.MINIMIZE,
        ] * self.number_of_objectives

        # the lower bound and upper bound of the decision variables
        # are integers from 1 to the number of classes
        self.lower_bound = [1 for _ in range(self.number_of_variables)]
        self.upper_bound = [
            self.no_class for _ in range(self.number_of_variables)
        ]

        IntegerSolution.lower_bound = self.lower_bound
        IntegerSolution.upper_bound = self.upper_bound

        # now set up the initialized label vector
        self.init_label_vector = self.initialize_label_vector(mode='full')
        self.init_index = 0
Пример #7
0
    def fit_predict(self, Xs, Ys, Xt, Yt):
        '''
        Transform and Predict
        :param Xs: ns * n_feature, source feature
        :param Ys: ns * 1, source label
        :param Xt: nt * n_feature, target feature
        :param Yt: nt * 1, target label
        :return: acc, y_pred, list_acc
        '''
        gfk = GFK.GFK(dim=self.dim)
        _, Xs_new, Xt_new = gfk.fit(Xs, Xt)
        Xs_new, Xt_new = Xs_new.T, Xt_new.T
        X = np.hstack((Xs_new, Xt_new))
        ns, nt = Xs_new.shape[1], Xt_new.shape[1]
        C = len(np.unique(Ys))
        list_acc = []
        YY = np.zeros((ns, C))
        for c in range(1, C + 1):
            ind = np.where(Ys == c)
            YY[ind, c - 1] = 1
        YY = np.vstack((YY, np.zeros((nt, C))))

        X /= np.linalg.norm(X, axis=0)
        L = laplacian_matrix(X.T, self.p)
        knn_clf = KNeighborsClassifier(n_neighbors=1)
        knn_clf.fit(X[:, :ns].T, Ys.ravel())
        Cls = knn_clf.predict(X[:, ns:].T)

        K = kernel(self.kernel_type, X, X2=None, gamma=self.gamma)
        E = np.diagflat(np.vstack((np.ones((ns, 1)), np.zeros((nt, 1)))))

        e = np.vstack((1.0 / ns * np.ones((ns, 1)), -1.0 / nt * np.ones(
            (nt, 1))))
        M0 = e * e.T * C

        for t in range(1, self.T + 1):
            mu = self.estimate_mu(Xs_new.T, Ys, Xt_new.T, Cls)
            # mu = 0.5
            N = 0
            for c in range(1, C + 1):
                e = np.zeros((ns + nt, 1))
                tt = Ys == c
                e[np.where(tt == True)] = 1.0 / len(Ys[np.where(Ys == c)])
                yy = Cls == c
                ind = np.where(yy == True)
                inds = [item + ns for item in ind]
                if len(Cls[np.where(Cls == c)]) == 0:
                    e[tuple(inds)] = 0.0
                else:
                    e[tuple(inds)] = -1.0 / len(Cls[np.where(Cls == c)])
                N += np.dot(e, e.T)
            M = (1 - mu) * M0 + mu * N
            M /= np.linalg.norm(M, 'fro')
            left = np.dot(E + self.lamb * M + self.rho * L,
                          K) + self.eta * np.eye(ns + nt, ns + nt)
            Beta = np.dot(np.linalg.inv(left), np.dot(E, YY))

            # For testing
            # Ytest = np.copy(YY)
            # for c in range(1, C + 1):
            #     yy = Cls == c
            #     inds = np.where(yy == True)
            #     inds = [item + ns for item in inds]
            #     Ytest[inds, c - 1] = 1
            # SRM = np.linalg.norm(np.dot(Ytest.T - np.dot(Beta.T, K), E)) \
            #       + self.eta * np.linalg.multi_dot([Beta.T, K, Beta]).trace()
            # MMD =  np.linalg.multi_dot([Beta.T, np.linalg.multi_dot([K, self.lamb *M + self.rho * L, K]), Beta]).trace()
            # fitness = SRM + MMD
            # print(fitness, SRM, MMD)

            F = np.dot(K, Beta)
            Cls = np.argmax(F, axis=1) + 1
            Cls = Cls[ns:]
            acc = np.mean(Cls == Yt.ravel())
            list_acc.append(acc)
            # self.out.write("Iteration %d, Fitness %f\n" % (t, fitness))
            # print('MEDA iteration [{}/{}]: mu={:.2f}, Acc={:.4f}'.format(t, self.T, mu, acc))
            # print('=============================================')
            # print('-----%d%%' %(t+1)*10,)
        return acc, Cls, list_acc
Пример #8
0
tar_data = np.genfromtxt("data/Target", delimiter=",")
Xt = tar_data[:, 0:no_features]
Yt = np.ravel(tar_data[:, no_features:no_features + 1])
Yt = np.array([int(label) for label in Yt])

ns, nt = Xs.shape[0], Xt.shape[0]
n = ns + nt

YY = np.zeros((ns, C))
for c in range(1, C + 1):
    ind = np.where(Ys == c)
    YY[ind, c - 1] = 1
YY = np.vstack((YY, np.zeros((nt, C))))

gfk = GFK.GFK(dim=20)
_, Xs_new, Xt_new = gfk.fit(Xs, Xt)
Xs_new /= np.linalg.norm(Xs_new, axis=1)[:, None]
Xt_new /= np.linalg.norm(Xt_new, axis=1)[:, None]

classifier = KNeighborsClassifier(n_neighbors=1)
classifier.fit(Xs_new, Ys)
Yt_pseu = classifier.predict(Xt_new)

e = np.vstack((1.0 / ns * np.ones((ns, 1)), -1.0 / nt * np.ones((nt, 1))))
M0 = e * e.T * C

X = np.vstack((Xs_new, Xt_new))
K = Utility.kernel(ker='rbf', X=X.T, X2=None, gamma=0.5)
A = np.diagflat(np.vstack((np.ones((ns, 1)), np.zeros((nt, 1)))))
Пример #9
0
        Y_gmeda = np.vstack((Ys, gmeda_target))
        # Y_pmeda = np.vstack((Ys, pmeda_target))

        data = pd.DataFrame()
        data['y'] = Y.tolist()
        data['y_meda'] = Y_meda.tolist()
        data['y_gmeda'] = Y_gmeda.tolist()
        # data['y_pmeda'] = Y_pmeda.tolist()

        for index, row in data.iterrows():
            row['y'] = row['y'][0]
            row['y_meda'] = row['y_meda'][0]
            row['y_gmeda'] = row['y_gmeda'][0]
            # row['y_pmeda'] = row['y_pmeda'][0]

        gfk = GFK.GFK(dim=dim)
        _, Xs_new, Xt_new = gfk.fit(Xs, Xt)
        Xs_new, Xt_new = Xs_new.T, Xt_new.T
        X = np.hstack((Xs_new, Xt_new))
        X /= np.linalg.norm(X, axis=0)
        X_gfk = X.T
        Xs_gfk = X[:, :ns].T
        Xt_gfk = X[:, ns:].T
        gfk_results = tSNETransform(X_gfk)
        data['gfk1'] = gfk_results[:, 0]
        data['gfk2'] = gfk_results[:, 1]


        #
        #
        #
Пример #10
0
    def evolve(self, Xs, Ys, Xt, Yt):
        self.Xs = Xs
        self.Ys = Ys
        self.Xt = Xt
        self.Yt = Yt

        self.ns, self.nt = Xs.shape[0], Xt.shape[0]
        self.C = len(np.unique(Ys))

        f_out = open(str(self.run) + '.txt', 'w')
        toPrint = ("Random_rate: %f, archive size: %d\n" % (self.random_rate, self.archive_size))

        start = time.time()
        # Transform data using gfk
        # should be done faster by reading from gfk file.
        gfk = GFK.GFK(dim=self.dim)
        _, Xs_new, Xt_new = gfk.fit(Xs, Xt)
        Xs_new, Xt_new = Xs_new.T, Xt_new.T
        X = np.hstack((Xs_new, Xt_new))
        X /= np.linalg.norm(X, axis=0)
        self.Xs = X[:, :self.ns].T
        self.Xt = X[:, self.ns:].T

        # build some matrices that are not changed
        self.K = kernel(self.kernel_type, X, X2=None, gamma=self.gamma)
        self.A = np.diagflat(np.vstack((np.ones((self.ns, 1)), np.zeros((self.nt, 1)))))
        e = np.vstack((1.0 / self.ns * np.ones((self.ns, 1)), -1.0 / self.nt * np.ones((self.nt, 1))))
        self.M0 = e * e.T * self.C

        self.YY = np.zeros((self.ns, self.C))
        for c in range(1, self.C + 1):
            ind = np.where(self.Ys == c)
            self.YY[ind, c - 1] = 1
        self.YY = np.vstack((self.YY, np.zeros((self.nt, self.C))))

        N = 10
        GEN = 100
        pos_min = -10
        pos_max = 10
        pop = []
        pop_mmd = [sys.float_info.max] * N
        pop_srm = [sys.float_info.max] * N
        pop_fit = [sys.float_info.max] * N
        pop_src_acc = [sys.float_info.max] * N
        pop_tar_acc = [sys.float_info.max] * N
        pop_label = [[1]] * N
        NBIT = (self.ns + self.nt) * self.C

        # initialization
        if self.init_op == 0:
            # start randomly
            for i in range(N):
                poistion = np.random.uniform(pos_min, pos_max, NBIT)
                beta = np.reshape(poistion, (self.ns + self.nt, self.C))
                pop.append(beta)
        elif self.init_op == 1:
            # using different KNN
            for i in range(N):
                classifier = KNeighborsClassifier(2 * i + 1)
                beta = self.initialize_with_classifier(classifier)
                pop.append(beta)
        elif self.init_op == 2:
            # using differnet classifiers
            classifiers = list([])
            classifiers.append(KNeighborsClassifier(1))
            classifiers.append(KNeighborsClassifier(3))
            classifiers.append(KNeighborsClassifier(5))
            classifiers.append(SVC(kernel="linear", C=0.025, random_state=np.random.randint(2 ** 10)))
            classifiers.append(SVC(kernel="rbf", C=1, gamma=2, random_state=np.random.randint(2 ** 10)))
            classifiers.append(GaussianProcessClassifier(1.0 * RBF(1.0), random_state=np.random.randint(2 ** 10)))
            classifiers.append(GaussianNB())
            classifiers.append(DecisionTreeClassifier(max_depth=5, random_state=np.random.randint(2 ** 10)))
            classifiers.append(
                RandomForestClassifier(max_depth=5, n_estimators=10, random_state=np.random.randint(2 ** 10)))
            classifiers.append(AdaBoostClassifier(random_state=np.random.randint(2 ** 10)))
            assert len(classifiers) == N
            for i in range(N):
                beta = self.initialize_with_classifier(classifiers[i])
                pop.append(beta)
        else:
            print("Unsupported Initialize Strategy")
            sys.exit(1)

        # evolution
        archive = []
        archive_fit = []
        archive_src_acc = []
        archive_tar_acc = []
        archive_mmd = []
        archive_srm = []
        archive_label = []
        best = None
        best_fitness = sys.float_info.max
        best_src_acc = -sys.float_info.max
        best_tar_acc = -sys.float_info.max
        best_mmd = 0
        best_srm = 0

        toPrint += ("# Form index: fitness, source accuracy, target accuracy\n")
        for g in range(GEN):
            toPrint += ('==============Gen %d===============\n' % g)
            for index, ind in enumerate(pop):
                # refine the position using gradient descent
                new_position, new_fitness, new_mmd, new_srm, new_src_acc, new_tar_acc, new_label = self.fit_predict(
                    pop[index])
                toPrint += ("%d: %f, %f, %f\n" % (index, new_fitness, new_src_acc, new_tar_acc))

                # if the fitness is not improved, store the previous position in the archive
                # randomly create a new position based on the best
                # store the current position to archive
                if pop_fit[index] <= new_fitness:
                    new_position = self.re_initialize(pop, best, pos_min, pos_max, archive_label,
                                                      strategy=self.re_init_op)
                    new_position, new_fitness, new_mmd, new_srm, new_src_acc, new_tar_acc, new_label = self.fit_predict(
                        new_position)
                    toPrint += ("Reset %d: %f, %f, %f\n" % (index, new_fitness, new_src_acc, new_tar_acc))

                    # append the old ind
                    archive.append(pop[index])
                    archive_fit.append(pop_fit[index])
                    archive_mmd.append(pop_mmd[index])
                    archive_srm.append(pop_srm[index])
                    archive_src_acc.append(pop_src_acc[index])
                    archive_tar_acc.append(pop_tar_acc[index])
                    archive_label.append(pop_label[index])

                # now update the new position with its fitness
                pop[index] = new_position
                pop_fit[index] = new_fitness
                pop_mmd[index] = new_mmd
                pop_srm[index] = new_srm
                pop_src_acc[index] = new_src_acc
                pop_tar_acc[index] = new_tar_acc
                pop_label[index] = new_label

                # update best if necessary
                if new_fitness < best_fitness:
                    best = np.copy(pop[index])
                    best_fitness = new_fitness
                    best_src_acc = new_src_acc
                    best_tar_acc = new_tar_acc
                    best_srm = new_srm
                    best_mmd = new_mmd

            toPrint += ("Best: %f, %f, %f\n" % (best_fitness, best_src_acc, best_tar_acc))

            if g % 10 == 0:
                tmp_archive = copy.deepcopy(archive)
                tmp_archive.append(best)
                all_labels = []

                all_indices = range(len(tmp_archive))
                class_indices = all_indices
                for index in class_indices:
                    ind = tmp_archive[index]
                    F = np.dot(self.K, ind)
                    Y_pseudo = np.argmax(F, axis=1) + 1
                    Yt_pseu = Y_pseudo[self.ns:].tolist()
                    all_labels.append(Yt_pseu)
                all_labels = np.array(all_labels)
                vote_label = []
                for ins_idx in range(all_labels.shape[1]):
                    ins_labels = all_labels[:, ins_idx]
                    counts = np.bincount(ins_labels)
                    label = np.argmax(counts)
                    vote_label.append(label)
                acc = np.mean(vote_label == Yt)
                toPrint += ("Accuracy temp archive:" + str(acc) + "\n")

        archive.append(best)
        archive_fit.append(best_fitness)
        archive_mmd.append(best_mmd)
        archive_srm.append(best_srm)
        archive_src_acc.append(best_src_acc)
        archive_tar_acc.append(best_tar_acc)

        time_eslape = (time.time() - start)
        nd_indices = self.get_non_dominated(archive, archive_srm, archive_mmd)

        all_labels = []
        toPrint += ("========From all archive========\n")
        all_indices = range(len(archive))
        class_indices = all_indices
        for index in class_indices:
            ind = archive[index]
            toPrint += ("%d: %f, %f, %f, %f, %f\n"
                        % (index, archive_fit[index], archive_srm[index],
                           archive_mmd[index], archive_src_acc[index],
                           archive_tar_acc[index]))
            F = np.dot(self.K, ind)
            Y_pseudo = np.argmax(F, axis=1) + 1
            Yt_pseu = Y_pseudo[self.ns:].tolist()
            all_labels.append(Yt_pseu)
        all_labels = np.array(all_labels)
        vote_label = []
        for ins_idx in range(all_labels.shape[1]):
            ins_labels = all_labels[:, ins_idx]
            counts = np.bincount(ins_labels)
            label = np.argmax(counts)
            vote_label.append(label)
        acc = np.mean(vote_label == Yt)
        toPrint += ("Accuracy archive:" + str(acc) + "\n")

        all_labels = []
        toPrint += ("========From non-dominated========\n")
        class_indices = nd_indices
        for index in class_indices:
            ind = archive[index]
            toPrint += ("%d: %f, %f, %f, %f, %f\n"
                        % (index, archive_fit[index], archive_srm[index],
                           archive_mmd[index], archive_src_acc[index],
                           archive_tar_acc[index]))
            F = np.dot(self.K, ind)
            Y_pseudo = np.argmax(F, axis=1) + 1
            Yt_pseu = Y_pseudo[self.ns:].tolist()
            all_labels.append(Yt_pseu)
        all_labels = np.array(all_labels)
        vote_label = []
        for ins_idx in range(all_labels.shape[1]):
            ins_labels = all_labels[:, ins_idx]
            counts = np.bincount(ins_labels)
            label = np.argmax(counts)
            vote_label.append(label)
        acc = np.mean(vote_label == Yt)
        toPrint += ("Accuracy non-dominated:" + str(acc) + "\n")

        toPrint += ("===================================\n")
        F = np.dot(self.K, best)
        Y_pseudo = np.argmax(F, axis=1) + 1
        Yt_pseu = Y_pseudo[self.ns:].tolist()
        acc = np.mean(Yt_pseu == Yt)
        toPrint += ("Accuracy best:" + str(acc) + "\n")
        toPrint += ("Execution time: " + str(time_eslape) + "\n")

        f_out.write(toPrint)
        f_out.close()