(self)
Calculate forecasted values using the Prophet library.
Calculate forecasted values using the Prophet library.
Calculate forecasted values using the Prophet library .
def predict(self):
        """
        Calculate forecasted values using the Prophet library.
        """
        
        # If the input data frame contains less than 2 non-Null rows, prediction is not possible
        if len(self.input_df) - self.input_df.y.isnull().sum() <= 2:
            
            if self.debug:
                self._print_log(3)
            
            # A series of null values is returned to avoid an error in Qlik
            return pd.Series([np.NaN for y in range(self.request_row_count)])
        
        # Instantiate a Prophet object and fit the input data frame:
        
        if len(self.prophet_kwargs) > 0:
            self.model = Prophet(**self.prophet_kwargs)
        else:
            self.model = Prophet()
        
        # Add custom seasonalities if defined in the arguments
        if self.name is not None and len(self.add_seasonality_kwargs) > 0:
            self.model.add_seasonality(**self.add_seasonality_kwargs)
        
        # Add additional regressors if defined in the arguments
        if self.has_regressors:
            i=0
            for regressor in self.regressors_df.columns:
                self.model.add_regressor(regressor, **self.regressor_kwargs[i])
                i+=1

        self.model.fit(self.input_df, **self.fit_kwargs)
             
        # Create a data frame for future values
        self.future_df = self.model.make_future_dataframe(**self.make_kwargs)
        
        # If a logistic growth model is applied add the cap and floor columns to the future data frame
        if self.cap is not None:
            self.future_df.loc[:,'cap'] = self.cap
            
            if self.floor is not None:
                self.future_df.loc[:,'floor'] = self.floor
        
        # Add additional regressors to the future data frame
        if self.has_regressors:
            # index_slice = self.regressors_df.shape[0] - self.periods
            for regressor in self.regressors_df.columns:
                self.future_df[regressor] = self.regressors_df.loc[:, regressor]

        if self.debug:
            self._print_log(4)

        # Prepare the forecast
        self._forecast()
                
        # If the function was called through the load script we return a Data Frame
        if self.load_script:            
            # If the response is the seasonality plot we return all seasonality components
            if self.is_seasonality_request:
                # Add an index column to the response
                self.response = self.forecast.reset_index()
            # Otherwise we add dates to the response
            else:
                # Set up the response data frame
                self.response = self.forecast if self.result_type == 'all' else self.forecast.loc[:, ['ds', self.result_type]]
                # Update the ds column as formatted strings
                self.response['ds'] = self.request_df['ds'].dt.strftime('%Y-%m-%d %r')
            
            if self.debug:
                self._print_log(5)
            
            # Send meta data on the response to Qlik
            self._send_table_description()
            
            return self.response
        else:
            if self.debug:
                self._print_log(5)

            return self.forecast.loc[:,self.result_type]
def predict ( self ) : # If the input data frame contains less than 2 non-Null rows, prediction is not possible if len ( self . input_df ) - self . input_df . y . isnull ( ) . sum ( ) <= 2 : if self . debug : self . _print_log ( 3 ) # A series of null values is returned to avoid an error in Qlik return pd . Series ( [ np . NaN for y in range ( self . request_row_count ) ] ) # Instantiate a Prophet object and fit the input data frame: if len ( self . prophet_kwargs ) > 0 : self . model = Prophet ( * * self . prophet_kwargs ) else : self . model = Prophet ( ) # Add custom seasonalities if defined in the arguments if self . name is not None and len ( self . add_seasonality_kwargs ) > 0 : self . model . add_seasonality ( * * self . add_seasonality_kwargs ) # Add additional regressors if defined in the arguments if self . has_regressors : i = 0 for regressor in self . regressors_df . columns : self . model . add_regressor ( regressor , * * self . regressor_kwargs [ i ] ) i += 1 self . model . fit ( self . input_df , * * self . fit_kwargs ) # Create a data frame for future values self . future_df = self . model . make_future_dataframe ( * * self . make_kwargs ) # If a logistic growth model is applied add the cap and floor columns to the future data frame if self . cap is not None : self . future_df . loc [ : , 'cap' ] = self . cap if self . floor is not None : self . future_df . loc [ : , 'floor' ] = self . floor # Add additional regressors to the future data frame if self . has_regressors : # index_slice = self.regressors_df.shape[0] - self.periods for regressor in self . regressors_df . columns : self . future_df [ regressor ] = self . regressors_df . loc [ : , regressor ] if self . debug : self . _print_log ( 4 ) # Prepare the forecast self . _forecast ( ) # If the function was called through the load script we return a Data Frame if self . load_script : # If the response is the seasonality plot we return all seasonality components if self . is_seasonality_request : # Add an index column to the response self . response = self . forecast . reset_index ( ) # Otherwise we add dates to the response else : # Set up the response data frame self . response = self . forecast if self . result_type == 'all' else self . forecast . loc [ : , [ 'ds' , self . result_type ] ] # Update the ds column as formatted strings self . response [ 'ds' ] = self . request_df [ 'ds' ] . dt . strftime ( '%Y-%m-%d %r' ) if self . debug : self . _print_log ( 5 ) # Send meta data on the response to Qlik self . _send_table_description ( ) return self . response else : if self . debug : self . _print_log ( 5 ) return self . forecast . loc [ : , self . result_type ]