Ejemplo n.º 1
0
    def train(self, x, y):

        if len(y.shape) == 1:
            y = y.reshape((y.size, 1))
        elif len(y.shape) == 2:
            if y.shape[1] != 1:
                # If y is provided as a row instead of a column,
                # just transpose it into a column
                if y.shape[0] == 1:
                    y = y.T
                else:
                    raise ValueError('all training results must have one column')
        else:
            raise ValueError('y must be a 1- or 2-dimensional matrix')

        if x.shape[0] != y.shape[0]:
            raise ValueError('you must provide the same number of input features as input results')

        if self.normalizer:
            self.normalizer.set_basis(x)

        x_train = self.normalizer.normalize(x) if self.normalizer else x
        if self.pad_x:
            x_train = pad_ones(x_train)

        self.theta = self.generator.calculate(x_train, y)
        self.theta = self.theta.reshape([self.theta.shape[0], 1])
Ejemplo n.º 2
0
    def predict(self, x):
        if not self.theta.size:
            raise Exception('this classifier has not been trained yet')

        if self.normalizer:
            x = self.normalizer.normalize(x)

        n = x.shape[1]
        expected_n = self.theta.shape[0] - (1 if self.pad_x else 0)
        if n != expected_n:
            raise ValueError('this classifier was trained using inputs with %s features but this input has %s features'
                % (str(expected_n), str(n)))

        x_predict = pad_ones(x) if self.pad_x else x
        return x_predict.dot(self.theta)