def predict(self, x): if hasattr(self, "scaler"): x = self.scaler.transform(x) x = add_bias_feature(x) return [self.predict_single_(x_) for x_ in x]
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()
def predict(self, x): if self.scale_data: x = self._scaler.transform(x) x = add_bias_feature(x) return np.array([self.predict_single_(x_) for x_ in x])
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
def predict(self, x): x = check_array(x) if hasattr(self, "scaler"): x = self.scaler.transform(x) x = add_bias_feature(x) return h(x, self._theta).reshape(len(x))
def predict_proba(self, x): if hasattr(self, "scaler"): x = self.scaler.transform(x) x = add_bias_feature(x) p = np.empty((len(x), len(self.class_data))) for i in range(len(x)): x_ = x[i] p[i, :] = [h(x_, self.class_data[c]["theta"])[0] for c in self.class_data] return p
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