⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fitfun.py

📁 PyChem是用Python语言编写的多元变量分析软件。它包括一个前端图形界面用于管理和保存试验数据
💻 PY
字号:
"""Fitness functions for use in genetic algorithm
optimisation

$Id: FitFun.py Copyright (C) 2005  Roger Jarvis

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

"""

import string, copy, scipy
from pychemlib.process import *
from pychemlib.chemometrics import *
from pychemlib.chemometrics import __slice__
from pychemlib.chemometrics import __split__
from pychemlib.chemometrics import _index
from pychemlib.chemometrics import __diag__
from pychemlib.chemometrics import _put
from pychemlib.chemometrics import __TW__
from genetic import _remdup

def __group__(x,mrep):
    grp = []
    for n in range(1,x.shape[0]/mrep+1,1):
        for cnt in range(0,mrep,1):
            grp.append(n)
    return scipy.reshape(scipy.asarray(grp,'i'),(len(grp),1))

def call_dfa(chrom,xdata,mask,groups,names,DFs):
    """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(0,chrom.shape[0],1):
        if _remdup(chrom[x]) == 0:
            #extract vars from xdata
            slice = __slice__(xdata,chrom[x])
            
            #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,groups,names,mask)
            
            try:
                u,v,eigs = DFA(tr_slice,tr_grp,DFs)
                projU = scipy.dot(scipy.dot(cv_slice,v),__diag__(eigs)).real
                u = scipy.concatenate((u,projU),0)
                group2 = scipy.concatenate((tr_grp,cv_grp),0)
        
                T,W = __TW__(u,group2)
                B = T-W
                P = scipy.dot(scipy.linalg.inv(W),B)
                eigval,eigvec = scipy.linalg.eig(P)
                
                Y.append(1.0/scipy.sum(eigval.real))
                    
            except:
                Y.append(10.0**5)
        else:
            Y.append(10.0**5)
            
    return scipy.array(Y)[:,scipy.NewAxis]        


def rerun_dfa(chrom,xdata,mask,groups,names,DFs):
    """Run DFA in min app"""
    #extract vars from xdata
    slice = __slice__(xdata,chrom)
    
    #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,groups,names,mask)
    
    #get indexes
    idx = scipy.arange(xdata.shape[0])[:,scipy.NewAxis]
    tr_idx = scipy.take(idx,_index(mask,0),0)
    cv_idx = scipy.take(idx,_index(mask,1),0)
    ts_idx = scipy.take(idx,_index(mask,2),0)
    
    #model DFA on training samples
    u,v,eigs = DFA(tr_slice,tr_grp,DFs)
    
    #project xval and test samples
    projUcv = scipy.dot(scipy.dot(cv_slice,v),__diag__(eigs)).real
    projUt = scipy.dot(scipy.dot(ts_slice,v),__diag__(eigs)).real
    
    uout = scipy.zeros((xdata.shape[0],DFs),'d')
    _put(uout,scipy.reshape(tr_idx,(len(tr_idx),)).tolist(),u)
    _put(uout,scipy.reshape(cv_idx,(len(cv_idx),)).tolist(),projUcv)
    _put(uout,scipy.reshape(ts_idx,(len(ts_idx),)).tolist(),projUt)
    
    return uout,v,eigs      


def call_pls(chrom,xdata,mask,names,groups,factors):
    """Runs pls on a subset of X-variables"""
    scores = []
    for i in range(chrom.shape[0]):
        if _remdup(chrom[i]) == 0:
            #extract vars from xdata
            try:
                slice = __slice__(xdata,chrom[i])
                W,T,P,Q,facs,predy,predyv,predyt,RMSEC,RMSEPC,rmsec,rmsepc,rmsept = PLS(slice,groups,mask,names,factors)
                if min(rmsec) < min(rmsepc):
                    scores.append(RMSEPC)
                else:
                    scores.append(10.0**5)
            except:
                scores.append(10.0**5)
        else:
            scores.append(10.0**5)
            
    return scipy.reshape(scipy.asarray(scores),(chrom.shape[0],1))    


if __name__=="__main__":
    import FitFun,doctest
    doctest.testmod(FitFun,verbose=True)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -