Python:尝试复现New Balanced Active Learning Model and Optimization Algorithm--2018IJCAI

 参考文献:New Balanced Active Learning Model and Optimization Algorithm--2018IJCAI

原文代码找了好久没有找到,至少Github上没有,为了深入学习只能自己撸码了。

代码还没进行类封装。目前效果还不好,可能是参数没设置好,也可能是代码有问题。有空好好改一下。

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn import datasets
from collections import OrderedDict


def t_updata(t_old):
    return .5 + 0.5 * np.sqrt(1 + 4 * t_old)

def C_cal(W,X,gamma):
    return W - gamma * (X.T * X * W - X.T * X )

def P_zeta_1(a,b,zeta):
    a_abs = np.abs(sorted(a,reverse=True))
    t = 0
    a_abs_temp = np.append(a_abs,0.0)
    for j in range(len(a)):
        t = t + a_abs[j]
        delta = (t + b) / (pow(zeta,-1) + j)
        if a_abs_temp[j+1] <= delta <= a_abs_temp[j]:
            a_abs_delta = a_abs - delta
            for i in range(len(a_abs_delta)):
                if a_abs_delta[i] < 0:
                    a_abs_delta[i] = 0
            X = np.sign(a) * a_abs_delta
            y = np.linalg.norm(X,1)
            return X,y
    X = a
    y = b
    return X,y

def KMeans_results(X,Cluster_Num):
    kmeans = KMeans(n_clusters=Cluster_Num,random_state=101).fit(X)
    clusterIndex = kmeans.labels_
    clusterDict = OrderedDict()
    for k in range(Cluster_Num):
        clusterDict[k] = []
    for i in range(len(X)):
        clusterDict[clusterIndex[i]].append(i)
    return clusterDict

def Problem(X,W_old,G,gamma):
    t_old = 1
    W_current = W_old
    W_new = np.zeros((len(W_old),len(W_old)))
    iter_num = 10
    while iter_num > 0:
        C = C_cal(W=W_current,X=X,gamma=gamma)
        for g in G.values():
            T = np.zeros(len(g))
            for i,gi in enumerate(g):
                T[i] = np.linalg.norm(C[gi])
            S,y = P_zeta_1(a=T,b=0,zeta=0.6)
            for i,gi in enumerate(g):
                W_new[gi,:] = (S[i]/T[i])*C[gi,:]
        t_new = t_updata(t_old)
        W_current = W_new + (W_new - W_old)*(t_old - 1)/t_new
        W_old = W_new
        t_old = t_new
        iter_num -= 1
    return W_new

if __name__ == '__main__':
    # X,y = datasets.make_blobs(n_samples=200,n_features=2,centers=3,cluster_std=[1.2,1.8,1.1],random_state=101)
    #--------------------------------------#
    path1 = r'E:\dataset\ExperimentalData\Aggregation\aggregation.csv'
    path2 = r'E:\dataset\ExperimentalData\Three blobs\ThreeBlobs.csv'
    path3 = r'E:\dataset\ExperimentalData\R15\R15.csv'
    data = np.array(pd.read_csv(path3, header=None))
    X = data[:, :-1]
    y = data[:, -1]
    #--------------------------------------#
    K = len(set(y))
    ClustG = KMeans_results(X, Cluster_Num=K)
    X = np.mat(X).T
    N = X.shape[1]
    # W = np.random.random((N,N))
    W = np.random.randint(1,10,[N,N])
    gamma = 0.1
    W_old = np.random.random((N,N))
    W_new = Problem(X=X, W_old=W_old, G=ClustG, gamma=gamma)
    row_sum_abs = np.linalg.norm(W_new,ord=1,axis=1)
    Ord_rank = np.flipud(np.argsort(row_sum_abs))

    X = np.array(X.T)
    print(X.shape)

    E = X[Ord_rank[0:30],:]
    plt.scatter(X[:,0],X[:,1],c=y)
    plt.scatter(E[:,0],E[:,1],c='r',marker='*',s=300)
    plt.show()

 

你可能感兴趣的:(Python学习,Active,Learning)