def predict_proba(self, X): assert self.theta_ is not None, 'you should fit before predict' if self.copy_X: X = X.copy() if self.normalize: X = StandardScaler_().fit_transform(X) X = np.hstack([np.ones((X.shape[0], 1)), X]) return self._sigmoid(self.theta_, X)
def fit(self, X, y): if self.copy_X: X = X.copy() if self.normalize: X = StandardScaler_().fit_transform(X) X_b = np.c_[np.ones((len(X), 1)), X] # 拼接构造含有常数项b的新样本数据 if self.solver == 'gd': self._fit_gd(X_b, y) else: pass return self
def fit(self, X, y): if self.copy_X: X = X.copy() if self.normalize: X = StandardScaler_().fit_transform(X) X = np.hstack([np.ones((X.shape[0], 1)), X]) theta = np.ones(X.shape[1]) for _ in range(self.n_iters): theta -= self.alpha * self._dJ(theta, X, y) self.theta_ = theta self.coef_ = self.theta_[1:] self.intercept_ = self.theta_[0] return self
def predict(self, X_test): if self.copy_X: X_test = X_test.copy() if self.normalize: X_test = StandardScaler_().fit_transform(X_test) return np.c_[np.ones((len(X_test), 1)), X_test].dot(self._theta)