def test_katz(nee): # Evaluate exact katz implementation exact = katz.Katz(nee.traintest_split.TG) train_pred = exact.predict(nee.traintest_split.train_edges) test_pred = exact.predict(nee.traintest_split.test_edges) ms = score.Results(method='Katz', params={}, train_pred=train_pred, train_labels=nee.traintest_split.train_labels, test_pred=test_pred, test_labels=nee.traintest_split.test_labels) ms.pretty_print(results='test', precatk_vals=[2, 4, 6, 10, 100, 1000]) # ms.plot() # Evaluate approx katz implementation approx = katz.KatzApprox(nee.traintest_split.TG) train_pred = approx.fit_predict(nee.traintest_split.train_edges) test_pred = approx.fit_predict(nee.traintest_split.test_edges) ms = score.Results(method='Katz', params={}, train_pred=train_pred, train_labels=nee.traintest_split.train_labels, test_pred=test_pred, test_labels=nee.traintest_split.test_labels) ms.pretty_print(results='test', precatk_vals=[2, 4, 6, 10, 100, 1000])
def compute_results(self, data_split, method_name, train_pred, test_pred, params=None): r""" Generates results from the given predictions and returns them. Parameters ---------- data_split : EvalSplit An EvalSplit object that encapsulates the train/test or train/validation data. method_name : basestring A string indicating the name of the method for which the results will be created. train_pred : The link predictions for the train data. test_pred : The link predictions for the test data. params : dict A dictionary of parameters : values to be added to the results class. Returns ------- results : Results Returns the evaluation results. """ # Obtain the evaluation parameters parameters = self.get_parameters() if params is not None: parameters.update(params) results = score.Results(method=method_name, params=parameters, train_pred=train_pred, train_labels=data_split.train_labels, test_pred=test_pred, test_labels=data_split.test_labels) return results
def compute_results(self, data_split, method_name, train_pred, test_pred, label_binarizer=LogisticRegression(solver='liblinear'), params=None): r""" Generates results from the given predictions and returns them. Parameters ---------- data_split : EvalSplit An EvalSplit object that encapsulates the train/test or train/validation data. method_name : basestring A string indicating the name of the method for which the results will be created. train_pred : The link predictions for the train data. test_pred : The link predictions for the test data. label_binarizer : string or Sklearn binary classifier, optional If the predictions returned by the model are not binary, this parameter indicates how these binary predictions should be computed in order to be able to provide metrics such as the confusion matrix. Any Sklear binary classifier can be used or the keyword 'median' which will used the prediction medians as binarization thresholds. Default is LogisticRegression(solver='liblinear') params : dict, optional A dictionary of parameters : values to be added to the results class. Default is None. Returns ------- results : Results Returns the evaluation results. """ # Get global parameters if self.edge_embed_method is not None: parameters = {'dim': self.dim, 'edge_embed_method': self.edge_embed_method} else: parameters = {'dim': self.dim} # Get data related parameters parameters.update(self.traintest_split.get_parameters()) # Obtain the evaluation parameters if params is not None: parameters.update(params) results = score.Results(method=method_name, params=parameters, train_pred=train_pred, train_labels=data_split.train_labels, test_pred=test_pred, test_labels=data_split.test_labels, label_binarizer=label_binarizer) return results
# Other methods are: jaccard_coefficient, adamic_adar_index, preferential_attachment, resource_allocation_index train_pred = np.array( sim.common_neighbours(TG, ebunch=train_edges, neighbourhood='in')) test_pred = np.array( sim.common_neighbours(TG, ebunch=test_edges, neighbourhood='in')) # ------------------------------- # Evaluate LP using Results class # ------------------------------- # The results class automatically binarizes the method predictions and provides useful functions for plotting and # outputting the method results. # Instantiate a Results class results = score.Results(method='CN', params={}, train_pred=train_pred, train_labels=train_labels, test_pred=test_pred, test_labels=test_labels) # Compute auroc train_auroc = results.train_scores.auroc() test_auroc = results.test_scores.auroc() # Visualize the results print("Train AUROC {}: {}".format("common neighbours", train_auroc)) print("Test AUROC {}: {}".format("common neighbours", test_auroc)) # Compute precision recall curves # Options are: pr, roc, all results.plot(results='train', curve='all', filename=output_path + 'CN_train') results.plot(results='test', curve='all', filename=output_path + 'CN_test')