def RFReprodExchTrees(list_indiv, max_id, options): list_indiv = list(list_indiv) clfp1 = list_indiv[0].clf clfp2 = list_indiv[1].clf clfchild = copy.deepcopy(clfp1) if options['cross_shuffle']: n1 = clfp1.n_estimators // 2 n2 = clfp2.n_estimators // 2 clf1_p = np.random.choice(clfp1.estimators_, size=n1, replace=False) clf2_p = np.random.choice(clfp2.estimators_, size=n2, replace=False) clfchild.estimators_ = list(clf1_p) + list(clf2_p) else: inds = np.random.randint(0, clfp2.n_estimators, clfp2.n_estimators // 2) for j in inds: clfchild.estimators_[j] = clfp2.estimators_[j] clfchild.n_estimators = len(clfchild.estimators_) new_id = max_id + 1 indiv3 = genetic.individual(clfchild, new_id, type_rf=True, alpha=options['alpha'], evaluate_on_data=options['on_data'], X=options['X'], y=options['y']) return indiv3
def RFReprodCrossOver(list_indiv, max_id, options): list_indiv = list(list_indiv) if len(list_indiv) > 2: print('Warning : more than 2 parents..') elif len(list_indiv) < 2: print('Warning : parent missing..') else: clfp1 = list_indiv[0].clf clfp2 = list_indiv[1].clf clfchild = copy.deepcopy(clfp1) new_id = max_id + 1 for k, e in enumerate(clfp1.estimators_): ### node = np.random.randint(e.tree_.node_count) ### clfchild.estimators_[k] = CoherentFusionDecisionTree( e, node, clfp2.estimators_[k]) indiv3 = genetic.individual(clfchild, new_id, type_rf=True, alpha=options['alpha'], evaluate_on_data=options['on_data'], X=options['X'], y=options['y']) return indiv3
def ReprodIndividualsFromRF(list_indiv, max_id, options): list_indiv = list(list_indiv) rf = RandomForestClassifier(n_estimators=len(list_indiv)) trees = list() for indiv in list_indiv: trees.append(indiv.clf) rf.estimators_ = trees rf.n_classes_ = trees[0].n_classes_ rf.classes_ = trees[0].classes_ new_dt = eqtree_rec_rf(rf, 0, max_depth=options['max_depth'], smallest_tree=False) new_id = max_id + 1 indiv3 = genetic.individual(new_dt, new_id, type_rf=False, alpha=options['alpha'], evaluate_on_data=options['on_data'], X=options['X'], y=options['y']) return indiv3
def ReprodCrossOver(list_indiv, max_id, options): list_indiv = list(list_indiv) if len(list_indiv) > 2: print('Warning : more than 2 parents..') elif len(list_indiv) < 2: print('Warning : parent missing..') else: new_id = max_id + 1 ### node = np.random.randint(list_indiv[0].clf.tree_.node_count) ### new_dt = CoherentFusionDecisionTree(list_indiv[0].clf, node, list_indiv[1].clf) indiv3 = genetic.individual(new_dt, new_id, type_rf=False, alpha=options['alpha'], evaluate_on_data=options['on_data'], X=options['X'], y=options['y']) return indiv3
1, L_c, 0.005, EV_type='depth', type_rf=True, flow_weighted_costs=True, on_data=True, X=X_train_budget, y=y_train_budget) for i in range(N_rep): ind_init = genetic.individual(clf_init, 0, alpha, type_rf=True, evaluate_on_data=True, X=X_train_budget, y=y_train_budget) init_value = ind_init.compute_value(budget) init_depth = ind_init.compute_depth() init_model_size = ind_init.compute_model_size() pop = genetic.population(alpha, type_rf=True, evaluate_on_data=True, X_source=X_train_init, y_source=y_train_init, X=X_train_budget, y=y_train_budget, budget_function=budget,
def test_individual(): """Test individual generation""" config = genetic.individual() assert(isinstance(config, list)) assert(len(config) == genetic.genes)