Ejemplo n.º 1
0
    def fit(self, x, y, plot_cost=False):
        if hasattr(self, "scaler"):
            x = self.scaler.fit_transform(x)

        x = add_bias_feature(x)

        # one-vs-all
        for class_type in set(y):
            cur_y = np.ones(len(y))
            cur_y[y != class_type] = 0
            self.class_data[class_type] = {"y": cur_y}

            gd = GradientDescent(mode='logistic',
                                 track_cost=plot_cost,
                                 learning_rate=self.learning_rate,
                                 penalty=self.penalty,
                                 alpha=self.alpha,
                                 max_iterations=self.max_iterations)

            cur_theta = gd.find_theta(x, cur_y)
            self.class_data[class_type] = {"theta": cur_theta}

            if plot_cost:
                cost_x, cost_y = gd.get_cost_history()
                plt.plot(cost_x, cost_y, "r-")
                plt.title(F"Cost for class '{class_type}' (last value={gd.last_cost:0.6f})")
                plt.show()
Ejemplo n.º 2
0
    def fit(self, x, y, plot_cost=False):
        x, y = check_X_y(x, y)

        if hasattr(self, "scaler"):
            x = self.scaler.fit_transform(x)

        x = add_bias_feature(x)

        gd = GradientDescent(mode='linear',
                             track_cost=plot_cost,
                             learning_rate=self.learning_rate,
                             penalty=self.penalty,
                             alpha=self.alpha,
                             max_iterations=self.max_iterations)

        theta = gd.find_theta(x, y)

        if plot_cost:
            cost_x, cost_y = gd.get_cost_history()
            plt.plot(cost_x, cost_y, "r-")
            plt.title(F"Cost (last value={gd.last_cost:0.6f})")
            plt.show()

        self._theta = theta
        self.intercept_ = theta[0, 0]
        self.coef_ = theta[1:].reshape(len(theta) - 1)

        check_is_fitted(self, attributes=['intercept_', 'coef_'])
        return self
Ejemplo n.º 3
0
    def fit(self, x, y, plot_cost=False):
        x, y = check_X_y(x, y)

        if self.scale_data:
            self._scaler = StandardScaler()
            x = self._scaler.fit_transform(x)

        x = add_bias_feature(x)

        # one-vs-all
        for class_type in set(y):
            cur_y = np.ones(len(y))
            cur_y[y != class_type] = 0
            self._class_data[class_type] = {"y": cur_y}

            gd = GradientDescent(mode='logistic',
                                 track_cost=plot_cost,
                                 learning_rate=self.learning_rate,
                                 penalty=self.penalty,
                                 alpha=self.alpha,
                                 max_iterations=self.max_iterations)

            cur_theta = gd.find_theta(x, cur_y)
            self._class_data[class_type] = {"theta": cur_theta}

            if plot_cost:
                cost_x, cost_y = gd.get_cost_history()
                plt.plot(cost_x, cost_y, "r-")
                plt.title(F"Cost for class '{class_type}' (last value={gd.last_cost:0.6f})")
                plt.show()

        coef = [self._class_data[key]["theta"][1:, 0] for key in self._class_data]
        intercept = [self._class_data[key]["theta"][0, 0] for key in self._class_data]

        self.coef_ = np.array(coef[1:]) if len(coef) == 2 else np.array(coef)
        self.intercept_ = np.array(intercept[1:]) if len(intercept) == 2 else np.array(intercept)

        check_is_fitted(self, attributes=['intercept_', 'coef_'])
        return self