Exemplo n.º 1
0
def call_dfa(chrom, xdata, DFs, mask, data):
    """Runs DFA on subset of variables from "xdata" as 
    defined by "chrom" and returns a vector of fitness 
    scores to be fed back into the GA
    """
    Y = []
    for x in range(len(chrom)):
        if _remdup(chrom[x]) == 0:
            #extract vars from xdata
            slice = meancent(_slice(xdata, chrom[x]))
            collate = 0
            for nF in range(mask.shape[1]):
                #split in to training and test
                tr_slice, cv_slice, ts_slice, tr_grp, cv_grp, ts_grp, tr_nm, cv_nm, ts_nm = _split(
                    slice, data['class'][:, 0], mask[:, nF].tolist(),
                    data['label'])

                try:
                    u, v, eigs, dummy = cva(tr_slice, tr_grp, DFs)
                    projU = scipy.dot(cv_slice, v)
                    u = scipy.concatenate((u, projU), 0)
                    group2 = scipy.concatenate((tr_grp, cv_grp), 0)

                    B, W = _BW(u, group2)
                    L, A = scipy.linalg.eig(B, W)
                    order = _flip(
                        scipy.argsort(scipy.reshape(L.real, (len(L), ))))
                    Ls = _flip(scipy.sort(L.real))
                    eigval = Ls[0:DFs]

                    collate += sum(eigval)
                except:
                    continue

            if collate != 0:
                Y.append(float(mask.shape[1]) / collate)
            else:
                Y.append(10.0**5)
        else:
            Y.append(10.0**5)

    return scipy.array(Y)[:, nA]
Exemplo n.º 2
0
def call_dfa(chrom,xdata,DFs,mask,data):
    """Runs DFA on subset of variables from "xdata" as 
    defined by "chrom" and returns a vector of fitness 
    scores to be fed back into the GA
    """
    Y = []
    for x in range(len(chrom)):
        if _remdup(chrom[x]) == 0:
            #extract vars from xdata
            slice = meancent(_slice(xdata,chrom[x]))
            collate = 0
            for nF in range(mask.shape[1]):
                #split in to training and test
                tr_slice,cv_slice,ts_slice,tr_grp,cv_grp,ts_grp,tr_nm,cv_nm,ts_nm=_split(slice,
                      data['class'][:,0],mask[:,nF].tolist(),data['label'])
                
                try:
                    u,v,eigs,dummy = cva(tr_slice,tr_grp,DFs)
                    projU = scipy.dot(cv_slice,v)
                    u = scipy.concatenate((u,projU),0)
                    group2 = scipy.concatenate((tr_grp,cv_grp),0)
            
                    B,W = _BW(u,group2)
                    L,A = scipy.linalg.eig(B,W)
                    order =  _flip(scipy.argsort(scipy.reshape(L.real,(len(L),))))
                    Ls =  _flip(scipy.sort(L.real))
                    eigval = Ls[0:DFs]
                    
                    collate += sum(eigval)
                except:
                    continue
                
            if collate != 0:
                Y.append(float(mask.shape[1])/collate)
            else:
                Y.append(10.0**5)
        else:
            Y.append(10.0**5)
            
    return scipy.array(Y)[:,nA]        
Exemplo n.º 3
0
def rank(chrom,score):
    """Linear ranking of individuals between
    0 (worst) and 2 (best)
    """
    order = _sortrows(scipy.concatenate((score,chrom),1))

    ranksc = scipy.zeros((chrom.shape[0],1),'d')
    for x in range(1,len(score),1):
        ranksc[x] = 2*(float(x)/(chrom.shape[0]-1)) 
    ranksc = _flip(ranksc)

    chrom = scipy.array(order[:,1:order.shape[1]])
    scores = scipy.reshape(order[:,0],(order.shape[0],1))

    return ranksc,chrom,scores