def score(self, x, y, block_to_score='y', sample_weight=None):
        """

        Predict and calculate the R2 for the model using one of the data blocks (X or Y) provided.
        Equivalent to the scikit-learn RegressorMixin score method.

        :param x: Data matrix to fit the PLS model.
        :type x: numpy.ndarray, shape [n_samples, n_features] or None
        :param y: Data matrix to fit the PLS model.
        :type y: numpy.ndarray, shape [n_samples, n_features] or None
        :param str block_to_score: Which of the data blocks (X or Y) to calculate the R2 goodness of fit.
        :param sample_weight: Optional sample weights to use in scoring.
        :type sample_weight: numpy.ndarray, shape [n_samples] or None
        :return R2Y: The model's R2Y, calculated by predicting Y from X and scoring.
        :rtype: float
        :return R2X: The model's R2X, calculated by predicting X from Y and scoring.
        :rtype: float
        :raise ValueError: If block to score argument is not acceptable or date mismatch issues with the provided data.
        """
        # TODO: Check how to implement sample_weight, which is expected in scikit-learn methods, for PLS algorithms
        try:
            if block_to_score not in ['x', 'y']:
                raise ValueError("x or y are the only accepted values for block_to_score")
            # Comply with the sklearn scaler behaviour
            if y.ndim == 1:
                y = y.reshape(-1, 1)
            # Not so important as don't expect a user applying a single x variable to a multivariate regression
            # method, but for consistency/testing purposes
            if x.ndim == 1:
                x = x.reshape(-1, 1)

            # Calculate RSSy/RSSx, R2Y/R2X
            if block_to_score == 'y':
                yscaled = deepcopy(self.y_scaler).fit_transform(y)
                # Calculate total sum of squares of X and Y for R2X and R2Y calculation
                tssy = np.sum(np.square(yscaled))
                ypred = self.y_scaler.transform(ChemometricsPLS.predict(self, x, y=None))
                rssy = np.sum(np.square(yscaled - ypred))
                R2Y = 1 - (rssy / tssy)
                return R2Y
            # The prediction here of both X and Y is done using the other block of data only
            # so these R2s can be interpreted as as a "classic" R2, and not as a proportion of variance modelled
            # Here we use X = Ub_uW', as opposed to (X = TP').
            else:
                xscaled = deepcopy(self.x_scaler).fit_transform(x)
                # Calculate total sum of squares of X and Y for R2X and R2Y calculation
                xpred = self.x_scaler.transform(ChemometricsPLS.predict(self, x=None, y=y))
                tssx = np.sum(np.square(xscaled))
                rssx = np.sum(np.square(xscaled - xpred))
                R2X = 1 - (rssx / tssx)
                return R2X
        except ValueError as verr:
            raise verr
def score ( self , x , y , block_to_score = 'y' , sample_weight = None ) : try : if block_to_score not in [ 'x' , 'y' ] : raise ValueError ( "x or y are the only accepted values for block_to_score" ) if y . ndim == 1 : y = y . reshape ( - 1 , 1 ) if x . ndim == 1 : x = x . reshape ( - 1 , 1 ) if block_to_score == 'y' : yscaled = deepcopy ( self . y_scaler ) . fit_transform ( y ) tssy = np . sum ( np . square ( yscaled ) ) ypred = self . y_scaler . transform ( ChemometricsPLS . predict ( self , x , y = None ) ) rssy = np . sum ( np . square ( yscaled - ypred ) ) R2Y = 1 - ( rssy / tssy ) return R2Y else : xscaled = deepcopy ( self . x_scaler ) . fit_transform ( x ) xpred = self . x_scaler . transform ( ChemometricsPLS . predict ( self , x = None , y = y ) ) tssx = np . sum ( np . square ( xscaled ) ) rssx = np . sum ( np . square ( xscaled - xpred ) ) R2X = 1 - ( rssx / tssx ) return R2X except ValueError as verr : raise verr
Predict and calculate the R2 for the model using one of the data blocks (X or Y) provided.
        Equivalent to the scikit-learn RegressorMixin score method.

        :param x: Data matrix to fit the PLS model.
        :type x: numpy.ndarray, shape [n_samples, n_features] or None
        :param y: Data matrix to fit the PLS model.
        :type y: numpy.ndarray, shape [n_samples, n_features] or None
        :param str block_to_score: Which of the data blocks (X or Y) to calculate the R2 goodness of fit.
        :param sample_weight: Optional sample weights to use in scoring.
        :type sample_weight: numpy.ndarray, shape [n_samples] or None
        :return R2Y: The model's R2Y, calculated by predicting Y from X and scoring.
        :rtype: float
        :return R2X: The model's R2X, calculated by predicting X from Y and scoring.
        :rtype: float
        :raise ValueError: If block to score argument is not acceptable or date mismatch issues with the provided data.