Ejemplo n.º 1
0
    def predict(self,
                xdata=None,
                spline_parameters=None,
                df=None,
                spline_type=None,
                *args,
                **kwargs):
        if xdata is None:
            xdata = self.xdata

        if spline_parameters is None:
            if self.spline_parameters is None:
                warn(
                    "Spline parameters are not specified, you should either fit a model or specify them. "
                    "Output is set to 0")
                return np.zeros((xdata.shape[0], 1))
            else:
                spline_parameters = self.spline_parameters

        if df is None:
            if self.df is None:
                warn(
                    "Splines df is not specified, you should either fit a model or specify them. "
                    "Output is set to 0")
                return np.zeros((xdata.shape[0], 1))
            else:
                df = self.df

        if spline_type is None:
            if self.spline_type is None:
                warn(
                    "Spline spline_type are not specified, you should either fit a model or specify them. "
                    "Output is set to 0")
                return np.zeros((xdata.shape[0], 1))
            else:
                spline_type = self.spline_type

        fit_wrapper = LR(fit_intercept=False)
        fit_wrapper.coef = spline_parameters

        if spline_type == RegressionSplinesSmoother._spline_type_list.index(
                'bs'):
            y_pred = np.dot(
                dmatrix("bs(predictor, df=" + str(self.df) +
                        ", include_intercept=False)", {"predictor": xdata},
                        return_type='dataframe'), spline_parameters)

        elif spline_type == RegressionSplinesSmoother._spline_type_list.index(
                'cr'):
            y_pred = np.dot(
                dmatrix("cr(predictor, df=" + str(self.df) + ")",
                        {"predictor": xdata},
                        return_type='dataframe'), spline_parameters)

        else:
            raise ValueError('Please, specify a valid splines_type')

        return y_pred