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 = NodeGenerator.secondary_node( composer_requirements.secondary[0]) for requirement_model in composer_requirements.primary: new_node = NodeGenerator.primary_node(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 = NodeGenerator.primary_node( composer_requirements.primary[0]) new_chain.add_node(first_node) prev_node = first_node for requirement_model in composer_requirements.secondary: new_node = NodeGenerator.secondary_node(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 metric_for_nodes(self, metric_function, train_data: InputData, test_data: InputData, is_chain_shared: bool, chain: Chain) -> float: validate(chain) if is_chain_shared: chain = SharedChain(base_chain=chain, shared_cache=self.shared_cache) chain.fit(input_data=train_data) return metric_function(chain, test_data)
def calculate_validation_metric(chain: Chain, dataset_to_validate: InputData) -> float: # the execution of the obtained composite models predicted = chain.predict(dataset_to_validate) # the quality assessment for the simulation results roc_auc_value = roc_auc(y_true=dataset_to_validate.target, y_score=predicted.predict) return roc_auc_value
def calculate_validation_metric( chain: Chain, dataset_to_validate: InputData) -> Tuple[float, float, float]: # the execution of the obtained composite models predicted = chain.predict(dataset_to_validate) # the quality assessment for the simulation results roc_auc_value = roc_auc(y_true=dataset_to_validate.target, y_score=predicted.predict, multi_class="ovo", average="macro") y_pred = [] y_values_pred = [[0, 0, 0] for _ in range(predicted.idx.size)] for i, predict in enumerate(predicted.predict): # true_class = dataset_to_validate.target[i] # y_class_pred = predict[true_class] y_class_pred = np.argmax(predict) # y_class_pred2 = np.argmax(predict) y_values_pred[i][y_class_pred] = 1 # y_pred.append(predicted.predict) y_pred = np.array([predict for predict in predicted.predict]) y_values_pred = np.array(y_values_pred) log_loss_value = log_loss(y_true=dataset_to_validate.target, y_pred=y_pred) # y_pred = [round(predict[0]) for predict in predicted.predict] # y_pred_acc = [predict for predict in y_values_pred] accuracy_score_value = accuracy_score(dataset_to_validate.target, y_values_pred) # np.ones((len(y_pred), len(dataset_to_validate.target)))) return roc_auc_value, log_loss_value, accuracy_score_value
def calculate_validation_metric_multiclass( chain: Chain, dataset_to_validate: InputData) -> Tuple[float, float, float]: # the execution of the obtained composite models predicted = chain.predict(dataset_to_validate) # the quality assessment for the simulation results # roc_auc_value = roc_auc(y_true=dataset_to_validate.target, # y_score=predicted.predict, # multi_class="ovr", average="weighted") y_pred = [] roc_auc_values = [] for predict, true in zip(predicted.predict, dataset_to_validate.target): roc_auc_score = roc_auc(y_true=true, y_score=predict) roc_auc_values.append(roc_auc_score) roc_auc_value = statistics.mean(roc_auc_values) for predict in predicted.predict: values = [] for val in predict: values.append(round(val)) y_pred.append(np.float64(values)) y_pred = np.array(y_pred) log_loss_value = log_loss(y_true=dataset_to_validate.target, y_pred=y_pred) accuracy_score_value = accuracy_score(y_true=dataset_to_validate.target, y_pred=y_pred) return roc_auc_value, log_loss_value, accuracy_score_value
def visualise(chain: Chain): try: chain.sort_nodes() graph, node_labels = as_nx_graph(chain=chain) pos = node_positions(graph.to_undirected()) plt.figure(figsize=(10, 16)) nx.draw(graph, pos=pos, with_labels=True, labels=node_labels, font_size=12, font_family='calibri', font_weight='bold', node_size=7000, width=2.0, node_color=colors_by_node_labels(node_labels), cmap='Set3') plt.show() except Exception as ex: print(f'Visualisation failed with {ex}')
def get_value(chain: Chain, reference_data: InputData) -> float: try: # validate(chain) results = chain.predict(reference_data) y_pred = [round(predict[0]) for predict in results.predict] score = round( accuracy_score(y_true=reference_data.target, y_pred=y_pred), 3) except Exception as ex: print(ex) score = 0.5 return score
def get_value(chain: Chain, reference_data: InputData) -> float: try: # validate(chain) results = chain.predict(reference_data) score = round( roc_auc_score(y_score=results.predict, y_true=reference_data.target), 3) except Exception as ex: print(ex) score = 0.5 return score
def calculate_validation_metric( chain: Chain, dataset_to_validate: InputData) -> Tuple[float, float, float]: # the execution of the obtained composite models predicted = chain.predict(dataset_to_validate) # the quality assessment for the simulation results roc_auc_value = roc_auc(y_true=dataset_to_validate.target, y_score=predicted.predict) y_pred = [np.float64(predict[0]) for predict in predicted.predict] log_loss_value = log_loss(y_true=dataset_to_validate.target, y_pred=y_pred) y_pred = [round(predict[0]) for predict in predicted.predict] accuracy_score_value = accuracy_score(y_true=dataset_to_validate.target, y_pred=y_pred) return roc_auc_value, log_loss_value, accuracy_score_value
def get_value(chain: Chain, reference_data: InputData) -> float: results = chain.predict(reference_data) return mean_squared_error(y_true=reference_data.target, y_pred=results.predict)