Ejemplo n.º 1
0
    def predict_proba(self, X, input_checks=True, **kwargs):
        """
        Find probability estimates for each class for all cases in X.
        Parameters
        ----------
        X : a nested pd.Dataframe, or (if input_checks=False) array-like of
        shape = (n_instances, series_length, n_dimensions)
            The training input samples. If a 2D array-like is passed,
            n_dimensions is assumed to be 1.
        input_checks: boolean
            whether to check the X parameter
        Returns
        -------
        output : array of shape = [n_instances, n_classes] of probabilities
        """
        check_is_fitted(self)

        X = check_and_clean_data(X, input_checks=input_checks)

        probs = self.model.predict(X, **kwargs)

        # check if binary classification
        if probs.shape[1] == 1:
            # first column is probability of class 0 and second is of class 1
            probs = np.hstack([1 - probs, probs])

        return probs
Ejemplo n.º 2
0
    def predict_proba(self, X, input_checks=True, **kwargs):
        """
        Find probability estimates for each class for all cases in X.
        Parameters
        ----------
        X : a nested pd.Dataframe, or (if input_checks=False) array-like of
        shape = (n_instances, series_length, n_dimensions)
            The training input samples. If a 2D array-like is passed,
            n_dimensions is assumed to be 1.
        input_checks: boolean
            whether to check the X parameter
        Returns
        -------
        output : array of shape = [n_instances, n_classes] of probabilities
        """
        check_is_fitted(self)

        X = check_and_clean_data(X, input_checks=input_checks)

        # transform and predict prodba on the ridge classifier.
        X_transformed = self.transform_to_feature_space(X)
        y_pred = self.model.predict(X_transformed)

        # self.reshape_prediction will give us PREDICTIONS,
        # not DISTRIBUTIONS (even if only one-hot)
        # Computing first 2 lines of that but not the last here

        # reshape so the first axis has the number of instances
        new_y_pred = y_pred.reshape(X.shape[0], X.shape[1], y_pred.shape[-1])
        # average the predictions of instances
        return np.average(new_y_pred, axis=1)
Ejemplo n.º 3
0
    def predict(self, X, input_checks=True, **kwargs):
        """
        Find regression estimate for all cases in X.
        Parameters
        ----------
        X : a nested pd.Dataframe, or (if input_checks=False) array-like of
        shape = (n_instances, series_length, n_dimensions)
            The training input samples. If a 2D array-like is passed,
            n_dimensions is assumed to be 1.
        input_checks: boolean
            whether to check the X parameter
        Returns
        -------
        predictions : 1d numpy array
            array of predictions of each instance
        """
        check_is_fitted(self)

        X = check_and_clean_data(X, input_checks=input_checks)

        y_pred = self.model.predict(X, **kwargs)

        if y_pred.ndim == 1:
            y_pred.ravel()
        return y_pred
Ejemplo n.º 4
0
    def predict(self, X, input_checks=True, **kwargs):
        """
        Find regression estimate for all cases in X.
        Parameters
        ----------
        X : a nested pd.Dataframe, or (if input_checks=False) array-like of
        shape = (n_instances, series_length, n_dimensions)
            The training input samples. If a 2D array-like is passed,
            n_dimensions is assumed to be 1.
        input_checks: boolean
            whether to check the X parameter
        Returns
        -------
        predictions : 1d numpy array
            array of predictions of each instance
        """
        check_is_fitted(self)

        X = check_and_clean_data(X, input_checks=input_checks)

        X, _, tot_increase_num = self.pre_processing(X)

        preds = self.model.predict(X, batch_size=self.batch_size)

        y_predicted = []
        test_num_batch = int(X.shape[0] / tot_increase_num)

        # TODO: could fix this to be an array literal.
        for i in range(test_num_batch):
            y_predicted.append(
                np.average(
                    preds[i * tot_increase_num:((i + 1) * tot_increase_num) -
                          1],
                    axis=0,
                ))

        y_pred = np.array(y_predicted)

        if y_pred.ndim == 1:
            y_pred.ravel()

        return y_pred
