def pls2_predict_cv(self): x_train, x_test, y_train, y_test = self.cv() y_allPredict = np.ones((1, self.max_components)) pls = _NIPALS(self.max_components) for i in range(self.n_fold): # print 'i:', i y_predict = np.zeros((y_test[i].shape[0], self.max_components)) # print 'y',np.shape(y_predict) x_trainMean = np.mean(x_train[i], axis=0) y_trainMean = np.mean(y_train[i], axis=0) x_testCenter = np.subtract(x_test[i], x_trainMean) # y_testCenter = np.subtract(y_test,y_trainMean) w, t, p, list_coef_B = pls.fit(x_train[i], y_train[i], self.max_components) for j in range(self.max_components): # print 'j:', j # print "textCenter", np.shape(x_testCenter), np.shape(list_coef_B[j]) y_pre = np.dot(x_testCenter, list_coef_B[j]) y_pre = y_pre + y_trainMean # print 'y_pre:', np.shape(y_pre) y_pre_sum = np.sum(y_pre, axis=1) # print 'y_pre_sum:' np.shape(y_pre_sum) y_predict[:, j] = y_pre_sum.ravel() y_allPredict = np.vstack((y_allPredict, y_predict)) y_allPredict = y_allPredict[1:] return y_allPredict, self.y
def pls_fit(self): pls_cv = PLSCV(self.x_train, self.y_train) y_predict_all, y_measure = pls_cv.cv_predict(self.n_folds, self.max_components) RMSECV, min_RMSECV, comp_best = pls_cv.cv_mse(y_predict_all, y_measure) # print RMSECV pls = _NIPALS(comp_best) W, T, P, lists_coefs = pls.fit(self.x_train, self.y_train, comp_best) # print self.y_train coefs_B = lists_coefs[comp_best - 1] # drawings.rmsecv_comp_line(self.max_components, RMSECV) return W, T, P, coefs_B, RMSECV, min_RMSECV, comp_best
def pls_fit(self): pls_cv_demo = Cross_Validation(self.X_train, self.y_train, self.folds, self.max_comp) # print np.shape(self.X_train),np.shape(self.y_train) y_allPredict, y_measure = pls_cv_demo.predict_cv() # print 'shape:', np.shape(y_allPredict), np.shape(y_measure) # print y_measure RMSECV, min_RMSECV, comp_best = pls_cv_demo.mse_cv(y_allPredict, y_measure) # print RMSECV , "rmsecv" # RMSECV, min_RMSECV, comp_best = pls_cv_demo.cv_mse_F(y_allPredict, y_measure, alpha=0.05, num_tr=self.X_train.shape[0]) # print "comp_best" , comp_best pls_demo = _NIPALS(comp_best) weight_W, score_T, loading_P, List_coef_B = pls_demo.fit(self.X_train, self.y_train, comp_best) coefs_B = List_coef_B[comp_best - 1] # print np.shape(coefs_B), np.shape(List_coef_B), comp_best return weight_W, score_T, loading_P, comp_best, coefs_B, RMSECV
def pls2_fit(self): pls2_cv_demo = Cross_Validation(self.X_train, self.y_train, self.folds, self.max_comp) y_allPredict, y_measure = pls2_cv_demo.pls2_predict_cv() y = np.sum(y_measure, axis=1) y = y.reshape(self.X_train.shape[0], 1) # print 'measure:', np.shape(y), y RMSECV, min_RMSECV, comp_best = pls2_cv_demo.pls2_mse_cv(y_allPredict, y) # RMSECV, min_RMSECV, comp_best = pls_cv_demo.cv_mse_F(y_allPredict, y_measure, alpha=0.05, num_tr=self.X_train.shape[0]) pls2_demo = _NIPALS(comp_best) weight_W, score_T, loading_P, List_coef_B = pls2_demo.fit(self.X_train, self.y_train, comp_best) coefs_B = List_coef_B[comp_best - 1] # print 'infor:', comp_best, np.shape(self.X_train), np.shape(self.y_train), np.shape(List_coef_B), np.shape(coefs_B) # print comp_best , "comp_best" # print coefs_B.shape , "shape" return weight_W, score_T, loading_P, comp_best, coefs_B, RMSECV
def cv_predict(self, n_folds, max_components): x_tr, x_te, y_tr, y_te = baseCV.CV(self, self.x, self.y, n_folds) # print x_train, x_test, y_train, y_test # print np.shape(y_predict) y_predict_all = np.ones((1, max_components)) # print np.shape(Y_predict) pls = _NIPALS(max_components) for i in range(n_folds): y_predict = np.zeros((x_te[i].shape[0], max_components)) xtrainmean = np.mean(x_tr[i], axis=0) ytrainmean = np.mean(y_tr[i], axis=0) xte_center = np.subtract(x_te[i], xtrainmean) yte_center = np.subtract(y_te[i], ytrainmean) w, T, P, lists_coefs_B = pls.fit(x_tr[i], y_tr[i], max_components) # print lists_coefs_B for j in range(max_components): y_pre_center = np.dot(xte_center, lists_coefs_B[j]) Y_pre = y_pre_center + ytrainmean # print np.shape(Y_pre) y_predict[:, j] = Y_pre.ravel() y_predict_all = np.vstack((y_predict_all, y_predict)) y_predict_all = y_predict_all[1:] # print np.shape(y_predict_all) # print np.shape(self.y) # print self.y return y_predict_all, self.y