0

我已经从 random_search(n,dim) 获得了功能或基因,但是如果我重新运行程序,结果总是会改变或与之前的结果不同。我希望功能选择(gens)具有相同的结果,尽管我想重新运行程序/代码。

如果我重新运行代码或程序,我无法得到相同的结果

' Python

用于特征选择的代码 BCS

def BCS(Eval_Func,m_i=200,n=24,minf=0,dim=None,prog=False,alpha=0.1,beta=1.5,param=0.25):

    estimate=Eval_Func().evaluate

    if dim==None:
        dim=Eval_Func().check_dimentions(dim)
    pa=param
    #flag=dr
    gens=random_search(n,dim)
    fit=[float("-inf") if minf == 0 else float("inf") for _ in range(n)]
    pos=[0 for _ in range(n)]
    g_pos=[0]*dim
    g_val=float("-inf") if minf == 0 else float("inf")
    gens_dict={tuple([0]*dim):float("-inf") if minf == 0 else float("inf")}
    if prog:
        miter=tqdm(range(m_i))
    else:
        miter=range(m_i)
    for it in miter:
        for i,g in enumerate(gens):
            if tuple(g) in gens_dict:
                score=gens_dict[tuple(g)]
            else:
                score=estimate(g)
                gens_dict[tuple(g)]=score
            if score > fit[i] if minf==0 else score < fit[i]:
                fit[i]=score
                pos[i]=g

        maxfit,maxind=max(fit),fit.index(max(fit))
        minfit,minind=min(fit),fit.index(min(fit))
        if minf==0:
            if maxfit > g_val:
                g_val=dc(maxfit)
                g_pos=dc(gens[maxind])
        else:
            if minfit < g_val:
                g_val=dc(minfit)
                g_pos=dc(gens[minind])

        if pa < random.uniform(0,1):
            if minf==0:
                gens[minind]=[0 if 0.5>random.uniform(0,1) else 1 for _ in  range(dim)]#rand_gen()
                fit[minind]=float("-inf") if minf == 0 else float("inf")
            else:
                gens[maxind]=[0 if 0.5>random.uniform(0,1) else 1 for _ in range(dim)]#rand_gen()
                fit[maxind]=float("-inf") if minf == 0 else float("inf")


        for g in gens:
            for d in range(dim):
                x=levy_flight(beta,g_pos[d],g[d],alpha)
                if random.uniform(0,1) < sigmoid(x):
                    g[d]=1
                else:
                    g[d]=0
    return g_val,g_pos,g_pos.count(1)

代码形式 Class Evaluate

class Evaluate:
    def __init__(self):
        self.train_l = tr_y
        self.train_d = tr_x
        self.test_l = te_y
        self.test_d=te_x
        self.K = 2
    def evaluate(self,gen):
        mask=np.array(gen) > 0
        al_data=np.array([al[mask] for al in self.train_d])
        #al_test_data=np.array([al[mask] for al in self.test_d])
        kf = ms.KFold(n_splits=self.K,random_state=42)
        s = 0
        for tr_ix,te_ix in kf.split(al_data):
            s+= svm.SVR(kernel = 'rbf', gamma='scale', C=1.0,  epsilon=0.2).fit(al_data[tr_ix],self.train_l[tr_ix]).score(al_data[te_ix],self.train_l[te_ix])#.predict(al_test_data)
        s/=self.K
        return s#np.count_nonzero(self.test_l==res)/len(self.test_l)

    def check_dimentions(self,dim):
        if dim==None:
            return len(self.train_d[0])
        else:
            return dim       

Test_score 的代码

def test_score(gen,tr_x,tr_y,te_x,te_y):
    mask=np.array(gen) == 1
    al_data=np.array(tr_x[:,mask])
    al_test_data=np.array(te_x[:,mask])
    return np.mean(np.abs(([svm.SVR(kernel = 'rbf', gamma='scale', C=1.0, epsilon=0.2).fit(al_data,tr_y).score(al_test_data,te_y) for i in range(4)])))*100         

调用程序 BCS 和类评估的代码

def fitureSelection(data):
    result = []
    for i in range(0,2):
        X = data.loc[data['label']== i].iloc[:,0:93]
        y = data.loc[data['label']== i].iloc[:,95]
        tr_x, te_x, tr_y, te_y = train_test_split(X,y,test_size=0.2)
        sc_X = StandardScaler()
        tr_x = sc_X.fit_transform(tr_x)
        te_x = sc_X.fit_transform(te_x)
        s,g,l = BCS(Eval_Func=Evaluate,n=20,m_i=200)
        result.append("".join(map(str,g)))
    return result

调用 def fitureSelection(data) 的代码

hasil = fitureSelection(Hasilcolend)

结果

['000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000',

'000000000000100000010000000000000000000100000000000000001100000000000000000001010001000000000'] '

4

0 回答 0