Exemplo n.º 1
0
def err_computing(nlp,nn,ni,D_ftr,sgma0,lamda0): 
### the error for ridge regression
    err_pres = np.zeros(nlp)
 
### the error for the rff
    err_rffs = np.zeros(nlp)
  
### the error difference for the two regression
    err_pre_rffs = np.zeros(nlp)
 
 
    for num in range(nlp):
### generating data
        xs,ys,xt,yt =dat_gen(nn,ni)
 
### run xvalidation
        kernel = GaussianKernel(sgma0)
        lamda_pre, width_pre = kernel.xvalidate(xs,ys,method="ridge_regress")
        kernel.rff_generate(D_ftr)
        lamda_rff,width_rff = kernel.xvalidate(xs,ys,method="ridge_regress_rff")
    
### perform ridge regression
        y_pre, err_pre0 = err_pre(xs,ys,xt,yt,width_pre,lamda_pre)
        err_pres[num] = err_pre0
### perform random fourier features
        y_rff, err_rff0 = err_rff(xs,ys,xt,yt,width_rff,lamda_rff,D_ftr)
        err_rffs[num] = err_rff0
### comparing the difference between two predictions        
        err_pre_rffs[num] = np.linalg.norm(y_pre-y_rff)**2/ni
   
  
### the mean square error for ridge regression
    mse_pre = np.mean(err_pres)
  
### the mean square error for rff
    mse_rff = np.mean(err_rffs)
  
### the mean square error for the difference between ridge and rff
    mse_pre_rff = np.mean(err_pre_rffs)
     
    results = np.array([mse_pre,mse_rff,mse_pre_rff])
    return results
def ridge_error(nlp, nn, ni, sgma=1.0):
    mse_pres = np.zeros(nlp)
    for ii in np.arange(nlp):
        x_tr, y_tr, x_tt, y_tt = dat_gen(nn, ni)
        kernel = GaussianKernel(float(sgma))
        lamda_pre, width_pre = kernel.xvalidate(x_tr,
                                                y_tr,
                                                method="ridge_regress")

        mse_pres[ii] = err_pre(x_tr, y_tr, x_tt, y_tt, width_pre, lamda_pre)
    mse_pre = np.mean(mse_pres)
    return mse_pre
def rff_error(nlp, nn, ni, D, sgma=1.0):
    mse_rffs = np.zeros(nlp)
    for ii in np.arange(nlp):
        x_tr, y_tr, x_tt, y_tt = dat_gen(nn, ni)
        kernel = GaussianKernel(float(sgma))
        kernel.rff_generate(D)
        lamda_rff, width_rff = kernel.xvalidate(x_tr,
                                                y_tr,
                                                method="ridge_regress_rff")

        mse_rffs[ii] = err_rff(x_tr, y_tr, x_tt, y_tt, width_rff, lamda_rff, D)

    mse_rff = np.mean(mse_rffs)
    return mse_rff