示例#1
0
def CV (X,Y,BLOCK,ITER,LV):
    RMSECV=np.zeros((ITER,LV))
    
    for i in range(ITER):
        A=range(X.shape[0])
        B=list(A)
        random.shuffle(B)
        X=X[B,:]
        Y=Y[B,:]
        C=int(X.shape[0]/BLOCK)
        Yval_pred=np.zeros((X.shape[0],LV)) 
        
        for r in range(BLOCK):
            if r==0:
                BGN_v=0
                END_v=C
                D=range(X.shape[0])
                E=list(D)
                c_=E[C+1:X.shape[0]:1]
    #            print('A')
    
            elif r==BLOCK-1:
                BGN_v=C*r
                END_v=X.shape[0]
                D=range(X.shape[0])
                E=list(D)
                c_=E[0:C*r:1]
    #            print('hehe')
            else:
                BGN_v=C*r
                END_v=C*(r+1)
                D=range(X.shape[0])
                E=list(D)
                temp_1=E[0:C*r:1]
                temp_2=E[C*(r+1)+1:X.shape[0]:1]
                c_=[*temp_1,*temp_2]
    #            print('haha')
    #            
            print(BGN_v)
            print(END_v)
    #        print(c_)
            Xcal=X[c_,:]
            Ycal=Y[c_,:]        
            Xval=X[BGN_v:END_v,:]
            Yval=Y[BGN_v:END_v,:]               
            
            for k in range(LV):
                pls_calibration=phi.pls(Xcal,Ycal,k+1,mcsX='center',mcsY=True)
                yhatval_pls = phi.pls_pred(Xval,pls_calibration)
                Yval_pred[BGN_v:END_v,k]=yhatval_pls['Yhat'].ravel()          
        
        for k in range(LV):
            RMSECV[i,k]=np.sqrt(np.sum((Y.ravel()-Yval_pred[:,k])**2)/Y.shape[0])
    
    from matplotlib.pylab import plt
    for i in range(RMSECV.shape[0]):
        plt.plot(np.arange(1,LV+1,1),RMSECV[i,:])
        plt.title("Scree plot", fontsize=16, fontweight='bold')
        plt.xlabel("# of LV")
        plt.ylabel("RMSECV")
        plt.show()
    
    return RMSECV
示例#2
0
def buildwPLS(Dm,R,A,*,num_si_u=0,R_ik=False):
    """
    PLS-EIOT BUILD Routines by Salvador Garcia-Munoz ([email protected], [email protected])

    Parameters
    ----------
    Dm : TYPE, Numpy Array.
        DESCRIPTION. Mixture spectra [o x lambda].
    R : TYPE, Numpy Array.
        DESCRIPTION. Compositional matrix [o x n] with mass/mole/vol fractions.
    A : TYPE, Integer.
        DESCRIPTION. Number of latent variables to use in PLS model.
    num_si_u : TYPE, optional.
        DESCRIPTION. Number of un-supervised non-chemical interferences, the default is 0.
    R_ik : TYPE, Numpy Array.
        DESCRIPTION. Matrix with supervised non-chemical interferences The default is False.

    Returns
    -------
    eiot_pred_obj a Dictionary with predicions and diagnostics

    """
    rk=R_ik
    Ck=R
    num_sI=num_si_u
    if isinstance(rk,bool):     
        print('Building a PLS based EIOT Unsupervised object')
        pls_obj=phi.pls(Ck,Dm,A)
        Dm_hat=phi.pls_pred(Ck,pls_obj)
    else:
        print('Building a PLS based EIOT Supervised object')
        aux=np.hstack((Ck,rk))
        pls_obj=phi.pls(aux,Dm,A)
        Dm_hat=phi.pls_pred(aux,pls_obj)
    pls_obj=phi.conv_pls_2_eiot(pls_obj,r_length=Ck.shape[1])
    
    Dm_hat=Dm_hat['Yhat']
    if num_sI>0:
        E_ch       = Dm-Dm_hat
        [U,S,Vh]   = np.linalg.svd(E_ch)
        V          = Vh.T
        S_I        = V[:,0:num_sI]
        S_short    = S[0:num_sI]
        r_I        = U[:,0:num_sI] * np.tile(S_short,(U.shape[0],1))
        SR         = E_ch- r_I @ S_I.T
        SSR        = np.sum(SR**2,1,keepdims=1)
        lambdas    = S[num_sI]
    else:
        S_I        = np.nan
        SR         = Dm-Dm_hat 
        E_ch       = SR
        [U,S,Vh]   = np.linalg.svd(E_ch)
        V          = Vh.T
        lambdas    = np.diag(S)
        lambdas    = np.diag(lambdas)
        SSR        = np.sum(SR**2,axis=1)  
        r_I        = np.nan
        
    if not(isinstance(S_I,float)):
        S_I=S_I.T
    
    eiot_obj                   = pls_obj
    eiot_obj['wPLS']           = True
    eiot_obj['S_I']            = S_I
    eiot_obj['E_ch']           = E_ch
    eiot_obj['r_I']            = r_I
    eiot_obj['SR']             = SR
    eiot_obj['SSR']            = SSR
    eiot_obj['num_sI']         = num_sI
    eiot_obj['num_e_sI']       = 0
    eiot_obj['abs_max_exc_ri'] = np.nan
    eiot_obj['S_E_CONF_INT']   = np.nan

#Convert Numpy objects to lists and dict for PYOMO    
    if not(isinstance(S_I,float)):
        pyo_S_I = ee.np2D2pyomo(eiot_obj['S_I'])   #convert numpy to dictionary
        pyo_K   = np.arange(1,eiot_obj['S_I'].shape[0]+1)    #index for non-chemical interferences
        pyo_K   = pyo_K.tolist()
    else:
        pyo_S_I = np.nan
        pyo_K   = [0]

    eiot_obj['pyo_K']     = pyo_K
    eiot_obj['pyo_S_I']   = pyo_S_I
    eiot_obj['lambdas'] = lambdas
    return eiot_obj
示例#3
0
Xcal = X[::2, :]
Xval = X[1:X.shape[1]:2, :]
Xcal = phi.snv(Xcal)
Xval = phi.snv(Xval)
Xcal, M = phi.savgol(5, 1, 2, Xcal)
Xval, M = phi.savgol(5, 1, 2, Xval)
pp.plot_spectra(Xcal)

Ycal = Y[::2]
Ycal = np.reshape(Ycal, (len(Ycal), -1))
Yval = Y[1:X.shape[1]:2]
Yval = np.reshape(Yval, (len(Yval), -1))

mvm_pls = phi.pls(Xcal, Ycal, 1, mcsX='center', mcsY='center')
yhatval_pls = phi.pls_pred(Xval, mvm_pls)
yhatcal_pls = phi.pls_pred(Xcal, mvm_pls)

PLSerrrCAL = []
LWPLSerrCAL = []

#This for loop will make predictions for the calibration set using various values for the localization parameter
for loc_param in [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 100, 250]:
    yhatlw = []
    for o in list(range(Xcal.shape[0])):
        yhatlw_ = phi.lwpls(Xcal[o, :],
                            loc_param,
                            mvm_pls,
                            Xcal,
                            Ycal,
                            shush=True)