def choose_nreduced(self, X, y, lossfunc=None, cv=None, verbose=False, cvplot=False):
        """
        Choose the number of features to use in the reduced feature set by minimizing the cross-validation error.

        @param X: The feature matrix, shape (n,p)
        @param y: The vector of response values, size n.
        @param lossfunc: The loss function to use for the CV error, callable. The default is mean squared error.
        @param cv: Number of CV folds (if int), or cross-validation iterator.
        @param verbose: Print helpful information.
        @param cvplot: Plot the CV error as a function of the number features in the reduced feature set.
        @return: The number of features in the reduced feature set that minimized the CV error.
        """
        if 1 > self.n_jobs:
            pool = multiprocessing.Pool(self.n_jobs)
            pool.map(int, range(self.n_jobs))  # Trick to "warm up" the Pool

        # setup cross-validation iterator
        if cv is None:
            K_folds = 8
        if isinstance(cv, int):
            K_folds = cv

        cv = cross_validation.KFold(y.size, n_folds=K_folds)

        if lossfunc is None:
            lossfunc = metrics.mean_squared_error

        if self.max_components is None:
            self.max_components = X.shape[1]

        if verbose:
            print 'Searching over', self.max_components, ' features to include in the reduced feature space.'
            print 'Computing univariate regression tests statistics for each feature...'

        # first compute coefficients scores
        sargs = []
        for train_idx, test_idx in cv:
            if self.n_jobs == 1:
                sargs.append((X[train_idx, :], y[train_idx]))
            else:
                sargs.append((self, X[train_idx, :], y[train_idx]))

        if self.n_jobs == 1:
            scoefs = map(self._launch_coef_scores, sargs)
        else:
            scoefs = pool.map(launch_coef_scores, sargs)

        # find optimal number of features to use in PCA on reduced feature set, do this by minimizing cross-validation
        # error on a grid.
        cverrors = np.zeros(self.max_components)

        if verbose:
            print 'Computing cross-validation errors on a grid of up to', self.max_components, 'features used in the', \
                'reduced feature space...'

        for k in xrange(self.max_components):
            cverror_args = []
            ytest = []
            fold_idx = 0
            for train_idx, test_idx in cv:
                if self.n_jobs == 1:
                    cverror_args.append((X[train_idx, :], y[train_idx], X[test_idx, :], k + 1, scoefs[fold_idx]))
                else:
                    cverror_args.append((self, X[train_idx, :], y[train_idx], X[test_idx, :], k + 1, scoefs[fold_idx]))
                ytest.append(y[test_idx])
                fold_idx += 1

            if self.n_jobs == 1:
                ypredictions = map(self._compute_cv_prediction, cverror_args)
            else:
                ypredictions = pool.map(compute_cv_prediction, cverror_args)

            cverror_k = 0.0
            for yt, yp in zip(ytest, ypredictions):
                cverror_k += lossfunc(yt, yp) / K_folds
            cverrors[k] = cverror_k

        if cvplot:
            plt.plot(np.arange(1, self.max_components + 1), cverrors)
            plt.xlabel('# of features in reduced set')
            plt.ylabel('CV Loss Function')
            plt.show()

        n_reduced = cverrors.argmin() + 1

        if verbose:
            print 'Selected', n_reduced, 'features to use in the reduced feature set.'

        return n_reduced
dataset/ETHPy150Open brandonckelly/bck_stats/bck_stats/super_pca.py/SupervisedPCA.choose_nreduced/swapped operands of `>`