BLOGLLM Logo
Go Pro
Guest
Not signed in
Tous les BlogsCoursEntraînementPréparation aux entretiensSériesTagsBookmarksTarifs
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardJeux
Articles
Entraînement / Data processing and features5 of 54 in topic

76. Transformer des tokens catégoriels en un vecteur de taille fixe avec le hashing trick

EasyData processing and features

Avec des millions d'user agents ou d'URLs possibles, un vocabulaire ne peut pas tenir en mémoire. Le hashing trick mappe chaque token directement vers un bucket.

Écrivez hash_features(tokens, n_buckets). Utilisez ce hash déterministe : h = sum of (i + 1) * ord(ch) sur les caractères du token, avec i la position du caractère. Bucket = h % n_buckets. Un sign bit évite les collisions systématiques : sign = +1 si (h // n_buckets) est pair, sinon -1. Retournez un float array de longueur n_buckets avec la somme des comptes signés.

Fonctiondef hash_features(tokens, n_buckets)

Exemple 1

Input: hash_features(["a"], 10) puts -1 in bucket 7: ord('a') = 97, 97 % 10 = 7, and 97 // 10 = 9

Output: odd

Explanation: So the sign is -1.

Exemple 2

Input: tokens = ['a'], n_buckets = 10

Output: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0]

Exemple 3

Input: tokens = ['cat', 'cat', 'dog'], n_buckets = 8

Output: [0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0]

Contraintes

  • Python 3 with NumPy. Code runs in your browser.
  • Your code must pass all 3 tests to be accepted.
Indice 1

Each token maps to one bucket and one sign, both derived from a single integer hash, and the output just accumulates those signed counts.

Indice 2

For each token compute h as the sum of (position + 1) * ord(char), find bucket h % n_buckets and the sign from (h // n_buckets), then add the sign into that bucket of a zero array.

Indice 3

Positions start at 1 in the weight, so use enumerate(t) with (i + 1) * ord(ch); the sign is +1 when (h // n_buckets) % 2 == 0, otherwise -1.

Comprendre l'idée

Lorsqu'il y a des millions de tokens possibles, comme des URLs ou des user agents, maintenir un vocabulaire est trop coûteux. Le hashing trick envoie chaque token directement dans l'un des n buckets via un hash. Différents tokens peuvent entrer en collision dans un bucket, donc un sign bit permet aux collisions de s'additionner ou de s'annuler parfois, au lieu de toujours s'accumuler. Worked example avec n_buckets = 10: le token 'ab' a h = 1 * 97 + 2 * 98 = 293. Bucket = 293 % 10 = 3. 293 // 10 = 29 est impair, donc le sign est -1, et le bucket 3 reçoit -1. Le token 'a' a h = 97, bucket 7, et 97 // 10 = 9 est impair, donc en suivant la règle, il reçoit aussi -1.

h = sum over characters of (position + 1) * ord(ch), positions starting at 0; bucket = h % n_buckets; sign = +1 if (h // n_buckets) is even, else -1; add the sign to that bucket for every token; return a float array of length n_buckets

Utilisé dans les modèles de spam et de click ad à grande échelle où les vocabulaires ne tiennent pas en mémoire.

Sujets
Data processing and features
Questions similaires
  • Standardiser les features sans fuite de données de testEasy
  • Scaler des features avec la médiane et l'IQR pour éviter la dominance des outliersEasy
  • Encoder l'heure du jour et le jour de la semaine sous forme de features sine et cosineEasy
  • Clip les outliers en utilisant des percentiles appris uniquement sur le set d'entraînementEasy
Tous les Data processing and features problèmes
Signaler
76 / 903
Python 3solution.pyPython en attente
1
2
3
4
5
6
import numpy as np
​
def hash_features(tokens, n_buckets):
# your code here
pass
​

Run vérifie ces cas dans votre navigateur, sans connexion requise. Submit vérifie tous les 3 tests.

Ctrl/Cmd + Entrée pour exécuter
© BlogLLM, Bengaluru, IndiaÀ proposConditionsConfidentialitéRemboursementsDMCAContactRSS