示例#1
0
    def predict(self, input_data):
        """
        Make a prediction from the input data.

        Parameters
        ----------
        input_data : array-like (n_samples, n_features)

        Raises
        ------
        ValueError
            In case if something is wrong with input data.

        Returns
        -------
        array-like (n_samples,)
        """
        if self.input_train is None:
            raise NotTrained("Cannot make a prediction. Network "
                             "hasn't been trained yet")

        input_data = format_data(input_data)

        input_data_size = input_data.shape[1]
        train_data_size = self.input_train.shape[1]

        if input_data_size != train_data_size:
            raise ValueError("Input data must contain {0} features, got "
                             "{1}".format(train_data_size, input_data_size))

        ratios = pdf_between_data(self.input_train, input_data, self.std)
        return (dot(self.target_train.T, ratios) / ratios.sum(axis=0)).T
示例#2
0
    def predict(self, X):
        """
        Make a prediction from the input data.

        Parameters
        ----------
        X : array-like (n_samples, n_features)

        Raises
        ------
        ValueError
            In case if something is wrong with input data.

        Returns
        -------
        array-like (n_samples,)
        """
        if self.X_train is None:
            raise NotTrained(
                "Cannot make a prediction. Network hasn't been trained yet")

        X = format_data(X)

        if X.shape[1] != self.X_train.shape[1]:
            raise ValueError("Input data must contain {0} features, got {1}"
                             "".format(self.X_train.shape[1], X.shape[1]))

        ratios = pdf_between_data(self.X_train, X, self.std)
        return (dot(self.y_train.T, ratios) / ratios.sum(axis=0)).T
示例#3
0
    def predict_raw(self, X):
        """
        Raw prediction.

        Parameters
        ----------
        X : array-like (n_samples, n_features)

        Raises
        ------
        NotTrained
            If network hasn't been trained.

        ValueError
            In case if something is wrong with input data.

        Returns
        -------
        array-like (n_samples, n_classes)
        """
        if self.classes is None:
            raise NotTrained(
                "Cannot make a prediction. Network hasn't been trained yet")

        if X.shape[1] != self.X_train.shape[1]:
            raise ValueError(
                "Input data must contain {0} features, got {1}"
                "".format(self.X_train.shape[1],  X.shape[1]))

        class_ratios = self.class_ratios.reshape((-1, 1))
        pdf_outputs = pdf_between_data(self.X_train, X, self.std)

        return np.dot(self.row_comb_matrix, pdf_outputs) / class_ratios
示例#4
0
    def reconstruct(self, X):
        if not isinstance(self.weight, np.ndarray):
            raise NotTrained("Network hasn't been trained yet")

        X = format_data(X)
        if X.shape[1] != self.minimized_data_size:
            raise ValueError("Invalid input data feature space, expected "
                             "{}, got {}.".format(X.shape[1],
                                                  self.minimized_data_size))

        return np.dot(X, self.weight.T)
示例#5
0
    def predict_output(self, X_bin, n_times=None):
        if self.weight is None:
            raise NotTrained("Network hasn't been trained yet")

        self.discrete_validation(X_bin)

        X_bin = format_data(X_bin, is_feature1d=False)
        X_sign = bin2sign(X_bin)
        y_sign = np.sign(X_sign.dot(self.weight))

        if self.mode == 'sync':
            return X_bin, sign2bin(y_sign)

        return self.apply_async_process(X_sign, y_sign, n_times)
示例#6
0
    def predict(self, input_data):
        if not self.initialized:
            raise NotTrained("LVQ network hasn't been trained yet")

        input_data = format_data(input_data)
        subclass_to_class = self.subclass_to_class
        weight = self.weight

        predictions = []
        for input_row in input_data:
            output = euclid_distance(input_row, weight)
            winner_subclass = int(output.argmin(axis=1))

            predicted_class = subclass_to_class[winner_subclass]
            predictions.append(predicted_class)

        return np.array(predictions)
示例#7
0
    def prediction(self, input_data=None, output_data=None, n_times=None):
        if self.weight is None:
            raise NotTrained("Network hasn't been trained yet")

        if input_data is None and output_data is not None:
            self.discrete_validation(output_data)
            output_data = bin2sign(output_data)
            input_data = np.sign(output_data.dot(self.weight.T))

        elif input_data is not None and output_data is None:
            self.discrete_validation(input_data)
            input_data = bin2sign(input_data)
            output_data = np.sign(input_data.dot(self.weight))

        else:
            raise ValueError("Prediction is possible only for input or output")

        n_output_features = output_data.shape[-1]
        n_input_features = input_data.shape[-1]

        if self.mode == 'async':
            if n_times is None:
                n_times = self.n_times

            for _ in range(n_times):
                input_position = randint(0, n_input_features - 1)
                output_position = randint(0, n_output_features - 1)

                input_data[:, input_position] = np.sign(
                    output_data.dot(self.weight[input_position, :]))
                output_data[:, output_position] = np.sign(
                    input_data.dot(self.weight[:, output_position]))

        return (
            self.format_predict(input_data),
            self.format_predict(output_data),
        )
示例#8
0
    def predict(self, X):
        if not isinstance(self.weight, np.ndarray):
            raise NotTrained("Network hasn't been trained yet")

        X = format_data(X)
        return np.dot(X, self.weight)
示例#9
0
    def predict(self, input_data):
        if not isinstance(self.weight, np.ndarray):
            raise NotTrained("Network hasn't been trained yet")

        input_data = format_data(input_data)
        return np.dot(input_data, self.weight)