Skip to content
DeepForgeby svx
All problems
EasyML Fundamentalsml-226

Feature Crossing Hash

Problem

Hash a crossed categorical pair into a bucket. Build the key str(a) + '_' + str(b) and fold each character with h = (h * 31 + ord(ch)) mod num_buckets.

Return the bucket index; a non-positive num_buckets returns 0.

Hint

Crossing two categoricals then hashing keeps the feature space bounded.

Test cases

case 1
in: ["red","S",10]
expected: 3
case 2
in: [1,2,10]
expected: 4
case 3
in: ["cat","dog",7]
expected: 0
case 4
in: ["x","y",5]
expected: 1
Starter codePython
def feature_cross_hash(a, b, num_buckets):
    # Your code here
    pass

4 test cases · run your code against them in the editor