def fit(self, X, y): """パラメータの推定を行う. Parameters ---------- X : array-like, shape = (n_samples, n_features) Data matrix y : structured array, shape = (n_samples,) [(打切り変数, 観測時間), ...] Returns ------- self """ X, event, time = check_arrays_survival(X, y) if self.alpha < 0: raise ValueError('alpha must be positive, but was %r' % self.alpha) log_likelihood = WeibullRMLogLikelihood(X, event, time, self.alpha) verbose = self.verbose n_params = X.shape[1] + 1 parameters = np.zeros(n_params) parameters[-1] = 0.5 # sigmaは分母として用いるので0 i = 0 while True: if i >= self.n_iter: if verbose: print('iter {:>6d}: reached maximum number of iterations. Stopping.'.format(i + 1)) print('Optimization did not converge: Maximum number of iterations has been exceeded.') break log_likelihood.update(parameters) delta = solve(log_likelihood.hessian, log_likelihood.gradient, overwrite_a=False, overwrite_b=False, check_finite=False) if not np.all(np.isfinite(delta)): raise ValueError('search direction contains NaN or infinite values') parameters_new = parameters - delta if verbose: print('iter {: > 6d}: update={}'.format(i + 1, delta)) parameters = parameters_new res = np.abs(delta) if np.all(res < self.tol): break else: i += 1 self.w_ = parameters[:-1] self.sigma_ = parameters[-1] # self.hessian, self.gradient は attribute で残しときたい. self.hessian = log_likelihood.hessian self.gradient = log_likelihood.gradient return self
def fit(self, X, y): """パラメータの推定を行う. Parameters ---------- X : pandas.DataFrame, shape = (n_samples, n_features) Data matrix y : structured array, shape = (n_samples,) [(打切り変数, 観測時間), ...] Returns ------- self """ # lifelines.CoxPHFitter の fit() に渡すためのデータフレームを整形. feature_name_list = X.columns.values.tolist() name_event, name_time = y.dtype.names x, event, time = check_arrays_survival(X, y) y_df = pd.DataFrame([time, event]).T y_df.columns = [name_time, name_event] x_df = pd.DataFrame(x, columns=feature_name_list) df = pd.merge(x_df, y_df, left_index=True, right_index=True) super().fit(df, duration_col=name_time, event_col=name_event, strata=self.strat_list) # self.w_ は attribute で残しときたい. self.w_ = self.hazards_.values[0] self.feature_name_list = feature_name_list return self
def fit(self, X, y, id_): """モデルの学習処理. Parameters ---------- X : array-like, shape = (n_samples, n_features) Data matrix y : structured array, shape = (n_samples,) [(打切り変数, 観測時間), ...] Returns ------- self """ X, event, time = check_arrays_survival(X, y) if self.alpha < 0: raise ValueError('alpha must be positive, but was %r' % self.alpha) init_parameters = np.zeros(X.shape[1]) parameter = optimize.minimize(logistic_log_likelihood, init_parameters, args=(X, event, time, id_), method='Nelder-Mead') print(parameter) return self
def fit(self, X, y, sample_weight=None): X, _, time = check_arrays_survival(X, y) return super().fit(X, time)