Ejemplo n.º 5
0
    def predict_proba(self, X, input_checks=True, **kwargs):
        """
        Find probability estimates for each class for all cases in X.
        Parameters
        ----------
        X : a nested pd.Dataframe, or (if input_checks=False) array-like of
        shape = (n_instances, series_length, n_dimensions)
            The training input samples. If a 2D array-like is passed,
            n_dimensions is assumed to be 1.
        input_checks: boolean
            whether to check the X parameter
        Returns
        -------
        output : array of shape = [n_instances, n_classes] of probabilities
        """
        check_is_fitted(self)

        X = check_and_clean_data(X, input_checks=input_checks)

        X, _, tot_increase_num = self.pre_processing(X)

        preds = self.model.predict(X, batch_size=self.batch_size)

        test_num_batch = int(X.shape[0] / tot_increase_num)

        y_predicted = [
            np.average(
                preds[i * tot_increase_num:((i + 1) * tot_increase_num) - 1],
                axis=0)
            for i in range(test_num_batch)
        ]

        y_pred = np.array(y_predicted)

        keras.backend.clear_session()

        return y_pred
Ejemplo n.º 6
0
    def predict_proba(self, X, input_checks=True, **kwargs):
        """
        Find probability estimates for each class for all cases in X.
        Parameters
        ----------
        X : a nested pd.Dataframe, or (if input_checks=False) array-like of
        shape = (n_instances, series_length, n_dimensions)
            The training input samples. If a 2D array-like is passed,
            n_dimensions is assumed to be 1.
        input_checks: boolean
            whether to check the X parameter
        Returns
        -------
        output : array of shape = [n_instances, n_classes] of probabilities
        """
        check_is_fitted(self)

        X = check_and_clean_data(X, input_checks=input_checks)

        ori_len = X.shape[1]  # original_length of time series

        # restrict slice ratio when data lenght is too large
        current_slice_ratio = self.slice_ratio
        if ori_len > 500:
            current_slice_ratio = (
                self.slice_ratio if self.slice_ratio > 0.98 else 0.98
            )

        increase_num = (
                ori_len - int(ori_len * current_slice_ratio) + 1
        )  # this can be used as the bath size

        # will need to slice at some poin

        x_test, _ = self.slice_data(X, None, current_slice_ratio)

        length_train = x_test.shape[1]  # length after slicing.

        current_window_size = (
            int(length_train * self.window_size)
            if self.window_size < 1
            else int(self.window_size)
        )

        ds_num_max = length_train / (
                self.best_pool_factor * current_window_size
        )
        current_ds_num = int(min(self.ds_num, ds_num_max))

        # need to batch and downsample the test data.
        ma_test, ma_lengths = self.movingavrg(
            x_test, self.ma_base, self.ma_step, self.ma_num
        )
        ds_test, ds_lengths = self.downsample(
            x_test, self.ds_base, self.ds_step, current_ds_num
        )

        test_set_x = x_test

        # concatenate directly
        data_lengths = [length_train]
        # downsample part:
        if ds_lengths != []:
            data_lengths += ds_lengths
            test_set_x = np.concatenate([test_set_x, ds_test], axis=1)
        # moving average part
        if ma_lengths != []:
            data_lengths += ma_lengths
            test_set_x = np.concatenate([test_set_x, ma_test], axis=1)

        test_num = x_test.shape[0]
        test_num_batch = int(test_num / increase_num)

        # get the true predictions of the test set
        y_predicted = []
        for i in range(test_num_batch):
            x = test_set_x[i * (increase_num): (i + 1) * (increase_num)]
            preds = self.model.predict_on_batch(
                self.split_input_for_model(x, self.input_shapes)
            )
            y_predicted.append(np.average(preds, axis=0))

        y_pred = np.array(y_predicted)

        return y_pred