def compute_hash(features, hash_matrix, hash_vector):
    'Compute hash values for features using the hash function (A * x + c) mod 2.\n\n  Args:\n    features: NumPy float array of shape (n, d), the features to hash.\n    hash_matrix: NumPy float array of shape (num_feature_bits, num_hash_bits),\n      a random matrix A to construct the hash function.\n    hash_vector: NumPy float array of shape (1, num_hash_bits),\n      a random vector c to construct the hash function.\n\n  Returns:\n    NumPy float array of shape (n, 1) containing the hashed values in [0, 1].\n  '

    def convert_int_to_bin(x, dimension):
        return '{:b}'.format(x).zfill(dimension)[(- dimension):]
    convert_int_to_bin = np.vectorize(convert_int_to_bin)
    convert_bin_to_int = np.vectorize((lambda x: int(x, 2)))
    num_features = features.shape[0]
    (num_feature_bits, num_hash_bits) = hash_matrix.shape
    feature_sum_str = [''.join(x) for x in features.astype('str')]
    feature_sum_hex = [hashlib.md5(s).hexdigest() for s in feature_sum_str]
    feature_sum_int = [int(h, 16) for h in feature_sum_hex]
    feature_sum_bin = convert_int_to_bin(feature_sum_int, dimension=num_feature_bits)
    feature_sum_bin_matrix = np.array([[int(c) for c in s] for s in feature_sum_bin])
    feature_hashed = (np.dot(feature_sum_bin_matrix, hash_matrix) + np.repeat(hash_vector, repeats=num_features, axis=0))
    feature_hashed_bits = np.mod(feature_hashed, 2)
    feature_hashed_bit_char = convert_int_to_bin(feature_hashed_bits, 1)
    feature_hashed_bit_str = [''.join(s) for s in feature_hashed_bit_char]
    feature_hashed_int = convert_bin_to_int(feature_hashed_bit_str)
    hashed_val = ((feature_hashed_int * 1.0) / (2 ** num_hash_bits))
    return hashed_val.reshape((- 1), 1)
Compute hash values for features using the hash function (A * x + c) mod 2.

Args:
  features: NumPy float array of shape (n, d), the features to hash.
  hash_matrix: NumPy float array of shape (num_feature_bits, num_hash_bits),
    a random matrix A to construct the hash function.
  hash_vector: NumPy float array of shape (1, num_hash_bits),
    a random vector c to construct the hash function.

Returns:
  NumPy float array of shape (n, 1) containing the hashed values in [0, 1].
def compute_hash(features, hash_matrix, hash_vector):
    'Compute hash values for features using the hash function (A * x + c) mod 2.\n\n  Args:\n    features: NumPy float array of shape (n, d), the features to hash.\n    hash_matrix: NumPy float array of shape (num_feature_bits, num_hash_bits),\n      a random matrix A to construct the hash function.\n    hash_vector: NumPy float array of shape (1, num_hash_bits),\n      a random vector c to construct the hash function.\n\n  Returns:\n    NumPy float array of shape (n, 1) containing the hashed values in [0, 1].\n  '

    def convert_int_to_bin(x, dimension):
        return '{:b}'.format(x).zfill(dimension)[(- dimension):]
    convert_int_to_bin = np.vectorize(convert_int_to_bin)
    convert_bin_to_int = np.vectorize((lambda x: int(x, 2)))
    num_features = features.shape[0]
    (num_feature_bits, num_hash_bits) = hash_matrix.shape
    feature_sum_str = [.join(x) for x in features.astype('str')]
    feature_sum_hex = [hashlib.md5(s).hexdigest() for s in feature_sum_str]
    feature_sum_int = [int(h, 16) for h in feature_sum_hex]
    feature_sum_bin = convert_int_to_bin(feature_sum_int, dimension=num_feature_bits)
    feature_sum_bin_matrix = np.array([[int(c) for c in s] for s in feature_sum_bin])
    feature_hashed = (np.dot(feature_sum_bin_matrix, hash_matrix) + np.repeat(hash_vector, repeats=num_features, axis=0))
    feature_hashed_bits = np.mod(feature_hashed, 2)
    feature_hashed_bit_char = convert_int_to_bin(feature_hashed_bits, 1)
    feature_hashed_bit_str = [.join(s) for s in feature_hashed_bit_char]
    feature_hashed_int = convert_bin_to_int(feature_hashed_bit_str)
    hashed_val = ((feature_hashed_int * 1.0) / (2 ** num_hash_bits))
    return hashed_val.reshape((- 1), 1)