def __init__(self, per_data, per_lens=None, n_states=None): if per_lens is None: per_lens = list(map(len, per_data)) if len(per_data.shape) > 2: per_data = per_data.reshape(-1, per_data.shape[-1]) if n_states is None: components = [2, 4, 6, 8, 10] hmms = [GaussianHMM(n_components=c) for c in components] map(lambda g: g.fit(per_data, per_lens), hmms) scores = map(lambda g: aic(g, per_data, per_lens), hmms) max_score, self.hmm = sorted(zip(scores, hmms))[0] else: self.hmm = GaussianHMM(n_components=n_states) self.hmm.fit(per_data, per_lens) ll = self.hmm.score(per_data, per_lens) print "Goal HMM n_components", self.hmm.n_components, "Log likelihood", ll upper_idxs = [per_lens[0] - 1] start_idxs = [0] for i in range(1, len(per_lens)): upper_idxs.append(upper_idxs[i - 1] + per_lens[i]) start_idxs.append(start_idxs[i - 1] + per_lens[i - 1]) self.final_states = np.array(self.hmm.predict(per_data, per_lens))[upper_idxs] print self.final_states self.T = int(np.mean(per_lens)) self.n_components = self.hmm.n_components
def aic(em_fit_result_dict): ''' Compute Akaike Information Criterion. ''' # Number of parameters: # - mixt_target: 1 # - mixt_random: 1 # - mixt_nontarget: K K = em_fit_result_dict['mixt_nontargets'].size + 2 return utils.aic(K, em_fit_result_dict['train_LL'])
def aic(em_fit_result_dict): ''' Compute Akaike Information Criterion. ''' # Number of parameters: # - mixt_target: 1 # - mixt_random: 1 # - mixt_nontarget: K # - kappa: 1 K = 3 + em_fit_result_dict['K'] return utils.aic(K, em_fit_result_dict['train_LL'])
def rf_func(k, combo): regr = RandomForestClassifier(random_state=0, n_estimators=10) ########## data = matrices[k, :, 1:][:, combo] response = binary_labels[k] reg_f = regr.fit(data, response) #features = reg_f.feature_importances_ loglik = utils.loglik_logistic(reg_f, data, response) aic = utils.aic(loglik, data.shape[0], data.shape[1]) locations = np.where(combo)[0] model_coefs = np.array(combo).astype(float) np.put(model_coefs, locations, reg_f.feature_importances_) return [aic, model_coefs]
def aic(em_fit_result_dict): ''' Compute Akaike Information Criterion. ''' # Number of parameters: # - mixt_target: Tnum # - mixt_random: Tnum # - mixt_nontargets: Tnum - 1 # - alpha: 1 # - beta: 1 K = em_fit_result_dict['mixt_target'].size + em_fit_result_dict['mixt_random'].size + em_fit_result_dict['mixt_nontargets'].size + 1 return utils.aic(K, em_fit_result_dict['train_LL'])
def aic(em_fit_result_dict): ''' Compute Akaike Information Criterion. ''' # Number of parameters: # - mixt_target_tr: Tnum # - mixt_random_tr: Tnum # - mixt_nontarget: sum(T_space - 1) # - alpha: 1 # - beta: 1 K = em_fit_result_dict['mixt_target_tr'].size + em_fit_result_dict['mixt_random_tr'].size + em_fit_result_dict['mixt_nontargets_tr'].size + 2 return utils.aic(K, em_fit_result_dict['train_LL'])