code: def estimate_density(DATA_PATH, feature_size):
      
      if not os.path.exists(DATA_PATH):
          raise Exception("Data is not there!")
      density = []
      P = 0.01
      for _ in range(10):
          num_non_zero = 0
          num_sample = 0
          with open(DATA_PATH) as f:
              for line in f:
                  if (random.random() < P):
                      num_non_zero += len(line.split(" ")) - 1
                      num_sample += 1
          density.append(num_non_zero * 1.0 / (feature_size * num_sample))
      return sum(density) / len(density)
docs: sample 10 times of a size of 1000 for estimating the density of the sparse dataset