def evaluate_fold(self, test_tensor, test_needs, idx_test): # execute the rescal algorithm useNeedTypeSlice = (self.args.rescalsim[2] == 'True') useConnectionSlice = (self.args.rescalsim[3] == 'True') A, R = execute_rescal(test_tensor, self.rank, useNeedTypeSlice, useConnectionSlice) # use the most similar needs per need to predict connections self.log1() P_bin = predict_rescal_connections_by_need_similarity(A, self.threshold, self.offers, self.wants, test_needs) binary_pred = matrix_to_array(P_bin, idx_test) self.report.add_evaluation_data(self.ground_truth.getArrayFromSliceMatrix( SparseTensor.CONNECTION_SLICE, idx_test), binary_pred) if self.args.statistics: S = similarity_ranking(A) y_prop = [1.0 - i for i in np.nan_to_num(S[idx_test])] precision, recall, threshold = m.precision_recall_curve( self.ground_truth.getArrayFromSliceMatrix(SparseTensor.CONNECTION_SLICE, idx_test), y_prop) write_precision_recall_curve_file( self.output_folder + "/statistics/rescalsim_" + self.start_time, "precision_recall_curve_fold%d.csv" % self.foldNumber, precision, recall, threshold) TP, FP, threshold = m.roc_curve(self.ground_truth.getArrayFromSliceMatrix( SparseTensor.CONNECTION_SLICE, idx_test), y_prop) write_ROC_curve_file(self.output_folder + "/statistics/rescalsim_" + self.start_time, "ROC_curve_fold%d.csv" % self.foldNumber, TP, FP, threshold) self.evalDetails.add_statistic_details(self.ground_truth.getSliceMatrix( SparseTensor.CONNECTION_SLICE), P_bin, idx_test)
def predict_intersect_cosine_rescal(self, input_tensor, test_needs, idx_test, rank, rescal_threshold, cosine_threshold, useNeedTypeSlice): wants = input_tensor.getWantIndices() offers = input_tensor.getOfferIndices() # execute the cosine algorithm binary_pred_cosine = cosinus_link_prediciton(input_tensor, test_needs, cosine_threshold, 0.0, False) # execute the rescal algorithm A,R = execute_rescal(input_tensor, rank) P_bin = predict_rescal_connections_by_threshold(A, R, rescal_threshold, offers, wants, test_needs) # return the intersection of the prediction of both algorithms binary_pred_cosine = matrix_to_array(binary_pred_cosine, idx_test) binary_pred_rescal = matrix_to_array(P_bin, idx_test) binary_pred = [min(binary_pred_cosine[i], binary_pred_rescal[i]) for i in range(len(binary_pred_cosine))] return binary_pred, binary_pred_cosine, binary_pred_rescal
def evaluate_fold(self, test_tensor, test_needs, idx_test): self.logEvaluationLine() binary_pred = cosinus_link_prediciton(test_tensor, test_needs, self.threshold, self.transitive_threshold, self.weighted) self.report.add_evaluation_data(self.ground_truth.getArrayFromSliceMatrix( SparseTensor.CONNECTION_SLICE, idx_test), matrix_to_array(binary_pred, idx_test)) if self.args.statistics: self.evalDetails.add_statistic_details( self.ground_truth.getSliceMatrix(SparseTensor.CONNECTION_SLICE), binary_pred, idx_test)
def evaluate_fold(self, test_tensor, test_needs, idx_test): # set transitive connections before execution if (self.args.rescal[3] == 'True'): self.logger.info('extend connections transitively to the next need for RESCAL learning') test_tensor = extend_next_hop_transitive_connections(test_tensor) # execute the rescal algorithm useNeedTypeSlice = (self.args.rescal[2] == 'True') A, R = execute_rescal( test_tensor, self.rank, useNeedTypeSlice, init=self.args.rescal[4], conv=float(self.args.rescal[5]), lambda_A=float(self.args.rescal[6]), lambda_R=float(self.args.rescal[7]), lambda_V=float(self.args.rescal[8])) # evaluate the predictions self.logger.info('start predict connections ...') prediction = np.round_(predict_rescal_connections_array(A, R, idx_test), decimals=5) self.logger.info('stop predict connections') precision, recall, threshold = m.precision_recall_curve( self.ground_truth.getArrayFromSliceMatrix(SparseTensor.CONNECTION_SLICE, idx_test), prediction) optimal_threshold = get_optimal_threshold(recall, precision, threshold, self.args.fbeta) self.logger.info('optimal RESCAL threshold would be ' + str(optimal_threshold) + ' (for maximum F' + str(self.args.fbeta) + '-score)') auc = m.auc(recall, precision) self.AUC_test.append(auc) self.logger.info('AUC test: ' + str(auc)) # use a fixed threshold to compute several measures self.log1() P_bin = predict_rescal_connections_by_threshold(A, R, self.threshold, self.offers, self.wants, test_needs) binary_pred = matrix_to_array(P_bin, idx_test) self.report.add_evaluation_data(self.ground_truth.getArrayFromSliceMatrix( SparseTensor.CONNECTION_SLICE, idx_test), binary_pred) if self.args.statistics: write_precision_recall_curve_file( self.output_folder + "/statistics/rescal_" + self.start_time, "precision_recall_curve_fold%d.csv" % self.foldNumber, precision, recall, threshold) TP, FP, threshold = m.roc_curve(self.ground_truth.getArrayFromSliceMatrix( SparseTensor.CONNECTION_SLICE, idx_test), prediction) write_ROC_curve_file(self.output_folder + "/statistics/rescal_" + self.start_time, "ROC_curve_fold%d.csv" % self.foldNumber, TP, FP, threshold) self.evalDetails.add_statistic_details(self.ground_truth.getSliceMatrix( SparseTensor.CONNECTION_SLICE), P_bin, idx_test, prediction) self.foldNumber += 1
def predict_combine_cosine_rescal(self, input_tensor, test_needs, idx_test, rank, rescal_threshold, cosine_threshold, useNeedTypeSlice): wants = input_tensor.getWantIndices() offers = input_tensor.getOfferIndices() # execute the cosine algorithm first binary_pred_cosine = cosinus_link_prediciton(input_tensor, test_needs, cosine_threshold, 0.0, False) # use the connection prediction of the cosine algorithm as input for rescal temp_tensor = input_tensor.copy() temp_tensor.addSliceMatrix(binary_pred_cosine, SparseTensor.CONNECTION_SLICE) A,R = execute_rescal(temp_tensor, rank) P_bin = predict_rescal_connections_by_threshold(A, R, rescal_threshold, offers, wants, test_needs) # return both predictions the earlier cosine and the combined rescal binary_pred_cosine = binary_pred_cosine[idx_test] binary_pred_rescal = matrix_to_array(P_bin, idx_test) return binary_pred_cosine, binary_pred_rescal