code: def _flip_random_edges(A, p_swap):
      if not A.shape[0] == A.shape[1]:
          raise ValueError("A must be a square matrix.")
      dtype = A.dtype
      A = sp.lil_matrix(A).astype(bool)
      n_elem = A.shape[0] ** 2
      n_elem_to_flip = round(p_swap * n_elem)
      unique_idx = np.random.choice(
          n_elem, replace=False, size=n_elem_to_flip
      )
      row_idx = unique_idx // A.shape[0]
      col_idx = unique_idx % A.shape[0]
      idxs = np.stack((row_idx, col_idx)).T
      for i in idxs:
          i = tuple(i)
          A[i] = np.logical_not(A[i])
      A = A.tocsr().astype(dtype)
      A.eliminate_zeros()
      return A
  
file_path: spektral/spektral/datasets/mnist.py
imports: import numpy
  import scipy.sparse
  from sklearn.neighbors import kneighbors_graph
  from tensorflow.keras.datasets import mnist
  from spektral.data import Dataset, Graph
label_desc: Use image node_id 4 for calling a global function with example usage: _flip_random_edges(A, p_swap) and returns: A