+ def _fill_nan(self):
+ '''
+ Fill NaN values, either by propagation or by interpolation (linear or splines)
+ '''
+ logger.info("Filling NaN numerical values in the feature dataframe")
+ # We interpolate (linearly or with splines) only numerical columns
+ # The interpolation
+ if self._config['PREPROCESSING']['fill_method'] == 'propagate':
+ self._dataframe[self._numerical_columns] =\
+ self._dataframe[self._numerical_columns].fillna(method='ffill')
+ elif self._config['PREPROCESSING']['fill_method'] == 'linear':
+ self._dataframe[self._numerical_columns] =\
+ self._dataframe[self._numerical_columns].interpolate()
+ elif self._config['PREPROCESSING']['fill_method'] == 'spline':
+ self._dataframe[self._numerical_columns] =\
+ self._dataframe[self._numerical_columns].interpolate(method='spline',
+ order=self._config['PREPROCESSING'].getint('order'))
+
+ # For the categorical columns, NaN values are filled by duplicating
+ # the last known value (forward fill method)
+ logger.info("Filling NaN categorical values in the feature dataframe")
+ self._dataframe[self._categorical_columns] =\
+ self._dataframe[self._categorical_columns].fillna(method='ffill')
+
+ # Uncomment this line to fill NaN values at the beginning of the
+ # dataframe. This may not be a good idea, especially for features
+ # that are available only for recent years, e.g., air quality
+ #self._dataframe = self._dataframe.fillna(method='bfill')
+
+ # Dropping rows that are not related to our datetime window (start/
+ # step / end)
+ logger.info("Dropping rows that are not related to our datetime window")
+ dates = tuple((x.year, x.month, x.day, x.hour) for x in self._datetimes)
+ self._dataframe['row_ok'] =\
+ self._dataframe.apply(lambda x: (int(x.year), int(x.month), int(x.dayInMonth), int(x.hour)) in dates, axis=1)
+ self._dataframe = self._dataframe[self._dataframe['row_ok']]
+ self._dataframe = self._dataframe.drop(['row_ok'], axis=1)
+ logger.info("Rows dropped")
+
+ def _add_history(self):
+ '''
+ Integrating previous nb of interventions as features
+ '''
+ logger.info("Integrating previous nb of interventions as features")
+ nb_lines = eval(self._config['HISTORY_KNOWLEDGE']['nb_lines'])
+ for k in range(1, nb_lines + 1):
+ name = 'history_' + str(nb_lines - k + 1)
+ self._dataframe[name] = [np.NaN] * k + list(self._dict_target.values())[:-k]
+ self._numerical_columns.append(name)
+ self._dataframe = self._dataframe[nb_lines:]
+
+ def _standardize(self):
+ '''
+ Normalizing numerical features
+ '''
+ logger.info("Standardizing numerical values in the feature dataframe")
+ # We operate only on numerical columns
+ self._dataframe[self._numerical_columns] =\
+ preprocessing.scale(self._dataframe[self._numerical_columns])