def mse(grid_pred, real_events): """ Computes the "mean aquared error" between the prediction and the ground truth. Is computed as the sum of each cell squared difference. :param grid_pred: An instance of :class:`GridPrediction` matrix attribute must be normalized. :param real_events: An instance of :class: open_cp.data.TimedPoints :return: A non-negative floating point value """ grid_pred._matrix = ProcessData.normalize_matrix(grid_pred._matrix) counting_matrix = make_counting_grid(grid_pred, real_events) counting_matrix._matrix = ProcessData.normalize_matrix( counting_matrix._matrix) return mean_squared_error(grid_pred._matrix, counting_matrix._matrix)
def test_mse_match(self): """ Test mse results match using different methods""" infile = open( '/Users/anamaria/Desktop/dev/security_project/aggressive_behavior_model/pkl/experiment_seppexp_10_2_siedco_prediction.pkl', 'rb') loaded_siedco = pickle.load(infile) infile.close() grid = loaded_siedco['prediction'].values[0] grid._matrix = ProcessData.normalize_matrix(grid._matrix) real_events = loaded_siedco['eval_pts'].values[0] mse_method_1 = prediction_metrics.mse(grid, real_events) counting_matrix = prediction_metrics.make_counting_grid( grid, real_events) counting_matrix._matrix = ProcessData.normalize_matrix( counting_matrix._matrix) mse_method_2 = np.sum((grid._matrix.astype("float") - counting_matrix._matrix.astype("float"))**2) mse_method_2 /= float(grid._matrix.shape[0] * grid._matrix.shape[1]) self.assertEqual(mse_method_1, mse_method_2)
def test_normalize_matrix(self): matrix = np.arange(1, 5) matrix = matrix.reshape(2, 2) matrix_expected = np.array([[0.25, 0.5], [0.75, 1]]) matrix_normalized = ProcessData.normalize_matrix(matrix) self.assertTrue((matrix_normalized == matrix_expected).all())