0

I have get the feature or gens from random_search(n,dim), but if i re-run the program, the result always change or different from the result befor. I want the feature selection (gens) have the same result, although i want to re-run the program/code.

I cant get the same result if i re-run the code or program

' python

Code BCS for feature selection

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)

Code form 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       

Code For 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         

Code for call the Program BCS and Class Evaluate

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

Code for call the def fitureSelection(data)

hasil = fitureSelection(Hasilcolend)

The result

['000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000',

'000000000000100000010000000000000000000100000000000000001100000000000000000001010001000000000'] '

dodo
  • 13
  • 1
  • 1
  • 7
  • I suggest editing the question to have one single code region, at present several individual copy-and-paste actions must be taken to try and run your code. Anyone trying to help would have to guess at the import statements you made, so please also include those to make running the code easier. People here should be able to easily copy the code you post into a single file and run it. – James Phillips May 29 '19 at 13:20
  • 1
    thank you for your suggestion – dodo May 30 '19 at 13:03

0 Answers0