def compose_chain(self, data: InputData, initial_chain: Optional[Chain], composer_requirements: ComposerRequirements, metrics: Optional[Callable], optimiser_parameters=None, is_visualise: bool = False) -> Chain: new_chain = Chain() if self.dummy_chain_type == DummyChainTypeEnum.hierarchical: # (y1, y2) -> y last_node = SecondaryNode(composer_requirements.secondary[0]) for requirement_model in composer_requirements.primary: new_node = PrimaryNode(requirement_model) new_chain.add_node(new_node) last_node.nodes_from.append(new_node) new_chain.add_node(last_node) elif self.dummy_chain_type == DummyChainTypeEnum.flat: # (y1) -> (y2) -> y first_node = PrimaryNode(composer_requirements.primary[0]) new_chain.add_node(first_node) prev_node = first_node for requirement_model in composer_requirements.secondary: new_node = SecondaryNode(requirement_model) new_node.nodes_from = [prev_node] prev_node = new_node new_chain.add_node(new_node) else: raise NotImplementedError() return new_chain
def chain_with_secondary_nodes_only(): first = SecondaryNode(model_type='logit', nodes_from=[]) second = SecondaryNode(model_type='logit', nodes_from=[first]) chain = Chain() chain.add_node(first) chain.add_node(second) return chain
def default_valid_chain(): first = PrimaryNode(model_type='logit') second = SecondaryNode(model_type='logit', nodes_from=[first]) third = SecondaryNode(model_type='logit', nodes_from=[first]) final = SecondaryNode(model_type='logit', nodes_from=[second, third]) chain = Chain(final) return chain
def chain_with_cycle(): first = PrimaryNode(model_type='logit') second = SecondaryNode(model_type='logit', nodes_from=[first]) third = SecondaryNode(model_type='logit', nodes_from=[second, first]) second.nodes_from.append(third) chain = Chain() for node in [first, second, third]: chain.add_node(node) return chain
def chain_with_multiple_roots(): first = PrimaryNode(model_type='logit') root_first = SecondaryNode(model_type='logit', nodes_from=[first]) root_second = SecondaryNode(model_type='logit', nodes_from=[first]) chain = Chain() for node in [first, root_first, root_second]: chain.add_node(node) return chain
def chain_with_isolated_components(): first = PrimaryNode(model_type='logit') second = SecondaryNode(model_type='logit', nodes_from=[first]) third = SecondaryNode(model_type='logit', nodes_from=[]) fourth = SecondaryNode(model_type='logit', nodes_from=[third]) chain = Chain() for node in [first, second, third, fourth]: chain.add_node(node) return chain
def chain_with_isolated_nodes(): first = PrimaryNode(model_type='logit') second = SecondaryNode(model_type='logit', nodes_from=[first]) third = SecondaryNode(model_type='logit', nodes_from=[second]) isolated = SecondaryNode(model_type='logit', nodes_from=[]) chain = Chain() for node in [first, second, third, isolated]: chain.add_node(node) return chain
def valid_chain(): first = PrimaryNode(model_type='logit') second = SecondaryNode(model_type='logit', nodes_from=[first]) third = SecondaryNode(model_type='logit', nodes_from=[second]) last = SecondaryNode(model_type='logit', nodes_from=[third]) chain = Chain() for node in [first, second, third, last]: chain.add_node(node) return chain
def test_secondary_nodes_is_invariant_to_inputs_order(data_setup): data = data_setup train, test = train_test_data_setup(data) first = PrimaryNode(model_type='logit') second = PrimaryNode(model_type='lda') third = PrimaryNode(model_type='knn') final = SecondaryNode(model_type='xgboost', nodes_from=[first, second, third]) chain = Chain() for node in [first, second, third, final]: chain.add_node(node) first = deepcopy(first) second = deepcopy(second) third = deepcopy(third) final_shuffled = SecondaryNode(model_type='xgboost', nodes_from=[third, first, second]) chain_shuffled = Chain() # change order of nodes in list for node in [final_shuffled, third, first, second]: chain_shuffled.add_node(node) train_predicted = chain.fit(input_data=train) train_predicted_shuffled = chain_shuffled.fit(input_data=train) # train results should be invariant assert chain.root_node.descriptive_id == chain_shuffled.root_node.descriptive_id assert np.equal(train_predicted.predict, train_predicted_shuffled.predict).all() test_predicted = chain.predict(input_data=test) test_predicted_shuffled = chain_shuffled.predict(input_data=test) # predict results should be invariant assert np.equal(test_predicted.predict, test_predicted_shuffled.predict).all() # change parents order for the nodes fitted chain nodes_for_change = chain.nodes[3].nodes_from chain.nodes[3].nodes_from = [ nodes_for_change[2], nodes_for_change[0], nodes_for_change[1] ] chain.nodes[3].cache.clear() chain.fit(train) test_predicted_re_shuffled = chain.predict(input_data=test) # predict results should be invariant assert np.equal(test_predicted.predict, test_predicted_re_shuffled.predict).all()
def get_composite_lstm_chain(): chain = Chain() node_trend = PrimaryNode('trend_data_model') node_lstm_trend = SecondaryNode('lasso', nodes_from=[node_trend]) node_residual = PrimaryNode('residual_data_model') node_ridge_residual = SecondaryNode('ridge', nodes_from=[node_residual]) node_final = SecondaryNode( 'additive_data_model', nodes_from=[node_ridge_residual, node_lstm_trend]) chain.add_node(node_final) return chain
def get_composite_lstm_chain(): chain = Chain() node_trend = PrimaryNode('trend_data_model') node_trend.labels = ["fixed"] node_lstm_trend = SecondaryNode('linear', nodes_from=[node_trend]) node_trend.labels = ["fixed"] node_residual = PrimaryNode('residual_data_model') node_ridge_residual = SecondaryNode('linear', nodes_from=[node_residual]) node_final = SecondaryNode( 'additive_data_model', nodes_from=[node_ridge_residual, node_lstm_trend]) node_final.labels = ["fixed"] chain.add_node(node_final) return chain
def run_chain_from_automl(train_file_path: str, test_file_path: str, max_run_time: timedelta = timedelta(minutes=10)): train_data = InputData.from_csv(train_file_path) test_data = InputData.from_csv(test_file_path) testing_target = test_data.target chain = Chain() node_tpot = PrimaryNode('tpot') node_tpot.model.params = {'max_run_time_sec': max_run_time.seconds} node_lda = PrimaryNode('lda') node_rf = SecondaryNode('rf') node_rf.nodes_from = [node_tpot, node_lda] chain.add_node(node_rf) chain.fit(train_data) results = chain.predict(test_data) roc_auc_value = roc_auc(y_true=testing_target, y_score=results.predict) print(roc_auc_value) return roc_auc_value
def test_cache_historical_state_using(data_setup): train, _ = data_setup chain = chain_first() # chain fitted, model goes to cache chain.fit(input_data=train) new_node = SecondaryNode(model_type='logit') old_node = chain.root_node.nodes_from[0] # change child node to new one chain.update_node(old_node=old_node, new_node=new_node) # cache is not actual assert not chain.root_node.cache.actual_cached_state # fit modified chain chain.fit(input_data=train) # cache is actual now assert chain.root_node.cache.actual_cached_state # change node back chain.update_node(old_node=chain.root_node.nodes_from[0], new_node=old_node) # cache is actual without new fitting, # because the cached model was saved after first fit assert chain.root_node.cache.actual_cached_state
def chain_simple() -> Chain: node_first = PrimaryNode('svc') node_second = PrimaryNode('lda') node_final = SecondaryNode('rf', nodes_from=[node_first, node_second]) chain = Chain(node_final) return chain
def get_simple_chain(): first = PrimaryNode(model_type='xgboost') second = PrimaryNode(model_type='knn') final = SecondaryNode(model_type='logit', nodes_from=[first, second]) chain = Chain(final) return chain
def chain_with_pca() -> Chain: node_first = PrimaryNode('pca_data_model') node_second = PrimaryNode('lda') node_final = SecondaryNode('rf', nodes_from=[node_first, node_second]) chain = Chain(node_final) return chain
def chain_with_incorrect_decomposition_structure(): first = PrimaryNode(model_type='trend_data_model') second = PrimaryNode(model_type='residual_data_model') final = SecondaryNode(model_type='linear', nodes_from=[first, second]) chain = Chain(final) return chain
def chain_with_incorrect_task_type(): first = PrimaryNode(model_type='linear') second = PrimaryNode(model_type='linear') final = SecondaryNode(model_type='kmeans', nodes_from=[first, second]) chain = Chain(final) return chain, Task(TaskTypesEnum.classification)
def get_comp_chain(): node_first = PrimaryNode('lstm') node_second = PrimaryNode('rfr') node_final = SecondaryNode('linear', nodes_from=[node_first, node_second], manual_preprocessing_func=EmptyStrategy) chain = Chain(node_final) return chain
def chain_with_incorrect_root_model(): first = PrimaryNode(model_type='logit') second = PrimaryNode(model_type='logit') final = SecondaryNode(model_type='direct_data_model', nodes_from=[first, second]) chain = Chain(final) return chain
def chain_with_primary_composition_model(): first = PrimaryNode(model_type='additive_data_model') second = PrimaryNode(model_type='residual_data_model') final = SecondaryNode(model_type='additive_data_model', nodes_from=[first, second]) chain = Chain(final) return chain
def get_decomposed_chain(model_trend='lstm', model_residual='ridge'): chain = Chain() node_trend = PrimaryNode('trend_data_model') node_first_trend = SecondaryNode('lstm', nodes_from=[node_trend]) if model_trend == 'lstm': # decrease the number of epochs to fit node_first_trend.model.params = {'epochs': 1} node_residual = PrimaryNode('residual_data_model') node_model_residual = SecondaryNode(model_residual, nodes_from=[node_residual]) node_final = SecondaryNode( 'additive_data_model', nodes_from=[node_model_residual, node_first_trend]) chain.add_node(node_final) return chain
def chain_fourth(): # XG # | \ # XG KNN # | \ | \ # QDA KNN LR LDA # | \ | \ # RF RF KNN KNN chain = chain_first() new_node = SecondaryNode('qda') for model_type in ('rf', 'rf'): new_node.nodes_from.append(PrimaryNode(model_type)) chain.replace_node_with_parents(chain.root_node.nodes_from[0].nodes_from[1], new_node) new_node = SecondaryNode('knn') for model_type in ('knn', 'knn'): new_node.nodes_from.append(PrimaryNode(model_type)) chain.replace_node_with_parents(chain.root_node.nodes_from[0].nodes_from[0], new_node) return chain
def test_nodes_sequence_fit_correct(data_fixture, request): data = request.getfixturevalue(data_fixture) train, _ = train_test_data_setup(data) first = PrimaryNode(model_type='logit') second = SecondaryNode(model_type='lda', nodes_from=[first]) third = SecondaryNode(model_type='qda', nodes_from=[first]) final = SecondaryNode(model_type='knn', nodes_from=[second, third]) train_predicted = final.fit(input_data=train) assert final.descriptive_id == ('((/n_logit_default_params;)/' 'n_lda_default_params;;(/' 'n_logit_default_params;)/' 'n_qda_default_params;)/' 'n_knn_default_params') assert train_predicted.predict.shape[0] == train.target.shape[0] assert final.cache.actual_cached_state is not None
def get_regr_chain(): # Chain composition first = PrimaryNode(model_type='xgbreg') second = PrimaryNode(model_type='knnreg') final = SecondaryNode(model_type='linear', nodes_from=[first, second]) chain = Chain() chain.add_node(final) return chain
def chain_with_self_cycle(): first = PrimaryNode(model_type='logit') second = SecondaryNode(model_type='logit', nodes_from=[first]) second.nodes_from.append(second) chain = Chain() chain.add_node(first) chain.add_node(second) return chain
def get_class_chain(): # Chain composition first = PrimaryNode(model_type='xgboost') second = PrimaryNode(model_type='knn') final = SecondaryNode(model_type='logit', nodes_from=[first, second]) chain = Chain() chain.add_node(final) return chain
def two_level_chain(): first = PrimaryNode(model_type='logit') second = PrimaryNode(model_type='knn') third = SecondaryNode(model_type='xgboost', nodes_from=[first, second]) chain = Chain() for node in [first, second, third]: chain.add_node(node) return chain
def baseline_chain(): chain = Chain() last_node = SecondaryNode(model_type='xgboost', nodes_from=[]) for requirement_model in ['knn', 'logit']: new_node = PrimaryNode(requirement_model) chain.add_node(new_node) last_node.nodes_from.append(new_node) chain.add_node(last_node) return chain
def chain_third(): # QDA # | \ # RF RF chain = Chain() new_node = SecondaryNode('qda') for model_type in ('rf', 'rf'): new_node.nodes_from.append(PrimaryNode(model_type)) chain.add_node(new_node) [chain.add_node(node_from) for node_from in new_node.nodes_from] return chain